1 cmake_minimum_required(VERSION 2.6)
4 # This test is meant both as a test and as a reference for supported
5 # syntax on native tool command lines.
7 # We need ansi C support.
9 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
10 ENDIF(CMAKE_ANSI_CFLAGS)
12 # Determine the build tool being used. Not all characters can be
13 # escaped for all build tools. This test checks all characters known
14 # to work with each tool and documents those known to not work.
15 if("${CMAKE_GENERATOR}" MATCHES "Xcode")
17 endif("${CMAKE_GENERATOR}" MATCHES "Xcode")
18 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
20 endif("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
21 if("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles")
23 endif("${CMAKE_GENERATOR}" MATCHES "Unix Makefiles")
24 if("${CMAKE_GENERATOR}" MATCHES "NMake Makefiles")
26 endif("${CMAKE_GENERATOR}" MATCHES "NMake Makefiles")
27 if("${CMAKE_GENERATOR}" MATCHES "MinGW Makefiles")
29 endif("${CMAKE_GENERATOR}" MATCHES "MinGW Makefiles")
30 if("${CMAKE_GENERATOR}" MATCHES "Borland Makefiles")
32 endif("${CMAKE_GENERATOR}" MATCHES "Borland Makefiles")
33 if("${CMAKE_GENERATOR}" MATCHES "Watcom WMake")
35 endif("${CMAKE_GENERATOR}" MATCHES "Watcom WMake")
36 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 7$")
38 endif("${CMAKE_GENERATOR}" MATCHES "Visual Studio 7$")
39 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
41 endif("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
43 # Some tests below check the PP_* variables set above. They are meant
44 # to test the case that the build tool is at fault. Other tests below
45 # check the compiler that will be used when the compiler is at fault
46 # (does not work even from a command shell).
48 #-----------------------------------------------------------------------------
49 # Construct a C-string literal to test passing through a definition on
50 # the command line. We configure the value into a header so it can be
51 # checked in the executable at runtime. The semicolon is handled
52 # specially because it needs to be escaped in the COMPILE_DEFINITIONS
53 # property value to avoid separating definitions but the string value
54 # must not have it escaped inside the configured header.
57 if(NOT BORLAND AND NOT PP_VS70)
58 # Borland, VS70 IDE: ;
59 # The Borland compiler will simply not accept a non-escaped semicolon
60 # on the command line. If it is escaped \; then the escape character
61 # shows up in the preprocessing output too.
63 # The VS 7.0 IDE separates definitions on semicolons and commas with
64 # no regard for quotes. Fortunately VS 7.1 and above are okay.
66 endif(NOT BORLAND AND NOT PP_VS70)
70 # The project parser unconditionally separates arguments at spaces.
71 set(STRING_EXTRA "${STRING_EXTRA} ")
73 if(NOT PP_BORLAND AND NOT PP_WATCOM)
74 # Borland, WMake: multiple spaces
75 # The make tool seems to remove extra whitespace from inside
76 # quoted strings when passing to the compiler. It does not have
77 # trouble passing to other tools, and the compiler may be directly
78 # invoked from the command line.
79 set(STRING_EXTRA "${STRING_EXTRA} ")
80 endif(NOT PP_BORLAND AND NOT PP_WATCOM)
85 # Visual Studio will not accept a comma in the value of a definition.
86 # The comma-separated list of PreprocessorDefinitions in the project
87 # file seems to be parsed before the content of entries is examined.
88 set(STRING_EXTRA "${STRING_EXTRA},")
93 # When inside -D"FOO=\"a & b\"" MinGW make wants -D"FOO=\"a "&" b\""
94 # but it does not like quoted ampersand elsewhere.
95 set(STRING_EXTRA "${STRING_EXTRA}&")
100 # When inside -D"FOO=\"a | b\"" MinGW make wants -D"FOO=\"a "|" b\""
101 # but it does not like quoted pipe elsewhere.
102 set(STRING_EXTRA "${STRING_EXTRA}|")
105 if(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
106 # Borland, NMake, MinGW: ^
107 # When inside -D"FOO=\"a ^ b\"" the make tools want -D"FOO=\"a "^" b\""
108 # but do not like quoted carrot elsewhere. In NMake the non-quoted
109 # syntax works when the flags are not in a make variable.
110 set(STRING_EXTRA "${STRING_EXTRA}^")
111 endif(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
113 if(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
114 # Borland, MinGW: < >
115 # Angle-brackets have funny behavior that is hard to escape.
116 set(STRING_EXTRA "${STRING_EXTRA}<>")
117 endif(NOT PP_BORLAND AND NOT PP_MINGW AND NOT PP_NMAKE)
120 if(NOT MSVC OR PP_NMAKE)
122 # When the cl compiler is invoked from the command line then % must
123 # be written %% (to distinguish from %ENV% syntax). However cl does
124 # not seem to accept the syntax when it is invoked from inside a
125 # make tool (nmake, mingw32-make, etc.). Instead the argument must
126 # be placed inside a response file. Then cl accepts it because it
127 # parses the response file as it would the normal windows command
128 # line. Currently only NMake supports running cl with a response
129 # file. Supporting other make tools would require CMake to generate
130 # response files explicitly for each object file.
131 set(STRING_EXTRA "${STRING_EXTRA}%")
133 endif(NOT MSVC OR PP_NMAKE)
136 # Make tools do not reliably accept \\\" syntax:
137 # - MinGW and MSYS make tools crash with \\\"
138 # - Borland make actually wants a mis-matched quote \\"
139 # or $(BACKSLASH)\" where BACKSLASH is a variable set to \\
140 # - VS IDE gets confused about the bounds of the definition value \\\"
141 # - NMake is okay with just \\\"
142 if(PP_NMAKE OR PP_UMAKE)
143 set(STRING_EXTRA "${STRING_EXTRA}\\\"")
144 endif(PP_NMAKE OR PP_UMAKE)
147 # MSVC will not accept a # in the value of a string definition on the
148 # command line. The character seems to be simply replaced by an
149 # equals =. According to "cl -help" definitions may be specified by
150 # -DMACRO#VALUE as well as -DMACRO=VALUE. It must be implemented by a
151 # simple search-and-replace.
153 # The Borland compiler will parse both # and \# as just # but the make
154 # tool seems to want \# sometimes and not others.
156 # Unix make does not like # in variable settings without extra
157 # escaping. This could probably be fixed but since MSVC does not
158 # support it and it is not an operator it is not worthwhile.
160 # Compose the final test string.
161 set(STRING_VALUE "hello`~!@$*)(_+-=}{][:'.?/${STRING_EXTRA}world")
163 #-----------------------------------------------------------------------------
164 # Function-style macro command-line support:
165 # - Borland does not support
166 # - MSVC does not support
167 # - Watcom does not support
170 # Too few platforms support this to bother implementing.
171 # People can just configure headers with the macros.
173 #-----------------------------------------------------------------------------
174 # Construct a sample expression to pass as a macro definition.
176 set(EXPR "x*y+!(x==(y+1*2))*f(x${EXPR_OP1}2)")
179 # Watcom does not support - or / because it parses them as options.
180 set(EXPR "${EXPR}+y/x-x")
183 #-----------------------------------------------------------------------------
185 # Inform the test if the debug configuration is getting built.
186 # The NDEBUG definition takes care of this for release.
187 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DPREPROCESS_DEBUG")
188 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DPREPROCESS_DEBUG")
190 # Inform the test if it built from Xcode or VS6 IDE.
192 set(PREPROCESS_XCODE 1)
195 set(PREPROCESS_VS6 1)
199 # Test old-style definitions.
200 add_definitions(-DOLD_DEF -DOLD_EXPR=2)
202 # Make sure old-style definitions are converted to directory property.
203 set(OLD_DEFS_EXPECTED "OLD_DEF;OLD_EXPR=2")
204 get_property(OLD_DEFS DIRECTORY PROPERTY COMPILE_DEFINITIONS)
205 if(NOT "${OLD_DEFS}" STREQUAL "${OLD_DEFS_EXPECTED}")
206 message(SEND_ERROR "add_definitions not converted to directory property!")
207 endif(NOT "${OLD_DEFS}" STREQUAL "${OLD_DEFS_EXPECTED}")
209 add_executable(Preprocess preprocess.c preprocess${VS6}.cxx)
211 set(FILE_PATH "${Preprocess_SOURCE_DIR}/file_def.h")
212 set(TARGET_PATH "${Preprocess_SOURCE_DIR}/target_def.h")
214 # Set some definition properties.
215 foreach(c "" "_DEBUG" "_RELEASE")
218 APPEND PROPERTY COMPILE_DEFINITIONS${c} "DIRECTORY_DEF${c}"
222 PROPERTY COMPILE_DEFINITIONS${c} "TARGET_DEF${c}"
225 SOURCE preprocess.c preprocess${VS6}.cxx
226 PROPERTY COMPILE_DEFINITIONS${c} "FILE_DEF${c}"
230 # Add definitions with values.
231 if(NOT PREPROCESS_VS6)
232 # The path might have spaces, which VS6 does not support.
233 set(DEF_TARGET_PATH "TARGET_PATH=\"${TARGET_PATH}\"")
234 set(DEF_FILE_PATH "FILE_PATH=\"${FILE_PATH}\"")
235 endif(NOT PREPROCESS_VS6)
238 APPEND PROPERTY COMPILE_DEFINITIONS
239 "TARGET_STRING=\"${STRING_VALUE}${SEMICOLON}\""
240 "TARGET_EXPR=${EXPR}"
244 SOURCE preprocess.c preprocess${VS6}.cxx
245 APPEND PROPERTY COMPILE_DEFINITIONS
246 "FILE_STRING=\"${STRING_VALUE}${SEMICOLON}\""
251 # Helper target for running test manually in build tree.
252 add_custom_target(drive COMMAND Preprocess)
254 # Configure the header file with the desired string value.
256 set(STRING_VALUE "${STRING_VALUE};")
258 configure_file(${Preprocess_SOURCE_DIR}/preprocess.h.in
259 ${Preprocess_BINARY_DIR}/preprocess.h)
260 include_directories(${Preprocess_BINARY_DIR})