3 # This test is meant both as a test and as a reference for supported
4 # syntax on native tool command lines.
6 # We need ansi C support.
8 SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_ANSI_CFLAGS}")
9 ENDIF(CMAKE_ANSI_CFLAGS)
11 #-----------------------------------------------------------------------------
12 # Construct a C-string literal to test passing through a definition on
13 # the command line. We configure the value into a header so it can be
14 # checked in the executable at runtime. The semicolon is handled
15 # specially because it needs to be escaped in the COMPILE_DEFINITIONS
16 # property value to avoid separating definitions but the string value
17 # must not have it escaped inside the configured header.
20 if("${CMAKE_GENERATOR}" MATCHES "Make" AND MSVC)
22 endif("${CMAKE_GENERATOR}" MATCHES "Make" AND MSVC)
24 if(NOT BORLAND AND NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 7$")
25 # Borland, VS70 IDE: ;
26 # The Borland compiler will simply not accept a non-escaped semicolon
27 # on the command line. If it is escaped \; then the escape character
28 # shows up in the preprocessing output too.
30 # The VS 7.0 IDE separates definitions on semicolons and commas with
31 # no regard for quotes. Fortunately VS 7.1 and above are okay.
33 endif(NOT BORLAND AND NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio 7$")
35 if(NOT BORLAND AND NOT WATCOM)
36 # Borland, WMake: multiple spaces
37 # The make tool seems to remove extra whitespace from inside
38 # quoted strings when passing to the compiler. It does not have
39 # trouble passing to other tools, and the compiler may be directly
40 # invoked from the command line.
41 set(STRING_EXTRA "${STRING_EXTRA} ")
42 endif(NOT BORLAND AND NOT WATCOM)
44 if(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
46 # Visual Studio will not accept a comma in the value of a definition.
47 # The comma-separated list of PreprocessorDefinitions in the project
48 # file seems to be parsed before the content of entries is examined.
49 set(STRING_EXTRA "${STRING_EXTRA},")
50 endif(NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
54 # When inside -D"FOO=\"a & b\"" MinGW make wants -D"FOO=\"a "&" b\""
55 # but it does not like quoted ampersand elsewhere.
56 set(STRING_EXTRA "${STRING_EXTRA}&")
61 # When inside -D"FOO=\"a | b\"" MinGW make wants -D"FOO=\"a "|" b\""
62 # but it does not like quoted pipe elsewhere.
63 set(STRING_EXTRA "${STRING_EXTRA}|")
66 if(NOT BORLAND AND NOT MINGW AND NOT NMAKE)
67 # Borland, NMake, MinGW: ^
68 # When inside -D"FOO=\"a ^ b\"" they make wants -D"FOO=\"a "^" b\""
69 # but do not like quoted carrot elsewhere. In NMake the non-quoted
70 # syntax works when the flags are not in a make variable.
71 set(STRING_EXTRA "${STRING_EXTRA}^")
72 endif(NOT BORLAND AND NOT MINGW AND NOT NMAKE)
74 if(NOT BORLAND AND NOT MINGW AND NOT NMAKE)
76 # Angle-brackets have funny behavior that is hard to escape.
77 set(STRING_EXTRA "${STRING_EXTRA}<>")
78 endif(NOT BORLAND AND NOT MINGW AND NOT NMAKE)
81 # Make tools do not reliably accept \\\" syntax:
82 # - MinGW and MSYS make tools crash with \\\"
83 # - Borland make actually wants a mis-matched quote \\"
84 # or $(BACKSLASH)\" where BACKSLASH is a variable set to \\
85 # - VS IDE gets confused about the bounds of the definition value \\\"
86 # - NMake is okay with just \\\"
87 if(NMAKE OR "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles")
88 set(STRING_EXTRA "${STRING_EXTRA}\\\"")
89 endif(NMAKE OR "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles")
92 # MSVC will not accept a # in the value of a string definition on the
93 # command line. The character seems to be simply replaced by an
94 # equals =. According to "cl -help" definitions may be specified by
95 # -DMACRO#VALUE as well as -DMACRO=VALUE. It must be implemented by a
96 # simple search-and-replace.
98 # The Borland compiler will parse both # and \# as just # but the make
99 # tool seems to want \# sometimes and not others.
101 # Unix make does not like # in variable settings without extra
102 # escaping. This could probably be fixed but since MSVC does not
103 # support it and it is not an operator it is not worthwhile.
105 # Compose the final test string.
106 set(STRING_VALUE "hello `~!@$%*)(_+-=}{][:'.?/ ${STRING_EXTRA}world")
108 #-----------------------------------------------------------------------------
109 # Function-style macro command-line support:
110 # - Borland does not support
111 # - MSVC does not support
112 # - Watcom does not support
115 # Too few platforms support this to bother implementing.
116 # People can just configure headers with the macros.
118 #-----------------------------------------------------------------------------
119 # Construct a sample expression to pass as a macro definition.
121 set(EXPR "x*y+!(x==(y+1*2))*f(x%2)")
124 # Watcom does not support - or / because it parses them as options.
125 set(EXPR "${EXPR}+y/x-x")
128 #-----------------------------------------------------------------------------
130 # Inform the test if the debug configuration is getting built.
131 # The NDEBUG definition takes care of this for release.
132 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DPREPROCESS_DEBUG")
133 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DPREPROCESS_DEBUG")
135 # Inform the test if it built from Xcode or VS6 IDE.
137 set(PREPROCESS_XCODE 1)
139 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
140 set(PREPROCESS_VS6 1)
142 endif("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
144 # Test old-style definitions.
145 add_definitions(-DOLD_DEF -DOLD_EXPR=2)
147 # Make sure old-style definitions are converted to directory property.
149 set(OLD_DEFS_EXPECTED "OLD_DEF")
151 set(OLD_DEFS_EXPECTED "OLD_DEF;OLD_EXPR=2")
152 endif(PREPROCESS_VS6)
153 get_property(OLD_DEFS DIRECTORY PROPERTY COMPILE_DEFINITIONS)
154 if(NOT "${OLD_DEFS}" STREQUAL "${OLD_DEFS_EXPECTED}")
155 message(SEND_ERROR "add_definitions not converted to directory property!")
156 endif(NOT "${OLD_DEFS}" STREQUAL "${OLD_DEFS_EXPECTED}")
158 add_executable(Preprocess preprocess.c preprocess${VS6}.cxx)
160 set(FILE_PATH "${Preprocess_SOURCE_DIR}/file_def.h")
161 set(TARGET_PATH "${Preprocess_SOURCE_DIR}/target_def.h")
163 # Set some definition properties.
164 foreach(c "" "_DEBUG" "_RELEASE")
167 APPEND PROPERTY COMPILE_DEFINITIONS${c} "DIRECTORY_DEF${c}"
171 PROPERTY COMPILE_DEFINITIONS${c} "TARGET_DEF${c}"
174 SOURCE preprocess.c preprocess${VS6}.cxx
175 PROPERTY COMPILE_DEFINITIONS${c} "FILE_DEF${c}"
179 # Add definitions with values. VS6 does not support this.
180 if(NOT PREPROCESS_VS6)
183 APPEND PROPERTY COMPILE_DEFINITIONS
184 "TARGET_STRING=\"${STRING_VALUE}${SEMICOLON}\""
185 "TARGET_EXPR=${EXPR}"
186 "TARGET_PATH=\"${TARGET_PATH}\""
189 SOURCE preprocess.c preprocess${VS6}.cxx
190 APPEND PROPERTY COMPILE_DEFINITIONS
191 "FILE_STRING=\"${STRING_VALUE}${SEMICOLON}\""
193 "FILE_PATH=\"${FILE_PATH}\""
195 endif(NOT PREPROCESS_VS6)
197 # Helper target for running test manually in build tree.
198 add_custom_target(drive COMMAND Preprocess)
200 # Configure the header file with the desired string value.
202 set(STRING_VALUE "${STRING_VALUE};")
204 configure_file(${Preprocess_SOURCE_DIR}/preprocess.h.in
205 ${Preprocess_BINARY_DIR}/preprocess.h)
206 include_directories(${Preprocess_BINARY_DIR})