Skip to content

Commit

Permalink
emscripten support
Browse files Browse the repository at this point in the history
  • Loading branch information
bha-evs committed Jan 22, 2024
1 parent 6e5dbf0 commit d947d1f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 85 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# History: 05 June 2004: BHA: Initial version
#compile with emscripten:
#cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/pro/emsdk/upstream/emscripten/cmake/Modules/Platform/emscripten-pthread.cmake -DCMAKE_TOOLCHAIN_FILE=C:/pro/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=wasm32-emscripten -DBUILD_SHARED_LIBS=OFF -DCMAKE_GENERATOR=Ninja -DCMAKE_BUILD_TYPE=Debug -SC:/pro/github/bofstd -BC:/bld/bofstd-web -DCMAKE_CROSSCOMPILING_EMULATOR=C:/pro/emsdk/node/16.20.0_64bit/bin/node.exe -G Ninja
#cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=C:/pro/emsdk/upstream/emscripten/cmake/Modules/Platform/emscripten.cmake -DCMAKE_TOOLCHAIN_FILE=C:/pro/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=wasm32-emscripten -DBUILD_SHARED_LIBS=OFF -DCMAKE_GENERATOR=Ninja -DCMAKE_BUILD_TYPE=Debug -SC:/pro/github/bofstd -BC:/bld/bofstd-web -DCMAKE_CROSSCOMPILING_EMULATOR=C:/pro/emsdk/node/16.20.0_64bit/bin/node.exe -G Ninja
#run
#cd C:\bld\bofstd-web\binaries\bin
#node AppBofStd.js
Expand All @@ -20,7 +20,7 @@
#select .html file
#compil windows:
#cmake -DCMAKE_TOOLCHAIN_FILE=C:/pro/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static -DVCPKG_HOST_TRIPLET=x64-windows-static -DBUILD_SHARED_LIBS=OFF -DVCPKG_OVERLAY_PORTS=C:/pro/github/onbings-vcpkg-registry/ports -DCMAKE_INSTALL_PREFIX=C:\tmp\bofstd -SC:/pro/github/bofstd -BC:/bld/bofstd-win
#cmake --build . --config Release -- -j 16
#cmake --build . --config Release -j 16
#cmake --install . --config Debug
cmake_minimum_required(VERSION 3.25)

Expand Down Expand Up @@ -129,8 +129,8 @@ if (EMSCRIPTEN)
message("===EMSCRIPTEN=== Generate .wasm and .js files")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -s NO_DISABLE_EXCEPTION_CATCHING")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -s NO_DISABLE_EXCEPTION_CATCHING")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

#The @ symbol is needed because sometimes it is useful to package files that are not nested below the compile-time directory, and for which #there is therefore no default mapping to a location in the virtual file system.
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s TOTAL_STACK=${BOFSTD_EMSCRIPTEN_STACK} -s TOTAL_MEMORY=${BOFSTD_EMSCRIPTEN_MEMORY} -s PTHREAD_POOL_SIZE=${BOFSTD_EMSCRIPTEN_THREAD_POOL} --embed-file ${BOFSTD_EMSCRIPTEN_FS_SRC_ROOT_DIR}@${BOFSTD_EMSCRIPTEN_FS_DST_ROOT_DIR}")
Expand All @@ -141,8 +141,8 @@ if (EMSCRIPTEN)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
endif ()
message("===EMSCRIPTEN=== CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
endif()
message("===EMSCRIPTEN=== CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
endif()

# For shared libraries we need to compile with "-fPIC"
# But for static libraries user can choose
Expand Down Expand Up @@ -270,6 +270,7 @@ message("==============================================start fetch==============
fetch_external_library_add(googletest "${LIST_OF_LIB}" https://github.com/google/googletest release-1.12.1)
fetch_external_library_add(spdlog "${LIST_OF_LIB}" https://github.com/gabime/spdlog v1.12.0)
make_external_library_available("${LIST_OF_LIB}")
#target_compile_definitions(spdlog PUBLIC SPDLOG_HEADER_ONLY)
message("==============================================END fetch=========================")
include(Dependencies.cmake)

Expand Down
30 changes: 0 additions & 30 deletions apps/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,6 @@ int main(int argc, char *argv[])
{
BOF::Bof_GetCurrentDirectory(Cwd_S);
printf("\nPwd %s\nRunning BofStd V %s on %s under %s\n", Cwd_S.c_str(), StdParam_X.Version_S.c_str(), StdParam_X.ComputerName_S.c_str(), StdParam_X.OsName_S.c_str());

#if 0
#if defined(__EMSCRIPTEN__)
// EM_ASM is a macro to call in-line JavaScript code.
EM_ASM(
Module.fs_is_ready = 0; // flag to check when data are synchronized
FS.mkdir('/offline'); // Make a directory other than '/'
FS.mount(IDBFS, {}, '/offline'); // Then mount with IDBFS type

FS.syncfs( // Then sync
true, function(err) {
Module.print("end file sync..");
assert(!err);
Module.fs_is_ready = 1;
}););
/*
0 lets the browser decide how often to call your callback, like v-sync. You can pass a positive integer to set a specific frame rate.
true stops execution at this point, your next code that runs will be the loop callback.
*/
emscripten_set_main_loop(game_loop_callback, 0, true);

#endif
#endif
const std::filesystem::directory_iterator end;
for (std::filesystem::directory_iterator it("/"); it != end; ++it)
{
const std::filesystem::path &entry = it->path();
printf("root: %s\n", entry.c_str());
}

BOF::Bof_Shutdown();
}
// emscripten_exit_with_live_runtime();
Expand Down
3 changes: 2 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ target_link_libraries(bofstd
jsoncpp::jsoncpp
#spdlog::spdlog
$<BUILD_INTERFACE:spdlog::spdlog> #https://discourse.cmake.org/t/using-a-header-only-library-with-fetchcontents-while-build-a-static-lib/2711/2
# spdlog::spdlog
)
if(WIN32)
target_link_libraries(bofstd
Expand Down Expand Up @@ -384,6 +385,7 @@ target_compile_definitions(bofstd
-DCRT_NONSTDC_NO_DEPRECATE
-DWINSOCK_DEPRECATED_NO_WARNINGS
-DIXML_STATIC_LIB
-DSPDLOG_HEADER_ONLY
)

# Features (We need at least C++11 support)
Expand Down Expand Up @@ -453,7 +455,6 @@ install(
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # For static libraries
COMPONENT bofstd-devel
)

# Install the public headers (don't forget the generated export directives)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Expand Down
9 changes: 6 additions & 3 deletions lib/include/bofstd/boflogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ BEGIN_BOF_NAMESPACE()
const uint32_t BOF_LOGGER_CHANNEL_FLAG_MASK_DBG_ALWAYS = 0x80000000; /*! Always display */
const uint32_t BOF_LOGGER_CHANNEL_FLAG_MASK_DBG_ERROR = 0x40000000; /*! Display error */
const uint32_t BOF_LOGGER_MAX_FAST_FORMAT_BUFFER_SIZE = 0x8000;

#if defined(_WIN32)
#define BOF_LOG_TO_DBG(pFormat,...) {std::string Dbg;BOF::Bof_Sprintf(pFormat, __VA_ARGS__);OutputDebugString(Dbg.c_str());}
#else
#define BOF_LOG_TO_DBG(pFormat,...) {printf(pFormat, __VA_ARGS__);}
#endif
// #define BOF_LOGGER_DEBUG_ON // To avoid extra cost when we want no log and no fct call at all
// The ##__VA_ARGS__ syntax is non-standard. It is a "swallow comma if the __VA_ARGS__ is empty" extension implemented by GCC, and seems to have been adopted by other compilers.
#ifdef BOF_LOGGER_DEBUG_OFF
Expand Down Expand Up @@ -122,7 +126,7 @@ const uint32_t BOF_LOGGER_MAX_FAST_FORMAT_BUFFER_SIZE = 0x8000;
typedef std::map<std::string, std::shared_ptr<IBofLogChannel>>::iterator LogFctLookupIterator;

// Singleton http://stackoverflow.com/questions/11711920/how-to-implement-multithread-safe-singleton-in-c11-without-using-mutex
class BOFSTD_EXPORT BofLogger final
class BOFSTD_EXPORT BofLogger
{
private:
BOF_LOGGER_PARAM mLoggerParam_X;
Expand All @@ -135,7 +139,6 @@ class BOFSTD_EXPORT BofLogger final

public:
static std::map<std::string, std::shared_ptr<IBofLogChannel>> S_mLogChannelList;

// delete copy and move constructors and assign operators
BofLogger(BofLogger const &) = delete; // Copy construct
BofLogger(BofLogger &&) = delete; // Move construct
Expand Down
1 change: 1 addition & 0 deletions lib/src/boflogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <spdlog/async.h>
#include <spdlog/spdlog.h>
//#include <spdlog/spdlog-inl.h>

BEGIN_BOF_NAMESPACE()

Expand Down
62 changes: 17 additions & 45 deletions tests/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <bofstd/boffs.h>
#include <bofstd/bofsystem.h>
#include <bofstd/boflogger.h>

#include "../include/gtestrunner.h"

Expand All @@ -20,45 +21,6 @@
#include <malloc.h>
#endif

#if 0
//#include "asio.hpp"
#include <bofstd/bofsocketio.h>
//Bof_GetNetworkInterfaceInfo
void f()
{
BOFSTDPARAM StdParam_X;
BOF::Bof_Initialize(StdParam_X);
std::vector<BOF::BOF_NETWORK_INTERFACE_PARAM> NetworkInterfaceCollection;
BOFERR e = BOF::Bof_GetListOfNetworkInterface(NetworkInterfaceCollection);
printf("Bof_GetListOfNetworkInterface found %zd Network board (%s)\n", NetworkInterfaceCollection.size(), BOF::Bof_ErrorCode(e));
for (auto &rIt : NetworkInterfaceCollection)
{
printf("name %s ip %s msk %s brd %s %s\n", rIt.Name_S.c_str(), rIt.IpAddress_S.c_str(), rIt.IpMask_S.c_str(), rIt.IpBroadcast_S.c_str(), rIt.IpV6_B ? "IpV6":"IpV4");
printf("Info: flg %X Gw %s Mtu %d Mac %02X:%02X:%02X:%02X:%02X:%02X\n", rIt.Info_X.InterfaceFlag_E, rIt.Info_X.IpGateway_S.c_str(), rIt.Info_X.MtuSize_U32,
rIt.Info_X.MacAddress[0], rIt.Info_X.MacAddress[1], rIt.Info_X.MacAddress[2], rIt.Info_X.MacAddress[3], rIt.Info_X.MacAddress[4], rIt.Info_X.MacAddress[5]);
}
/*
asio::io_context io;
asio::ip::tcp::resolver resolver(io);
asio::ip::tcp::resolver::query query(asio::ip::host_name(), "");
asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
while (it != asio::ip::tcp::resolver::iterator())
{
asio::ip::address addr = (it++)->endpoint().address();
if (addr.is_v6())
{
std::cout << "ipv6 address: ";
}
else
std::cout << "ipv4 address: ";

std::cout << addr.to_string() << std::endl;

}
*/
}
#endif

USE_BOF_NAMESPACE()

BOFERR AppBofAssertCallback(const std::string &_rFile_S, uint32_t _Line_U32, const std::string &_rMasg_S)
Expand All @@ -84,12 +46,22 @@ int main(int argc, char *argv[])
BOF_ASSERT(Sts_E == BOF_ERR_NO_ERROR);
Bof_GetCurrentDirectory(Cwd_S);
printf("\nPwd %s\nRunning BofStd V %s on %s under %s\n", Cwd_S.c_str(), StdParam_X.Version_S.c_str(), StdParam_X.ComputerName_S.c_str(), StdParam_X.OsName_S.c_str());
// for (int i = 0; i < 7; i++)
//{
// printf("hello world %d\n", i);
// }
// const char *pp = StdParam_X.ComputerName_S.c_str();

/*
BOF::BofLogger &rBofLog = BOF::BofLogger::S_Instance();
BOF::BOF_LOGGER_PARAM LoggerParam_X;
BOF::BOF_LOG_CHANNEL_PARAM LogParam_X;
LoggerParam_X.Name_S = "Xt Ftp Diag";
// LoggerParam_X.LogPattern_S = "%i %L %^%v%$";
LoggerParam_X.MaxNumberOfAsyncLogQueueEntry_U32 = 0x800;
LoggerParam_X.AsyncAutoFushIntervalInMs_U32 = 0;
LoggerParam_X.FastFormat_B = true;
LoggerParam_X.OverflowPolicy_E = BOF::BOF_LOGGER_OVERFLOW_POLICY::DISCARD;
LoggerParam_X.OnError = nullptr;
LoggerParam_X.OnErrorCodeToString = nullptr;
Sts_E = rBofLog.InitializeLogger(LoggerParam_X);
Sts_E = rBofLog.ShutdownLogger();
*/
#if defined(_WIN32)
#else
// Ok on tge2, there is an uart ::testing::GTEST_FLAG(filter) = "-Uart_Test.*"; // No hw
Expand Down Expand Up @@ -154,7 +126,7 @@ int main(int argc, char *argv[])
// "Threading_Test.*:BofThreadPool_Test.*:Timecode_Test.*:Uart_Test.*:Uri_Test.*";

//::testing::GTEST_FLAG(filter) = "DateTime_Test.StringDateTime:SocketTcp_Test.TcpClientTest:DateTime_Test.ValidateDateTime:SocketOs_Test.SocketAddress";
//::testing::GTEST_FLAG(filter) = "Timecode_Test.*";
//::testing::GTEST_FLAG(filter) = "Logger_Test.*";
//::testing::GTEST_FLAG(filter) = "XmlParser_Test.*:JsonParser_Test.*:JsonWriter_Test.*:Pipe_Test.*";
//::testing::GTEST_FLAG(filter) = "JsonParser_Test.*:XmlWriter_Test.*";
//::testing::GTEST_FLAG(filter) = "CmdLineParser_Test.*:Uri_Test.*";
Expand Down

0 comments on commit d947d1f

Please sign in to comment.