1# Function which creates appropriate "source groups" (filter folders in Visual Studio) for the given list of source files 2function(createSourceGroups source1) 3 set(sources ${source1} ${ARGN}) 4 foreach(source ${sources}) 5 get_filename_component(source_path ${source} PATH) 6 string(REPLACE "/" "\\" source_path_backslashes "${source_path}") 7 source_group(${source_path_backslashes} FILES ${source}) 8 endforeach() 9endfunction() 10 11# Further processes a target and its list of source files adding extra touches useful for some generators 12# (filter folders, group targets in folders, etc.). 13# All optional arguments are treated as additional source files. 14function(setup_target targetName source1) 15 set(sources ${source1} ${ARGN}) 16 17 createSourceGroups(${sources}) 18 19 # Enable USE_FOLDERS. This is required by the set_target_properties(... FOLDER ...) call below. 20 # We prefer to set it here rather than globally at the top of the file so that we only modify 21 # the Cmake environment if/when the functionality is actually required. 22 set_property(GLOBAL PROPERTY USE_FOLDERS ON) 23 file(RELATIVE_PATH projectFolder ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) 24 set_target_properties(${targetName} PROPERTIES FOLDER "${projectFolder}") 25endfunction() 26 27# Convenience replacement of add_executable(), which besides adding an executable to the project 28# further configures the target via setup_target(). 29# All optional arguments are treated as additional source files. 30function(add_executable_ex targetName source1) 31 set(sources ${source1} ${ARGN}) 32 add_executable(${targetName} ${sources}) 33 setup_target(${targetName} ${sources}) 34endfunction() 35 36# Convenience replacement of add_library(), which besides adding a library to the project 37# further configures the target via setup_target(). 38# All optional arguments are treated as additional source files. 39function(add_library_ex targetName libraryType source1) 40 set(sources ${source1} ${ARGN}) 41 add_library(${targetName} ${libraryType} ${sources}) 42 setup_target(${targetName} ${sources}) 43endfunction() 44