From d947d1f08cd09b155b75c5b64c32696a83b207cd Mon Sep 17 00:00:00 2001 From: "HARMEL, Bernard" Date: Mon, 22 Jan 2024 11:34:17 +0100 Subject: [PATCH] emscripten support --- CMakeLists.txt | 13 +++---- apps/src/main.cpp | 30 ---------------- lib/CMakeLists.txt | 3 +- lib/include/bofstd/boflogger.h | 9 +++-- lib/src/boflogger.cpp | 1 + tests/src/main.cpp | 62 ++++++++++------------------------ 6 files changed, 33 insertions(+), 85 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ec3231f..8f4dce19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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) @@ -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}") @@ -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 @@ -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) diff --git a/apps/src/main.cpp b/apps/src/main.cpp index 945f24ec..067678b7 100644 --- a/apps/src/main.cpp +++ b/apps/src/main.cpp @@ -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(); diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 51f71348..c6474de1 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -340,6 +340,7 @@ target_link_libraries(bofstd jsoncpp::jsoncpp #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 @@ -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) @@ -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} diff --git a/lib/include/bofstd/boflogger.h b/lib/include/bofstd/boflogger.h index 51571ab2..c322ec5a 100644 --- a/lib/include/bofstd/boflogger.h +++ b/lib/include/bofstd/boflogger.h @@ -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 @@ -122,7 +126,7 @@ const uint32_t BOF_LOGGER_MAX_FAST_FORMAT_BUFFER_SIZE = 0x8000; typedef std::map>::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; @@ -135,7 +139,6 @@ class BOFSTD_EXPORT BofLogger final public: static std::map> S_mLogChannelList; - // delete copy and move constructors and assign operators BofLogger(BofLogger const &) = delete; // Copy construct BofLogger(BofLogger &&) = delete; // Move construct diff --git a/lib/src/boflogger.cpp b/lib/src/boflogger.cpp index d2e8b644..01b1fb0c 100644 --- a/lib/src/boflogger.cpp +++ b/lib/src/boflogger.cpp @@ -24,6 +24,7 @@ #include #include +//#include BEGIN_BOF_NAMESPACE() diff --git a/tests/src/main.cpp b/tests/src/main.cpp index a53323e4..613b8bd7 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "../include/gtestrunner.h" @@ -20,45 +21,6 @@ #include #endif -#if 0 -//#include "asio.hpp" -#include -//Bof_GetNetworkInterfaceInfo -void f() -{ - BOFSTDPARAM StdParam_X; - BOF::Bof_Initialize(StdParam_X); - std::vector 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) @@ -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 @@ -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.*";