CMakeLists.txt 10 KB

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