|
1 | 1 | # Common helper functions and defaults for project targets |
2 | 2 |
|
3 | 3 | function(itlabai_target_defaults target_name) |
| 4 | + if(CMAKE_CONFIGURATION_TYPES) |
| 5 | + foreach(_cfg IN LISTS CMAKE_CONFIGURATION_TYPES) |
| 6 | + string(TOUPPER "${_cfg}" _cfg_upper) |
| 7 | + set_target_properties(${target_name} PROPERTIES |
| 8 | + ARCHIVE_OUTPUT_DIRECTORY_${_cfg_upper} "${CMAKE_BINARY_DIR}/lib" |
| 9 | + LIBRARY_OUTPUT_DIRECTORY_${_cfg_upper} "${CMAKE_BINARY_DIR}/lib" |
| 10 | + RUNTIME_OUTPUT_DIRECTORY_${_cfg_upper} "${CMAKE_BINARY_DIR}/bin" |
| 11 | + ) |
| 12 | + endforeach() |
| 13 | + else() |
| 14 | + set_target_properties(${target_name} PROPERTIES |
| 15 | + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 16 | + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" |
| 17 | + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" |
| 18 | + ) |
| 19 | + endif() |
| 20 | + |
4 | 21 | if(MSVC) |
5 | | - target_compile_options(${target_name} PRIVATE /W4 /permissive- /EHsc) |
6 | | - if(ITLABAI_WARN_AS_ERROR) |
7 | | - target_compile_options(${target_name} PRIVATE /WX) |
8 | | - endif() |
| 22 | + target_compile_options(${target_name} PRIVATE /W4 /permissive- /EHsc /WX) |
9 | 23 | else() |
10 | | - target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wpedantic) |
11 | | - if(ITLABAI_WARN_AS_ERROR) |
12 | | - target_compile_options(${target_name} PRIVATE -Werror) |
13 | | - endif() |
| 24 | + target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wpedantic -Werror) |
14 | 25 | endif() |
15 | 26 | target_compile_features(${target_name} PRIVATE cxx_std_20) |
16 | 27 | endfunction() |
17 | 28 |
|
18 | | -function(itlabai_link_externals target_name) |
| 29 | +function(itlabai_use_opencv target_name) |
| 30 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 31 | + if(TARGET OpenCV::opencv_world) |
| 32 | + target_link_libraries(${target_name} ${_scope} OpenCV::opencv_world) |
| 33 | + endif() |
| 34 | + if(TARGET opencv_external) |
| 35 | + add_dependencies(${target_name} opencv_external) |
| 36 | + endif() |
| 37 | +endfunction() |
| 38 | + |
| 39 | +function(itlabai_use_tbb target_name) |
| 40 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 41 | + if(TARGET TBB_unified) |
| 42 | + target_link_libraries(${target_name} ${_scope} TBB_unified) |
| 43 | + elseif(TARGET TBB::tbb) |
| 44 | + target_link_libraries(${target_name} ${_scope} TBB::tbb) |
| 45 | + endif() |
| 46 | + if(TARGET tbb_external) |
| 47 | + add_dependencies(${target_name} tbb_external) |
| 48 | + endif() |
| 49 | +endfunction() |
| 50 | + |
| 51 | +function(itlabai_use_onednn target_name) |
| 52 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 53 | + if(TARGET dnnl) |
| 54 | + target_link_libraries(${target_name} ${_scope} dnnl) |
| 55 | + endif() |
| 56 | + if(TARGET onednn_external) |
| 57 | + add_dependencies(${target_name} onednn_external) |
| 58 | + endif() |
| 59 | +endfunction() |
| 60 | + |
| 61 | +function(itlabai_use_kokkos target_name) |
| 62 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 63 | + if(TARGET Kokkos_imported) |
| 64 | + target_link_libraries(${target_name} ${_scope} Kokkos_imported) |
| 65 | + endif() |
| 66 | + if(TARGET kokkos_external) |
| 67 | + add_dependencies(${target_name} kokkos_external) |
| 68 | + endif() |
| 69 | + if(MSVC) |
| 70 | + # Suppress Kokkos header warning C4702 only on targets that use Kokkos. |
| 71 | + target_compile_options(${target_name} ${_scope} /wd4702) |
| 72 | + endif() |
| 73 | +endfunction() |
| 74 | + |
| 75 | +function(itlabai_use_openmp target_name) |
| 76 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 77 | + if(TARGET OpenMP::OpenMP_CXX) |
| 78 | + target_link_libraries(${target_name} ${_scope} OpenMP::OpenMP_CXX) |
| 79 | + endif() |
| 80 | + if(OpenMP_FOUND) |
| 81 | + # Ensure both compile and link stages pick up OpenMP flags. |
| 82 | + if(DEFINED OpenMP_CXX_FLAGS AND NOT OpenMP_CXX_FLAGS STREQUAL "") |
| 83 | + string(REPLACE " " ";" _omp_flags "${OpenMP_CXX_FLAGS}") |
| 84 | + target_compile_options(${target_name} ${_scope} ${_omp_flags}) |
| 85 | + target_link_options(${target_name} ${_scope} ${_omp_flags}) |
| 86 | + endif() |
| 87 | + if(DEFINED OpenMP_EXE_LINKER_FLAGS AND NOT OpenMP_EXE_LINKER_FLAGS STREQUAL "") |
| 88 | + string(REPLACE " " ";" _omp_lflags "${OpenMP_EXE_LINKER_FLAGS}") |
| 89 | + target_link_options(${target_name} ${_scope} ${_omp_lflags}) |
| 90 | + endif() |
| 91 | + endif() |
| 92 | +endfunction() |
| 93 | + |
| 94 | +function(itlabai_use_gtest target_name) |
| 95 | + itlabai_use_externals_scope(_scope ${target_name}) |
| 96 | + if(TARGET gtest_main) |
| 97 | + target_link_libraries(${target_name} ${_scope} gtest_main) |
| 98 | + endif() |
| 99 | + if(TARGET gtest) |
| 100 | + target_link_libraries(${target_name} ${_scope} gtest) |
| 101 | + endif() |
| 102 | + if(TARGET gtest_external) |
| 103 | + add_dependencies(${target_name} gtest_external) |
| 104 | + endif() |
| 105 | +endfunction() |
| 106 | + |
| 107 | +function(itlabai_use_externals_scope out_var target_name) |
19 | 108 | get_target_property(_tgt_type ${target_name} TYPE) |
20 | 109 | if(_tgt_type STREQUAL "INTERFACE_LIBRARY") |
21 | | - set(_scope INTERFACE) |
| 110 | + set(${out_var} INTERFACE PARENT_SCOPE) |
22 | 111 | else() |
23 | | - set(_scope PUBLIC) |
24 | | - endif() |
25 | | - set(_ext_map |
26 | | - "opencv;OpenCV::opencv_world" |
27 | | - "tbb;TBB_unified;TBB::tbb" |
28 | | - "onednn;dnnl" |
29 | | - "kokkos;Kokkos_imported" |
30 | | - "openmp;OpenMP::OpenMP_CXX" |
31 | | - "gtest;gtest_main;gtest" |
32 | | - ) |
| 112 | + set(${out_var} PUBLIC PARENT_SCOPE) |
| 113 | + endif() |
| 114 | +endfunction() |
| 115 | + |
| 116 | +function(itlabai_link_externals target_name) |
33 | 117 | foreach(ext IN LISTS ARGN) |
34 | | - foreach(pair IN LISTS _ext_map) |
35 | | - list(GET pair 0 key) |
36 | | - if(ext STREQUAL key) |
37 | | - list(REMOVE_AT pair 0) |
38 | | - foreach(dep IN LISTS pair) |
39 | | - if(TARGET ${dep}) |
40 | | - target_link_libraries(${target_name} ${_scope} ${dep}) |
41 | | - endif() |
42 | | - endforeach() |
43 | | - if(ext STREQUAL "openmp" AND OpenMP_FOUND) |
44 | | - # Ensure both compile and link stages pick up OpenMP flags. |
45 | | - if(DEFINED OpenMP_CXX_FLAGS AND NOT OpenMP_CXX_FLAGS STREQUAL "") |
46 | | - string(REPLACE " " ";" _omp_flags "${OpenMP_CXX_FLAGS}") |
47 | | - target_compile_options(${target_name} ${_scope} ${_omp_flags}) |
48 | | - target_link_options(${target_name} ${_scope} ${_omp_flags}) |
49 | | - endif() |
50 | | - if(DEFINED OpenMP_EXE_LINKER_FLAGS AND NOT OpenMP_EXE_LINKER_FLAGS STREQUAL "") |
51 | | - string(REPLACE " " ";" _omp_lflags "${OpenMP_EXE_LINKER_FLAGS}") |
52 | | - target_link_options(${target_name} ${_scope} ${_omp_lflags}) |
53 | | - endif() |
54 | | - endif() |
55 | | - if(TARGET ${ext}_external) |
56 | | - add_dependencies(${target_name} ${ext}_external) |
57 | | - endif() |
58 | | - if(ext STREQUAL "kokkos" AND MSVC) |
59 | | - # Suppress Kokkos header warning C4702 only on targets that use Kokkos. |
60 | | - target_compile_options(${target_name} ${_scope} /wd4702) |
61 | | - endif() |
62 | | - if(ITLABAI_AUTOINCLUDE_EXTERNALS) |
63 | | - if(ext STREQUAL "tbb" AND DEFINED TBB_INSTALL_DIR) |
64 | | - target_include_directories(${target_name} SYSTEM ${_scope} $<BUILD_INTERFACE:${TBB_INSTALL_DIR}/include>) |
65 | | - endif() |
66 | | - if(ext STREQUAL "kokkos" AND DEFINED KOKKOS_INSTALL_DIR) |
67 | | - target_include_directories(${target_name} SYSTEM ${_scope} $<BUILD_INTERFACE:${KOKKOS_INSTALL_DIR}/include>) |
68 | | - endif() |
69 | | - if(ext STREQUAL "gtest" AND DEFINED GTEST_INSTALL_DIR) |
70 | | - target_include_directories(${target_name} SYSTEM ${_scope} $<BUILD_INTERFACE:${GTEST_INSTALL_DIR}/include>) |
71 | | - endif() |
72 | | - if(ext STREQUAL "opencv" AND DEFINED OPENCV_INSTALL_DIR) |
73 | | - set(_opencv_inc "") |
74 | | - if(DEFINED OPENCV_INCLUDE_DIR AND NOT OPENCV_INCLUDE_DIR STREQUAL "") |
75 | | - set(_opencv_inc "${OPENCV_INCLUDE_DIR}") |
76 | | - elseif(WIN32) |
77 | | - set(_opencv_inc "${OPENCV_INSTALL_DIR}/include") |
78 | | - else() |
79 | | - set(_opencv_inc "${OPENCV_INSTALL_DIR}/include/opencv4") |
80 | | - endif() |
81 | | - if(_opencv_inc) |
82 | | - target_include_directories(${target_name} SYSTEM ${_scope} $<BUILD_INTERFACE:${_opencv_inc}>) |
83 | | - endif() |
84 | | - endif() |
85 | | - endif() |
86 | | - endif() |
87 | | - endforeach() |
| 118 | + if(ext STREQUAL "opencv") |
| 119 | + itlabai_use_opencv(${target_name}) |
| 120 | + elseif(ext STREQUAL "tbb") |
| 121 | + itlabai_use_tbb(${target_name}) |
| 122 | + elseif(ext STREQUAL "onednn") |
| 123 | + itlabai_use_onednn(${target_name}) |
| 124 | + elseif(ext STREQUAL "kokkos") |
| 125 | + itlabai_use_kokkos(${target_name}) |
| 126 | + elseif(ext STREQUAL "openmp") |
| 127 | + itlabai_use_openmp(${target_name}) |
| 128 | + elseif(ext STREQUAL "gtest") |
| 129 | + itlabai_use_gtest(${target_name}) |
| 130 | + endif() |
88 | 131 | endforeach() |
89 | 132 | endfunction() |
90 | 133 |
|
91 | | -function(itlabai_set_rpath) |
92 | | - set(_paths ${ARGN}) |
| 134 | +function(itlabai_apply_runtime_rpath target_name) |
| 135 | + set(_paths "") |
| 136 | + foreach(_var IN ITEMS ONEDNN_INSTALL_DIR OPENCV_INSTALL_DIR TBB_INSTALL_DIR KOKKOS_INSTALL_DIR) |
| 137 | + if(DEFINED ${_var} AND NOT "${${_var}}" STREQUAL "") |
| 138 | + list(APPEND _paths "${${_var}}/lib") |
| 139 | + endif() |
| 140 | + endforeach() |
93 | 141 | list(REMOVE_DUPLICATES _paths) |
94 | 142 | if(_paths) |
95 | | - set(CMAKE_BUILD_RPATH "${_paths}" PARENT_SCOPE) |
96 | | - set(CMAKE_INSTALL_RPATH "${_paths}" PARENT_SCOPE) |
97 | | - set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE PARENT_SCOPE) |
98 | | - set(CMAKE_SKIP_BUILD_RPATH FALSE PARENT_SCOPE) |
99 | | - set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE PARENT_SCOPE) |
| 143 | + set_target_properties(${target_name} PROPERTIES |
| 144 | + BUILD_RPATH "${_paths}" |
| 145 | + INSTALL_RPATH "${_paths}" |
| 146 | + INSTALL_RPATH_USE_LINK_PATH TRUE |
| 147 | + SKIP_BUILD_RPATH FALSE |
| 148 | + BUILD_WITH_INSTALL_RPATH FALSE |
| 149 | + ) |
100 | 150 | endif() |
101 | 151 | endfunction() |
0 commit comments