1 # - import build settings from another project
2 # CMAKE_IMPORT_BUILD_SETTINGS(SETTINGS_FILE)
3 # macro defined to import the build settings from another project.
4 # SETTINGS_FILE is a file created by the other project's call to the
5 # CMAKE_EXPORT_BUILD_SETTINGS macro, see CMakeExportBuildSettings.
6 MACRO(CMAKE_IMPORT_BUILD_SETTINGS SETTINGS_FILE)
7 IF(${SETTINGS_FILE} MATCHES ".+")
9 INCLUDE(${SETTINGS_FILE})
11 # Check the CMake version that stored the settings.
12 IF(${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}
13 GREATER ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
15 "${CMAKE_BUILD_SETTING_PROJECT_NAME} was built using CMake "
16 "${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}, "
17 "but this is CMake${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}. "
18 "Please upgrade CMake to a more recent version.")
19 ENDIF(${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}
20 GREATER ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
22 # Check the build tool.
23 SET(CMAKE_BUILD_TOOL1 "")
25 GET_FILENAME_COMPONENT (CMAKE_BUILD_TOOL1 ${CMAKE_BUILD_TOOL} NAME_WE)
26 STRING(TOLOWER ${CMAKE_BUILD_TOOL1} CMAKE_BUILD_TOOL1)
27 ENDIF(CMAKE_BUILD_TOOL)
28 SET(CMAKE_BUILD_TOOL2 "")
29 IF(CMAKE_BUILD_SETTING_BUILD_TOOL)
30 GET_FILENAME_COMPONENT (CMAKE_BUILD_TOOL2 ${CMAKE_BUILD_SETTING_BUILD_TOOL} NAME_WE)
31 STRING(TOLOWER ${CMAKE_BUILD_TOOL2} CMAKE_BUILD_TOOL2)
32 ENDIF(CMAKE_BUILD_SETTING_BUILD_TOOL)
33 STRING(COMPARE NOTEQUAL "x${CMAKE_BUILD_TOOL1}" "x${CMAKE_BUILD_TOOL2}"
34 CMAKE_BUILD_TOOL_MISMATCH)
36 IF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^msdev$")
37 SET(CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO 1)
38 ENDIF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^msdev$")
39 IF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^devenv$")
40 SET(CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO 1)
41 ENDIF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^devenv$")
43 # Check the C++ compiler setting. If it is empty, the imported
44 # project is not a C++ project, and doesn't need a matching compiler.
45 IF(CMAKE_BUILD_SETTING_CXX_COMPILER)
47 STRING(TOLOWER "x${CMAKE_CXX_COMPILER}" COMPARE_CXX_LOCAL)
48 STRING(TOLOWER "x${CMAKE_BUILD_SETTING_CXX_COMPILER}" COMPARE_CXX_REMOTE)
49 STRING(COMPARE NOTEQUAL "${COMPARE_CXX_LOCAL}" "${COMPARE_CXX_REMOTE}"
50 CMAKE_CXX_COMPILER_MISMATCH)
52 STRING(COMPARE NOTEQUAL
53 "x${CMAKE_CXX_COMPILER}" "x${CMAKE_BUILD_SETTING_CXX_COMPILER}"
54 CMAKE_CXX_COMPILER_MISMATCH)
56 ENDIF(CMAKE_BUILD_SETTING_CXX_COMPILER)
58 # Check the C build variation flags.
59 STRING(COMPARE NOTEQUAL
60 "x${CMAKE_C_FLAGS_DEBUG}" "x${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}"
61 CMAKE_C_FLAGS_DEBUG_MISMATCH)
62 STRING(COMPARE NOTEQUAL
63 "x${CMAKE_C_FLAGS_RELEASE}" "x${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}"
64 CMAKE_C_FLAGS_RELEASE_MISMATCH)
65 STRING(COMPARE NOTEQUAL
66 "x${CMAKE_C_FLAGS_MINSIZEREL}" "x${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}"
67 CMAKE_C_FLAGS_MINSIZEREL_MISMATCH)
68 STRING(COMPARE NOTEQUAL
69 "x${CMAKE_C_FLAGS_RELWITHDEBINFO}" "x${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}"
70 CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH)
72 # Check the C++ build variation flags.
73 STRING(COMPARE NOTEQUAL
74 "x${CMAKE_CXX_FLAGS_DEBUG}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}"
75 CMAKE_CXX_FLAGS_DEBUG_MISMATCH)
76 STRING(COMPARE NOTEQUAL
77 "x${CMAKE_CXX_FLAGS_RELEASE}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}"
78 CMAKE_CXX_FLAGS_RELEASE_MISMATCH)
79 STRING(COMPARE NOTEQUAL
80 "x${CMAKE_CXX_FLAGS_MINSIZEREL}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}"
81 CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH)
82 STRING(COMPARE NOTEQUAL
83 "x${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}"
84 CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH)
86 # Check the build type.
87 SET(CMAKE_BUILD_TYPE_MISMATCH 0)
89 IF(NOT CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO)
90 STRING(COMPARE NOTEQUAL
91 "x${CMAKE_BUILD_TYPE}" "x${CMAKE_BUILD_SETTING_BUILD_TYPE}"
92 CMAKE_BUILD_TYPE_MISMATCH)
93 ENDIF(NOT CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO)
96 # Build tool must match on Windows.
98 IF(CMAKE_BUILD_TOOL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
100 "This project, ${PROJECT_NAME}, depends on the project "
101 "${CMAKE_BUILD_SETTING_PROJECT_NAME}. Unfortunately "
102 "${CMAKE_BUILD_SETTING_PROJECT_NAME} was built using "
103 "\"${CMAKE_BUILD_TOOL2}\", but you are trying to build "
104 "${PROJECT_NAME} using \"${CMAKE_BUILD_TOOL1}\"."
105 "In order for this build to succeed, both projects must be "
106 "built with the same Generator. To change the Generator you "
107 "are using for the project ${PROJECT_NAME}, you must "
108 "delete the cache, and then rerun cmake and this "
109 "time select the same Generator that was used to build "
110 "${CMAKE_BUILD_SETTING_PROJECT_NAME}. If "
111 "${CMAKE_BUILD_SETTING_PROJECT_NAME} was built using a "
112 "generator that you do not have (for example it was built "
113 "with Visual Studio 6 and you only have 7) then you will "
114 "need to select a different version of "
115 "${CMAKE_BUILD_SETTING_PROJECT_NAME} or rebuild "
116 "${CMAKE_BUILD_SETTING_PROJECT_NAME} with the correct "
118 ENDIF(CMAKE_BUILD_TOOL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
121 # Enforce the C++ compiler setting.
122 # CMAKE_OVERRIDE_COMPILER_MISMATCH allow advanced user to override cmake detection of
123 # compiler mismatch between imported projects. Typical case on UNIX could be:
124 # 1. Compile a project with g++-3.3 while the imported project was configured
125 # using the 'c++' alias (which at the time meant g++-3.3)
126 # 2. This variable also becomes handy when the project your are importing has been
127 # compiled with a compiler you do not have access to, but offer a compatible ABI with
129 # WARNING: Do not use this variable with C++ compiler with incompatible ABI
130 IF(CMAKE_CXX_COMPILER_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
131 MESSAGE("Warning: CMake is forcing CMAKE_CXX_COMPILER to "
132 "\"${CMAKE_BUILD_SETTING_CXX_COMPILER}\" to match that imported "
133 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. This is required "
134 "because C++ projects must use the same compiler. "
135 "If this message appears for more than one imported project, "
136 "you have conflicting C++ compilers and will have to "
137 "re-build one of those projects. Was set to ${CMAKE_CXX_COMPILER}")
138 SET(CMAKE_CXX_COMPILER ${CMAKE_BUILD_SETTING_CXX_COMPILER}
139 CACHE STRING "C++ compiler imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
140 ENDIF(CMAKE_CXX_COMPILER_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
142 # Enforce the build type.
143 IF(CMAKE_BUILD_TYPE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
144 MESSAGE("Warning: CMake is forcing CMAKE_BUILD_TYPE to "
145 "\"${CMAKE_BUILD_SETTING_BUILD_TYPE}\" to match that imported "
146 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. This is required "
147 "because projects must use the same compiler settings. "
148 "If this message appears for more than one imported project, "
149 "you have conflicting compiler settings and will have to "
150 "re-build one of those projects.")
151 SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_SETTING_BUILD_TYPE}
152 CACHE STRING "Build type imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
153 ENDIF(CMAKE_BUILD_TYPE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
155 # Enforce the C build variation flags.
157 IF(CMAKE_C_FLAGS_DEBUG_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
158 MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_DEBUG to "
159 "\"${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}\" to match that imported "
160 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
161 "If this message appears for more than one imported project, "
162 "you have conflicting options and will have to "
163 "re-build one of those projects.")
164 SET(CMAKE_C_FLAGS_DEBUG ${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}
165 CACHE STRING "C DEBUG flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
166 ENDIF(CMAKE_C_FLAGS_DEBUG_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
168 IF(CMAKE_C_FLAGS_RELEASE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
169 MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_RELEASE to "
170 "\"${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}\" to match that imported "
171 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
172 "If this message appears for more than one imported project, "
173 "you have conflicting options and will have to "
174 "re-build one of those projects.")
175 SET(CMAKE_C_FLAGS_RELEASE ${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}
176 CACHE STRING "C RELEASE flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
177 ENDIF(CMAKE_C_FLAGS_RELEASE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
179 IF(CMAKE_C_FLAGS_MINSIZEREL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
180 MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_MINSIZEREL to "
181 "\"${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}\" to match that imported "
182 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
183 "If this message appears for more than one imported project, "
184 "you have conflicting options and will have to "
185 "re-build one of those projects.")
186 SET(CMAKE_C_FLAGS_MINSIZEREL ${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}
187 CACHE STRING "C MINSIZEREL flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
188 ENDIF(CMAKE_C_FLAGS_MINSIZEREL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
190 IF(CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
191 MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_RELWITHDEBINFO to "
192 "\"${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}\" to match that imported "
193 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
194 "If this message appears for more than one imported project, "
195 "you have conflicting options and will have to "
196 "re-build one of those projects.")
197 SET(CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}
198 CACHE STRING "C RELWITHDEBINFO flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
199 ENDIF(CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
201 # Enforce the C++ build variation flags.
203 IF(CMAKE_CXX_FLAGS_DEBUG_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
204 MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_DEBUG to "
205 "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}\" to match that imported "
206 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
207 "If this message appears for more than one imported project, "
208 "you have conflicting options and will have to "
209 "re-build one of those projects.")
210 SET(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}
211 CACHE STRING "C++ DEBUG flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
212 ENDIF(CMAKE_CXX_FLAGS_DEBUG_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
214 IF(CMAKE_CXX_FLAGS_RELEASE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
215 MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_RELEASE to "
216 "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}\" to match that imported "
217 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
218 "If this message appears for more than one imported project, "
219 "you have conflicting options and will have to "
220 "re-build one of those projects.")
221 SET(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}
222 CACHE STRING "C++ RELEASE flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
223 ENDIF(CMAKE_CXX_FLAGS_RELEASE_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
225 IF(CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
226 MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_MINSIZEREL to "
227 "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}\" to match that imported "
228 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
229 "If this message appears for more than one imported project, "
230 "you have conflicting options and will have to "
231 "re-build one of those projects.")
232 SET(CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}
233 CACHE STRING "C++ MINSIZEREL flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
234 ENDIF(CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
236 IF(CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
237 MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_RELWITHDEBINFO to "
238 "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}\" to match that imported "
239 "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
240 "If this message appears for more than one imported project, "
241 "you have conflicting options and will have to "
242 "re-build one of those projects.")
243 SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}
244 CACHE STRING "C++ RELWITHDEBINFO flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
245 ENDIF(CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH AND NOT CMAKE_OVERRIDE_COMPILER_MISMATCH)
247 ELSE(${SETTINGS_FILE} MATCHES ".+")
248 MESSAGE(SEND_ERROR "CMAKE_IMPORT_BUILD_SETTINGS called with no argument.")
249 ENDIF(${SETTINGS_FILE} MATCHES ".+")
250 ENDMACRO(CMAKE_IMPORT_BUILD_SETTINGS)