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