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