ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Tests / Preprocess / CMakeLists.txt
blobee2e355d60f1e8983691958bb130b3d7459263af
1 project(Preprocess)
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.
7 IF(CMAKE_ANSI_CFLAGS)
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.
18 set(STRING_EXTRA "")
20 if("${CMAKE_GENERATOR}" MATCHES "Make" AND MSVC)
21   set(NMAKE 1)
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.
29   #
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.
32   set(SEMICOLON "\;")
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")
45   # VS: ,
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")
52 if(NOT MINGW)
53   # MinGW: &
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}&")
57 endif(NOT MINGW)
59 if(NOT MINGW)
60   # MinGW: |
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}|")
64 endif(NOT MINGW)
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)
75   # Borland, MinGW: < >
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)
80 # General: \"
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")
91 # General: #
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
113 #   - GCC supports
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)")
123 if(NOT WATCOM)
124   # Watcom does not support - or / because it parses them as options.
125   set(EXPR "${EXPR}+y/x-x")
126 endif(NOT WATCOM)
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.
136 if(XCODE)
137   set(PREPROCESS_XCODE 1)
138 endif(XCODE)
139 if("${CMAKE_GENERATOR}" MATCHES "Visual Studio 6")
140   set(PREPROCESS_VS6 1)
141   set(VS6 _vs6)
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.
148 if(PREPROCESS_VS6)
149   set(OLD_DEFS_EXPECTED "OLD_DEF")
150 else(PREPROCESS_VS6)
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")
165   set_property(
166     DIRECTORY .
167     APPEND PROPERTY COMPILE_DEFINITIONS${c} "DIRECTORY_DEF${c}"
168     )
169   set_property(
170     TARGET Preprocess
171     PROPERTY COMPILE_DEFINITIONS${c} "TARGET_DEF${c}"
172     )
173   set_property(
174     SOURCE preprocess.c preprocess${VS6}.cxx
175     PROPERTY COMPILE_DEFINITIONS${c} "FILE_DEF${c}"
176     )
177 endforeach(c)
179 # Add definitions with values.  VS6 does not support this.
180 if(NOT PREPROCESS_VS6)
181   set_property(
182     TARGET Preprocess
183     APPEND PROPERTY COMPILE_DEFINITIONS
184     "TARGET_STRING=\"${STRING_VALUE}${SEMICOLON}\""
185     "TARGET_EXPR=${EXPR}"
186     "TARGET_PATH=\"${TARGET_PATH}\""
187     )
188   set_property(
189     SOURCE preprocess.c preprocess${VS6}.cxx
190     APPEND PROPERTY COMPILE_DEFINITIONS
191     "FILE_STRING=\"${STRING_VALUE}${SEMICOLON}\""
192     "FILE_EXPR=${EXPR}"
193     "FILE_PATH=\"${FILE_PATH}\""
194     )
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.
201 if(SEMICOLON)
202   set(STRING_VALUE "${STRING_VALUE};")
203 endif(SEMICOLON)
204 configure_file(${Preprocess_SOURCE_DIR}/preprocess.h.in
205                ${Preprocess_BINARY_DIR}/preprocess.h)
206 include_directories(${Preprocess_BINARY_DIR})