Add test cases that will show the bug that was fixed in r256725.
[oota-llvm.git] / CMakeLists.txt
1 # See docs/CMake.html for instructions about how to build LLVM with CMake.
2
3 cmake_minimum_required(VERSION 2.8.12.2)
4
5 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
6   message(STATUS "No build type selected, default to Debug")
7   set(CMAKE_BUILD_TYPE "Debug")
8 endif()
9
10 if(POLICY CMP0022)
11   cmake_policy(SET CMP0022 NEW) # automatic when 2.8.12 is required
12 endif()
13
14 if (POLICY CMP0051)
15   # CMake 3.1 and higher include generator expressions of the form
16   # $<TARGETLIB:obj> in the SOURCES property.  These need to be
17   # stripped everywhere that access the SOURCES property, so we just
18   # defer to the OLD behavior of not including generator expressions
19   # in the output for now.
20   cmake_policy(SET CMP0051 OLD)
21 endif()
22
23 if(CMAKE_VERSION VERSION_LESS 3.1.20141117)
24   set(cmake_3_2_USES_TERMINAL)
25 else()
26   set(cmake_3_2_USES_TERMINAL USES_TERMINAL)
27 endif()
28
29 if(NOT DEFINED LLVM_VERSION_MAJOR)
30   set(LLVM_VERSION_MAJOR 3)
31 endif()
32 if(NOT DEFINED LLVM_VERSION_MINOR)
33   set(LLVM_VERSION_MINOR 8)
34 endif()
35 if(NOT DEFINED LLVM_VERSION_PATCH)
36   set(LLVM_VERSION_PATCH 0)
37 endif()
38 if(NOT DEFINED LLVM_VERSION_SUFFIX)
39   set(LLVM_VERSION_SUFFIX svn)
40 endif()
41
42 if (POLICY CMP0048)
43   cmake_policy(SET CMP0048 NEW)
44   set(cmake_3_0_PROJ_VERSION
45     VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH})
46   set(cmake_3_0_LANGUAGES LANGUAGES)
47 endif()
48
49 if (NOT PACKAGE_VERSION)
50   set(PACKAGE_VERSION
51     "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}")
52 endif()
53
54 project(LLVM
55   ${cmake_3_0_PROJ_VERSION}
56   ${cmake_3_0_LANGUAGES}
57   C CXX ASM)
58
59 # The following only works with the Ninja generator in CMake >= 3.0.
60 set(LLVM_PARALLEL_COMPILE_JOBS "" CACHE STRING
61   "Define the maximum number of concurrent compilation jobs.")
62 if(LLVM_PARALLEL_COMPILE_JOBS)
63   if(CMAKE_VERSION VERSION_LESS 3.0 OR NOT CMAKE_MAKE_PROGRAM MATCHES "ninja")
64     message(WARNING "Job pooling is only available with Ninja generators and CMake 3.0 and later.")
65   else()
66     set_property(GLOBAL APPEND PROPERTY JOB_POOLS compile_job_pool=${LLVM_PARALLEL_COMPILE_JOBS})
67     set(CMAKE_JOB_POOL_COMPILE compile_job_pool)
68   endif()
69 endif()
70
71 set(LLVM_PARALLEL_LINK_JOBS "" CACHE STRING
72   "Define the maximum number of concurrent link jobs.")
73 if(LLVM_PARALLEL_LINK_JOBS)
74   if(CMAKE_VERSION VERSION_LESS 3.0 OR NOT CMAKE_MAKE_PROGRAM MATCHES "ninja")
75     message(WARNING "Job pooling is only available with Ninja generators and CMake 3.0 and later.")
76   else()
77     set_property(GLOBAL APPEND PROPERTY JOB_POOLS link_job_pool=${LLVM_PARALLEL_LINK_JOBS})
78     set(CMAKE_JOB_POOL_LINK link_job_pool)
79   endif()
80 endif()
81
82 # Add path for custom modules
83 set(CMAKE_MODULE_PATH
84   ${CMAKE_MODULE_PATH}
85   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
86   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
87   )
88
89 # Generate a CompilationDatabase (compile_commands.json file) for our build,
90 # for use by clang_complete, YouCompleteMe, etc.
91 set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
92
93 option(LLVM_INSTALL_UTILS "Include utility binaries in the 'install' target." OFF)
94
95 option(LLVM_INSTALL_TOOLCHAIN_ONLY "Only include toolchain files in the 'install' target." OFF)
96
97 option(LLVM_USE_FOLDERS "Enable solution folders in Visual Studio. Disable for Express versions." ON)
98 if ( LLVM_USE_FOLDERS )
99   set_property(GLOBAL PROPERTY USE_FOLDERS ON)
100 endif()
101
102 include(VersionFromVCS)
103
104 option(LLVM_APPEND_VC_REV
105   "Append the version control system revision id to LLVM version" OFF)
106
107 if( LLVM_APPEND_VC_REV )
108   add_version_info_from_vcs(PACKAGE_VERSION)
109 endif()
110
111 set(PACKAGE_NAME LLVM)
112 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
113 set(PACKAGE_BUGREPORT "http://llvm.org/bugs/")
114
115 set(BUG_REPORT_URL "${PACKAGE_BUGREPORT}" CACHE STRING
116   "Default URL where bug reports are to be submitted.")
117
118 # Configure CPack.
119 set(CPACK_PACKAGE_INSTALL_DIRECTORY "LLVM")
120 set(CPACK_PACKAGE_VENDOR "LLVM")
121 set(CPACK_PACKAGE_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
122 set(CPACK_PACKAGE_VERSION_MINOR ${LLVM_VERSION_MINOR})
123 set(CPACK_PACKAGE_VERSION_PATCH ${LLVM_VERSION_PATCH})
124 set(CPACK_PACKAGE_VERSION ${PACKAGE_VERSION})
125 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.TXT")
126 set(CPACK_NSIS_COMPRESSOR "/SOLID lzma \r\n SetCompressorDictSize 32")
127 if(WIN32 AND NOT UNIX)
128   set(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "LLVM")
129   set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_logo.bmp")
130   set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_icon.ico")
131   set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_icon.ico")
132   set(CPACK_NSIS_MODIFY_PATH "ON")
133   set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL "ON")
134   set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS
135     "ExecWait '$INSTDIR/tools/msbuild/install.bat'")
136   set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS
137     "ExecWait '$INSTDIR/tools/msbuild/uninstall.bat'")
138   if( CMAKE_CL_64 )
139     set(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
140   endif()
141 endif()
142 include(CPack)
143
144 # Sanity check our source directory to make sure that we are not trying to
145 # generate an in-tree build (unless on MSVC_IDE, where it is ok), and to make
146 # sure that we don't have any stray generated files lying around in the tree
147 # (which would end up getting picked up by header search, instead of the correct
148 # versions).
149 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
150   message(FATAL_ERROR "In-source builds are not allowed.
151 CMake would overwrite the makefiles distributed with LLVM.
152 Please create a directory and run cmake from there, passing the path
153 to this source directory as the last argument.
154 This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
155 Please delete them.")
156 endif()
157 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
158   file(GLOB_RECURSE
159     tablegenned_files_on_include_dir
160     "${CMAKE_CURRENT_SOURCE_DIR}/include/llvm/*.gen")
161   file(GLOB_RECURSE
162     tablegenned_files_on_lib_dir
163     "${CMAKE_CURRENT_SOURCE_DIR}/lib/Target/*.inc")
164   if( tablegenned_files_on_include_dir OR tablegenned_files_on_lib_dir)
165     message(FATAL_ERROR "Apparently there is a previous in-source build,
166 probably as the result of running `configure' and `make' on
167 ${CMAKE_CURRENT_SOURCE_DIR}.
168 This may cause problems. The suspicious files are:
169 ${tablegenned_files_on_lib_dir}
170 ${tablegenned_files_on_include_dir}
171 Please clean the source directory.")
172   endif()
173 endif()
174
175 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
176
177 if (CMAKE_BUILD_TYPE AND
178     NOT uppercase_CMAKE_BUILD_TYPE MATCHES "^(DEBUG|RELEASE|RELWITHDEBINFO|MINSIZEREL)$")
179   message(FATAL_ERROR "Invalid value for CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
180 endif()
181
182 set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
183
184 # They are used as destination of target generators.
185 set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
186 set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
187 if(WIN32 OR CYGWIN)
188   # DLL platform -- put DLLs into bin.
189   set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
190 else()
191   set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
192 endif()
193
194 # Each of them corresponds to llvm-config's.
195 set(LLVM_TOOLS_BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) # --bindir
196 set(LLVM_LIBRARY_DIR      ${LLVM_LIBRARY_OUTPUT_INTDIR}) # --libdir
197 set(LLVM_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  ) # --src-root
198 set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include ) # --includedir
199 set(LLVM_BINARY_DIR       ${CMAKE_CURRENT_BINARY_DIR}  ) # --prefix
200
201 set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
202 set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
203
204 set(LLVM_ALL_TARGETS
205   AArch64
206   AMDGPU
207   ARM
208   BPF
209   CppBackend
210   Hexagon
211   Mips
212   MSP430
213   NVPTX
214   PowerPC
215   Sparc
216   SystemZ
217   X86
218   XCore
219   )
220
221 # List of targets with JIT support:
222 set(LLVM_TARGETS_WITH_JIT X86 PowerPC AArch64 ARM Mips SystemZ)
223
224 set(LLVM_TARGETS_TO_BUILD "all"
225     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
226
227 set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ""
228   CACHE STRING "Semicolon-separated list of experimental targets to build.")
229
230 option(BUILD_SHARED_LIBS
231   "Build all libraries as shared libraries instead of static" OFF)
232
233 option(LLVM_ENABLE_TIMESTAMPS "Enable embedding timestamp information in build" ON)
234 if(LLVM_ENABLE_TIMESTAMPS)
235   set(ENABLE_TIMESTAMPS 1)
236 endif()
237
238 option(LLVM_ENABLE_BACKTRACES "Enable embedding backtraces on crash." ON)
239 if(LLVM_ENABLE_BACKTRACES)
240   set(ENABLE_BACKTRACES 1)
241 endif()
242
243 option(LLVM_ENABLE_CRASH_OVERRIDES "Enable crash overrides." ON)
244 if(LLVM_ENABLE_CRASH_OVERRIDES)
245   set(ENABLE_CRASH_OVERRIDES 1)
246 endif()
247
248 option(LLVM_ENABLE_FFI "Use libffi to call external functions from the interpreter" OFF)
249 set(FFI_LIBRARY_DIR "" CACHE PATH "Additional directory, where CMake should search for libffi.so")
250 set(FFI_INCLUDE_DIR "" CACHE PATH "Additional directory, where CMake should search for ffi.h or ffi/ffi.h")
251
252 set(LLVM_TARGET_ARCH "host"
253   CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")
254
255 option(LLVM_ENABLE_TERMINFO "Use terminfo database if available." ON)
256
257 option(LLVM_ENABLE_THREADS "Use threads if available." ON)
258
259 option(LLVM_ENABLE_ZLIB "Use zlib for compression/decompression if available." ON)
260
261 if( LLVM_TARGETS_TO_BUILD STREQUAL "all" )
262   set( LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS} )
263 endif()
264
265 set(LLVM_TARGETS_TO_BUILD
266    ${LLVM_TARGETS_TO_BUILD}
267    ${LLVM_EXPERIMENTAL_TARGETS_TO_BUILD})
268 list(REMOVE_DUPLICATES LLVM_TARGETS_TO_BUILD)
269
270 include(AddLLVMDefinitions)
271
272 option(LLVM_ENABLE_PIC "Build Position-Independent Code" ON)
273 option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
274 option(LLVM_ENABLE_MODULES "Compile with C++ modules enabled." OFF)
275 option(LLVM_ENABLE_CXX1Y "Compile with C++1y enabled." OFF)
276 option(LLVM_ENABLE_LIBCXX "Use libc++ if available." OFF)
277 option(LLVM_ENABLE_LIBCXXABI "Use libc++abi when using libc++." OFF)
278 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
279 option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
280
281 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
282   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)
283 else()
284   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
285 endif()
286
287 set(LLVM_ABI_BREAKING_CHECKS "WITH_ASSERTS" CACHE STRING
288   "Enable abi-breaking checks.  Can be WITH_ASSERTS, FORCE_ON or FORCE_OFF.")
289
290 option(LLVM_FORCE_USE_OLD_HOST_TOOLCHAIN
291        "Set to ON to force using an old, unsupported host toolchain." OFF)
292
293 option(LLVM_USE_INTEL_JITEVENTS
294   "Use Intel JIT API to inform Intel(R) VTune(TM) Amplifier XE 2011 about JIT code"
295   OFF)
296
297 if( LLVM_USE_INTEL_JITEVENTS )
298   # Verify we are on a supported platform
299   if( NOT CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
300     message(FATAL_ERROR
301       "Intel JIT API support is available on Linux and Windows only.")
302   endif()
303 endif( LLVM_USE_INTEL_JITEVENTS )
304
305 option(LLVM_USE_OPROFILE
306   "Use opagent JIT interface to inform OProfile about JIT code" OFF)
307
308 option(LLVM_EXTERNALIZE_DEBUGINFO
309   "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
310
311 # If enabled, verify we are on a platform that supports oprofile.
312 if( LLVM_USE_OPROFILE )
313   if( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
314     message(FATAL_ERROR "OProfile support is available on Linux only.")
315   endif( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
316 endif( LLVM_USE_OPROFILE )
317
318 set(LLVM_USE_SANITIZER "" CACHE STRING
319   "Define the sanitizer used to build binaries and tests.")
320
321 option(LLVM_USE_SPLIT_DWARF
322   "Use -gsplit-dwarf when compiling llvm." OFF)
323
324 option(WITH_POLLY "Build LLVM with Polly" ON)
325 option(LINK_POLLY_INTO_TOOLS "Static link Polly into tools" OFF)
326
327 # Define an option controlling whether we should build for 32-bit on 64-bit
328 # platforms, where supported.
329 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
330   # TODO: support other platforms and toolchains.
331   option(LLVM_BUILD_32_BITS "Build 32 bits executables and libraries." OFF)
332 endif()
333
334 # Define the default arguments to use with 'lit', and an option for the user to
335 # override.
336 set(LIT_ARGS_DEFAULT "-sv")
337 if (MSVC_IDE OR XCODE)
338   set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
339 endif()
340 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
341
342 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
343 if( WIN32 AND NOT CYGWIN )
344   set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
345 endif()
346
347 # Define options to control the inclusion and default build behavior for
348 # components which may not strictly be necessary (tools, examples, and tests).
349 #
350 # This is primarily to support building smaller or faster project files.
351 option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
352 option(LLVM_BUILD_TOOLS
353   "Build the LLVM tools. If OFF, just generate build targets." ON)
354
355 option(LLVM_INCLUDE_UTILS "Generate build targets for the LLVM utils." ON)
356
357 option(LLVM_BUILD_RUNTIME
358   "Build the LLVM runtime libraries." ON)
359 option(LLVM_BUILD_EXAMPLES
360   "Build the LLVM example programs. If OFF, just generate build targets." OFF)
361 option(LLVM_INCLUDE_EXAMPLES "Generate build targets for the LLVM examples" ON)
362
363 option(LLVM_BUILD_TESTS
364   "Build LLVM unit tests. If OFF, just generate build targets." OFF)
365 option(LLVM_INCLUDE_TESTS "Generate build targets for the LLVM unit tests." ON)
366 option(LLVM_INCLUDE_GO_TESTS "Include the Go bindings tests in test build targets." ON)
367
368 option (LLVM_BUILD_DOCS "Build the llvm documentation." OFF)
369 option (LLVM_INCLUDE_DOCS "Generate build targets for llvm documentation." ON)
370 option (LLVM_ENABLE_DOXYGEN "Use doxygen to generate llvm API documentation." OFF)
371 option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF)
372
373 option (LLVM_BUILD_EXTERNAL_COMPILER_RT
374   "Build compiler-rt as an external project." OFF)
375
376 # You can configure which libraries from LLVM you want to include in the
377 # shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
378 # list of LLVM components. All component names handled by llvm-config are valid.
379 if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
380   set(LLVM_DYLIB_COMPONENTS "all" CACHE STRING
381     "Semicolon-separated list of components to include in libLLVM, or \"all\".")
382 endif()
383 option(LLVM_LINK_LLVM_DYLIB "Link tools against the libllvm dynamic library" OFF)
384 option(LLVM_BUILD_LLVM_C_DYLIB "Build libllvm-c re-export library (Darwin Only)" OFF)
385 set(LLVM_BUILD_LLVM_DYLIB_default OFF)
386 if(LLVM_LINK_LLVM_DYLIB OR LLVM_BUILD_LLVM_C_DYLIB)
387   set(LLVM_BUILD_LLVM_DYLIB_default ON)
388 endif()
389 option(LLVM_BUILD_LLVM_DYLIB "Build libllvm dynamic library" ${LLVM_BUILD_LLVM_DYLIB_default})
390 set(LLVM_DISABLE_LLVM_DYLIB_ATEXIT_DEFAULT ON)
391 if (LLVM_LINK_LLVM_DYLIB)
392   set(LLVM_DISABLE_LLVM_DYLIB_ATEXIT_DEFAULT OFF)
393 endif()
394 option(LLVM_DISABLE_LLVM_DYLIB_ATEXIT "Disable llvm-shlib's atexit destructors." ${LLVM_DISABLE_LLVM_DYLIB_ATEXIT_DEFAULT})
395 if(LLVM_DISABLE_LLVM_DYLIB_ATEXIT)
396   set(DISABLE_LLVM_DYLIB_ATEXIT 1)
397 endif()
398
399 option(LLVM_OPTIMIZED_TABLEGEN "Force TableGen to be built with optimization" OFF)
400 if(CMAKE_CROSSCOMPILING OR (LLVM_OPTIMIZED_TABLEGEN AND LLVM_ENABLE_ASSERTIONS))
401   set(LLVM_USE_HOST_TOOLS ON)
402 endif()
403
404 # All options referred to from HandleLLVMOptions have to be specified
405 # BEFORE this include, otherwise options will not be correctly set on
406 # first cmake run
407 include(config-ix)
408
409 # By default, we target the host, but this can be overridden at CMake
410 # invocation time.
411 set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_HOST_TRIPLE}" CACHE STRING
412   "Default target for which LLVM will generate code." )
413 set(TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
414
415 include(HandleLLVMOptions)
416
417 # Verify that we can find a Python 2 interpreter.  Python 3 is unsupported.
418 # FIXME: We should support systems with only Python 3, but that requires work
419 # on LLDB.
420 set(Python_ADDITIONAL_VERSIONS 2.7)
421 include(FindPythonInterp)
422 if( NOT PYTHONINTERP_FOUND )
423   message(FATAL_ERROR
424 "Unable to find Python interpreter, required for builds and testing.
425
426 Please install Python or specify the PYTHON_EXECUTABLE CMake variable.")
427 endif()
428
429 if( ${PYTHON_VERSION_STRING} VERSION_LESS 2.7 )
430   message(FATAL_ERROR "Python 2.7 or newer is required")
431 endif()
432
433 ######
434 # LLVMBuild Integration
435 #
436 # We use llvm-build to generate all the data required by the CMake based
437 # build system in one swoop:
438 #
439 #  - We generate a file (a CMake fragment) in the object root which contains
440 #    all the definitions that are required by CMake.
441 #
442 #  - We generate the library table used by llvm-config.
443 #
444 #  - We generate the dependencies for the CMake fragment, so that we will
445 #    automatically reconfigure outselves.
446
447 set(LLVMBUILDTOOL "${LLVM_MAIN_SRC_DIR}/utils/llvm-build/llvm-build")
448 set(LLVMCONFIGLIBRARYDEPENDENCIESINC
449   "${LLVM_BINARY_DIR}/tools/llvm-config/LibraryDependencies.inc")
450 set(LLVMBUILDCMAKEFRAG
451   "${LLVM_BINARY_DIR}/LLVMBuild.cmake")
452
453 # Create the list of optional components that are enabled
454 if (LLVM_USE_INTEL_JITEVENTS)
455   set(LLVMOPTIONALCOMPONENTS IntelJITEvents)
456 endif (LLVM_USE_INTEL_JITEVENTS)
457 if (LLVM_USE_OPROFILE)
458   set(LLVMOPTIONALCOMPONENTS ${LLVMOPTIONALCOMPONENTS} OProfileJIT)
459 endif (LLVM_USE_OPROFILE)
460
461 message(STATUS "Constructing LLVMBuild project information")
462 execute_process(
463   COMMAND ${PYTHON_EXECUTABLE} ${LLVMBUILDTOOL}
464             --native-target "${LLVM_NATIVE_ARCH}"
465             --enable-targets "${LLVM_TARGETS_TO_BUILD}"
466             --enable-optional-components "${LLVMOPTIONALCOMPONENTS}"
467             --write-library-table ${LLVMCONFIGLIBRARYDEPENDENCIESINC}
468             --write-cmake-fragment ${LLVMBUILDCMAKEFRAG}
469             OUTPUT_VARIABLE LLVMBUILDOUTPUT
470             ERROR_VARIABLE LLVMBUILDERRORS
471             OUTPUT_STRIP_TRAILING_WHITESPACE
472             ERROR_STRIP_TRAILING_WHITESPACE
473   RESULT_VARIABLE LLVMBUILDRESULT)
474
475 # On Win32, CMake doesn't properly handle piping the default output/error
476 # streams into the GUI console. So, we explicitly catch and report them.
477 if( NOT "${LLVMBUILDOUTPUT}" STREQUAL "")
478   message(STATUS "llvm-build output: ${LLVMBUILDOUTPUT}")
479 endif()
480 if( NOT "${LLVMBUILDRESULT}" STREQUAL "0" )
481   message(FATAL_ERROR
482     "Unexpected failure executing llvm-build: ${LLVMBUILDERRORS}")
483 endif()
484
485 # Include the generated CMake fragment. This will define properties from the
486 # LLVMBuild files in a format which is easy to consume from CMake, and will add
487 # the dependencies so that CMake will reconfigure properly when the LLVMBuild
488 # files change.
489 include(${LLVMBUILDCMAKEFRAG})
490
491 ######
492
493 # Configure all of the various header file fragments LLVM uses which depend on
494 # configuration variables.
495 set(LLVM_ENUM_TARGETS "")
496 set(LLVM_ENUM_ASM_PRINTERS "")
497 set(LLVM_ENUM_ASM_PARSERS "")
498 set(LLVM_ENUM_DISASSEMBLERS "")
499 foreach(t ${LLVM_TARGETS_TO_BUILD})
500   set( td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t} )
501
502   list(FIND LLVM_ALL_TARGETS ${t} idx)
503   list(FIND LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ${t} idy)
504   if( idx LESS 0 AND idy LESS 0 )
505     message(FATAL_ERROR "The target `${t}' does not exist.
506     It should be one of\n${LLVM_ALL_TARGETS}")
507   else()
508     set(LLVM_ENUM_TARGETS "${LLVM_ENUM_TARGETS}LLVM_TARGET(${t})\n")
509   endif()
510
511   file(GLOB asmp_file "${td}/*AsmPrinter.cpp")
512   if( asmp_file )
513     set(LLVM_ENUM_ASM_PRINTERS
514       "${LLVM_ENUM_ASM_PRINTERS}LLVM_ASM_PRINTER(${t})\n")
515   endif()
516   if( EXISTS ${td}/AsmParser/CMakeLists.txt )
517     set(LLVM_ENUM_ASM_PARSERS
518       "${LLVM_ENUM_ASM_PARSERS}LLVM_ASM_PARSER(${t})\n")
519   endif()
520   if( EXISTS ${td}/Disassembler/CMakeLists.txt )
521     set(LLVM_ENUM_DISASSEMBLERS
522       "${LLVM_ENUM_DISASSEMBLERS}LLVM_DISASSEMBLER(${t})\n")
523   endif()
524 endforeach(t)
525
526 # Produce the target definition files, which provide a way for clients to easily
527 # include various classes of targets.
528 configure_file(
529   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmPrinters.def.in
530   ${LLVM_INCLUDE_DIR}/llvm/Config/AsmPrinters.def
531   )
532 configure_file(
533   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmParsers.def.in
534   ${LLVM_INCLUDE_DIR}/llvm/Config/AsmParsers.def
535   )
536 configure_file(
537   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Disassemblers.def.in
538   ${LLVM_INCLUDE_DIR}/llvm/Config/Disassemblers.def
539   )
540 configure_file(
541   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
542   ${LLVM_INCLUDE_DIR}/llvm/Config/Targets.def
543   )
544
545 # Configure the three LLVM configuration header files.
546 configure_file(
547   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/config.h.cmake
548   ${LLVM_INCLUDE_DIR}/llvm/Config/config.h)
549 configure_file(
550   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/llvm-config.h.cmake
551   ${LLVM_INCLUDE_DIR}/llvm/Config/llvm-config.h)
552 configure_file(
553   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support/DataTypes.h.cmake
554   ${LLVM_INCLUDE_DIR}/llvm/Support/DataTypes.h)
555
556 # They are not referenced. See set_output_directory().
557 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/bin )
558 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
559 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
560
561 set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
562 if (APPLE)
563   set(CMAKE_INSTALL_NAME_DIR "@rpath")
564   set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
565 else(UNIX)
566   if(NOT DEFINED CMAKE_INSTALL_RPATH)
567     set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}")
568     if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
569       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,origin")
570       set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,origin")
571     endif()
572   endif(NOT DEFINED CMAKE_INSTALL_RPATH)
573 endif()
574
575 if(APPLE AND DARWIN_LTO_LIBRARY)
576   set(CMAKE_EXE_LINKER_FLAGS
577     "${CMAKE_EXE_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
578   set(CMAKE_SHARED_LINKER_FLAGS
579     "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
580   set(CMAKE_MODULE_LINKER_FLAGS
581     "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
582 endif()
583
584 # Work around a broken bfd ld behavior. When linking a binary with a
585 # foo.so library, it will try to find any library that foo.so uses and
586 # check its symbols. This is wasteful (the check was done when foo.so
587 # was created) and can fail since it is not the dynamic linker and
588 # doesn't know how to handle search paths correctly.
589 if (UNIX AND NOT APPLE AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
590   set(CMAKE_EXE_LINKER_FLAGS
591       "${CMAKE_EXE_LINKER_FLAGS} -Wl,-allow-shlib-undefined")
592 endif()
593
594 set(CMAKE_INCLUDE_CURRENT_DIR ON)
595
596 include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
597
598 # when crosscompiling import the executable targets from a file
599 if(LLVM_USE_HOST_TOOLS)
600   include(CrossCompile)
601 endif(LLVM_USE_HOST_TOOLS)
602 if(LLVM_TARGET_IS_CROSSCOMPILE_HOST)
603 # Dummy use to avoid CMake Wraning: Manually-specified variables were not used
604 # (this is a variable that CrossCompile sets on recursive invocations)
605 endif()
606
607 if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
608   # On FreeBSD, /usr/local/* is not used by default. In order to build LLVM
609   # with libxml2, iconv.h, etc., we must add /usr/local paths.
610   include_directories("/usr/local/include")
611   link_directories("/usr/local/lib")
612 endif(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
613
614 if( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
615    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -include llvm/Support/Solaris.h")
616 endif( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
617
618 # Make sure we don't get -rdynamic in every binary. For those that need it,
619 # use export_executable_symbols(target).
620 set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
621
622 set(LLVM_PROFDATA_FILE "" CACHE FILEPATH
623   "Profiling data file to use when compiling in order to improve runtime performance.")
624
625 if(LLVM_PROFDATA_FILE AND EXISTS ${LLVM_PROFDATA_FILE})
626   if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
627     add_definitions("-fprofile-instr-use=${LLVM_PROFDATA_FILE}")
628   else()
629     message(FATAL_ERROR "LLVM_PROFDATA_FILE can only be specified when compiling with clang")
630   endif()
631 endif()
632
633 include(AddLLVM)
634 include(TableGen)
635
636 if( MINGW )
637   # People report that -O3 is unreliable on MinGW. The traditional
638   # build also uses -O2 for that reason:
639   llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
640 endif()
641
642 # Put this before tblgen. Else we have a circular dependence.
643 add_subdirectory(lib/Support)
644 add_subdirectory(lib/TableGen)
645
646 add_subdirectory(utils/TableGen)
647
648 add_subdirectory(include/llvm)
649
650 add_subdirectory(lib)
651
652 if( LLVM_INCLUDE_UTILS )
653   add_subdirectory(utils/FileCheck)
654   add_subdirectory(utils/PerfectShuffle)
655   add_subdirectory(utils/count)
656   add_subdirectory(utils/not)
657   add_subdirectory(utils/llvm-lit)
658   add_subdirectory(utils/yaml-bench)
659 else()
660   if ( LLVM_INCLUDE_TESTS )
661     message(FATAL_ERROR "Including tests when not building utils will not work.
662     Either set LLVM_INCLUDE_UTILS to On, or set LLVM_INCLDE_TESTS to Off.")
663   endif()
664 endif()
665
666 if(LLVM_INCLUDE_TESTS)
667   add_subdirectory(utils/unittest)
668 endif()
669
670 foreach( binding ${LLVM_BINDINGS_LIST} )
671   if( EXISTS "${LLVM_MAIN_SRC_DIR}/bindings/${binding}/CMakeLists.txt" )
672     add_subdirectory(bindings/${binding})
673   endif()
674 endforeach()
675
676 add_subdirectory(projects)
677
678 if(WITH_POLLY)
679   if(NOT EXISTS ${LLVM_MAIN_SRC_DIR}/tools/polly/CMakeLists.txt)
680     set(WITH_POLLY OFF)
681   endif()
682 endif(WITH_POLLY)
683
684 if( LLVM_INCLUDE_TOOLS )
685   add_subdirectory(tools)
686 endif()
687
688 if( LLVM_INCLUDE_EXAMPLES )
689   add_subdirectory(examples)
690 endif()
691
692 if( LLVM_INCLUDE_TESTS )
693   if(EXISTS ${LLVM_MAIN_SRC_DIR}/projects/test-suite AND TARGET clang)
694     include(LLVMExternalProjectUtils)
695     llvm_ExternalProject_Add(test-suite ${LLVM_MAIN_SRC_DIR}/projects/test-suite
696       USE_TOOLCHAIN
697       EXCLUDE_FROM_ALL
698       NO_INSTALL)
699   endif()
700   add_subdirectory(test)
701   add_subdirectory(unittests)
702   if (MSVC)
703     # This utility is used to prevent crashing tests from calling Dr. Watson on
704     # Windows.
705     add_subdirectory(utils/KillTheDoctor)
706   endif()
707
708   # Add a global check rule now that all subdirectories have been traversed
709   # and we know the total set of lit testsuites.
710   get_property(LLVM_LIT_TESTSUITES GLOBAL PROPERTY LLVM_LIT_TESTSUITES)
711   get_property(LLVM_LIT_PARAMS GLOBAL PROPERTY LLVM_LIT_PARAMS)
712   get_property(LLVM_LIT_DEPENDS GLOBAL PROPERTY LLVM_LIT_DEPENDS)
713   get_property(LLVM_LIT_EXTRA_ARGS GLOBAL PROPERTY LLVM_LIT_EXTRA_ARGS)
714   add_lit_target(check-all
715     "Running all regression tests"
716     ${LLVM_LIT_TESTSUITES}
717     PARAMS ${LLVM_LIT_PARAMS}
718     DEPENDS ${LLVM_LIT_DEPENDS}
719     ARGS ${LLVM_LIT_EXTRA_ARGS}
720     )
721 endif()
722
723 if (LLVM_INCLUDE_DOCS)
724   add_subdirectory(docs)
725 endif()
726
727 add_subdirectory(cmake/modules)
728
729 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
730   install(DIRECTORY include/llvm include/llvm-c
731     DESTINATION include
732     COMPONENT llvm-headers
733     FILES_MATCHING
734     PATTERN "*.def"
735     PATTERN "*.h"
736     PATTERN "*.td"
737     PATTERN "*.inc"
738     PATTERN "LICENSE.TXT"
739     PATTERN ".svn" EXCLUDE
740     )
741
742   install(DIRECTORY ${LLVM_INCLUDE_DIR}/llvm
743     DESTINATION include
744     COMPONENT llvm-headers
745     FILES_MATCHING
746     PATTERN "*.def"
747     PATTERN "*.h"
748     PATTERN "*.gen"
749     PATTERN "*.inc"
750     # Exclude include/llvm/CMakeFiles/intrinsics_gen.dir, matched by "*.def"
751     PATTERN "CMakeFiles" EXCLUDE
752     PATTERN "config.h" EXCLUDE
753     PATTERN ".svn" EXCLUDE
754     )
755
756   if (NOT CMAKE_CONFIGURATION_TYPES)
757     add_custom_target(installhdrs
758                       DEPENDS ${name}
759                       COMMAND "${CMAKE_COMMAND}"
760                               -DCMAKE_INSTALL_COMPONENT=llvm-headers
761                               -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
762   endif()
763 endif()