CMake: generate targets for tools and examples even when
[oota-llvm.git] / CMakeLists.txt
1 # See docs/CMake.html for instructions about how to build LLVM with CMake.
2
3 project(LLVM)
4 cmake_minimum_required(VERSION 2.6.1)
5
6 set(PACKAGE_NAME llvm)
7 set(PACKAGE_VERSION 2.7svn)
8 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
9 set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu")
10
11 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
12   message(FATAL_ERROR "In-source builds are not allowed.
13 CMake would overwrite the makefiles distributed with LLVM.
14 Please create a directory and run cmake from there, passing the path
15 to this source directory as the last argument.
16 This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
17 Please delete them.")
18 endif()
19
20 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
21
22 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
23 set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include)
24 set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
25 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
26 set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
27 set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
28
29 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
30   file(GLOB_RECURSE
31     tablegenned_files_on_include_dir
32     "${LLVM_MAIN_SRC_DIR}/include/llvm/*.gen")
33   file(GLOB_RECURSE
34     tablegenned_files_on_lib_dir
35     "${LLVM_MAIN_SRC_DIR}/lib/Target/*.inc")
36   if( tablegenned_files_on_include_dir OR tablegenned_files_on_lib_dir)
37     message(FATAL_ERROR "Apparently there is a previous in-source build,
38 probably as the result of running `configure' and `make' on
39 ${LLVM_MAIN_SRC_DIR}.
40 This may cause problems. The suspicious files are:
41 ${tablegenned_files_on_lib_dir}
42 ${tablegenned_files_on_include_dir}
43 Please clean the source directory.")
44   endif()
45 endif()
46
47 set(LLVM_ALL_TARGETS
48   Alpha
49   ARM
50   Blackfin
51   CBackend
52   CellSPU
53   CppBackend
54   Mips
55   MSIL
56   MSP430
57   PIC16
58   PowerPC
59   Sparc
60   SystemZ
61   X86
62   XCore
63   )
64
65 if( MSVC )
66   set(LLVM_TARGETS_TO_BUILD X86
67     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
68 else( MSVC )
69   set(LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS}
70     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
71 endif( MSVC )
72
73 set(C_INCLUDE_DIRS "" CACHE STRING
74   "Colon separated list of directories clang will search for headers.")
75
76 set(LLVM_TARGET_ARCH "host"
77   CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")
78
79 option(LLVM_ENABLE_THREADS "Use threads if available." ON)
80
81 if( uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
82   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)
83 else()
84   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
85 endif()
86
87 if( LLVM_ENABLE_ASSERTIONS )
88   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
89   if( NOT MSVC )
90     add_definitions( -D_DEBUG )
91   endif()
92   # On Release builds cmake automatically defines NDEBUG, so we
93   # explicitly undefine it:
94   if( uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
95     add_definitions( -UNDEBUG )
96   endif()
97 else()
98   if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
99     add_definitions( -DNDEBUG )
100   endif()
101 endif()
102
103 if( LLVM_TARGETS_TO_BUILD STREQUAL "all" )
104   set( LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS} )
105 endif()
106
107 set(LLVM_ENUM_TARGETS "")
108 foreach(c ${LLVM_TARGETS_TO_BUILD})
109   list(FIND LLVM_ALL_TARGETS ${c} idx)
110   if( idx LESS 0 )
111     message(FATAL_ERROR "The target `${c}' does not exist.
112     It should be one of\n${LLVM_ALL_TARGETS}")
113   else()
114     set(LLVM_ENUM_TARGETS "${LLVM_ENUM_TARGETS}LLVM_TARGET(${c})\n")
115   endif()
116 endforeach(c)
117
118 # Produce llvm/Config/Targets.def
119 configure_file(
120   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
121   ${LLVM_BINARY_DIR}/include/llvm/Config/Targets.def
122   )
123
124 set(llvm_builded_incs_dir ${LLVM_BINARY_DIR}/include/llvm)
125
126 # Add path for custom modules
127 set(CMAKE_MODULE_PATH
128   ${CMAKE_MODULE_PATH}
129   "${LLVM_MAIN_SRC_DIR}/cmake"
130   "${LLVM_MAIN_SRC_DIR}/cmake/modules"
131   )
132
133 include(AddLLVMDefinitions)
134
135 if(WIN32)
136   if(CYGWIN)
137     set(LLVM_ON_WIN32 0)
138     set(LLVM_ON_UNIX 1)
139   else(CYGWIN)
140     set(LLVM_ON_WIN32 1)
141     set(LLVM_ON_UNIX 0)
142   endif(CYGWIN)
143   set(LTDL_SHLIB_EXT ".dll")
144   set(EXEEXT ".exe")
145   # Maximum path length is 160 for non-unicode paths
146   set(MAXPATHLEN 160)
147 else(WIN32)
148   if(UNIX)
149     set(LLVM_ON_WIN32 0)
150     set(LLVM_ON_UNIX 1)
151     if(APPLE)
152       set(LTDL_SHLIB_EXT ".dylib")
153     else(APPLE)
154       set(LTDL_SHLIB_EXT ".so")
155     endif(APPLE)
156     set(EXEEXT "")
157     # FIXME: Maximum path length is currently set to 'safe' fixed value
158     set(MAXPATHLEN 2024)
159   else(UNIX)
160     MESSAGE(SEND_ERROR "Unable to determine platform")
161   endif(UNIX)
162 endif(WIN32)
163
164 include(config-ix)
165
166 option(LLVM_ENABLE_PIC "Build Position-Independent Code" ON)
167
168 set(ENABLE_PIC 0)
169 if( LLVM_ENABLE_PIC )
170  if( XCODE )
171    # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
172    # know how to disable this, so just force ENABLE_PIC off for now.
173    message(STATUS "Warning: -fPIC not supported with Xcode.")
174  else( XCODE )
175    if( SUPPORTS_FPIC_FLAG )
176       message(STATUS "Building with -fPIC")
177       add_llvm_definitions(-fPIC)
178       set(ENABLE_PIC 1)
179    else( SUPPORTS_FPIC_FLAG )
180       message(STATUS "Warning: -fPIC not supported.")
181    endif()
182  endif()
183 endif()
184
185 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR} )
186 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
187 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
188
189 # set(CMAKE_VERBOSE_MAKEFILE true)
190
191 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
192 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
193
194 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
195   # TODO: support other platforms and toolchains.
196   option(LLVM_BUILD_32_BITS "Build 32 bits executables and libraries." OFF)
197   if( LLVM_BUILD_32_BITS )
198     message(STATUS "Building 32 bits executables and libraries.")
199     add_llvm_definitions( -m32 )
200     list(APPEND CMAKE_EXE_LINKER_FLAGS -m32)
201     list(APPEND CMAKE_SHARED_LINKER_FLAGS -m32)
202   endif( LLVM_BUILD_32_BITS )
203 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
204
205 if( MSVC )
206   # List of valid CRTs for MSVC
207   set(MSVC_CRT
208     MD
209     MDd)
210
211   set(LLVM_USE_CRT "" CACHE STRING "Specify VC++ CRT to use for debug/release configurations.")
212   add_llvm_definitions( -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS )
213   add_llvm_definitions( -D_SCL_SECURE_NO_WARNINGS -DCRT_NONSTDC_NO_WARNINGS )
214   add_llvm_definitions( -D_SCL_SECURE_NO_DEPRECATE )
215   add_llvm_definitions( -wd4146 -wd4503 -wd4996 -wd4800 -wd4244 -wd4624 )
216   add_llvm_definitions( -wd4355 -wd4715 -wd4180 -wd4345 -wd4224 )
217
218   # Suppress 'new behavior: elements of array 'array' will be default initialized'
219   add_llvm_definitions( -wd4351 )
220
221   if (NOT ${LLVM_USE_CRT} STREQUAL "")
222     list(FIND MSVC_CRT ${LLVM_USE_CRT} idx)
223     if (idx LESS 0)
224       message(FATAL_ERROR "Invalid value for LLVM_USE_CRT: ${LLVM_USE_CRT}. Valid options are one of: ${MSVC_CRT}")
225     endif (idx LESS 0)
226     add_llvm_definitions("/${LLVM_USE_CRT}")
227     message(STATUS "Using VC++ CRT: ${LLVM_USE_CRT}")
228   endif (NOT ${LLVM_USE_CRT} STREQUAL "")
229 endif( MSVC )
230
231 include_directories( ${LLVM_BINARY_DIR}/include ${LLVM_MAIN_INCLUDE_DIR})
232
233 if( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
234    SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-include llvm/System/Solaris.h")
235 endif( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
236
237 include(AddLLVM)
238 include(TableGen)
239
240 add_subdirectory(lib/Support)
241 add_subdirectory(lib/System)
242
243 # Everything else depends on Support and System:
244 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${LLVM_LIBS} )
245
246 set(LLVM_TABLEGEN "tblgen" CACHE
247   STRING "Native TableGen executable. Saves building one when cross-compiling.")
248 # Effective tblgen executable to be used:
249 set(LLVM_TABLEGEN_EXE ${LLVM_TABLEGEN})
250
251 add_subdirectory(utils/TableGen)
252
253 if( CMAKE_CROSSCOMPILING )
254   # This adds a dependency on target `tblgen', so must go after utils/TableGen
255   include( CrossCompileLLVM )
256 endif( CMAKE_CROSSCOMPILING )
257
258 add_subdirectory(include/llvm)
259
260 add_subdirectory(lib/VMCore)
261 add_subdirectory(lib/CodeGen)
262 add_subdirectory(lib/CodeGen/SelectionDAG)
263 add_subdirectory(lib/CodeGen/AsmPrinter)
264 add_subdirectory(lib/Bitcode/Reader)
265 add_subdirectory(lib/Bitcode/Writer)
266 add_subdirectory(lib/Transforms/Utils)
267 add_subdirectory(lib/Transforms/Instrumentation)
268 add_subdirectory(lib/Transforms/Scalar)
269 add_subdirectory(lib/Transforms/IPO)
270 add_subdirectory(lib/Transforms/Hello)
271 add_subdirectory(lib/Linker)
272 add_subdirectory(lib/Analysis)
273 add_subdirectory(lib/Analysis/IPA)
274 add_subdirectory(lib/MC)
275 add_subdirectory(test)
276
277 add_subdirectory(utils/FileCheck)
278 add_subdirectory(utils/count)
279 add_subdirectory(utils/not)
280
281 set(LLVM_ENUM_ASM_PRINTERS "")
282 set(LLVM_ENUM_ASM_PARSERS "")
283 foreach(t ${LLVM_TARGETS_TO_BUILD})
284   message(STATUS "Targeting ${t}")
285   add_subdirectory(lib/Target/${t})
286   add_subdirectory(lib/Target/${t}/TargetInfo)
287   if( EXISTS ${LLVM_MAIN_SRC_DIR}/lib/Target/${t}/AsmPrinter/CMakeLists.txt )
288     add_subdirectory(lib/Target/${t}/AsmPrinter)
289     set(LLVM_ENUM_ASM_PRINTERS 
290       "${LLVM_ENUM_ASM_PRINTERS}LLVM_ASM_PRINTER(${t})\n")
291   endif( EXISTS ${LLVM_MAIN_SRC_DIR}/lib/Target/${t}/AsmPrinter/CMakeLists.txt )
292   if( EXISTS ${LLVM_MAIN_SRC_DIR}/lib/Target/${t}/AsmParser/CMakeLists.txt )
293     add_subdirectory(lib/Target/${t}/AsmParser)
294     set(LLVM_ENUM_ASM_PARSERS 
295       "${LLVM_ENUM_ASM_PARSERS}LLVM_ASM_PARSER(${t})\n")
296   endif( EXISTS ${LLVM_MAIN_SRC_DIR}/lib/Target/${t}/AsmParser/CMakeLists.txt )
297   set(CURRENT_LLVM_TARGET)
298 endforeach(t)
299
300 # Produce llvm/Config/AsmPrinters.def
301 configure_file(
302   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmPrinters.def.in
303   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmPrinters.def
304   )
305
306 # Produce llvm/Config/AsmParsers.def
307 configure_file(
308   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmParsers.def.in
309   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmParsers.def
310   )
311
312 add_subdirectory(lib/ExecutionEngine)
313 add_subdirectory(lib/ExecutionEngine/Interpreter)
314 add_subdirectory(lib/ExecutionEngine/JIT)
315 add_subdirectory(lib/Target)
316 add_subdirectory(lib/AsmParser)
317 add_subdirectory(lib/Archive)
318
319 add_subdirectory(projects)
320
321 option(LLVM_BUILD_TOOLS "Build LLVM tool programs." ON)
322 add_subdirectory(tools)
323
324 option(LLVM_BUILD_EXAMPLES "Build LLVM example programs." OFF)
325 add_subdirectory(examples)
326
327 install(DIRECTORY include/
328   DESTINATION include
329   FILES_MATCHING
330   PATTERN "*.def"
331   PATTERN "*.h"
332   PATTERN "*.td"
333   PATTERN "*.inc"
334   PATTERN ".svn" EXCLUDE
335   )
336
337 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
338   DESTINATION include
339   FILES_MATCHING
340   PATTERN "*.def"
341   PATTERN "*.h"
342   PATTERN "*.gen"
343   PATTERN "*.inc"
344   # Exclude include/llvm/CMakeFiles/intrinsics_gen.dir, matched by "*.def"
345   PATTERN "CMakeFiles" EXCLUDE
346   PATTERN ".svn" EXCLUDE
347   )
348
349 # TODO: make and install documentation.