CMakeLists.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. cmake_minimum_required(VERSION 2.6)
  2. project(Preprocess)
  3. # This test is meant both as a test and as a reference for supported
  4. # syntax on native tool command lines.
  5. # Determine the build tool being used. Not all characters can be
  6. # escaped for all build tools. This test checks all characters known
  7. # to work with each tool and documents those known to not work.
  8. if("${CMAKE_GENERATOR}" MATCHES "Xcode")
  9. set(PP_XCODE 1)
  10. endif()
  11. if("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles")
  12. set(PP_UMAKE 1)
  13. endif()
  14. if("${CMAKE_GENERATOR}" MATCHES "NMake Makefiles")
  15. set(PP_NMAKE 1)
  16. endif()
  17. if("${CMAKE_GENERATOR}" MATCHES "MinGW Makefiles")
  18. set(PP_MINGW 1)
  19. endif()
  20. if("${CMAKE_GENERATOR}" MATCHES "Borland Makefiles")
  21. set(PP_BORLAND 1)
  22. endif()
  23. if("${CMAKE_GENERATOR}" MATCHES "Watcom WMake")
  24. set(PP_WATCOM 1)
  25. endif()
  26. if("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  27. set(PP_VS 1)
  28. endif()
  29. if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
  30. "x${CMAKE_C_SIMULATE_ID}" STREQUAL "xMSVC")
  31. set(CLANG_MSVC_WINDOWS 1)
  32. endif()
  33. if(CLANG_MSVC_WINDOWS AND
  34. "x${CMAKE_C_COMPILER_FRONTEND_VARIANT}" STREQUAL "xGNU")
  35. set(CLANG_GNULIKE_WINDOWS 1)
  36. endif()
  37. # Some tests below check the PP_* variables set above. They are meant
  38. # to test the case that the build tool is at fault. Other tests below
  39. # check the compiler that will be used when the compiler is at fault
  40. # (does not work even from a command shell).
  41. #-----------------------------------------------------------------------------
  42. # Construct a C-string literal to test passing through a definition on
  43. # the command line. We configure the value into a header so it can be
  44. # checked in the executable at runtime. The semicolon is handled
  45. # specially because it needs to be escaped in the COMPILE_DEFINITIONS
  46. # property value to avoid separating definitions but the string value
  47. # must not have it escaped inside the configured header.
  48. set(STRING_EXTRA "")
  49. if(NOT BORLAND)
  50. # Borland: ;
  51. # The Borland compiler will simply not accept a non-escaped semicolon
  52. # on the command line. If it is escaped \; then the escape character
  53. # shows up in the preprocessing output too.
  54. set(SEMICOLON "\;")
  55. endif()
  56. string(APPEND STRING_EXTRA " ")
  57. if(NOT PP_BORLAND AND NOT PP_WATCOM AND NOT CLANG_GNULIKE_WINDOWS)
  58. # Borland, WMake: multiple spaces
  59. # The make tool seems to remove extra whitespace from inside
  60. # quoted strings when passing to the compiler. It does not have
  61. # trouble passing to other tools, and the compiler may be directly
  62. # invoked from the command line.
  63. string(APPEND STRING_EXTRA " ")
  64. endif()
  65. if(NOT PP_VS)
  66. # VS: ,
  67. # Visual Studio will not accept a comma in the value of a definition.
  68. # The comma-separated list of PreprocessorDefinitions in the project
  69. # file seems to be parsed before the content of entries is examined.
  70. string(APPEND STRING_EXTRA ",")
  71. endif()
  72. if(NOT PP_MINGW AND NOT CLANG_GNULIKE_WINDOWS)
  73. # MinGW: &
  74. # When inside -D"FOO=\"a & b\"" MinGW make wants -D"FOO=\"a "&" b\""
  75. # but it does not like quoted ampersand elsewhere.
  76. string(APPEND STRING_EXTRA "&")
  77. endif()
  78. if(NOT PP_MINGW AND NOT CLANG_GNULIKE_WINDOWS)
  79. # MinGW: |
  80. # When inside -D"FOO=\"a | b\"" MinGW make wants -D"FOO=\"a "|" b\""
  81. # but it does not like quoted pipe elsewhere.
  82. string(APPEND STRING_EXTRA "|")
  83. endif()
  84. if(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
  85. # Borland, NMake, MinGW: ^
  86. # When inside -D"FOO=\"a ^ b\"" the make tools want -D"FOO=\"a "^" b\""
  87. # but do not like quoted carrot elsewhere. In NMake the non-quoted
  88. # syntax works when the flags are not in a make variable.
  89. string(APPEND STRING_EXTRA "^")
  90. endif()
  91. if(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
  92. # Borland, MinGW: < >
  93. # Angle-brackets have funny behavior that is hard to escape.
  94. string(APPEND STRING_EXTRA "<>")
  95. endif()
  96. set(EXPR_OP1 "/")
  97. if((NOT MSVC OR PP_NMAKE) AND
  98. NOT CMAKE_C_COMPILER_ID STREQUAL "Intel" AND
  99. NOT CLANG_MSVC_WINDOWS)
  100. # MSVC cl, Intel icl: %
  101. # When the cl compiler is invoked from the command line then % must
  102. # be written %% (to distinguish from %ENV% syntax). However cl does
  103. # not seem to accept the syntax when it is invoked from inside a
  104. # make tool (nmake, mingw32-make, etc.). Instead the argument must
  105. # be placed inside a response file. Then cl accepts it because it
  106. # parses the response file as it would the normal windows command
  107. # line. Currently only NMake supports running cl with a response
  108. # file. Supporting other make tools would require CMake to generate
  109. # response files explicitly for each object file.
  110. #
  111. # When the icl compiler is invoked from the command line then % must
  112. # be written just '%'. However nmake requires '%%' except when using
  113. # response files. Currently we have no way to affect escaping based
  114. # on whether flags go in a response file, so we just have to skip it.
  115. string(APPEND STRING_EXTRA "%")
  116. set(EXPR_OP1 "%")
  117. endif()
  118. # XL: )(
  119. # The XL compiler cannot pass unbalanced parens correctly to a tool
  120. # it launches internally.
  121. if(CMAKE_C_COMPILER_ID STREQUAL "XL")
  122. string(APPEND STRING_EXTRA "()")
  123. else()
  124. string(APPEND STRING_EXTRA ")(")
  125. endif()
  126. # General: \"
  127. # Make tools do not reliably accept \\\" syntax:
  128. # - MinGW and MSYS make tools crash with \\\"
  129. # - Borland make actually wants a mis-matched quote \\"
  130. # or $(BACKSLASH)\" where BACKSLASH is a variable set to \\
  131. # - VS IDE gets confused about the bounds of the definition value \\\"
  132. # - NMake is okay with just \\\"
  133. # - The XL compiler does not re-escape \\\" when launching an
  134. # internal tool to do preprocessing .
  135. if((PP_NMAKE OR PP_UMAKE) AND
  136. NOT CMAKE_C_COMPILER_ID STREQUAL "XL")
  137. string(APPEND STRING_EXTRA "\\\"")
  138. endif()
  139. # General: #
  140. # MSVC will not accept a # in the value of a string definition on the
  141. # command line. The character seems to be simply replaced by an
  142. # equals =. According to "cl -help" definitions may be specified by
  143. # -DMACRO#VALUE as well as -DMACRO=VALUE. It must be implemented by a
  144. # simple search-and-replace.
  145. #
  146. # The Borland compiler will parse both # and \# as just # but the make
  147. # tool seems to want \# sometimes and not others.
  148. #
  149. # Unix make does not like # in variable settings without extra
  150. # escaping. This could probably be fixed but since MSVC does not
  151. # support it and it is not an operator it is not worthwhile.
  152. # Compose the final test string.
  153. set(STRING_VALUE "hello`~!@$*_+-=}{][:'.?/${STRING_EXTRA}world")
  154. #-----------------------------------------------------------------------------
  155. # Function-style macro command-line support:
  156. # - Borland does not support
  157. # - MSVC does not support
  158. # - Watcom does not support
  159. # - GCC supports
  160. # Too few platforms support this to bother implementing.
  161. # People can just configure headers with the macros.
  162. #-----------------------------------------------------------------------------
  163. # Construct a sample expression to pass as a macro definition.
  164. set(EXPR "x*y+!(x==(y+1*2))*f(x${EXPR_OP1}2)")
  165. if(NOT WATCOM)
  166. # Watcom does not support - or / because it parses them as options.
  167. string(APPEND EXPR " + y/x-x")
  168. endif()
  169. #-----------------------------------------------------------------------------
  170. # Inform the test if the debug configuration is getting built.
  171. # The NDEBUG definition takes care of this for release.
  172. string(APPEND CMAKE_C_FLAGS_DEBUG " -DPREPROCESS_DEBUG")
  173. string(APPEND CMAKE_CXX_FLAGS_DEBUG " -DPREPROCESS_DEBUG")
  174. # Inform the test if it built from Xcode.
  175. if(PP_XCODE)
  176. set(PREPROCESS_XCODE 1)
  177. endif()
  178. # Test old-style definitions.
  179. add_definitions(-DOLD_DEF -DOLD_EXPR=2)
  180. # Make sure old-style definitions are converted to directory property.
  181. set(OLD_DEFS_EXPECTED "OLD_DEF;OLD_EXPR=2")
  182. get_property(OLD_DEFS DIRECTORY PROPERTY COMPILE_DEFINITIONS)
  183. if(NOT "${OLD_DEFS}" STREQUAL "${OLD_DEFS_EXPECTED}")
  184. message(SEND_ERROR "add_definitions not converted to directory property!")
  185. endif()
  186. add_executable(Preprocess preprocess.c preprocess.cxx)
  187. set(FILE_PATH "${Preprocess_SOURCE_DIR}/file_def.h")
  188. set(TARGET_PATH "${Preprocess_SOURCE_DIR}/target_def.h")
  189. # Set some definition properties.
  190. foreach(c "" "_DEBUG" "_RELEASE" "_RELWITHDEBINFO" "_MINSIZEREL")
  191. set(FLAVOR "${c}")
  192. # Treat RelWithDebInfo and MinSizeRel as Release to avoid having
  193. # an exponentional matrix of inclusions and exclusions of defines
  194. if("${c}" STREQUAL "_RELWITHDEBINFO" OR "${c}" STREQUAL "_MINSIZEREL")
  195. set(FLAVOR "_RELEASE")
  196. endif()
  197. set_property(
  198. DIRECTORY .
  199. APPEND PROPERTY COMPILE_DEFINITIONS${c} "DIRECTORY_DEF${FLAVOR}"
  200. )
  201. set_property(
  202. TARGET Preprocess
  203. PROPERTY COMPILE_DEFINITIONS${c} "TARGET_DEF${FLAVOR}"
  204. )
  205. set_property(
  206. SOURCE preprocess.c preprocess.cxx
  207. PROPERTY COMPILE_DEFINITIONS${c} "FILE_DEF${FLAVOR}"
  208. )
  209. endforeach()
  210. # Add definitions with values.
  211. set(DEF_TARGET_PATH "TARGET_PATH=\"${TARGET_PATH}\"")
  212. set(DEF_FILE_PATH "FILE_PATH=\"${FILE_PATH}\"")
  213. set_property(
  214. TARGET Preprocess
  215. APPEND PROPERTY COMPILE_DEFINITIONS
  216. "TARGET_STRING=\"${STRING_VALUE}${SEMICOLON}\""
  217. "TARGET_EXPR=${EXPR}"
  218. ${DEF_TARGET_PATH}
  219. )
  220. set_property(
  221. SOURCE preprocess.c preprocess.cxx
  222. APPEND PROPERTY COMPILE_DEFINITIONS
  223. "FILE_STRING=\"${STRING_VALUE}${SEMICOLON}\""
  224. "FILE_EXPR=${EXPR}"
  225. ${DEF_FILE_PATH}
  226. )
  227. # Try reading and writing the property value to ensure the string is
  228. # preserved.
  229. get_property(defs1 TARGET Preprocess PROPERTY COMPILE_DEFINITIONS)
  230. set_property(TARGET Preprocess PROPERTY COMPILE_DEFINITIONS "${defs1}")
  231. get_property(defs2 TARGET Preprocess PROPERTY COMPILE_DEFINITIONS)
  232. if(NOT "x${defs1}" STREQUAL "x${defs2}")
  233. message(FATAL_ERROR "get/set/get COMPILE_DEFINITIONS round trip failed. "
  234. "First get:\n"
  235. " ${defs1}\n"
  236. "Second get:\n"
  237. " ${defs2}")
  238. endif()
  239. # Helper target for running test manually in build tree.
  240. add_custom_target(drive COMMAND Preprocess)
  241. # Configure the header file with the desired string value.
  242. if(SEMICOLON)
  243. string(APPEND STRING_VALUE ";")
  244. endif()
  245. configure_file(${Preprocess_SOURCE_DIR}/preprocess.h.in
  246. ${Preprocess_BINARY_DIR}/preprocess.h)
  247. include_directories(${Preprocess_BINARY_DIR})