Browse Source

CMake: Make test executables work in Emscripten

Michael Ragazzon 1 year ago
parent
commit
0907b970cf

+ 32 - 31
CMake/Utilities.cmake

@@ -86,6 +86,38 @@ function(set_common_target_options target)
 			message(FATAL_ERROR "Unknown compiler, cannot enable option RMLUI_WARNINGS_AS_ERRORS.")
 		endif()
 	endif()
+
+	# Set Emscripten-specific properties and assets for samples and test executables.
+	get_target_property(target_type ${target} TYPE)
+	if(EMSCRIPTEN AND target_type STREQUAL "EXECUTABLE")
+		# Make Emscripten generate the default HTML shell for the sample.
+		set_property(TARGET ${target} PROPERTY SUFFIX ".html")
+
+		# Enables Asyncify which we only need since the backend doesn't control the main loop. This enables us to yield
+		# to the browser during the backend call to Backend::ProcessEvents(). Asyncify results in larger and slower
+		# code, users are instead encouraged to use 'emscripten_set_main_loop()' and family.
+		target_link_libraries(${target} PRIVATE "-sASYNCIFY")
+
+		# We don't know the needed memory beforehand, allow it to grow.
+		target_link_libraries(${target} PRIVATE "-sALLOW_MEMORY_GROWTH")
+
+		# Add common assets.
+		set(common_assets_dir "Samples/assets")
+		target_link_libraries(${target} PRIVATE "--preload-file ${PROJECT_SOURCE_DIR}/${common_assets_dir}/@/${common_assets_dir}/")
+		file(GLOB asset_files "${PROJECT_SOURCE_DIR}/${common_assets_dir}/*")
+
+		# Add sample-specific assets.
+		foreach(source_relative_dir IN LISTS data_dirs)
+			set(abs_dir "${CMAKE_CURRENT_SOURCE_DIR}/${source_relative_dir}")
+			file(RELATIVE_PATH root_relative_dir "${PROJECT_SOURCE_DIR}" "${abs_dir}")
+			target_link_libraries(${target} PRIVATE "--preload-file ${abs_dir}/@/${root_relative_dir}/")
+			file(GLOB sample_data_files "${abs_dir}/*")
+			list(APPEND asset_files "${sample_data_files}")
+		endforeach()
+
+		# Add a linker dependency to all asset files, so that the linker runs again if any asset is modified.
+		set_target_properties(${target} PROPERTIES LINK_DEPENDS "${asset_files}")
+	endif()
 endfunction()
 
 #[[
@@ -132,37 +164,6 @@ function(install_sample_target target)
 	install(DIRECTORY ${install_dirs}
 		DESTINATION "${CMAKE_INSTALL_DATADIR}/${sample_path}"
 	)
-
-	# Set Emscripten-specific sample properties and assets.
-	if(EMSCRIPTEN)
-		# Make Emscripten generate the default HTML shell for the sample.
-		set_property(TARGET ${target} PROPERTY SUFFIX ".html")
-
-		# Enables Asyncify which we only need since the backend doesn't control the main loop. This enables us to yield
-		# to the browser during the backend call to Backend::ProcessEvents(). Asyncify results in larger and slower
-		# code, users are instead encouraged to use 'emscripten_set_main_loop()' and family.
-		target_link_libraries(${target} PRIVATE "-sASYNCIFY")
-
-		# We don't know the needed memory beforehand, allow it to grow.
-		target_link_libraries(${target} PRIVATE "-sALLOW_MEMORY_GROWTH")
-
-		# Add common assets.
-		set(common_assets_dir "Samples/assets")
-		target_link_libraries(${target} PRIVATE "--preload-file ${PROJECT_SOURCE_DIR}/${common_assets_dir}/@/${common_assets_dir}/")
-		file(GLOB asset_files "${PROJECT_SOURCE_DIR}/${common_assets_dir}/*")
-
-		# Add sample-specific assets.
-		foreach(source_relative_dir IN LISTS data_dirs)
-			set(abs_dir "${CMAKE_CURRENT_SOURCE_DIR}/${source_relative_dir}")
-			file(RELATIVE_PATH root_relative_dir "${PROJECT_SOURCE_DIR}" "${abs_dir}")
-			target_link_libraries(${target} PRIVATE "--preload-file ${abs_dir}/@/${root_relative_dir}/")
-			file(GLOB sample_data_files "${abs_dir}/*")
-			list(APPEND asset_files "${sample_data_files}")
-		endforeach()
-
-		# Add a linker dependency to all asset files, so that the linker runs again if any asset is modified.
-		set_target_properties(${target} PROPERTIES LINK_DEPENDS "${asset_files}")
-	endif()
 endfunction()
 
 #[[

+ 4 - 2
Tests/Source/Benchmarks/CMakeLists.txt

@@ -23,11 +23,13 @@ target_link_libraries(${TARGET_NAME} PRIVATE
 	nanobench::nanobench
 )
 
-doctest_discover_tests(${TARGET_NAME})
+if(NOT EMSCRIPTEN)
+	doctest_discover_tests(${TARGET_NAME})
+endif()
 
 if(EMSCRIPTEN)
 	# The benchmarks additionally use data from the benchmark sample.
-	target_link_libraries(${TARGET_NAME} "--preload-file \"${PROJECT_SOURCE_DIR}/Samples/basic/benchmark/data/@/Samples/basic/benchmark/data/\"")
+	target_link_libraries(${TARGET_NAME} PRIVATE "--preload-file \"${PROJECT_SOURCE_DIR}/Samples/basic/benchmark/data/@/Samples/basic/benchmark/data/\"")
 endif()
 
 if(MSVC)

+ 3 - 1
Tests/Source/UnitTests/CMakeLists.txt

@@ -43,4 +43,6 @@ target_link_libraries(${TARGET_NAME} PRIVATE
 	trompeloeil::trompeloeil
 )
 
-doctest_discover_tests(${TARGET_NAME})
+if(NOT EMSCRIPTEN)
+	doctest_discover_tests(${TARGET_NAME})
+endif()