VLA code: minor fix
[tinycc.git] / CMakeLists.txt
Commit [+]AuthorDateLineData
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +01001project(tcc C)
2cmake_minimum_required(VERSION 2.6)
3enable_testing()
4
e7a7efed James Lyon2013-04-25 01:08:18 +01005# Detect native platform
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +01006if(WIN32)
7 set(BUILD_SHARED_LIBS ON)
8 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM")
e7a7efed James Lyon2013-04-25 01:08:18 +01009 set(TCC_NATIVE_TARGET "WinCE")
8a81f9e1 James Lyon2013-04-21 11:20:20 +010010 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
e7a7efed James Lyon2013-04-25 01:08:18 +010011 set(TCC_NATIVE_TARGET "Win64")
8a81f9e1 James Lyon2013-04-21 11:20:20 +010012 else()
e7a7efed James Lyon2013-04-25 01:08:18 +010013 set(TCC_NATIVE_TARGET "Win32")
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +010014 endif()
15else()
16 if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
e7a7efed James Lyon2013-04-25 01:08:18 +010017 set(TCC_NATIVE_TARGET "ARM")
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +010018 if(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv5")
19 set(TCC_ARM_VERSION_DEFAULT 5)
20 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv6")
21 set(TCC_ARM_VERSION_DEFAULT 6)
22 elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^armv7")
23 set(TCC_ARM_VERSION_DEFAULT 7)
24 else()
25 set(TCC_ARM_VERSION_DEFAULT 4)
26 endif()
27 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
e7a7efed James Lyon2013-04-25 01:08:18 +010028 set(TCC_NATIVE_TARGET "x86_64")
ae2ece93 James Lyon2013-04-26 00:31:46 +010029 set(TCC_ARCH_DIR "x86_64-linux-gnu")
8a81f9e1 James Lyon2013-04-21 11:20:20 +010030 else()
e7a7efed James Lyon2013-04-25 01:08:18 +010031 set(TCC_NATIVE_TARGET "i386")
ae2ece93 James Lyon2013-04-26 00:31:46 +010032 set(TCC_ARCH_DIR "i386-linux-gnu")
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +010033 endif()
34endif()
35
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010036if(WIN32)
37 set(EXE_PATH ".")
38 set(TCC_LIB_PATH lib)
39 set(NATIVE_LIB_PATH)
40 set(DOC_PATH doc)
41else()
42 set(EXE_PATH bin)
43 set(TCC_LIB_PATH lib/tcc)
44 set(NATIVE_LIB_PATH lib)
45 set(DOC_PATH share/doc/tcc)
46endif()
8a81f9e1 James Lyon2013-04-21 11:20:20 +010047
1caee8ab James Lyon2013-04-25 22:30:53 +010048if(NOT WIN32)
ae2ece93
JL
James Lyon2013-04-26 00:31:46 +010049 if(EXISTS /usr/lib/${TCC_ARCH_DIR}/crti.o)
50 set(CONFIG_LDDIR lib/${TCC_ARCH_DIR})
51 set(CONFIG_MULTIARCHDIR ${TCC_ARCH_DIR})
1caee8ab
JL
James Lyon2013-04-25 22:30:53 +010052 elseif(EXISTS /usr/lib64/crti.o)
53 set(CONFIG_LDDIR lib64)
54 endif()
55endif()
56
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010057# Use two variables to keep CMake configuration variable names consistent
58set(TCC_BCHECK OFF CACHE BOOL "Enable bounds checking")
59set(CONFIG_TCC_BCHECK ${TCC_BCHECK})
41b3c7a5
JL
James Lyon2013-04-27 20:39:34 +010060set(TCC_ASSERT OFF CACHE BOOL "Enable assertions")
61set(CONFIG_TCC_ASSERT ${TCC_ASSERT})
8a81f9e1 James Lyon2013-04-21 11:20:20 +010062
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010063set(TCC_BUILD_NATIVE ON CACHE BOOL "Build native compiler")
64set(TCC_BUILD_I386 OFF CACHE BOOL "Build i386 cross compiler")
65set(TCC_BUILD_X64 OFF CACHE BOOL "Build x86-64 cross compiler")
66set(TCC_BUILD_WIN32 OFF CACHE BOOL "Build Windows i386 cross compiler")
67set(TCC_BUILD_WIN64 OFF CACHE BOOL "Build Windows x86-64 cross compiler")
68set(TCC_BUILD_WINCE OFF CACHE BOOL "Build Windows CE cross compiler")
69set(TCC_BUILD_ARM_FPA OFF CACHE BOOL "Build arm-fpa cross compiler")
70set(TCC_BUILD_ARM_FPA_LD OFF CACHE BOOL "Build arm-fpa-ld cross compiler")
71set(TCC_BUILD_ARM_VFP OFF CACHE BOOL "Build arm-vfp cross compiler")
72set(TCC_BUILD_ARM_EABI OFF CACHE BOOL "Build ARM EABI cross compiler")
73set(TCC_BUILD_ARM_EABIHF OFF CACHE BOOL "Build ARM EABI hardware float cross compiler")
74set(TCC_BUILD_ARM OFF CACHE BOOL "Build ARM cross compiler")
75set(TCC_BUILD_C67 OFF CACHE BOOL "Build C67 cross compiler")
8a81f9e1 James Lyon2013-04-21 11:20:20 +010076
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010077set(TCC_ARM_VERSION ${TCC_ARM_VERSION_DEFAULT} CACHE STRING "ARM target CPU version")
78set_property(CACHE TCC_ARM_VERSION PROPERTY STRINGS 4 5 6 7)
8a81f9e1 James Lyon2013-04-21 11:20:20 +010079
1caee8ab
JL
James Lyon2013-04-25 22:30:53 +010080if(WIN32)
81 set(CONFIG_TCCDIR ${CMAKE_INSTALL_PREFIX})
82else()
83 set(CONFIG_TCCDIR ${CMAKE_INSTALL_PREFIX}/lib/tcc)
84endif()
85
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +010086file(STRINGS "VERSION" TCC_VERSION)
87list(GET TCC_VERSION 0 TCC_VERSION)
88include_directories(${CMAKE_BINARY_DIR})
89configure_file(config.h.in config.h)
e7a7efed James Lyon2013-04-25 01:08:18 +010090configure_file(config.texi.in config.texi)
8a81f9e1 James Lyon2013-04-21 11:20:20 +010091
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010092# Utility variables
93set(I386_SOURCES i386-gen.c i386-asm.c i386-asm.h i386-tok.h)
94set(X86_64_SOURCES x86_64-gen.c i386-asm.c x86_64-asm.h)
95set(ARM_SOURCES arm_gen.c)
8a81f9e1 James Lyon2013-04-21 11:20:20 +010096
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +010097set(LIBTCC1_I386_SOURCES lib/alloca86.S lib/alloca86-bt.S)
98set(LIBTCC1_WIN_SOURCES win32/lib/crt1.c win32/lib/wincrt1.c win32/lib/dllcrt1.c win32/lib/dllmain.c win32/lib/chkstk.S)
99if(NOT WIN32)
100 set(LIBTCC1_I386_SOURCES ${LIBTCC1_I386_SOURCES} lib/bcheck.c)
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +0100101endif()
102
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100103if(WIN32)
104 add_executable(tiny_impdef win32/tools/tiny_impdef.c)
105endif()
106add_executable(tiny_libmaker_32 win32/tools/tiny_libmaker.c)
107set_target_properties(tiny_libmaker_32 PROPERTIES COMPILE_DEFINITIONS TCC_TARGET_I386)
108add_executable(tiny_libmaker_64 win32/tools/tiny_libmaker.c)
109set_target_properties(tiny_libmaker_64 PROPERTIES COMPILE_DEFINITIONS TCC_TARGET_X86_64)
8a81f9e1 James Lyon2013-04-21 11:20:20 +0100110
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100111macro(make_libtcc1 prefix xcc xar defs includes sources)
112 set(libtcc1_prefix)
113 if("${prefix}" STRGREATER "")
114 set(libtcc1_prefix ${prefix}-)
115 endif()
116 set(libtcc1_flags -I${CMAKE_SOURCE_DIR}/include)
117 foreach(i ${defs})
118 set(libtcc1_flags ${libtcc1_flags} -D${i})
119 endforeach()
120 foreach(i ${includes})
121 set(libtcc1_flags ${libtcc1_flags} -I${CMAKE_SOURCE_DIR}/${i})
122 endforeach()
123 set(libtcc1_objects)
124 foreach(libtcc1_c lib/libtcc1.c ${sources})
125 string(REGEX MATCH "[^/]+$" libtcc1_o ${libtcc1_c})
126 string(REGEX MATCH "^[^.]+" libtcc1_o ${libtcc1_o})
127 set(libtcc1_o ${libtcc1_prefix}${libtcc1_o}.o)
128 add_custom_command(OUTPUT ${libtcc1_o}
129 COMMAND ${xcc} ${libtcc1_flags} -c ${CMAKE_SOURCE_DIR}/${libtcc1_c} -o ${libtcc1_o}
130 DEPENDS ${xcc} ${CMAKE_SOURCE_DIR}/${libtcc1_c}
131 )
132 list(APPEND libtcc1_objects ${libtcc1_o})
133 endforeach()
134 add_custom_command(OUTPUT ${libtcc1_prefix}libtcc1.a
135 COMMAND ${xar} ${libtcc1_prefix}libtcc1.a ${libtcc1_objects}
136 DEPENDS ${xar} ${libtcc1_objects}
137 )
138 add_custom_target(${libtcc1_prefix}libtcc1 ALL DEPENDS ${libtcc1_prefix}libtcc1.a)
139 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${libtcc1_prefix}libtcc1.a
140 DESTINATION ${TCC_LIB_PATH}/${prefix} RENAME libtcc1.a)
141endmacro()
8a81f9e1 James Lyon2013-04-21 11:20:20 +0100142
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100143macro(make_tcc native_name cross_name cross_enabled definitions tcc_sources libtcc_ar libtcc_sources libtcc_includes)
144 if (xx${native_name} STREQUAL xx${TCC_NATIVE_TARGET})
1caee8ab
JL
James Lyon2013-04-25 22:30:53 +0100145 set(TCC_NATIVE_DEFINITIONS ${definitions})
146 if("${CONFIG_MULTIARCHDIR}" STRGREATER "")
147 set(TCC_NATIVE_DEFINITIONS ${TCC_NATIVE_DEFINITIONS} CONFIG_MULTIARCHDIR="${CONFIG_MULTIARCHDIR}")
148 endif()
149 if("${CONFIG_LDDIR}" STRGREATER "")
150 set(TCC_NATIVE_DEFINITIONS ${TCC_NATIVE_DEFINITIONS} CONFIG_LDDIR="${CONFIG_LDDIR}")
151 endif()
152
e7a7efed James Lyon2013-04-25 01:08:18 +0100153 set(TCC_NATIVE_FLAGS)
1caee8ab James Lyon2013-04-25 22:30:53 +0100154 foreach(make_tcc_def ${TCC_NATIVE_DEFINITIONS})
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100155 set(TCC_NATIVE_FLAGS ${TCC_NATIVE_FLAGS} -D${make_tcc_def})
156 endforeach()
157
158 if (TCC_BUILD_NATIVE)
159 add_library(libtcc
160 libtcc.c
161 tccpp.c
162 tccgen.c
163 tccelf.c
164 tccasm.c
165 tccrun.c
166 tcc.h
167 libtcc.h
168 tcctok.h
169 ${tcc_sources}
170 )
ae2ece93
JL
James Lyon2013-04-26 00:31:46 +0100171 set_target_properties(libtcc PROPERTIES OUTPUT_NAME tcc PREFIX lib)
172 if(WIN32)
173 set_target_properties(libtcc PROPERTIES LINK_FLAGS "-Wl,--output-def,libtcc.def")
174 endif()
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100175 add_executable(tcc tcc.c)
176 target_link_libraries(tcc libtcc)
1caee8ab
JL
James Lyon2013-04-25 22:30:53 +0100177 if(NOT WIN32)
178 target_link_libraries(tcc dl)
179 endif()
180 install(TARGETS tcc libtcc RUNTIME DESTINATION ${EXE_PATH} LIBRARY DESTINATION ${NATIVE_LIB_PATH} ARCHIVE DESTINATION ${NATIVE_LIB_PATH})
181 set_target_properties(tcc libtcc PROPERTIES COMPILE_DEFINITIONS "${TCC_NATIVE_DEFINITIONS}")
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100182
183 if("${libtcc_sources}" STRGREATER "")
1caee8ab James Lyon2013-04-25 22:30:53 +0100184 make_libtcc1("" tcc "${libtcc_ar}" "${TCC_NATIVE_DEFINITIONS}" "${libtcc_includes}" "${libtcc_sources}")
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100185 endif()
186 endif()
1caee8ab James Lyon2013-04-25 22:30:53 +0100187 elseif(${cross_enabled})
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100188 add_executable(${cross_name}-tcc tcc.c)
189 set_target_properties(${cross_name}-tcc PROPERTIES COMPILE_DEFINITIONS "ONE_SOURCE;${definitions}")
190 install(TARGETS ${cross_name}-tcc RUNTIME DESTINATION ${EXE_PATH})
191
192 if("${libtcc_sources}" STRGREATER "")
193 make_libtcc1(${cross_name} "${cross_name}-tcc" "${libtcc_ar}" "${definitions}" "${libtcc_includes}" "${libtcc_sources}")
194 endif()
195 endif()
196endmacro()
197
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800198make_tcc("Win32" i386-w64-mingw32 TCC_BUILD_WIN32
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100199 "TCC_TARGET_I386;TCC_TARGET_PE"
200 "${I386_SOURCES};tccpe.c"
201 tiny_libmaker_32 "${LIBTCC1_I386_SOURCES};${LIBTCC1_WIN_SOURCES}" "win32/include;win32/include/winapi"
202)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800203make_tcc("Win64" x86_64-w64-mingw32 TCC_BUILD_WIN64
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100204 "TCC_TARGET_X86_64;TCC_TARGET_PE"
205 "${X86_64_SOURCES};tccpe.c"
206 tiny_libmaker_64 "lib/alloca86_64.S;${LIBTCC1_WIN_SOURCES}" "win32/include;win32/include/winapi"
207)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800208make_tcc("WinCE" arm-wince-mingw32ce TCC_BUILD_WINCE
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100209 "TCC_TARGET_ARM;TCC_ARM_VERSION=${TCC_ARM_VERSION};TCC_TARGET_PE"
210 "${ARM_SOURCES};tccpe.c"
211 "" "" ""
212)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800213make_tcc("i386" i386-linux-gnu TCC_BUILD_I386
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100214 TCC_TARGET_I386
215 "${I386_SOURCES}"
216 tiny_libmaker_32 "${LIBTCC1_I386_SOURCES}" ""
217)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800218make_tcc("x86_64" x86_64-linux-gnu TCC_BUILD_X64
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100219 TCC_TARGET_X86_64
220 "${X86_64_SOURCES}"
221 tiny_libmaker_64 "lib/alloca86_64.S" ""
222)
223set(ARM_DEFINITIONS TCC_TARGET_ARM TCC_ARM_VERSION=${TCC_ARM_VERSION})
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800224make_tcc("" arm-linux-gnueabihf TCC_BUILD_ARM_EABIHF
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100225 "${ARM_DEFINITIONS};TCC_ARM_EABI;TCC_ARM_HARDFLOAT"
226 "${ARM_SOURCES}"
227 "" "" ""
228)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800229make_tcc("" arm-linux-gnueabi TCC_BUILD_ARM_EABI
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100230 "${ARM_DEFINITIONS};TCC_ARM_EABI"
231 "${ARM_SOURCES}"
232 "" "" ""
233)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800234make_tcc("" arm-linux-fpa TCC_BUILD_ARM_FPA
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100235 "${ARM_DEFINITIONS}"
236 "${ARM_SOURCES}"
237 "" "" ""
238)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800239make_tcc("" arm-linux-fpa-ld TCC_BUILD_ARM_FPA_LD
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100240 "${ARM_DEFINITIONS};LDOUBLE_SIZE=12"
241 "${ARM_SOURCES}"
242 "" "" ""
243)
bf2854d2 Thomas Preud'homme2014-01-04 21:57:52 +0800244make_tcc("" arm-linux-gnu TCC_BUILD_ARM_VFP
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100245 "${ARM_DEFINITIONS};TCC_ARM_VFP"
246 "${ARM_SOURCES}"
247 "" "" ""
248)
249make_tcc("" c67 TCC_BUILD_C67
250 TCC_TARGET_C67
251 "c67-gen.c;tcccoff.c"
252 "" "" ""
253)
254
255add_subdirectory(tests)
8a81f9e1 James Lyon2013-04-21 11:20:20 +0100256
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100257find_program(MAKEINFO NAMES makeinfo PATHS C:/MinGW/MSYS/1.0/bin)
258if(MAKEINFO)
259 add_custom_command(OUTPUT tcc-doc.html
260 COMMAND ${MAKEINFO} --no-split --html -o tcc-doc.html ${CMAKE_CURRENT_SOURCE_DIR}/tcc-doc.texi
261 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tcc-doc.texi
262 )
263 set(TCC_DOC_FILES tcc-doc.html)
264 if(NOT WIN32)
265 add_custom_command(OUTPUT tcc-doc.info
266 COMMAND ${MAKEINFO} -o tcc-doc.info ${CMAKE_CURRENT_SOURCE_DIR}/tcc-doc.texi
267 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tcc-doc.texi
268 )
269 set(TCC_DOC_FILES ${TCC_DOC_FILES} tcc-doc.info)
270 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tcc-doc.info DESTINATION share/info)
271 endif()
272 add_custom_target(tcc-doc ALL DEPENDS ${TCC_DOC_FILES})
273 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tcc-doc.html DESTINATION ${DOC_PATH})
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +0100274endif()
275
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100276if(WIN32)
277 file(GLOB WIN32_DEFS win32/lib/*.def)
278 install(FILES ${WIN32_DEFS} DESTINATION lib)
1caee8ab James Lyon2013-04-25 22:30:53 +0100279 install(FILES tcclib.h DESTINATION include)
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100280 install(DIRECTORY include/ DESTINATION include)
281 install(DIRECTORY win32/include/ DESTINATION include)
282 install(DIRECTORY win32/examples/ DESTINATION examples)
283 install(FILES win32/tcc-win32.txt DESTINATION doc)
284 install(FILES ${CMAKE_BINARY_DIR}/libtcc.def DESTINATION libtcc)
285 install(FILES ${CMAKE_BINARY_DIR}/libtcc.dll.a DESTINATION libtcc RENAME libtcc.a)
286 install(FILES libtcc.h DESTINATION libtcc)
287else()
1caee8ab James Lyon2013-04-25 22:30:53 +0100288 install(FILES libtcc.h tcclib.h DESTINATION include)
e7a7efed
JL
James Lyon2013-04-25 01:08:18 +0100289 install(DIRECTORY include/ DESTINATION lib/tcc/include)
290 install(DIRECTORY win32/include/ DESTINATION lib/tcc/win32/include)
291 install(DIRECTORY include/ DESTINATION lib/tcc/win32/include)
8a81f9e1
JL
James Lyon2013-04-21 11:20:20 +0100292endif()
293