Commits

Christian Fischer  committed 2e3e66b

added some magic to move all module registrations into the executable targets

  • Participants
  • Parent commits 3532f94

Comments (0)

Files changed (6)

File build/build.cmake

 set(WIESEL_MODULES_DIR   ${WIESEL_DIR}/libraries/desktop)
 
 # load standard modules
-include(wiesel-module)
+include(wiesel-registry)
+include(wiesel-module)
 include(wiesel-testsuite)
 
 

File build/cmake/modules/wiesel-module.cmake

 		wiesel_target_add_includes(${parent_target} ${MODULE_INCLUDE_DIRECTORIES})
 	endif()
 	
+	# the parent target inherits all exported loaders from it's dependency
+	get_target_property(MODULE_LOADERS ${module} EXPORT_MODULE_LOADERS)
+	if (NOT MODULE_LOADERS STREQUAL "MODULE_LOADERS-NOTFOUND")
+		wiesel_module_export_loaders(${parent_target} ${MODULE_LOADERS})
+		
+		# on executable targets, we need to append the module's registry
+		set(MODULE_REGISTRY_FILE ${CMAKE_CURRENT_BINARY_DIR}/generated/${parent_target}-module-registry.cpp)
+		wiesel_append_module_registry(${parent_target} ${MODULE_REGISTRY_FILE} ${MODULE_LOADERS})
+	endif()
+	
 endfunction(wiesel_module_add_dependency)
 
 
 
 
 function(wiesel_create_executable target source_dir)
+	set(MODULE_REGISTRY_FILE ${CMAKE_CURRENT_BINARY_DIR}/generated/${target}-module-registry.cpp)
+
 	# get all sources from this directory
 	wiesel_module_get_files(MODULE_SRC_FILES ${source_dir})
 	
 	# add sources to the module's target
-	add_executable(${target} ${MODULE_SRC_FILES})
+	add_executable(${target} ${MODULE_REGISTRY_FILE} ${MODULE_SRC_FILES})
+	
+	# scan for all loadable modules in the library's source-tree
+	wiesel_module_scan_for_loaders(MODULE_LOADERS ${source_dir})
+	wiesel_module_export_loaders(${target} ${MODULE_LOADERS})
+	
+	# executables get an module registry file which contains the collected module loaders
+	wiesel_create_module_registry(${target} ${MODULE_REGISTRY_FILE})
 
 	# also create according test package
 	# SORRY! no test support for executables yet :-/
 	# add sources to the module's target
 	add_library(${target} SHARED ${MODULE_SRC_FILES})
 	
+	# scan for all loadable modules in the library's source-tree
+	wiesel_module_scan_for_loaders(MODULE_LOADERS ${source_dir})
+	wiesel_module_export_loaders(${target} ${MODULE_LOADERS})
+	
 	# also create according test package
 	wiesel_create_test_package_for(${target} "${test_dir}")
 	

File build/cmake/modules/wiesel-registry.cmake

+
+function(wiesel_module_scan_for_loaders var path)
+	file(GLOB_RECURSE MY_FILES ${path}/*.loadable_module)
+	set(${var} ${MY_FILES} PARENT_SCOPE)
+endfunction(wiesel_module_scan_for_loaders)
+
+
+
+function(wiesel_module_export_loaders module)
+	set_property(TARGET ${module} APPEND PROPERTY EXPORT_MODULE_LOADERS ${ARGN})
+	
+	foreach(loader ${ARGN})
+		message("${module} exports loader: ${loader}")
+	endforeach()
+endfunction(wiesel_module_export_loaders)
+
+
+
+function(wiesel_create_module_registry target registry_file)
+	# get the list of exported modules from the target
+	get_target_property(MODULE_LOADERS ${target} EXPORT_MODULE_LOADERS)
+	
+	# create the file
+	file(WRITE ${registry_file} "")
+	
+	# add each module to the registry
+	if (NOT MODULE_LOADERS STREQUAL "MODULE_LOADERS-NOTFOUND")
+		foreach(module ${MODULE_LOADERS})
+			file(APPEND ${registry_file} "#include \"${module}\"")
+			file(APPEND ${registry_file} "\n")
+		endforeach()
+	endif()
+
+	# generated flag required, because otherwise cmake will report an error if the file is missing
+	set_source_files_properties(${registry_file} PROPERTIES GENERATED TRUE)
+endfunction(wiesel_create_module_registry)
+
+
+
+function(wiesel_append_module_registry target registry_file)
+	get_target_property(TARGET_TYPE ${target} TYPE)
+	
+	if (TARGET_TYPE STREQUAL "EXECUTABLE")
+		# add each module to the registry
+		foreach(module ${ARGN})
+			file(APPEND ${registry_file} "#include \"${module}\"")
+			file(APPEND ${registry_file} "\n")
+		endforeach()
+	endif()
+endfunction(wiesel_append_module_registry)
+

File libraries/desktop/wiesel-sdl/module.cmake

 # create the SDL module
 wiesel_create_module(wiesel-sdl ${WIESEL_SRC_DIR}/sdl ${WIESEL_TESTS_DIR}/sdl)
 
+# export include directories of this library for other targets
+wiesel_module_export_includes(wiesel-sdl ${WIESEL_SRC_DIR}/sdl)
+
 # add required modules
 wiesel_module_add_dependency(wiesel-sdl wiesel-base)
 wiesel_module_add_dependency(wiesel-sdl wiesel-common)

File src/common/wiesel/modules.h

  *								\ref IModuleLoader::PriorityNormal, \ref IModuleLoader::PriorityHigh.
  */
 #define REGISTER_MODULE(interface_class, implementation_class, factory, api, api_version, priotiry) \
-	ModuleLoaderImpl<interface_class, implementation_class> implementation_class##_Loader(factory, api, api_version, priotiry);
+	::wiesel::ModuleLoaderImpl<interface_class, implementation_class> implementation_class##_Loader(factory, api, api_version, priotiry);
 
 /**
  * @brief Registers a new module class.
  *								\ref IModuleLoader::PriorityNormal, \ref IModuleLoader::PriorityHigh.
  */
 #define REGISTER_MODULE_SINGLETON(interface_class, implementation_class, factory, api, api_version, priotiry) \
-	ModuleLoaderSingletonImpl<interface_class, implementation_class> implementation_class##_Loader(factory, api, api_version, priotiry);
+	::wiesel::ModuleLoaderSingletonImpl<interface_class, implementation_class> implementation_class##_Loader(factory, api, api_version, priotiry);
 
 
 namespace wiesel {

File src/sdl/wiesel/sdl/loaders/sdl_image_loader.loadable_module

+
+#include "sdl_image_loader.h"
+
+// add the module to the module registry
+namespace wiesel {
+	REGISTER_MODULE_SINGLETON(
+			IImageLoader,
+			SdlImageLoader,
+			&SdlImageLoader::create,
+			"SDL",
+			0x01020000u,
+			IModuleLoader::PriorityNormal
+	)
+}