Debian Codegraph @

cmake/psi4OptionsTools.cmake

raw
1 ###This file contains functions used throughout the Psi4 build. Like source
2 ###code, the build system should be factored and common code extracted out into
3 ###functions/macros. If you find repetitive code throughout the build scripts
4 ###this is the place to add it (make sure you document it too).
5
6 #Macro for printing an option in a consistent manner
7 #
8 #Syntax: print_option(<option to print> <was specified>)
9 #
10 macro(print_option variable default)
11 if(NOT DEFINED ${variable} OR "${${variable}}" STREQUAL "")
12 message(STATUS "Setting (unspecified) option ${variable}: ${default}")
13 else()
14 message(STATUS "Setting option ${variable}: ${${variable}}")
15 endif()
16 endmacro()
17
18 # Wraps an option with default ON/OFF. Adds nice messaging to option()
19 #
20 #Syntax: option_with_print(<option name> <description> <default value>)
21 #
22 macro(option_with_print variable msge default)
23 print_option(${variable} ${default})
24 option(${variable} ${msge} ${default})
25 endmacro(option_with_print)
26
27 #Wraps an option with a default other than ON/OFF and prints it
28 #NOTE: Can't combine with above b/c CMake handles ON/OFF options specially
29 #NOTE2: CMAKE_BUILD_TYPE (and other CMake variables) are always defined so need
30 # to further check for if they are the NULL string. This is also why we
31 # need the force
32 #
33 #Syntax: option_with_default(<option name> <description> <default value>)
34 #
35 macro(option_with_default variable msge default)
36 print_option(${variable} ${default})
37 if(NOT DEFINED ${variable} OR "${${variable}}" STREQUAL "")
38 set(${variable} ${default} CACHE STRING ${msge} FORCE)
39 endif()
40 endmacro(option_with_default)
41
42 # Common guts to adding a Psi4 library irrespective of bin vs. lib home
43 #
44 # Syntax psi4_add_module(<lib or bin> <library name> <CMake list of sources> <dependencies>)
45 #
46 macro(psi4_add_module binlib libname sources)
47
48 set(current_sources ${${sources}};)
49 list(SORT current_sources)
50
51 add_library(${libname} STATIC ${current_sources})
52 set_target_properties(${libname} PROPERTIES POSITION_INDEPENDENT_CODE ${BUILD_FPIC})
53
54 # library modules get their headers installed
55 if((${binlib} MATCHES lib) OR (${binlib} MATCHES binlib))
56 install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
57 DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/psi4
58 FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "*.i")
59 endif()
60
61 # binary modules explicitly compiled into psi4.so
62 if((${binlib} MATCHES bin) OR (${binlib} MATCHES binlib))
63 set_property(GLOBAL APPEND PROPERTY BINLIST ${libname})
64 endif()
65
66 set(depend_name "${ARGN}")
67 foreach(name_i IN LISTS depend_name)
68 target_link_libraries(${libname} PRIVATE ${name_i})
69 endforeach()
70 target_link_libraries(${libname} PRIVATE pybind11::module)
71 target_link_libraries(${libname} PRIVATE tgt::lapack)
72 endmacro()
73
74 include(CheckCCompilerFlag)
75 include(CheckCXXCompilerFlag)
76 if(CMAKE_Fortran_COMPILER)
77 include(CheckFortranCompilerFlag) # CMake >= 3.3, so local copy in cmake/
78 endif()
79
80 #The guts of the next two functions, use the wrappers please
81 #
82 #Syntax: add_C_or_CXX_flags(<True for C, False for CXX>)
83 #
84 # Note: resist adding -Werror to the check_X_compiler_flag calls,
85 # as (i) the flag for Intel is actually -diag-error warn, (ii)
86 # Intel ifort doesn't define -Werror, and (iii) passing it
87 # changes REQUIRED_DEFINITIONS.
88 macro(add_C_or_CXX_flags is_C)
89 set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET})
90 set(CMAKE_REQUIRED_QUIET ON)
91 set(flags_to_try "${ARGN}")
92 foreach(flag_i IN LISTS flags_to_try ITEMS -brillig)
93 if(${flag_i} STREQUAL "-brillig")
94 message(WARNING "Option unfulfilled as none of ${flags_to_try} valid")
95 break()
96 endif()
97 unset(test_option CACHE)
98 if(${is_C} EQUAL 0)
99 CHECK_C_COMPILER_FLAG("${flag_i}" test_option)
100 set(description_to_print CMAKE_C_FLAGS)
101 elseif(${is_C} EQUAL 1)
102 CHECK_CXX_COMPILER_FLAG("${flag_i}" test_option)
103 set(description_to_print CMAKE_CXX_FLAGS)
104 elseif(${is_C} EQUAL 2)
105 CHECK_Fortran_COMPILER_FLAG("${flag_i}" test_option)
106 set(description_to_print CMAKE_Fortran_FLAGS)
107 endif()
108 set(msg_base "Performing Test ${description_to_print} [${flag_i}] -")
109 if(${test_option})
110 set(${description_to_print} "${${description_to_print}} ${flag_i}")
111 if(NOT CMAKE_REQUIRED_QUIET_SAVE)
112 message(STATUS "${msg_base} Success, Appending")
113 endif()
114 break()
115 else()
116 if(NOT CMAKE_REQUIRED_QUIET_SAVE)
117 message(STATUS "${msg_base} Failed")
118 endif()
119 endif()
120 endforeach()
121 set(CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE})
122 endmacro()
123
124
125
126 #Checks if C flags are valid, if so adds them to CMAKE_C_FLAGS
127 #Input should be a list of flags to try. If two flags are to be tried together
128 #enclose them in quotes, e.g. "-L/path/to/dir -lmylib" is tried as a single
129 #flag, whereas "-L/path/to/dir" "-lmylib" is tried as two separate flags.
130 #The first list item to succeed is added to CMAKE_C_FLAGS, then try loop
131 #breaks. Warning issued if no flags in list succeed.
132 #
133 #
134 #Syntax: add_C_flags(<flags to add>)
135 #
136 macro(add_C_flags)
137 add_C_or_CXX_flags(0 ${ARGN})
138 endmacro()
139
140 #Checks if CXX flags are valid, if so adds them to CMAKE_CXX_FLAGS
141 #See add_C_flags for more info on syntax
142 #
143 #Syntax: add_CXX_flags(<flags to add>)
144 #
145 macro(add_CXX_flags)
146 add_C_or_CXX_flags(1 ${ARGN})
147 endmacro()
148
149 #Checks if Fortran flags are valid, if so adds them to CMAKE_Fortran_FLAGS
150 #See add_C_flags for more info on syntax
151 #
152 #Syntax: add_Fortran_flags(<flags to add>)
153 #
154 macro(add_Fortran_flags)
155 add_C_or_CXX_flags(2 ${ARGN})
156 endmacro()
157
158 #Macro for adding flags common to both C and CXX, if the compiler supports them
159 #
160 #Syntax: add_flags(<flags to add>)
161 #
162 macro(add_flags FLAGS)
163 get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
164 list(FIND languages "C" _index_c)
165 list(FIND languages "CXX" _index_cxx)
166 list(FIND languages "Fortran" _index_fortran)
167 if (${_index_c} GREATER -1)
168 add_C_flags(${FLAGS})
169 endif()
170 if (${_index_cxx} GREATER -1)
171 add_CXX_flags(${FLAGS})
172 endif()
173 if (${_index_fortran} GREATER -1)
174 add_Fortran_flags(${FLAGS})
175 endif()
176 endmacro()
177
178 #Defines an option that if enabled turns on some compiler flags
179 #
180 #Syntax: option_with_flags(<option> <description> <default value> <flags>)
181 #
182 macro(option_with_flags option msg default)
183 print_option(${option} ${default})
184 option(${option} ${msg} ${default})
185 if(${${option}})
186 add_flags("${ARGN}")
187 endif()
188 endmacro()
189
190 #Macro so I don't have to look at a ton of if statements for adding each plugin
191 #
192 #Syntax: optional_plugin(<plugin name>)
193 #
194 macro(optional_plugin plugin_name)
195 string(TOUPPER ${plugin_name} PLUGIN_NAME)
196 if(${ENABLE_${PLUGIN_NAME}})
197 find_package(${plugin_name} REQUIRED)
198 set_property(GLOBAL APPEND PROPERTY PSI4_MODULES ${${PLUGIN_NAME}_LIBRARIES})
199 add_definitions(-DENABLE_${PLUGIN_NAME})
200 else()
201 add_library(${plugin_name} INTERFACE)
202 endif()
203 endmacro(optional_plugin plugin_name test_names)
204
205 #Macro for adding a skeleton plugin
206 macro(add_skeleton_plugin PLUG TEMPLATE TESTLABELS)
207 set(CCSD "${CMAKE_CURRENT_SOURCE_DIR}")
208 set(CCBD "${CMAKE_CURRENT_BINARY_DIR}")
209 set(PSIEXE ${STAGED_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/psi4)
210 set(DIR_2_PASS ${CMAKE_PREFIX_PATH} ${STAGED_INSTALL_PREFIX})
211
212 add_custom_target(plugin_${PLUG}
213 ALL
214 DEPENDS psi4-core
215 COMMAND ${CMAKE_COMMAND} -E remove_directory ${CCBD}/${PLUG}
216 COMMAND ${PSIEXE} --plugin-name ${PLUG} --plugin-template ${TEMPLATE}
217 COMMAND ${CMAKE_COMMAND} -E chdir "${CCBD}/${PLUG}" cmake -C ${STAGED_INSTALL_PREFIX}/share/cmake/psi4/psi4PluginCache.cmake "-DCMAKE_PREFIX_PATH=${DIR_2_PASS}" .
218 COMMAND ${CMAKE_COMMAND} -E chdir "${CCBD}/${PLUG}" ${CMAKE_MAKE_PROGRAM}
219 COMMAND ${CMAKE_COMMAND} -E create_symlink ${CCBD}/${PLUG}/input.dat ${CCSD}/input.dat
220 COMMAND ${CMAKE_COMMAND} -E create_symlink "${PLUG}/${PLUG}.so" "${PLUG}.so"
221 COMMAND ${CMAKE_COMMAND} -E create_symlink "${PLUG}/__init__.py" "__init__.py"
222 COMMAND ${CMAKE_COMMAND} -E create_symlink "${PLUG}/pymodule.py" "pymodule.py"
223 COMMENT "Build ${PLUG} example plugin"
224 VERBATIM)
225
226 include(TestingMacros)
227 add_regression_test(${PLUG} "${TESTLABELS}")
228 endmacro()
229
230