Skip to content

Commit

Permalink
towards tests passing in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Mar 8, 2022
1 parent 20a57ff commit e6d3334
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -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":
Expand Down
50 changes: 50 additions & 0 deletions 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 <name>
# it will append a link flag like:
# --preload-file <name>
# 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()
2 changes: 2 additions & 0 deletions libs/obs/src/CObservationIMU_unittest.cpp
Expand Up @@ -34,6 +34,7 @@ TEST(CObservationIMU, Deserialize_v3)

mrpt::obs::CRawlog dataset;

//! JS_PRELOAD_FILE <tests/test-imu-obs-format3.rawlog>
const auto sFil =
mrpt::UNITTEST_BASEDIR + "/tests/test-imu-obs-format3.rawlog"s;

Expand All @@ -51,6 +52,7 @@ TEST(CObservationIMU, Deserialize_v4)

mrpt::obs::CRawlog dataset;

//! JS_PRELOAD_FILE <tests/test-imu-obs-format4.rawlog>
const auto sFil =
mrpt::UNITTEST_BASEDIR + "/tests/test-imu-obs-format4.rawlog"s;

Expand Down
1 change: 1 addition & 0 deletions libs/obs/src/CSimpleMap_unittest.cpp
Expand Up @@ -13,6 +13,7 @@

TEST(CSimpleMap, ParseFileInFormat_v1_5)
{
//! JS_PRELOAD_FILE <share/mrpt/datasets/localization_demo.simplemap.gz>
const std::string fil = mrpt::UNITTEST_BASEDIR +
std::string("/share/mrpt/datasets/localization_demo.simplemap.gz");

Expand Down
1 change: 1 addition & 0 deletions libs/opengl/src/serializations_unittest.cpp
Expand Up @@ -95,6 +95,7 @@ TEST(SerializeTestOpenGL, PredefinedSceneFile)
{
using namespace std::string_literals;

//! JS_PRELOAD_FILE <tests/default-scene.3Dscene>
const std::string fil =
mrpt::UNITTEST_BASEDIR + "/tests/default-scene.3Dscene"s;

Expand Down
12 changes: 9 additions & 3 deletions tests/CMakeLists.txt
Expand Up @@ -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}")
Expand All @@ -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})
Expand All @@ -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:
Expand Down Expand Up @@ -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 "$<TARGET_FILE:test_${_TSTLIB}>")
Expand Down

0 comments on commit e6d3334

Please sign in to comment.