Fix undefined behavior.
[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.8)
5
6 # Add path for custom modules
7 set(CMAKE_MODULE_PATH
8   ${CMAKE_MODULE_PATH}
9   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
10   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
11   )
12
13 set(LLVM_VERSION_MAJOR 3)
14 set(LLVM_VERSION_MINOR 1)
15
16 set(PACKAGE_VERSION "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}svn")
17
18 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
19
20 include(VersionFromVCS)
21
22 option(LLVM_APPEND_VC_REV
23   "Append the version control system revision id to LLVM version" OFF)
24
25 if( LLVM_APPEND_VC_REV )
26   add_version_info_from_vcs(PACKAGE_VERSION)
27 endif()
28
29 set(PACKAGE_NAME LLVM)
30 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
31 set(PACKAGE_BUGREPORT "http://llvm.org/bugs/")
32
33 # Sanity check our source directory to make sure that we are not trying to
34 # generate an in-tree build (unless on MSVC_IDE, where it is ok), and to make
35 # sure that we don't have any stray generated files lying around in the tree
36 # (which would end up getting picked up by header search, instead of the correct
37 # versions).
38 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
39   message(FATAL_ERROR "In-source builds are not allowed.
40 CMake would overwrite the makefiles distributed with LLVM.
41 Please create a directory and run cmake from there, passing the path
42 to this source directory as the last argument.
43 This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
44 Please delete them.")
45 endif()
46 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
47   file(GLOB_RECURSE
48     tablegenned_files_on_include_dir
49     "${CMAKE_CURRENT_SOURCE_DIR}/include/llvm/*.gen")
50   file(GLOB_RECURSE
51     tablegenned_files_on_lib_dir
52     "${CMAKE_CURRENT_SOURCE_DIR}/lib/Target/*.inc")
53   if( tablegenned_files_on_include_dir OR tablegenned_files_on_lib_dir)
54     message(FATAL_ERROR "Apparently there is a previous in-source build,
55 probably as the result of running `configure' and `make' on
56 ${CMAKE_CURRENT_SOURCE_DIR}.
57 This may cause problems. The suspicious files are:
58 ${tablegenned_files_on_lib_dir}
59 ${tablegenned_files_on_include_dir}
60 Please clean the source directory.")
61   endif()
62 endif()
63
64 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
65
66 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
67 set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include)
68 set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
69 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
70 set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
71 set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
72
73 set(LLVM_ALL_TARGETS
74   ARM
75   CBackend
76   CellSPU
77   CppBackend
78   Hexagon
79   Mips
80   MBlaze
81   MSP430
82   PowerPC
83   PTX
84   Sparc
85   X86
86   XCore
87   )
88
89 # List of targets with JIT support:
90 set(LLVM_TARGETS_WITH_JIT X86 PowerPC ARM Mips)
91
92 if( MSVC )
93   set(LLVM_TARGETS_TO_BUILD X86
94     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
95 else( MSVC )
96   set(LLVM_TARGETS_TO_BUILD "all"
97     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
98 endif( MSVC )
99
100 option(BUILD_SHARED_LIBS
101   "Build all libraries as shared libraries instead of static" OFF)
102
103 option(LLVM_ENABLE_CBE_PRINTF_A "Set to ON if CBE is enabled for printf %a output" ON)
104 if(LLVM_ENABLE_CBE_PRINTF_A)
105   set(ENABLE_CBE_PRINTF_A 1)
106 endif()
107
108 option(LLVM_ENABLE_TIMESTAMPS "Enable embedding timestamp information in build" ON)
109 if(LLVM_ENABLE_TIMESTAMPS)
110   set(ENABLE_TIMESTAMPS 1)
111 endif()
112
113 option(LLVM_ENABLE_FFI "Use libffi to call external functions from the interpreter" OFF)
114 set(FFI_LIBRARY_DIR "" CACHE PATH "Additional directory, where CMake should search for libffi.so")
115 set(FFI_INCLUDE_DIR "" CACHE PATH "Additional directory, where CMake should search for ffi.h or ffi/ffi.h")
116
117 set(LLVM_TARGET_ARCH "host"
118   CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")
119
120 option(LLVM_ENABLE_THREADS "Use threads if available." ON)
121
122 if( LLVM_TARGETS_TO_BUILD STREQUAL "all" )
123   set( LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS} )
124 endif()
125
126 set(LLVM_ENUM_TARGETS "")
127 foreach(c ${LLVM_TARGETS_TO_BUILD})
128   list(FIND LLVM_ALL_TARGETS ${c} idx)
129   if( idx LESS 0 )
130     message(FATAL_ERROR "The target `${c}' does not exist.
131     It should be one of\n${LLVM_ALL_TARGETS}")
132   else()
133     set(LLVM_ENUM_TARGETS "${LLVM_ENUM_TARGETS}LLVM_TARGET(${c})\n")
134   endif()
135 endforeach(c)
136
137 set(llvm_builded_incs_dir ${LLVM_BINARY_DIR}/include/llvm)
138
139 include(AddLLVMDefinitions)
140
141 option(LLVM_ENABLE_PIC "Build Position-Independent Code" ON)
142
143 # MSVC has a gazillion warnings with this.
144 if( MSVC )
145   option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." OFF)
146 else( MSVC )
147   option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
148 endif()
149
150 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
151 option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
152
153 if( uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
154   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)
155 else()
156   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
157 endif()
158
159 # Define an option controlling whether we should build for 32-bit on 64-bit
160 # platforms, where supported.
161 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
162   # TODO: support other platforms and toolchains.
163   option(LLVM_BUILD_32_BITS "Build 32 bits executables and libraries." OFF)
164 endif()
165
166 # Define the default arguments to use with 'lit', and an option for the user to
167 # override.
168 set(LIT_ARGS_DEFAULT "-sv")
169 if (MSVC OR XCODE)
170   set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
171 endif()
172 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
173
174 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
175 if( WIN32 AND NOT CYGWIN )
176   set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
177 endif()
178
179 # On Win32 using MS tools, provide an option to set the number of parallel jobs
180 # to use.
181 if( MSVC_IDE AND ( MSVC90 OR MSVC10 ) )
182   # Only Visual Studio 2008 and 2010 officially supports /MP.  Visual Studio
183   # 2005 supports it but it is experimental.
184   set(LLVM_COMPILER_JOBS "0" CACHE STRING
185     "Number of parallel compiler jobs. 0 means use all processors. Default is 0.")
186 endif()
187
188 # Define options to control the inclusion and default build behavior for
189 # components which may not strictly be necessary (tools, runtime, examples, and
190 # tests).
191 #
192 # This is primarily to support building smaller or faster project files.
193 option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
194 option(LLVM_BUILD_TOOLS
195   "Build the LLVM tools. If OFF, just generate build targets." ON)
196
197 option(LLVM_INCLUDE_RUNTIME "Generate build targets for the LLVM runtimes" ON)
198 option(LLVM_BUILD_RUNTIME
199   "Build the LLVM runtime libraries. If OFF, just generate build targets." ON)
200
201 option(LLVM_BUILD_EXAMPLES
202   "Build the LLVM example programs. If OFF, just generate build targets." OFF)
203 option(LLVM_INCLUDE_EXAMPLES "Generate build targets for the LLVM examples" ON)
204
205 option(LLVM_BUILD_TESTS
206   "Build LLVM unit tests. If OFF, just generate build targets." OFF)
207 option(LLVM_INCLUDE_TESTS "Generate build targets for the LLVM unit tests." ON)
208
209 # All options referred to from HandleLLVMOptions have to be specified
210 # BEFORE this include, otherwise options will not be correctly set on
211 # first cmake run
212 include(config-ix)
213 include(HandleLLVMOptions)
214
215 # Verify that we can find a Python interpreter,
216 include(FindPythonInterp)
217 if( NOT PYTHONINTERP_FOUND )
218   message(FATAL_ERROR
219 "Unable to find Python interpreter, required for builds and testing.
220
221 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
222 endif()
223
224 ######
225 # LLVMBuild Integration
226 #
227 # We use llvm-build to generate all the data required by the CMake based
228 # build system in one swoop:
229 #
230 #  - We generate a file (a CMake fragment) in the object root which contains
231 #    all the definitions that are required by CMake.
232 #
233 #  - We generate the library table used by llvm-config.
234 #
235 #  - We generate the dependencies for the CMake fragment, so that we will
236 #    automatically reconfigure outselves.
237
238 set(LLVMBUILDTOOL "${LLVM_MAIN_SRC_DIR}/utils/llvm-build/llvm-build")
239 set(LLVMCONFIGLIBRARYDEPENDENCIESINC
240   "${LLVM_BINARY_DIR}/tools/llvm-config/LibraryDependencies.inc")
241 set(LLVMBUILDCMAKEFRAG
242   "${LLVM_BINARY_DIR}/LLVMBuild.cmake")
243 message(STATUS "Constructing LLVMBuild project information")
244 execute_process(
245   COMMAND ${PYTHON_EXECUTABLE} ${LLVMBUILDTOOL}
246             --native-target "${LLVM_NATIVE_ARCH}"
247             --enable-targets "${LLVM_TARGETS_TO_BUILD}"
248             --write-library-table ${LLVMCONFIGLIBRARYDEPENDENCIESINC}
249             --write-cmake-fragment ${LLVMBUILDCMAKEFRAG}
250             ERROR_VARIABLE LLVMBUILDOUTPUT
251             ERROR_VARIABLE LLVMBUILDERRORS
252             OUTPUT_STRIP_TRAILING_WHITESPACE
253             ERROR_STRIP_TRAILING_WHITESPACE
254   RESULT_VARIABLE LLVMBUILDRESULT)
255
256 # On Win32, CMake doesn't properly handle piping the default output/error
257 # streams into the GUI console. So, we explicitly catch and report them.
258 if( NOT "${LLVMBUILDOUTPUT}" STREQUAL "")
259   message(STATUS "llvm-build output: ${LLVMBUILDOUTPUT}")
260 endif()
261 if( NOT "${LLVMBUILDRESULT}" STREQUAL "0" )
262   message(FATAL_ERROR
263     "Unexpected failure executing llvm-build: ${LLVMBUILDERRORS}")
264 endif()
265
266 # Include the generated CMake fragment. This will define properties from the
267 # LLVMBuild files in a format which is easy to consume from CMake, and will add
268 # the dependencies so that CMake will reconfigure properly when the LLVMBuild
269 # files change.
270 include(${LLVMBUILDCMAKEFRAG})
271
272 ######
273
274 # Configure all of the various header file fragments LLVM uses which depend on
275 # configuration variables.
276 set(LLVM_ENUM_ASM_PRINTERS "")
277 set(LLVM_ENUM_ASM_PARSERS "")
278 set(LLVM_ENUM_DISASSEMBLERS "")
279 foreach(t ${LLVM_TARGETS_TO_BUILD})
280   set( td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t} )
281   file(GLOB asmp_file "${td}/*AsmPrinter.cpp")
282   if( asmp_file )
283     set(LLVM_ENUM_ASM_PRINTERS
284       "${LLVM_ENUM_ASM_PRINTERS}LLVM_ASM_PRINTER(${t})\n")
285   endif()
286   if( EXISTS ${td}/AsmParser/CMakeLists.txt )
287     set(LLVM_ENUM_ASM_PARSERS
288       "${LLVM_ENUM_ASM_PARSERS}LLVM_ASM_PARSER(${t})\n")
289   endif()
290   if( EXISTS ${td}/Disassembler/CMakeLists.txt )
291     set(LLVM_ENUM_DISASSEMBLERS
292       "${LLVM_ENUM_DISASSEMBLERS}LLVM_DISASSEMBLER(${t})\n")
293   endif()
294 endforeach(t)
295
296 # Produce the target definition files, which provide a way for clients to easily
297 # include various classes of targets.
298 configure_file(
299   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmPrinters.def.in
300   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmPrinters.def
301   )
302 configure_file(
303   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmParsers.def.in
304   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmParsers.def
305   )
306 configure_file(
307   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Disassemblers.def.in
308   ${LLVM_BINARY_DIR}/include/llvm/Config/Disassemblers.def
309   )
310 configure_file(
311   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
312   ${LLVM_BINARY_DIR}/include/llvm/Config/Targets.def
313   )
314
315 # Configure the three LLVM configuration header files.
316 configure_file(
317   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/config.h.cmake
318   ${LLVM_BINARY_DIR}/include/llvm/Config/config.h)
319 configure_file(
320   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/llvm-config.h.cmake
321   ${LLVM_BINARY_DIR}/include/llvm/Config/llvm-config.h)
322 configure_file(
323   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support/DataTypes.h.cmake
324   ${LLVM_BINARY_DIR}/include/llvm/Support/DataTypes.h)
325
326 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR} )
327 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
328 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
329
330 set(CMAKE_INCLUDE_CURRENT_DIR ON)
331
332 include_directories( ${LLVM_BINARY_DIR}/include ${LLVM_MAIN_INCLUDE_DIR})
333
334 if( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
335    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include llvm/Support/Solaris.h")
336 endif( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
337
338 include(AddLLVM)
339 include(TableGen)
340
341 if( MINGW )
342   # People report that -O3 is unreliable on MinGW. The traditional
343   # build also uses -O2 for that reason:
344   llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
345 endif()
346
347 # Put this before tblgen. Else we have a circular dependence.
348 add_subdirectory(lib/Support)
349 add_subdirectory(lib/TableGen)
350
351 add_subdirectory(utils/TableGen)
352
353 add_subdirectory(include/llvm)
354
355 add_subdirectory(lib)
356
357 add_subdirectory(utils/FileCheck)
358 add_subdirectory(utils/FileUpdate)
359 add_subdirectory(utils/count)
360 add_subdirectory(utils/not)
361 add_subdirectory(utils/llvm-lit)
362 add_subdirectory(utils/json-bench)
363
364 add_subdirectory(projects)
365
366 if( LLVM_INCLUDE_TOOLS )
367   add_subdirectory(tools)
368 endif()
369
370 if( LLVM_INCLUDE_RUNTIME )
371   add_subdirectory(runtime)
372 endif()
373
374 if( LLVM_INCLUDE_EXAMPLES )
375   add_subdirectory(examples)
376 endif()
377
378 if( LLVM_INCLUDE_TESTS )
379   add_subdirectory(test)
380   add_subdirectory(utils/unittest)
381   add_subdirectory(unittests)
382   if (MSVC)
383     # This utility is used to prevent chrashing tests from calling Dr. Watson on
384     # Windows.
385     add_subdirectory(utils/KillTheDoctor)
386   endif()
387 endif()
388
389 add_subdirectory(cmake/modules)
390
391 install(DIRECTORY include/
392   DESTINATION include
393   FILES_MATCHING
394   PATTERN "*.def"
395   PATTERN "*.h"
396   PATTERN "*.td"
397   PATTERN "*.inc"
398   PATTERN "LICENSE.TXT"
399   PATTERN ".svn" EXCLUDE
400   )
401
402 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
403   DESTINATION include
404   FILES_MATCHING
405   PATTERN "*.def"
406   PATTERN "*.h"
407   PATTERN "*.gen"
408   PATTERN "*.inc"
409   # Exclude include/llvm/CMakeFiles/intrinsics_gen.dir, matched by "*.def"
410   PATTERN "CMakeFiles" EXCLUDE
411   PATTERN ".svn" EXCLUDE
412   )
413
414 # TODO: make and install documentation.
415
416 set(CPACK_PACKAGE_VENDOR "LLVM")
417 set(CPACK_PACKAGE_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
418 set(CPACK_PACKAGE_VERSION_MINOR ${LLVM_VERSION_MINOR})
419 add_version_info_from_vcs(CPACK_PACKAGE_VERSION_PATCH)
420 include(CPack)
421
422 # Workaround for MSVS10 to avoid the Dialog Hell
423 # FIXME: This could be removed with future version of CMake.
424 if(MSVC_VERSION EQUAL 1600)
425   set(LLVM_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/LLVM.sln")
426   if( EXISTS "${LLVM_SLN_FILENAME}" )
427     file(APPEND "${LLVM_SLN_FILENAME}" "\n# This should be regenerated!\n")
428   endif()
429 endif()