CTK 0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
Loading...
Searching...
No Matches
ctkFunctionCompileSnippets.cmake
Go to the documentation of this file.
1
2#! Compile code snippets from a given directory
3#!
4#! This CMake function globs all files called \e main.cpp in the
5#! directory given by <code>snippet_path</code> and creates
6#! an executable for each found main.cpp.
7#!
8#! If a \e files.cmake file exists in the same directory where a
9#! main.cpp file was found, it is included. This function assumes
10#! that the files.cmake file sets a list named \e snippet_src_files
11#! containing a list of files which should be compiled into the
12#! executable. If no files.cmake file is found, all files in the
13#! directory are compiled into the executable.
14#!
15#! This function uses all additionally passed arguments as link
16#! dependencies for the created executable.
17#!
18function(ctkFunctionCompileSnippets snippet_path)
19
20 # get all files called "main.cpp"
21 file(GLOB_RECURSE main_cpp_list "${snippet_path}/main.cpp")
22
23 foreach(main_cpp_file ${main_cpp_list})
24 # get the directory containing the main.cpp file
25 get_filename_component(main_cpp_dir "${main_cpp_file}" PATH)
26
27 set(snippet_src_files )
28
29 # If there exists a "files.cmake" file in the snippet directory,
30 # include it and assume it sets the variable "snippet_src_files"
31 # to a list of source files for the snippet.
32 if(EXISTS "${main_cpp_dir}/files.cmake")
33 include("${main_cpp_dir}/files.cmake")
34 set(_tmp_src_files ${snippet_src_files})
35 set(snippet_src_files )
36 foreach(_src_file ${_tmp_src_files})
37 if(IS_ABSOLUTE ${_src_file})
38 list(APPEND snippet_src_files ${_src_file})
39 else()
40 list(APPEND snippet_src_files ${main_cpp_dir}/${_src_file})
41 endif()
42 endforeach()
43 else()
44 # glob all files in the directory and add them to the snippet src list
45 file(GLOB_RECURSE snippet_src_files "${main_cpp_dir}/*")
46 endif()
47
48 # Uset the top-level directory name as the executable name
49 string(REPLACE "/" ";" main_cpp_dir_tokens "${main_cpp_dir}")
50 list(GET main_cpp_dir_tokens -1 snippet_exec_name)
51 set(snippet_target_name "Snippet-${snippet_exec_name}")
52 add_executable(${snippet_target_name} ${snippet_src_files})
53 if(ARGN)
54 target_link_libraries(${snippet_target_name} ${ARGN})
55 endif()
56 set_target_properties(${snippet_target_name} PROPERTIES
57 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/snippets"
58 ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/snippets"
59 LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/snippets"
60 OUTPUT_NAME ${snippet_exec_name}
61 )
62
63 endforeach()
64
65endfunction()