Debian Codegraph @

cmake/tribits/xsdk/XSDKDefaults.cmake

raw
1 ##################################################################################
2 #
3 # Set defaults for XSDK CMake projects
4 #
5 ##################################################################################
6
7 #
8 # This module implements standard behavior for XSDK CMake projects. The main
9 # thing it does in XSDK mode (i.e. USE_XSDK_DEFAULTS=TRUE) is to print out
10 # when the env vars CC, CXX, FC and compiler flags CFLAGS, CXXFLAGS, and
11 # FFLAGS/FCFLAGS are used to select the compilers and compiler flags (raw
12 # CMake does this silently) and to set BUILD_SHARED_LIBS=TRUE and
13 # CMAKE_BUILD_TYPE=DEBUG by default. It does not implement *all* of the
14 # standard XSDK configuration parameters. The parent CMake project must do
15 # that.
16 #
17 # Note that when USE_XSDK_DEFAULTS=TRUE, then the Fortran flags will be read
18 # from either of the env vars FFLAGS or FCFLAGS. If both are set, but are the
19 # same, then FFLAGS it used (which is the same as FCFLAGS). However, if both
20 # are set but are not equal, then a FATAL_ERROR is raised and CMake configure
21 # processing is stopped.
22 #
23 # To be used in a parent project, this module must be included after
24 #
25 # project(${PROJECT_NAME} NONE)
26 #
27 # is called but before the compilers are defined and processed using:
28 #
29 # enable_language(<LANG>)
30 #
31 # For example, one would do:
32 #
33 # project(${PROJECT_NAME} NONE)
34 # ...
35 # set(USE_XSDK_DEFAULTS_DEFAULT TRUE) # Set to false if desired
36 # include("${CMAKE_CURRENT_SOURCE_DIR}/stdk/XSDKDefaults.cmake")
37 # ...
38 # enable_language(C)
39 # enable_language(C++)
40 # enable_language(Fortran)
41 #
42 # The variable `USE_XSDK_DEFAULTS_DEFAULT` is used as the default for the
43 # cache var `USE_XSDK_DEFAULTS`. That way, a project can decide if it wants
44 # XSDK defaults turned on or off by default and users can independently decide
45 # if they want the CMake project to use standard XSDK behavior or raw CMake
46 # behavior.
47 #
48 # By default, the XSDKDefaults.cmake module assumes that the project will need
49 # C, C++, and Fortran. If any language is not needed then, set
50 # XSDK_ENABLE_C=OFF, XSDK_ENABLE_CXX=OFF, or XSDK_ENABLE_Fortran=OFF *before*
51 # including this module. Note, these variables are *not* cache vars because a
52 # project either does or does not have C, C++ or Fortran source files, the
53 # user has nothing to do with this so there is no need for cache vars. The
54 # parent CMake project just needs to tell XSDKDefault.cmake what languages is
55 # needs or does not need.
56 #
57 # For example, if the parent CMake project only needs C, then it would do:
58 #
59 # project(${PROJECT_NAME} NONE)'
60 # ...
61 # set(USE_XSDK_DEFAULTS_DEFAULT TRUE)
62 # set(XSDK_ENABLE_CXX OFF)
63 # set(XSDK_ENABLE_Fortran OFF)
64 # include("${CMAKE_CURRENT_SOURCE_DIR}/stdk/XSDKDefaults.cmake")
65 # ...
66 # enable_langauge(C)
67 #
68 # This module code will announce when it sets any variables.
69 #
70
71 #
72 # Helper functions
73 #
74
75 if (NOT COMMAND PRINT_VAR)
76 function(print_var VAR_NAME)
77 message("${VAR_NAME} = '${${VAR_NAME}}'")
78 endfunction()
79 endif()
80
81 if (NOT COMMAND SET_DEFAULT)
82 macro(set_default VAR)
83 if ("${${VAR}}" STREQUAL "")
84 set(${VAR} ${ARGN})
85 endif()
86 endmacro()
87 endif()
88
89 #
90 # XSDKDefaults.cmake control variables
91 #
92
93 # USE_XSDK_DEFAULTS
94 if ("${USE_XSDK_DEFAULTS_DEFAULT}" STREQUAL "")
95 set(USE_XSDK_DEFAULTS_DEFAULT FALSE)
96 endif()
97 set(USE_XSDK_DEFAULTS ${USE_XSDK_DEFAULTS_DEFAULT} CACHE BOOL
98 "Use XSDK defaults and behavior.")
99 print_var(USE_XSDK_DEFAULTS)
100
101 set_default(XSDK_ENABLE_C TRUE)
102 set_default(XSDK_ENABLE_CXX TRUE)
103 set_default(XSDK_ENABLE_Fortran TRUE)
104
105 # Handle the compiler and flags for a language
106 macro(xsdk_handle_lang_defaults CMAKE_LANG_NAME ENV_LANG_NAME
107 ENV_LANG_FLAGS_NAMES
108 )
109
110 # Announce using env var ${ENV_LANG_NAME}
111 if (NOT "$ENV{${ENV_LANG_NAME}}" STREQUAL "" AND
112 "${CMAKE_${CMAKE_LANG_NAME}_COMPILER}" STREQUAL ""
113 )
114 message("-- " "XSDK: Setting CMAKE_${CMAKE_LANG_NAME}_COMPILER from env var"
115 " ${ENV_LANG_NAME}='$ENV{${ENV_LANG_NAME}}'!")
116 set(CMAKE_${CMAKE_LANG_NAME}_COMPILER "$ENV{${ENV_LANG_NAME}}" CACHE FILEPATH
117 "XSDK: Set by default from env var ${ENV_LANG_NAME}")
118 endif()
119
120 # Announce using env var ${ENV_LANG_FLAGS_NAME}
121 foreach(ENV_LANG_FLAGS_NAME ${ENV_LANG_FLAGS_NAMES})
122 if (NOT "$ENV{${ENV_LANG_FLAGS_NAME}}" STREQUAL "" AND
123 "${CMAKE_${CMAKE_LANG_NAME}_FLAGS}" STREQUAL ""
124 )
125 message("-- " "XSDK: Setting CMAKE_${CMAKE_LANG_NAME}_FLAGS from env var"
126 " ${ENV_LANG_FLAGS_NAME}='$ENV{${ENV_LANG_FLAGS_NAME}}'!")
127 set(CMAKE_${CMAKE_LANG_NAME}_FLAGS "$ENV{${ENV_LANG_FLAGS_NAME}} " CACHE STRING
128 "XSDK: Set by default from env var ${ENV_LANG_FLAGS_NAME}")
129 # NOTE: CMake adds the space after $ENV{${ENV_LANG_FLAGS_NAME}} so we
130 # duplicate that here!
131 endif()
132 endforeach()
133
134 endmacro()
135
136
137 #
138 # Set XSDK Defaults
139 #
140
141 # Set default compilers and flags
142 if (USE_XSDK_DEFAULTS)
143
144 # Handle env vars for languages C, C++, and Fortran
145
146 if (XSDK_ENABLE_C)
147 xsdk_handle_lang_defaults(C CC CFLAGS)
148 endif()
149
150 if (XSDK_ENABLE_CXX)
151 xsdk_handle_lang_defaults(CXX CXX CXXFLAGS)
152 endif()
153
154 if (XSDK_ENABLE_Fortran)
155 set(ENV_FFLAGS "$ENV{FFLAGS}")
156 set(ENV_FCFLAGS "$ENV{FCFLAGS}")
157 if (
158 (NOT "${ENV_FFLAGS}" STREQUAL "") AND (NOT "${ENV_FCFLAGS}" STREQUAL "")
159 AND
160 ("${CMAKE_Fortran_FLAGS}" STREQUAL "")
161 )
162 if (NOT "${ENV_FFLAGS}" STREQUAL "${ENV_FCFLAGS}")
163 message(FATAL_ERROR "Error, env vars FFLAGS='${ENV_FFLAGS}' and"
164 " FCFLAGS='${ENV_FCFLAGS}' are both set in the env but are not equal!")
165 endif()
166 endif()
167 xsdk_handle_lang_defaults(Fortran FC "FFLAGS;FCFLAGS")
168 endif()
169
170 # Set XSDK defaults for other CMake variables
171
172 if ("${BUILD_SHARED_LIBS}" STREQUAL "")
173 message("-- " "XSDK: Setting default BUILD_SHARED_LIBS=TRUE")
174 set(BUILD_SHARED_LIBS TRUE CACHE BOOL "Set by default in XSDK mode")
175 endif()
176
177 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
178 message("-- " "XSDK: Setting default CMAKE_BUILD_TYPE=DEBUG")
179 set(CMAKE_BUILD_TYPE DEBUG CACHE STRING "Set by default in XSDK mode")
180 endif()
181
182 endif()