CMakeImportBuildSettings.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Macro to import the build settings from another project. Provide as
  2. # an argument the file created by the other project's
  3. # CMAKE_EXPORT_BUILD_SETTINGS command.
  4. MACRO(CMAKE_IMPORT_BUILD_SETTINGS SETTINGS_FILE)
  5. IF(${SETTINGS_FILE} MATCHES ".+")
  6. # Load the settings.
  7. INCLUDE(${SETTINGS_FILE})
  8. # Check the CMake version that stored the settings.
  9. IF(${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}
  10. GREATER ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  11. MESSAGE(SEND_ERROR
  12. "${CMAKE_BUILD_SETTING_PROJECT_NAME} was built using CMake "
  13. "${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}, "
  14. "but this is CMake${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}. "
  15. "Please upgrade CMake to a more recent version.")
  16. ENDIF(${CMAKE_BUILD_SETTING_CMAKE_MAJOR_VERSION}.${CMAKE_BUILD_SETTING_CMAKE_MINOR_VERSION}
  17. GREATER ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
  18. # Check the build tool.
  19. STRING(COMPARE NOTEQUAL
  20. "x${CMAKE_BUILD_TOOL}" "x${CMAKE_BUILD_SETTING_BUILD_TOOL}"
  21. CMAKE_BUILD_TOOL_MISMATCH)
  22. IF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^msdev$")
  23. SET(CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO 1)
  24. ENDIF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^msdev$")
  25. IF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^devenv$")
  26. SET(CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO 1)
  27. ENDIF(CMAKE_BUILD_SETTING_BUILD_TOOL MATCHES "^devenv$")
  28. # Check the C++ compiler setting. If it is empty, the imported
  29. # project is not a C++ project, and doesn't need a matching compiler.
  30. IF(CMAKE_BUILD_SETTING_CXX_COMPILER)
  31. STRING(COMPARE NOTEQUAL
  32. "x${CMAKE_CXX_COMPILER}" "x${CMAKE_BUILD_SETTING_CXX_COMPILER}"
  33. CMAKE_CXX_COMPILER_MISMATCH)
  34. ENDIF(CMAKE_BUILD_SETTING_CXX_COMPILER)
  35. # Check the C build variation flags.
  36. STRING(COMPARE NOTEQUAL
  37. "x${CMAKE_C_FLAGS_DEBUG}" "x${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}"
  38. CMAKE_C_FLAGS_DEBUG_MISMATCH)
  39. STRING(COMPARE NOTEQUAL
  40. "x${CMAKE_C_FLAGS_RELEASE}" "x${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}"
  41. CMAKE_C_FLAGS_RELEASE_MISMATCH)
  42. STRING(COMPARE NOTEQUAL
  43. "x${CMAKE_C_FLAGS_MINSIZEREL}" "x${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}"
  44. CMAKE_C_FLAGS_MINSIZEREL_MISMATCH)
  45. STRING(COMPARE NOTEQUAL
  46. "x${CMAKE_C_FLAGS_RELWITHDEBINFO}" "x${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}"
  47. CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH)
  48. # Check the C++ build variation flags.
  49. STRING(COMPARE NOTEQUAL
  50. "x${CMAKE_CXX_FLAGS_DEBUG}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}"
  51. CMAKE_CXX_FLAGS_DEBUG_MISMATCH)
  52. STRING(COMPARE NOTEQUAL
  53. "x${CMAKE_CXX_FLAGS_RELEASE}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}"
  54. CMAKE_CXX_FLAGS_RELEASE_MISMATCH)
  55. STRING(COMPARE NOTEQUAL
  56. "x${CMAKE_CXX_FLAGS_MINSIZEREL}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}"
  57. CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH)
  58. STRING(COMPARE NOTEQUAL
  59. "x${CMAKE_CXX_FLAGS_RELWITHDEBINFO}" "x${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}"
  60. CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH)
  61. # Check the build type.
  62. SET(CMAKE_BUILD_TYPE_MISMATCH 0)
  63. IF(WIN32)
  64. IF(NOT CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO)
  65. STRING(COMPARE NOTEQUAL
  66. "x${CMAKE_BUILD_TYPE}" "x${CMAKE_BUILD_SETTING_BUILD_TYPE}"
  67. CMAKE_BUILD_TYPE_MISMATCH)
  68. ENDIF(NOT CMAKE_IMPORT_BUILD_SETTINGS_IMPORTING_FROM_MS_STUDIO)
  69. ENDIF(WIN32)
  70. # Build tool must match on Windows.
  71. IF(WIN32)
  72. IF(CMAKE_BUILD_TOOL_MISMATCH)
  73. MESSAGE(SEND_ERROR
  74. "${CMAKE_BUILD_SETTING_PROJECT_NAME} was built using "
  75. "\"${CMAKE_BUILD_SETTING_BUILD_TOOL}\", but ${PROJECT_NAME} "
  76. "is using \"${CMAKE_BUILD_TOOL}\". The build will fail. "
  77. "Try selecting a different CMake Generator.")
  78. ENDIF(CMAKE_BUILD_TOOL_MISMATCH)
  79. ENDIF(WIN32)
  80. # Enforce the C++ compiler setting.
  81. IF(CMAKE_CXX_COMPILER_MISMATCH)
  82. MESSAGE("Warning: CMake is forcing CMAKE_CXX_COMPILER to "
  83. "\"${CMAKE_BUILD_SETTING_CXX_COMPILER}\" to match that imported "
  84. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. This is required "
  85. "because C++ projects must use the same compiler. "
  86. "If this message appears for more than one imported project, "
  87. "you have conflicting C++ compilers and will have to "
  88. "re-build one of those projects.")
  89. SET(CMAKE_CXX_COMPILER ${CMAKE_BUILD_SETTING_CXX_COMPILER}
  90. CACHE STRING "C++ compiler imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  91. ENDIF(CMAKE_CXX_COMPILER_MISMATCH)
  92. # Enforce the build type.
  93. IF(CMAKE_BUILD_TYPE_MISMATCH)
  94. MESSAGE("Warning: CMake is forcing CMAKE_BUILD_TYPE to "
  95. "\"${CMAKE_BUILD_SETTING_BUILD_TYPE}\" to match that imported "
  96. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. This is required "
  97. "because projects must use the same compiler settings. "
  98. "If this message appears for more than one imported project, "
  99. "you have conflicting compiler settings and will have to "
  100. "re-build one of those projects.")
  101. SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_SETTING_BUILD_TYPE}
  102. CACHE STRING "Build type imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  103. ENDIF(CMAKE_BUILD_TYPE_MISMATCH)
  104. # Enforce the C build variation flags.
  105. IF(CMAKE_C_FLAGS_DEBUG_MISMATCH)
  106. MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_DEBUG to "
  107. "\"${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}\" to match that imported "
  108. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  109. "If this message appears for more than one imported project, "
  110. "you have conflicting options and will have to "
  111. "re-build one of those projects.")
  112. SET(CMAKE_C_FLAGS_DEBUG ${CMAKE_BUILD_SETTING_C_FLAGS_DEBUG}
  113. CACHE STRING "C DEBUG flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  114. ENDIF(CMAKE_C_FLAGS_DEBUG_MISMATCH)
  115. IF(CMAKE_C_FLAGS_RELEASE_MISMATCH)
  116. MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_RELEASE to "
  117. "\"${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}\" to match that imported "
  118. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  119. "If this message appears for more than one imported project, "
  120. "you have conflicting options and will have to "
  121. "re-build one of those projects.")
  122. SET(CMAKE_C_FLAGS_RELEASE ${CMAKE_BUILD_SETTING_C_FLAGS_RELEASE}
  123. CACHE STRING "C RELEASE flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  124. ENDIF(CMAKE_C_FLAGS_RELEASE_MISMATCH)
  125. IF(CMAKE_C_FLAGS_MINSIZEREL_MISMATCH)
  126. MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_MINSIZEREL to "
  127. "\"${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}\" to match that imported "
  128. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  129. "If this message appears for more than one imported project, "
  130. "you have conflicting options and will have to "
  131. "re-build one of those projects.")
  132. SET(CMAKE_C_FLAGS_MINSIZEREL ${CMAKE_BUILD_SETTING_C_FLAGS_MINSIZEREL}
  133. CACHE STRING "C MINSIZEREL flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  134. ENDIF(CMAKE_C_FLAGS_MINSIZEREL_MISMATCH)
  135. IF(CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH)
  136. MESSAGE("Warning: CMake is forcing CMAKE_C_FLAGS_RELWITHDEBINFO to "
  137. "\"${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}\" to match that imported "
  138. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  139. "If this message appears for more than one imported project, "
  140. "you have conflicting options and will have to "
  141. "re-build one of those projects.")
  142. SET(CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_BUILD_SETTING_C_FLAGS_RELWITHDEBINFO}
  143. CACHE STRING "C RELWITHDEBINFO flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  144. ENDIF(CMAKE_C_FLAGS_RELWITHDEBINFO_MISMATCH)
  145. # Enforce the C++ build variation flags.
  146. IF(CMAKE_CXX_FLAGS_DEBUG_MISMATCH)
  147. MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_DEBUG to "
  148. "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}\" to match that imported "
  149. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  150. "If this message appears for more than one imported project, "
  151. "you have conflicting options and will have to "
  152. "re-build one of those projects.")
  153. SET(CMAKE_CXX_FLAGS_DEBUG ${CMAKE_BUILD_SETTING_CXX_FLAGS_DEBUG}
  154. CACHE STRING "C++ DEBUG flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  155. ENDIF(CMAKE_CXX_FLAGS_DEBUG_MISMATCH)
  156. IF(CMAKE_CXX_FLAGS_RELEASE_MISMATCH)
  157. MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_RELEASE to "
  158. "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}\" to match that imported "
  159. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  160. "If this message appears for more than one imported project, "
  161. "you have conflicting options and will have to "
  162. "re-build one of those projects.")
  163. SET(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_BUILD_SETTING_CXX_FLAGS_RELEASE}
  164. CACHE STRING "C++ RELEASE flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  165. ENDIF(CMAKE_CXX_FLAGS_RELEASE_MISMATCH)
  166. IF(CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH)
  167. MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_MINSIZEREL to "
  168. "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}\" to match that imported "
  169. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  170. "If this message appears for more than one imported project, "
  171. "you have conflicting options and will have to "
  172. "re-build one of those projects.")
  173. SET(CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_BUILD_SETTING_CXX_FLAGS_MINSIZEREL}
  174. CACHE STRING "C++ MINSIZEREL flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  175. ENDIF(CMAKE_CXX_FLAGS_MINSIZEREL_MISMATCH)
  176. IF(CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH)
  177. MESSAGE("Warning: CMake is forcing CMAKE_CXX_FLAGS_RELWITHDEBINFO to "
  178. "\"${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}\" to match that imported "
  179. "from ${CMAKE_BUILD_SETTING_PROJECT_NAME}. "
  180. "If this message appears for more than one imported project, "
  181. "you have conflicting options and will have to "
  182. "re-build one of those projects.")
  183. SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_BUILD_SETTING_CXX_FLAGS_RELWITHDEBINFO}
  184. CACHE STRING "C++ RELWITHDEBINFO flags imported from ${CMAKE_BUILD_SETTING_PROJECT_NAME}." FORCE)
  185. ENDIF(CMAKE_CXX_FLAGS_RELWITHDEBINFO_MISMATCH)
  186. ELSE(${SETTINGS_FILE} MATCHES ".+")
  187. MESSAGE(SEND_ERROR "CMAKE_IMPORT_BUILD_SETTINGS called with no argument.")
  188. ENDIF(${SETTINGS_FILE} MATCHES ".+")
  189. ENDMACRO(CMAKE_IMPORT_BUILD_SETTINGS)