diff --git a/CMakeLists.txt b/CMakeLists.txt index ce4653d2c4..846f349aad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,6 +285,8 @@ include(cmakemodules/script_wxwidgets.cmake REQUIRED) # Check for wxWidgets + include(cmakemodules/script_xsens.cmake REQUIRED) # XSens Motion trackers / IMU drivers include(cmakemodules/script_zlib.cmake REQUIRED) # Check for zlib +include(cmakemodules/process_emscripten_embedded_files.cmake REQUIRED) + # --------------------------------------------------------------------------- # OPTIONS #The options for the user when using "cmakesetup" or "ccmake": diff --git a/cmakemodules/process_emscripten_embedded_files.cmake b/cmakemodules/process_emscripten_embedded_files.cmake new file mode 100644 index 0000000000..ef087abb15 --- /dev/null +++ b/cmakemodules/process_emscripten_embedded_files.cmake @@ -0,0 +1,50 @@ + +# Parse all .cpp sources of a given `target`, and for each comment like: +# //! JS_PRELOAD_FILE +# it will append a link flag like: +# --preload-file +# Only if building for Emscripten. It has no effects otherwise. +# +# 07/March/2022, Jose Luis Blanco Claraco +# +function(process_emscripten_embedded_files targetName_) + if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") + return() + endif() + + get_target_property(targetCWD ${targetName_} BINARY_DIR) + + get_target_property(MY_SOURCES ${targetName_} SOURCES) + foreach(fil_ ${MY_SOURCES}) + #message(STATUS "Processing: ${fil_}") + file(STRINGS "${fil_}" content_) + string(REGEX MATCHALL "//! JS_PRELOAD_FILE.+\\|([-A-Za-z0-9_\\./]+)\\|" myMatches_ "${content_}") + foreach(match_ ${myMatches_}) + message(STATUS "match_ 1: ${match_}") + string(REGEX REPLACE "<(.+)>" "\\1" f_ "${match_}") + message(STATUS "f: ${f_}") + if (NOT f_) + continue() + endif() + message(STATUS "[emscripten] Embedding file '${f_}' into '${targetName_}'") + + if(NOT COMMAND target_link_options) + message("Warning: This feature requires target_link_options() [cmake >=3.13]") + return() + endif() + + set(absPath ${MRPT_SOURCE_DIR}/${f_}) + set(relativePath ${f_}) + + # Create copies (symlinks dont work...) to access the files via relative paths: + execute_process(COMMAND bash -c "mkdir -p \$(dirname ${relativePath})" WORKING_DIRECTORY ${targetCWD}) + execute_process(COMMAND bash -c "cp ${absPath} ${relativePath}" WORKING_DIRECTORY ${targetCWD}) + + target_link_options(${targetName_} PRIVATE --preload-file ${relativePath}) + endforeach() + endforeach() + + # Make the .data file accessible to the emscripten linker: + execute_process(COMMAND bash -c "ln -s ${EXECUTABLE_OUTPUT_PATH}/${targetName_}.data ${targetCWD}/${targetName_}.data" OUTPUT_QUIET ERROR_QUIET) + +endfunction() diff --git a/libs/obs/src/CObservationIMU_unittest.cpp b/libs/obs/src/CObservationIMU_unittest.cpp index 31ea9ee828..12dd61e9f7 100644 --- a/libs/obs/src/CObservationIMU_unittest.cpp +++ b/libs/obs/src/CObservationIMU_unittest.cpp @@ -34,6 +34,7 @@ TEST(CObservationIMU, Deserialize_v3) mrpt::obs::CRawlog dataset; + //! JS_PRELOAD_FILE const auto sFil = mrpt::UNITTEST_BASEDIR + "/tests/test-imu-obs-format3.rawlog"s; @@ -51,6 +52,7 @@ TEST(CObservationIMU, Deserialize_v4) mrpt::obs::CRawlog dataset; + //! JS_PRELOAD_FILE const auto sFil = mrpt::UNITTEST_BASEDIR + "/tests/test-imu-obs-format4.rawlog"s; diff --git a/libs/obs/src/CSimpleMap_unittest.cpp b/libs/obs/src/CSimpleMap_unittest.cpp index d9e311f03c..d1e4e48ba1 100644 --- a/libs/obs/src/CSimpleMap_unittest.cpp +++ b/libs/obs/src/CSimpleMap_unittest.cpp @@ -13,6 +13,7 @@ TEST(CSimpleMap, ParseFileInFormat_v1_5) { + //! JS_PRELOAD_FILE const std::string fil = mrpt::UNITTEST_BASEDIR + std::string("/share/mrpt/datasets/localization_demo.simplemap.gz"); diff --git a/libs/opengl/src/serializations_unittest.cpp b/libs/opengl/src/serializations_unittest.cpp index 0761e38010..4fd198a765 100644 --- a/libs/opengl/src/serializations_unittest.cpp +++ b/libs/opengl/src/serializations_unittest.cpp @@ -95,6 +95,7 @@ TEST(SerializeTestOpenGL, PredefinedSceneFile) { using namespace std::string_literals; + //! JS_PRELOAD_FILE const std::string fil = mrpt::UNITTEST_BASEDIR + "/tests/default-scene.3Dscene"s; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7dc1c86aa6..f4d8f16096 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -21,7 +21,12 @@ if (UNIX) endif() -add_definitions(-DCMAKE_UNITTEST_BASEDIR="${MRPT_SOURCE_DIR}") +if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") + add_definitions(-DCMAKE_UNITTEST_BASEDIR="${MRPT_SOURCE_DIR}") +else() + # Use relative paths for embedded files: + add_definitions(-DCMAKE_UNITTEST_BASEDIR=".") +endif() # Allow tests to include sources from examples directory: include_directories("${MRPT_SOURCE_DIR}") @@ -46,7 +51,6 @@ if(MSVC) add_definitions(/D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING) endif() - get_property(LST_LIB_TESTS GLOBAL PROPERTY "MRPT_TEST_LIBS") foreach(_TSTLIB ${LST_LIB_TESTS}) string(REGEX REPLACE "mrpt_(.*)" "mrpt-\\1" _TSTLIB_DASH ${_TSTLIB}) @@ -63,6 +67,8 @@ foreach(_TSTLIB ${LST_LIB_TESTS}) add_coverage(test_${_TSTLIB}) + process_emscripten_embedded_files(test_${_TSTLIB}) + # Add the required libraries for linking: if (CMAKE_MRPT_HAS_GTEST_SYSTEM) # System vesion: @@ -94,7 +100,7 @@ foreach(_TSTLIB ${LST_LIB_TESTS}) else() target_link_libraries(test_${_TSTLIB} ${_DEPLIB}) endif() - endforeach(_DEPLIB) + endforeach() # Run it: set(GENERATED_EXE "$")