CMakeLists.txt 9.5 KB

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