Debian Codegraph @

cmake/tribits/examples/TribitsExampleApp2/AppHelperFuncs.cmake

raw
1 include(CMakePrintHelpers)
2
3
4 # Find TribitsExProj2 package(s), load compilers and compiler options, and get
5 # CMake lib targets (which have include dirs also) to link against.
6 #
7 # On return, sets the vars in the current scope:
8 #
9 # * TribitsExProj2_SELECTED_PACKAGE_LIST: List of all of the packages pulled in
10 # * from the TribitsExProj2.
11 #
12 # * APP_DEPS_LIB_TARGETS: List of all of the IMPORTED CMake targets that 'app'
13 # must link against
14 #
15 # * CMAKE_<LANG>_COMPILER and CMAKE_<LANG>_FLAGS pulled in from the
16 # TribitsExProj2Config.config or a <Package>Config.cmake file
17 #
18 macro(getTribitsExProj2StuffForApp)
19
20 set(${PROJECT_NAME}_FIND_INDIVIDUAL_PACKAGES OFF CACHE BOOL
21 "Set to TRUE to find individual packages and OFF to find project TribitsExProj2")
22
23 if (${PROJECT_NAME}_FIND_INDIVIDUAL_PACKAGES)
24 getTribitsExProj2StuffForAppByPackage()
25 else()
26 getTribitsExProj2StuffForAppByProject()
27 endif()
28
29 endmacro()
30
31
32 # Get TribitsExProj2 stuff with find_package(<Package>) for each
33 # package/component independently.
34 #
35 macro(getTribitsExProj2StuffForAppByPackage)
36
37 # Find each package and gather up all the <Package>::all_libs targets
38 set(APP_DEPS_LIB_TARGETS "")
39 foreach (packageName IN LISTS ${PROJECT_NAME}_USE_COMPONENTS)
40 find_package(${packageName} CONFIG REQUIRED)
41 message("Found ${packageName}!")
42 list(APPEND APP_DEPS_LIB_TARGETS ${packageName}::all_libs)
43 endforeach()
44 print_var(APP_DEPS_LIB_TARGETS)
45
46 # Set TribitsExProj2_SELECTED_PACKAGE_LIST
47 set(TribitsExProj2_SELECTED_PACKAGE_LIST ${${PROJECT_NAME}_USE_COMPONENTS})
48 # NOTE: We are setting his here since TribitsExProj2Config.cmake is not being
49 # read in in this case.
50
51 # Get compilers from first package listed
52 list(GET ${PROJECT_NAME}_USE_COMPONENTS 0 firstPkg)
53 setCompilersForAppFromConfigFileCompilers(${firstPkg})
54
55 endmacro()
56
57
58 # Get TribitsExProj2 stuff from find_package(TribitsExProj2)
59 #
60 macro(getTribitsExProj2StuffForAppByProject)
61
62 find_package(TribitsExProj2 CONFIG REQUIRED
63 COMPONENTS ${${PROJECT_NAME}_USE_COMPONENTS})
64
65 message("\nFound TribitsExProj2! Here are the details: ")
66 message(" TribitsExProj2_DIR = ${TribitsExProj2_DIR}")
67 message(" TribitsExProj2_VERSION = ${TribitsExProj2_VERSION}")
68 message(" TribitsExProj2_PACKAGE_LIST = ${TribitsExProj2_PACKAGE_LIST}")
69 message(" TribitsExProj2_TPL_LIST = ${TribitsExProj2_TPL_LIST}")
70 message(" TribitsExProj2_BUILD_SHARED_LIBS = ${TribitsExProj2_BUILD_SHARED_LIBS}")
71 message("End of TribitsExProj2 details\n")
72
73 # Make sure to use same compilers and flags as TribitsExProj2
74 setCompilersForAppFromConfigFileCompilers(TribitsExProj2)
75
76 # Get the libraries for building and linking
77 if (${PROJECT_NAME}_USE_COMPONENTS)
78 set(APP_DEPS_LIB_TARGETS TribitsExProj2::all_selected_libs)
79 else()
80 set(APP_DEPS_LIB_TARGETS TribitsExProj2::all_libs)
81 endif()
82
83 endmacro()
84
85
86 # Get compilers and compiler flags from the imported
87 # ``TribitsExProj2Config.cmake`` or ``<Package>Config.cmake`` file.
88 #
89 # Here ``prefix`` is the prefix for the variables read in from the
90 # *Config.cmake file.
91 #
92 macro(setCompilersForAppFromConfigFileCompilers prefix)
93
94 message("-- Setting compilers and flags read in from '${prefix}Config.cmake' file:")
95
96 set(CMAKE_CXX_COMPILER ${${prefix}_CXX_COMPILER} )
97 set(CMAKE_C_COMPILER ${${prefix}_C_COMPILER} )
98 set(CMAKE_Fortran_COMPILER ${${prefix}_Fortran_COMPILER} )
99
100 set(CMAKE_CXX_FLAGS "${${prefix}_CXX_COMPILER_FLAGS} ${CMAKE_CXX_FLAGS}")
101 set(CMAKE_C_FLAGS "${${prefix}_C_COMPILER_FLAGS} ${CMAKE_C_FLAGS}")
102 set(CMAKE_Fortran_FLAGS "${${prefix}_Fortran_COMPILER_FLAGS} ${CMAKE_Fortran_FLAGS}")
103
104 cmake_print_variables(CMAKE_CXX_COMPILER)
105 cmake_print_variables(CMAKE_C_COMPILER)
106 cmake_print_variables(CMAKE_Fortran_COMPILER)
107 cmake_print_variables(CMAKE_CXX_FLAGS)
108 cmake_print_variables(CMAKE_C_FLAGS)
109 cmake_print_variables(CMAKE_Fortran_FLAGS)
110
111 endmacro()
112
113
114 # Add compiler defines to the ``app`` target for optionally supported packages
115 # from upstream TribitExProj
116 #
117 function(addAppDepCompileDefines)
118 addAppDepCompileDefine("Package1")
119 addAppDepCompileDefine("Package2")
120 addAppDepCompileDefine("Package3")
121 endfunction()
122
123
124 function(addAppDepCompileDefine componentName)
125 if (${componentName} IN_LIST TribitsExProj2_SELECTED_PACKAGE_LIST)
126 string(TOUPPER "${componentName}" componentNameUpper)
127 target_compile_definitions(app PRIVATE TRIBITSEXAPP2_HAVE_${componentNameUpper})
128 endif()
129 endfunction()
130
131
132 # Return the extended dependency string from the app at runtime given the
133 # enabled packages from TribitsExProj2.
134 #
135 function(getExpectedAppDepsStr expectedDepsStrOut)
136
137 set(tpl1 "tpl1") # ToDo: Change to Tpl1{no deps}?
138 set(tpl2a "Tpl2a{${tpl1}}")
139 set(tpl2b "Tpl2b{no deps}")
140 set(tpl3 "Tpl3{${tpl2a}, ${tpl2b}}")
141 set(tpl4 "Tpl4{${tpl3}, ${tpl2a}, ${tpl2b}}")
142
143 set(package1 "Package1{${tpl1}}")
144
145 if (Package2_ENABLE_Tpl3)
146 set(package2_tpl3_deps_str ", ${tpl3}")
147 else()
148 set(package2_tpl3_deps_str "")
149 endif()
150 set(package2 "Package2{${package1}${package2_tpl3_deps_str}}")
151
152 if (Package3_ENABLE_Package2)
153 set(package3_package2_deps_str "${package2}, ")
154 else()
155 set(package3_package2_deps_str "")
156 endif()
157 if (Package3_ENABLE_Tpl4)
158 set(package3_tpl4_deps_str "${tpl4}, ")
159 else()
160 set(package3_tpl4_deps_str "")
161 endif()
162 set(package3
163 "Package3{${package3_package2_deps_str}${package1}, ${package3_tpl4_deps_str}${tpl2a}, ${tpl2b}}")
164
165 if (TARGET Package3::all_libs)
166 set(depsStr "${package3}")
167 elseif (TARGET Package2::all_libs)
168 set(depsStr "${package2}")
169 elseif (TARGET Package1::all_libs)
170 set(depsStr "${package1}")
171 else()
172 message("No package targets exist!")
173 set(depsStr "no deps")
174 endif()
175
176 set(${expectedDepsStrOut} "${depsStr}" PARENT_SCOPE)
177
178 endfunction()
179
180
181 function(print_var varName)
182 message("-- ${varName} = '${${varName}}'")
183 endfunction()