Makefile: Add rules to create tags and TAGS.
[tinycc.git] / CMakeLists.txt
1 project(tcc C)
2 cmake_minimum_required(VERSION 2.6)
3 enable_testing()
4
5 # Detect native platform
6 if(WIN32)
7   set(BUILD_SHARED_LIBS ON)
8   if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM")
9     set(TCC_NATIVE_TARGET "WinCE")
10   elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
11     set(TCC_NATIVE_TARGET "Win64")
12   else()
13     set(TCC_NATIVE_TARGET "Win32")
14   endif()
15 else()
16   if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
17     set(TCC_NATIVE_TARGET "ARM")
18     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)
28     set(TCC_NATIVE_TARGET "x86_64")
29     set(TCC_ARCH_DIR "x86_64-linux-gnu")
30   else()
31     set(TCC_NATIVE_TARGET "i386")
32     set(TCC_ARCH_DIR "i386-linux-gnu")
33   endif()
34 endif()
35
36 if(WIN32)
37   set(EXE_PATH ".")
38   set(TCC_LIB_PATH lib)
39   set(NATIVE_LIB_PATH)
40   set(DOC_PATH doc)
41 else()
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)
46 endif()
47
48 if(NOT WIN32)
49   if(EXISTS /usr/lib/${TCC_ARCH_DIR}/crti.o)
50     set(CONFIG_LDDIR lib/${TCC_ARCH_DIR})
51     set(CONFIG_MULTIARCHDIR ${TCC_ARCH_DIR})
52   elseif(EXISTS /usr/lib64/crti.o)
53     set(CONFIG_LDDIR lib64)
54   endif()
55 endif()
56
57 # Use two variables to keep CMake configuration variable names consistent
58 set(TCC_BCHECK OFF CACHE BOOL "Enable bounds checking")
59 set(CONFIG_TCC_BCHECK ${TCC_BCHECK})
60 set(TCC_ASSERT OFF CACHE BOOL "Enable assertions")
61 set(CONFIG_TCC_ASSERT ${TCC_ASSERT})
62
63 set(TCC_BUILD_NATIVE ON CACHE BOOL "Build native compiler")
64 set(TCC_BUILD_I386 OFF CACHE BOOL "Build i386 cross compiler")
65 set(TCC_BUILD_X64 OFF CACHE BOOL "Build x86-64 cross compiler")
66 set(TCC_BUILD_WIN32 OFF CACHE BOOL "Build Windows i386 cross compiler")
67 set(TCC_BUILD_WIN64 OFF CACHE BOOL "Build Windows x86-64 cross compiler")
68 set(TCC_BUILD_WINCE OFF CACHE BOOL "Build Windows CE cross compiler")
69 set(TCC_BUILD_ARM_FPA OFF CACHE BOOL "Build arm-fpa cross compiler")
70 set(TCC_BUILD_ARM_FPA_LD OFF CACHE BOOL "Build arm-fpa-ld cross compiler")
71 set(TCC_BUILD_ARM_VFP OFF CACHE BOOL "Build arm-vfp cross compiler")
72 set(TCC_BUILD_ARM_EABI OFF CACHE BOOL "Build ARM EABI cross compiler")
73 set(TCC_BUILD_ARM_EABIHF OFF CACHE BOOL "Build ARM EABI hardware float cross compiler")
74 set(TCC_BUILD_ARM OFF CACHE BOOL "Build ARM cross compiler")
75 set(TCC_BUILD_C67 OFF CACHE BOOL "Build C67 cross compiler")
76
77 set(TCC_ARM_VERSION ${TCC_ARM_VERSION_DEFAULT} CACHE STRING "ARM target CPU version")
78 set_property(CACHE TCC_ARM_VERSION PROPERTY STRINGS 4 5 6 7)
79
80 if(WIN32)
81   set(CONFIG_TCCDIR ${CMAKE_INSTALL_PREFIX})
82 else()
83   set(CONFIG_TCCDIR ${CMAKE_INSTALL_PREFIX}/lib/tcc)
84 endif()
85
86 file(STRINGS "VERSION" TCC_VERSION)
87 list(GET TCC_VERSION 0 TCC_VERSION)
88 include_directories(${CMAKE_BINARY_DIR})
89 configure_file(config.h.in config.h)
90 configure_file(config.texi.in config.texi)
91
92 # Utility variables
93 set(I386_SOURCES i386-gen.c i386-asm.c i386-asm.h i386-tok.h)
94 set(X86_64_SOURCES x86_64-gen.c i386-asm.c x86_64-asm.h)
95 set(ARM_SOURCES arm_gen.c)
96
97 set(LIBTCC1_I386_SOURCES lib/alloca86.S lib/alloca86-bt.S)
98 set(LIBTCC1_WIN_SOURCES win32/lib/crt1.c win32/lib/wincrt1.c win32/lib/dllcrt1.c win32/lib/dllmain.c win32/lib/chkstk.S)
99 if(NOT WIN32)
100   set(LIBTCC1_I386_SOURCES ${LIBTCC1_I386_SOURCES} lib/bcheck.c)
101 endif()
102
103 if(WIN32)
104   add_executable(tiny_impdef win32/tools/tiny_impdef.c)
105 endif()
106 add_executable(tiny_libmaker_32 win32/tools/tiny_libmaker.c)
107 set_target_properties(tiny_libmaker_32 PROPERTIES COMPILE_DEFINITIONS TCC_TARGET_I386)
108 add_executable(tiny_libmaker_64 win32/tools/tiny_libmaker.c)
109 set_target_properties(tiny_libmaker_64 PROPERTIES COMPILE_DEFINITIONS TCC_TARGET_X86_64)
110
111 macro(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)
141 endmacro()
142
143 macro(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})
145     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
153     set(TCC_NATIVE_FLAGS)
154     foreach(make_tcc_def ${TCC_NATIVE_DEFINITIONS})
155       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       )
171       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()
175       add_executable(tcc tcc.c)
176       target_link_libraries(tcc libtcc)
177       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}")
182       
183       if("${libtcc_sources}" STRGREATER "")
184         make_libtcc1("" tcc "${libtcc_ar}" "${TCC_NATIVE_DEFINITIONS}" "${libtcc_includes}" "${libtcc_sources}")
185       endif()
186     endif()
187   elseif(${cross_enabled})
188     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()
196 endmacro()
197
198 make_tcc("Win32" i386-w64-mingw32 TCC_BUILD_WIN32
199   "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 )
203 make_tcc("Win64" x86_64-w64-mingw32 TCC_BUILD_WIN64
204   "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 )
208 make_tcc("WinCE" arm-wince-mingw32ce TCC_BUILD_WINCE
209   "TCC_TARGET_ARM;TCC_ARM_VERSION=${TCC_ARM_VERSION};TCC_TARGET_PE"
210   "${ARM_SOURCES};tccpe.c"
211   "" "" ""
212 )
213 make_tcc("i386" i386-linux-gnu TCC_BUILD_I386
214   TCC_TARGET_I386
215   "${I386_SOURCES}"
216   tiny_libmaker_32 "${LIBTCC1_I386_SOURCES}" ""
217 )
218 make_tcc("x86_64" x86_64-linux-gnu TCC_BUILD_X64
219   TCC_TARGET_X86_64
220   "${X86_64_SOURCES}"
221   tiny_libmaker_64 "lib/alloca86_64.S" ""
222 )
223 set(ARM_DEFINITIONS TCC_TARGET_ARM TCC_ARM_VERSION=${TCC_ARM_VERSION})
224 make_tcc("" arm-linux-gnueabihf TCC_BUILD_ARM_EABIHF
225   "${ARM_DEFINITIONS};TCC_ARM_EABI;TCC_ARM_HARDFLOAT"
226   "${ARM_SOURCES}"
227   "" "" ""
228 )
229 make_tcc("" arm-linux-gnueabi TCC_BUILD_ARM_EABI
230   "${ARM_DEFINITIONS};TCC_ARM_EABI"
231   "${ARM_SOURCES}"
232   "" "" ""
233 )
234 make_tcc("" arm-linux-fpa TCC_BUILD_ARM_FPA
235   "${ARM_DEFINITIONS}"
236   "${ARM_SOURCES}"
237   "" "" ""
238 )
239 make_tcc("" arm-linux-fpa-ld TCC_BUILD_ARM_FPA_LD
240   "${ARM_DEFINITIONS};LDOUBLE_SIZE=12"
241   "${ARM_SOURCES}"
242   "" "" ""
243 )
244 make_tcc("" arm-linux-gnu TCC_BUILD_ARM_VFP
245   "${ARM_DEFINITIONS};TCC_ARM_VFP"
246   "${ARM_SOURCES}"
247   "" "" ""
248 )
249 make_tcc("" c67 TCC_BUILD_C67
250   TCC_TARGET_C67
251   "c67-gen.c;tcccoff.c"
252   "" "" ""
253 )
254
255 add_subdirectory(tests)
256
257 find_program(MAKEINFO NAMES makeinfo PATHS C:/MinGW/MSYS/1.0/bin)
258 if(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})
274 endif()
275
276 if(WIN32)
277   file(GLOB WIN32_DEFS win32/lib/*.def)
278   install(FILES ${WIN32_DEFS} DESTINATION lib)
279   install(FILES tcclib.h DESTINATION include)
280   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)
287 else()
288   install(FILES libtcc.h tcclib.h DESTINATION include)
289   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)
292 endif()
293