xref: /llvm-project/llvm/CMakeLists.txt (revision b593110d89aea76b8b10152b24ece154bff3e4b5)
1# See docs/CMake.html for instructions about how to build LLVM with CMake.
2
3cmake_minimum_required(VERSION 3.20.0)
4
5include(CMakeDependentOption)
6
7set(LLVM_COMMON_CMAKE_UTILS ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
8include(${LLVM_COMMON_CMAKE_UTILS}/Modules/CMakePolicy.cmake
9  NO_POLICY_SCOPE)
10
11# Builds with custom install names and installation rpath setups may not work
12# in the build tree. Allow these cases to use CMake's default build tree
13# behavior by setting `LLVM_NO_INSTALL_NAME_DIR_FOR_BUILD_TREE` to do this.
14option(LLVM_NO_INSTALL_NAME_DIR_FOR_BUILD_TREE "If set, use CMake's default build tree install name directory logic (Darwin only)" OFF)
15mark_as_advanced(LLVM_NO_INSTALL_NAME_DIR_FOR_BUILD_TREE)
16if(NOT LLVM_NO_INSTALL_NAME_DIR_FOR_BUILD_TREE)
17  set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
18endif()
19
20include(${LLVM_COMMON_CMAKE_UTILS}/Modules/LLVMVersion.cmake)
21
22set_directory_properties(PROPERTIES LLVM_VERSION_MAJOR "${LLVM_VERSION_MAJOR}")
23
24if (NOT PACKAGE_VERSION)
25  set(PACKAGE_VERSION
26    "${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}${LLVM_VERSION_SUFFIX}")
27endif()
28
29if(NOT DEFINED LLVM_SHLIB_SYMBOL_VERSION)
30  # "Symbol version prefix for libLLVM.so and libclang-cpp.so"
31  set(LLVM_SHLIB_SYMBOL_VERSION "LLVM_${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}")
32endif()
33
34if ((CMAKE_GENERATOR MATCHES "Visual Studio") AND (MSVC_TOOLSET_VERSION LESS 142) AND (CMAKE_GENERATOR_TOOLSET STREQUAL ""))
35  message(WARNING "Visual Studio generators use the x86 host compiler by "
36                  "default, even for 64-bit targets. This can result in linker "
37                  "instability and out of memory errors. To use the 64-bit "
38                  "host compiler, pass -Thost=x64 on the CMake command line.")
39endif()
40
41if (CMAKE_GENERATOR STREQUAL "Xcode" AND NOT CMAKE_OSX_ARCHITECTURES)
42  # Some CMake features like object libraries get confused if you don't
43  # explicitly specify an architecture setting with the Xcode generator.
44  set(CMAKE_OSX_ARCHITECTURES "x86_64")
45endif()
46
47project(LLVM
48  VERSION ${LLVM_VERSION_MAJOR}.${LLVM_VERSION_MINOR}.${LLVM_VERSION_PATCH}
49  LANGUAGES C CXX ASM)
50
51if (NOT DEFINED CMAKE_INSTALL_LIBDIR AND DEFINED LLVM_LIBDIR_SUFFIX)
52  # Must go before `include(GNUInstallDirs)`.
53  set(CMAKE_INSTALL_LIBDIR "lib${LLVM_LIBDIR_SUFFIX}")
54endif()
55
56# Must go after `DEFINED LLVM_LIBDIR_SUFFIX` check.
57set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
58
59# Must go after `project(..)`.
60include(GNUInstallDirs)
61
62# This C++ standard is required to build LLVM.
63set(LLVM_REQUIRED_CXX_STANDARD 17)
64
65# If we find that the cache contains CMAKE_CXX_STANDARD it means that it's a old CMakeCache.txt
66# and we can just inform the user and then reset it.
67if($CACHE{CMAKE_CXX_STANDARD} AND $CACHE{CMAKE_CXX_STANDARD} LESS ${LLVM_REQUIRED_CXX_STANDARD})
68  message(WARNING "Resetting cache value for CMAKE_CXX_STANDARD to ${LLVM_REQUIRED_CXX_STANDARD}")
69  unset(CMAKE_CXX_STANDARD CACHE)
70endif()
71
72# if CMAKE_CXX_STANDARD is still set after the cache unset above it means that the user requested it
73# and we allow it to be set to something newer than the required standard but otherwise we fail.
74if(DEFINED CMAKE_CXX_STANDARD AND CMAKE_CXX_STANDARD LESS ${LLVM_REQUIRED_CXX_STANDARD})
75  message(FATAL_ERROR "Requested CMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD} which is less than the required ${LLVM_REQUIRED_CXX_STANDARD}.")
76endif()
77
78set(CMAKE_CXX_STANDARD ${LLVM_REQUIRED_CXX_STANDARD} CACHE STRING "C++ standard to conform to")
79set(CMAKE_CXX_STANDARD_REQUIRED YES)
80
81if (CYGWIN)
82  # Cygwin is a bit stricter and lack things like 'strdup', 'stricmp', etc in
83  # c++xx mode.
84  set(CMAKE_CXX_EXTENSIONS YES)
85else()
86  set(CMAKE_CXX_EXTENSIONS NO)
87endif()
88
89if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
90  message(FATAL_ERROR "
91No build type selected. You need to pass -DCMAKE_BUILD_TYPE=<type> in order to configure LLVM.
92Available options are:
93  * -DCMAKE_BUILD_TYPE=Release - For an optimized build with no assertions or debug info.
94  * -DCMAKE_BUILD_TYPE=Debug - For an unoptimized build with assertions and debug info.
95  * -DCMAKE_BUILD_TYPE=RelWithDebInfo - For an optimized build with no assertions but with debug info.
96  * -DCMAKE_BUILD_TYPE=MinSizeRel - For a build optimized for size instead of speed.
97Learn more about these options in our documentation at https://llvm.org/docs/CMake.html#cmake-build-type
98")
99endif()
100
101# Set default build type for cmake's try_compile module.
102# CMake 3.17 or newer sets CMAKE_DEFAULT_BUILD_TYPE to one of the
103# items from CMAKE_CONFIGURATION_TYPES. Logic below can be further
104# simplified once LLVM's minimum CMake version is updated to 3.17.
105if(CMAKE_DEFAULT_BUILD_TYPE)
106  set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_DEFAULT_BUILD_TYPE})
107else()
108  if(CMAKE_CONFIGURATION_TYPES)
109    list(GET CMAKE_CONFIGURATION_TYPES 0 CMAKE_TRY_COMPILE_CONFIGURATION)
110  elseif(CMAKE_BUILD_TYPE)
111    set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
112  endif()
113endif()
114
115# Side-by-side subprojects layout: automatically set the
116# LLVM_EXTERNAL_${project}_SOURCE_DIR using LLVM_ALL_PROJECTS
117# This allows an easy way of setting up a build directory for llvm and another
118# one for llvm+clang+... using the same sources.
119set(LLVM_ALL_PROJECTS "bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl")
120if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
121  # Disallow 'openmp' as a LLVM PROJECT on AIX as the supported way is to use
122  # LLVM_ENABLE_RUNTIMES.
123  list(REMOVE_ITEM LLVM_ALL_PROJECTS openmp)
124endif()
125
126# The flang project is not yet part of "all" projects (see C++ requirements)
127set(LLVM_EXTRA_PROJECTS "flang")
128# List of all known projects in the mono repo
129set(LLVM_KNOWN_PROJECTS "${LLVM_ALL_PROJECTS};${LLVM_EXTRA_PROJECTS}")
130set(LLVM_ENABLE_PROJECTS "" CACHE STRING
131    "Semicolon-separated list of projects to build (${LLVM_KNOWN_PROJECTS}), or \"all\".")
132# Make sure expansion happens first to not handle "all" in rest of the checks.
133if( LLVM_ENABLE_PROJECTS STREQUAL "all" )
134  set( LLVM_ENABLE_PROJECTS ${LLVM_ALL_PROJECTS})
135endif()
136foreach(proj ${LLVM_ENABLE_PROJECTS})
137  if (NOT proj STREQUAL "llvm" AND NOT "${proj}" IN_LIST LLVM_KNOWN_PROJECTS)
138     MESSAGE(FATAL_ERROR "${proj} isn't a known project: ${LLVM_KNOWN_PROJECTS}. Did you mean to enable it as a runtime in LLVM_ENABLE_RUNTIMES?")
139  endif()
140endforeach()
141
142if ("flang" IN_LIST LLVM_ENABLE_PROJECTS)
143  if (NOT "mlir" IN_LIST LLVM_ENABLE_PROJECTS)
144    message(STATUS "Enabling MLIR as a dependency to flang")
145    list(APPEND LLVM_ENABLE_PROJECTS "mlir")
146  endif()
147
148  if (NOT "clang" IN_LIST LLVM_ENABLE_PROJECTS)
149    message(FATAL_ERROR "Clang is not enabled, but is required for the Flang driver")
150  endif()
151endif()
152
153if ("libc" IN_LIST LLVM_ENABLE_PROJECTS)
154  message(WARNING "Using LLVM_ENABLE_PROJECTS=libc is deprecated now, and will "
155    "become a fatal error in the LLVM 21 release.  Please use "
156    "-DLLVM_ENABLE_RUNTIMES=libc or see the instructions at "
157    "https://libc.llvm.org/ for building the runtimes.")
158endif()
159
160if ("compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS)
161  message(WARNING "Using LLVM_ENABLE_PROJECTS=compiler-rt is deprecated now, and will "
162    "become a fatal error in the LLVM 21 release.  Please use "
163    "-DLLVM_ENABLE_RUNTIMES=compiler-rt or see the instructions at "
164    "https://compiler-rt.llvm.org/ for building the runtimes.")
165endif()
166
167# Select the runtimes to build
168#
169# As we migrate runtimes to using the bootstrapping build, the set of default runtimes
170# should grow as we remove those runtimes from LLVM_ENABLE_PROJECTS above.
171set(LLVM_DEFAULT_RUNTIMES "libcxx;libcxxabi;libunwind")
172set(LLVM_SUPPORTED_RUNTIMES "libc;libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp;llvm-libgcc;offload")
173set(LLVM_ENABLE_RUNTIMES "" CACHE STRING
174  "Semicolon-separated list of runtimes to build, or \"all\" (${LLVM_DEFAULT_RUNTIMES}). Supported runtimes are ${LLVM_SUPPORTED_RUNTIMES}.")
175if(LLVM_ENABLE_RUNTIMES STREQUAL "all")
176  set(LLVM_ENABLE_RUNTIMES ${LLVM_DEFAULT_RUNTIMES})
177endif()
178foreach(proj IN LISTS LLVM_ENABLE_RUNTIMES)
179  if (NOT "${proj}" IN_LIST LLVM_SUPPORTED_RUNTIMES)
180    message(FATAL_ERROR "Runtime \"${proj}\" is not a supported runtime. Supported runtimes are: ${LLVM_SUPPORTED_RUNTIMES}")
181  endif()
182endforeach()
183
184# Set a shorthand option to enable the GPU build of the 'libc' project.
185option(LIBC_GPU_BUILD "Enable the 'libc' project targeting the GPU" OFF)
186if(LIBC_GPU_BUILD)
187  if(LLVM_RUNTIME_TARGETS)
188    list(APPEND LLVM_RUNTIME_TARGETS "nvptx64-nvidia-cuda" "amdgcn-amd-amdhsa")
189  else()
190    set(LLVM_RUNTIME_TARGETS "default;nvptx64-nvidia-cuda;amdgcn-amd-amdhsa")
191  endif()
192  list(APPEND RUNTIMES_nvptx64-nvidia-cuda_LLVM_ENABLE_RUNTIMES "libc")
193  list(APPEND RUNTIMES_amdgcn-amd-amdhsa_LLVM_ENABLE_RUNTIMES "libc")
194endif()
195
196foreach(_name ${LLVM_RUNTIME_TARGETS})
197  if("libc" IN_LIST RUNTIMES_${_name}_LLVM_ENABLE_RUNTIMES)
198    if("${_name}" STREQUAL "amdgcn-amd-amdhsa" OR "${_name}" STREQUAL "nvptx64-nvidia-cuda")
199      set(LLVM_LIBC_GPU_BUILD ON)
200    endif()
201  endif()
202endforeach()
203if("${LIBC_TARGET_TRIPLE}" STREQUAL "amdgcn-amd-amdhsa" OR
204   "${LIBC_TARGET_TRIPLE}" STREQUAL "nvptx64-nvidia-cuda")
205  set(LLVM_LIBC_GPU_BUILD ON)
206endif()
207if (NOT "libc" IN_LIST LLVM_ENABLE_PROJECTS AND LLVM_LIBC_GPU_BUILD)
208  message(STATUS "Enabling libc project to build libc testing tools")
209  list(APPEND LLVM_ENABLE_PROJECTS "libc")
210endif()
211
212# LLVM_ENABLE_PROJECTS_USED is `ON` if the user has ever used the
213# `LLVM_ENABLE_PROJECTS` CMake cache variable.  This exists for
214# several reasons:
215#
216# * As an indicator that the `LLVM_ENABLE_PROJECTS` list is now the single
217# source of truth for which projects to build. This means we will ignore user
218# supplied `LLVM_TOOL_<project>_BUILD` CMake cache variables and overwrite
219# them.
220#
221# * The case where the user previously had `LLVM_ENABLE_PROJECTS` set to a
222# non-empty list but now the user wishes to disable building all other projects
223# by setting `LLVM_ENABLE_PROJECTS` to an empty string. In that case we still
224# need to set the `LLVM_TOOL_${upper_proj}_BUILD` variables so that we disable
225# building all the projects that were previously enabled.
226set(LLVM_ENABLE_PROJECTS_USED OFF CACHE BOOL "")
227mark_as_advanced(LLVM_ENABLE_PROJECTS_USED)
228
229if (LLVM_ENABLE_PROJECTS_USED OR NOT LLVM_ENABLE_PROJECTS STREQUAL "")
230  set(LLVM_ENABLE_PROJECTS_USED ON CACHE BOOL "" FORCE)
231  foreach(proj ${LLVM_KNOWN_PROJECTS} ${LLVM_EXTERNAL_PROJECTS})
232    string(TOUPPER "${proj}" upper_proj)
233    string(REGEX REPLACE "-" "_" upper_proj ${upper_proj})
234    if ("${proj}" IN_LIST LLVM_ENABLE_PROJECTS)
235      message(STATUS "${proj} project is enabled")
236      set(SHOULD_ENABLE_PROJECT TRUE)
237      set(PROJ_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}")
238      if(NOT EXISTS "${PROJ_DIR}" OR NOT IS_DIRECTORY "${PROJ_DIR}")
239        message(FATAL_ERROR "LLVM_ENABLE_PROJECTS requests ${proj} but directory not found: ${PROJ_DIR}")
240      endif()
241      if( LLVM_EXTERNAL_${upper_proj}_SOURCE_DIR STREQUAL "" )
242        set(LLVM_EXTERNAL_${upper_proj}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}" CACHE PATH "" FORCE)
243      else()
244        set(LLVM_EXTERNAL_${upper_proj}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../${proj}" CACHE PATH "")
245      endif()
246    elseif ("${proj}" IN_LIST LLVM_EXTERNAL_PROJECTS)
247      message(STATUS "${proj} project is enabled")
248      set(SHOULD_ENABLE_PROJECT TRUE)
249    else()
250      message(STATUS "${proj} project is disabled")
251      set(SHOULD_ENABLE_PROJECT FALSE)
252    endif()
253    # Force `LLVM_TOOL_${upper_proj}_BUILD` variables to have values that
254    # corresponds with `LLVM_ENABLE_PROJECTS`. This prevents the user setting
255    # `LLVM_TOOL_${upper_proj}_BUILD` variables externally. At some point
256    # we should deprecate allowing users to set these variables by turning them
257    # into normal CMake variables rather than cache variables.
258    set(LLVM_TOOL_${upper_proj}_BUILD
259      ${SHOULD_ENABLE_PROJECT}
260      CACHE
261      BOOL "Whether to build ${upper_proj} as part of LLVM" FORCE
262    )
263  endforeach()
264endif()
265unset(SHOULD_ENABLE_PROJECT)
266
267# Build llvm with ccache if the package is present
268set(LLVM_CCACHE_BUILD OFF CACHE BOOL "Set to ON for a ccache enabled build")
269if(LLVM_CCACHE_BUILD)
270  find_program(CCACHE_PROGRAM ccache)
271  if(CCACHE_PROGRAM)
272    set(LLVM_CCACHE_MAXSIZE "" CACHE STRING "Size of ccache")
273    set(LLVM_CCACHE_DIR "" CACHE STRING "Directory to keep ccached data")
274    set(LLVM_CCACHE_PARAMS "CCACHE_CPP2=yes CCACHE_HASHDIR=yes"
275        CACHE STRING "Parameters to pass through to ccache")
276
277    if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
278      set(CCACHE_PROGRAM "${LLVM_CCACHE_PARAMS} ${CCACHE_PROGRAM}")
279      if (LLVM_CCACHE_MAXSIZE)
280        set(CCACHE_PROGRAM "CCACHE_MAXSIZE=${LLVM_CCACHE_MAXSIZE} ${CCACHE_PROGRAM}")
281      endif()
282      if (LLVM_CCACHE_DIR)
283        set(CCACHE_PROGRAM "CCACHE_DIR=${LLVM_CCACHE_DIR} ${CCACHE_PROGRAM}")
284      endif()
285      set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
286    else()
287      if(LLVM_CCACHE_MAXSIZE OR LLVM_CCACHE_DIR OR
288         NOT LLVM_CCACHE_PARAMS MATCHES "CCACHE_CPP2=yes CCACHE_HASHDIR=yes")
289        message(FATAL_ERROR "Ccache configuration through CMake is not supported on Windows. Please use environment variables.")
290      endif()
291      # RULE_LAUNCH_COMPILE should work with Ninja but currently has issues
292      # with cmd.exe and some MSVC tools other than cl.exe
293      set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
294      set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
295    endif()
296  else()
297    message(FATAL_ERROR "Unable to find the program ccache. Set LLVM_CCACHE_BUILD to OFF")
298  endif()
299endif()
300
301set(LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS "" CACHE STRING
302  "Optional arguments for the native tool used in CMake --build invocations for external projects.")
303mark_as_advanced(LLVM_EXTERNAL_PROJECT_BUILD_TOOL_ARGS)
304
305option(LLVM_DEPENDENCY_DEBUGGING "Dependency debugging mode to verify correctly expressed library dependencies (Darwin only)" OFF)
306
307# Some features of the LLVM build may be disallowed when dependency debugging is
308# enabled. In particular you cannot use ccache because we want to force compile
309# operations to always happen.
310if(LLVM_DEPENDENCY_DEBUGGING)
311  if(NOT CMAKE_HOST_APPLE)
312    message(FATAL_ERROR "Dependency debugging is only currently supported on Darwin hosts.")
313  endif()
314  if(LLVM_CCACHE_BUILD)
315    message(FATAL_ERROR "Cannot enable dependency debugging while using ccache.")
316  endif()
317endif()
318
319option(LLVM_ENABLE_DAGISEL_COV "Debug: Prints tablegen patterns that were used for selecting" OFF)
320option(LLVM_ENABLE_GISEL_COV "Enable collection of GlobalISel rule coverage" OFF)
321if(LLVM_ENABLE_GISEL_COV)
322  set(LLVM_GISEL_COV_PREFIX "${CMAKE_BINARY_DIR}/gisel-coverage-" CACHE STRING "Provide a filename prefix to collect the GlobalISel rule coverage")
323endif()
324
325# Add path for custom modules
326list(INSERT CMAKE_MODULE_PATH 0
327  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
328  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
329  "${LLVM_COMMON_CMAKE_UTILS}/Modules"
330  )
331
332# Generate a CompilationDatabase (compile_commands.json file) for our build,
333# for use by clang_complete, YouCompleteMe, etc.
334set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
335
336option(LLVM_INSTALL_BINUTILS_SYMLINKS
337  "Install symlinks from the binutils tool names to the corresponding LLVM tools." OFF)
338
339option(LLVM_INSTALL_CCTOOLS_SYMLINKS
340  "Install symlinks from the cctools tool names to the corresponding LLVM tools." OFF)
341
342# By default we use symlinks on Unix platforms and copy binaries on Windows
343# If you have the correct setup on Windows you can use this option to enable
344# symlinks and save a lot of diskspace.
345option(LLVM_USE_SYMLINKS "Use symlinks instead of copying binaries" ${CMAKE_HOST_UNIX})
346
347option(LLVM_INSTALL_UTILS "Include utility binaries in the 'install' target." OFF)
348
349option(LLVM_INSTALL_TOOLCHAIN_ONLY "Only include toolchain files in the 'install' target." OFF)
350
351# Unfortunatly Clang is too eager to search directories for module maps, which can cause the
352# installed version of the maps to be found when building LLVM from source. Therefore we turn off
353# the installation by default. See llvm.org/PR31905.
354option(LLVM_INSTALL_MODULEMAPS "Install the modulemap files in the 'install' target." OFF)
355
356option(LLVM_USE_FOLDERS "Enable solution folders in Visual Studio. Disable for Express versions." ON)
357if ( LLVM_USE_FOLDERS )
358  set_property(GLOBAL PROPERTY USE_FOLDERS ON)
359endif()
360
361include(VersionFromVCS)
362
363option(LLVM_APPEND_VC_REV
364  "Embed the version control system revision in LLVM" ON)
365
366set(LLVM_FORCE_VC_REVISION
367  "" CACHE STRING "Force custom VC revision for LLVM_APPEND_VC_REV")
368
369set(LLVM_FORCE_VC_REPOSITORY
370  "" CACHE STRING "Force custom VC repository for LLVM_APPEND_VC_REV")
371
372option(LLVM_TOOL_LLVM_DRIVER_BUILD "Enables building the llvm multicall tool" OFF)
373
374set(PACKAGE_NAME LLVM)
375set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
376set(PACKAGE_BUGREPORT "https://github.com/llvm/llvm-project/issues/")
377
378set(BUG_REPORT_URL "${PACKAGE_BUGREPORT}" CACHE STRING
379  "Default URL where bug reports are to be submitted.")
380set(LLDB_BUG_REPORT_URL "${BUG_REPORT_URL}" CACHE STRING
381  "Default URL where lldb bug reports are to be submitted.")
382
383# Configure CPack.
384if(NOT DEFINED CPACK_PACKAGE_INSTALL_DIRECTORY)
385  set(CPACK_PACKAGE_INSTALL_DIRECTORY "LLVM")
386endif()
387if(NOT DEFINED CPACK_PACKAGE_VENDOR)
388  set(CPACK_PACKAGE_VENDOR "LLVM")
389endif()
390set(CPACK_PACKAGE_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
391set(CPACK_PACKAGE_VERSION_MINOR ${LLVM_VERSION_MINOR})
392set(CPACK_PACKAGE_VERSION_PATCH ${LLVM_VERSION_PATCH})
393set(CPACK_PACKAGE_VERSION ${PACKAGE_VERSION})
394set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.TXT")
395if(WIN32 AND NOT UNIX)
396  set(CPACK_NSIS_COMPRESSOR "/SOLID lzma \r\n SetCompressorDictSize 32")
397  if(NOT DEFINED CPACK_PACKAGE_INSTALL_REGISTRY_KEY)
398    set(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "LLVM")
399  endif()
400  set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_logo.bmp")
401  set(CPACK_NSIS_MUI_ICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_icon.ico")
402  set(CPACK_NSIS_MUI_UNIICON "${CMAKE_CURRENT_SOURCE_DIR}\\\\cmake\\\\nsis_icon.ico")
403  set(CPACK_NSIS_MODIFY_PATH "ON")
404  set(CPACK_NSIS_ENABLE_UNINSTALL_BEFORE_INSTALL "ON")
405  if( CMAKE_CL_64 )
406    if(NOT DEFINED CPACK_NSIS_INSTALL_ROOT)
407      set(CPACK_NSIS_INSTALL_ROOT "$PROGRAMFILES64")
408    endif()
409  endif()
410endif()
411include(CPack)
412
413# Sanity check our source directory to make sure that we are not trying to
414# generate an in-source build (unless on MSVC_IDE, where it is ok), and to make
415# sure that we don't have any stray generated files lying around in the tree
416# (which would end up getting picked up by header search, instead of the correct
417# versions).
418if( CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR AND NOT MSVC_IDE )
419  message(FATAL_ERROR "In-source builds are not allowed.
420Please create a directory and run cmake from there, passing the path
421to this source directory as the last argument.
422This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
423Please delete them.")
424endif()
425
426string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
427
428option(LLVM_ADDITIONAL_BUILD_TYPES "Additional build types that are allowed to be passed into CMAKE_BUILD_TYPE" "")
429
430set(ALLOWED_BUILD_TYPES DEBUG RELEASE RELWITHDEBINFO MINSIZEREL ${LLVM_ADDITIONAL_BUILD_TYPES})
431string (REPLACE ";" "|" ALLOWED_BUILD_TYPES_STRING "${ALLOWED_BUILD_TYPES}")
432string (TOUPPER "${ALLOWED_BUILD_TYPES_STRING}" uppercase_ALLOWED_BUILD_TYPES)
433
434if (CMAKE_BUILD_TYPE AND
435    NOT uppercase_CMAKE_BUILD_TYPE MATCHES "^(${uppercase_ALLOWED_BUILD_TYPES})$")
436  message(FATAL_ERROR "Unknown value for CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
437endif()
438
439# LLVM_INSTALL_PACKAGE_DIR needs to be declared prior to adding the tools
440# subdirectory in order to have the value available for llvm-config.
441include(GNUInstallPackageDir)
442set(LLVM_INSTALL_PACKAGE_DIR "${CMAKE_INSTALL_PACKAGEDIR}/llvm" CACHE STRING
443  "Path for CMake subdirectory for LLVM (defaults to '${CMAKE_INSTALL_PACKAGEDIR}/llvm')")
444
445set(LLVM_TOOLS_INSTALL_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
446    "Path for binary subdirectory (defaults to '${CMAKE_INSTALL_BINDIR}')")
447mark_as_advanced(LLVM_TOOLS_INSTALL_DIR)
448
449set(LLVM_UTILS_INSTALL_DIR "${LLVM_TOOLS_INSTALL_DIR}" CACHE STRING
450    "Path to install LLVM utilities (enabled by LLVM_INSTALL_UTILS=ON) (defaults to LLVM_TOOLS_INSTALL_DIR)")
451mark_as_advanced(LLVM_UTILS_INSTALL_DIR)
452
453set(LLVM_EXAMPLES_INSTALL_DIR "examples" CACHE STRING
454    "Path for examples subdirectory (enabled by LLVM_BUILD_EXAMPLES=ON) (defaults to 'examples')")
455mark_as_advanced(LLVM_EXAMPLES_INSTALL_DIR)
456
457# They are used as destination of target generators.
458set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/bin)
459set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
460if(WIN32 OR CYGWIN)
461  # DLL platform -- put DLLs into bin.
462  set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
463else()
464  set(LLVM_SHLIB_OUTPUT_INTDIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
465endif()
466
467# Each of them corresponds to llvm-config's.
468set(LLVM_TOOLS_BINARY_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR}) # --bindir
469set(LLVM_LIBRARY_DIR      ${LLVM_LIBRARY_OUTPUT_INTDIR}) # --libdir
470set(LLVM_MAIN_SRC_DIR     ${CMAKE_CURRENT_SOURCE_DIR}  ) # --src-root
471set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include ) # --includedir
472set(LLVM_BINARY_DIR       ${CMAKE_CURRENT_BINARY_DIR}  ) # --prefix
473
474
475# Note: LLVM_CMAKE_DIR does not include generated files
476set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules)
477set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
478set(LLVM_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
479
480# List of all targets to be built by default:
481set(LLVM_ALL_TARGETS
482  AArch64
483  AMDGPU
484  ARM
485  AVR
486  BPF
487  Hexagon
488  Lanai
489  LoongArch
490  Mips
491  MSP430
492  NVPTX
493  PowerPC
494  RISCV
495  Sparc
496  SPIRV
497  SystemZ
498  VE
499  WebAssembly
500  X86
501  XCore
502  )
503
504set(LLVM_ALL_EXPERIMENTAL_TARGETS
505  ARC
506  CSKY
507  DirectX
508  M68k
509  Xtensa
510)
511
512# List of targets with JIT support:
513set(LLVM_TARGETS_WITH_JIT X86 PowerPC AArch64 ARM Mips SystemZ)
514
515set(LLVM_TARGETS_TO_BUILD "all"
516    CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
517
518set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ""
519    CACHE STRING "Semicolon-separated list of experimental targets to build, or \"all\".")
520
521option(BUILD_SHARED_LIBS
522  "Build all libraries as shared libraries instead of static" OFF)
523
524option(LLVM_ENABLE_BACKTRACES "Enable embedding backtraces on crash." ON)
525if(LLVM_ENABLE_BACKTRACES)
526  set(ENABLE_BACKTRACES 1)
527endif()
528
529option(LLVM_ENABLE_UNWIND_TABLES "Emit unwind tables for the libraries" ON)
530
531option(LLVM_ENABLE_CRASH_OVERRIDES "Enable crash overrides." ON)
532if(LLVM_ENABLE_CRASH_OVERRIDES)
533  set(ENABLE_CRASH_OVERRIDES 1)
534endif()
535
536option(LLVM_ENABLE_CRASH_DUMPS "Turn on memory dumps on crashes. Currently only implemented on Windows." OFF)
537
538set(LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING "DISABLED" CACHE STRING
539  "Enhance Debugify's line number coverage tracking; enabling this is ABI-breaking. Can be DISABLED, or COVERAGE.")
540set_property(CACHE LLVM_ENABLE_DEBUGLOC_COVERAGE_TRACKING PROPERTY STRINGS DISABLED COVERAGE)
541
542set(WINDOWS_PREFER_FORWARD_SLASH_DEFAULT OFF)
543if (MINGW)
544  # Cygwin doesn't identify itself as Windows, and thus gets path::Style::posix
545  # as native path style, regardless of what this is set to.
546  set(WINDOWS_PREFER_FORWARD_SLASH_DEFAULT ON)
547endif()
548option(LLVM_WINDOWS_PREFER_FORWARD_SLASH "Prefer path names with forward slashes on Windows." ${WINDOWS_PREFER_FORWARD_SLASH_DEFAULT})
549
550option(LLVM_ENABLE_FFI "Use libffi to call external functions from the interpreter" OFF)
551set(FFI_LIBRARY_DIR "" CACHE PATH "Additional directory, where CMake should search for libffi.so")
552set(FFI_INCLUDE_DIR "" CACHE PATH "Additional directory, where CMake should search for ffi.h or ffi/ffi.h")
553
554set(LLVM_TARGET_ARCH "host"
555  CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")
556
557set(LLVM_ENABLE_LIBXML2 "ON" CACHE STRING "Use libxml2 if available. Can be ON, OFF, or FORCE_ON")
558
559option(LLVM_ENABLE_LIBEDIT "Use libedit if available." ON)
560
561option(LLVM_ENABLE_LIBPFM "Use libpfm for performance counters if available." ON)
562
563# On z/OS, threads cannot be used because TLS is not supported.
564if (CMAKE_SYSTEM_NAME MATCHES "OS390")
565  option(LLVM_ENABLE_THREADS "Use threads if available." OFF)
566else()
567  option(LLVM_ENABLE_THREADS "Use threads if available." ON)
568endif()
569
570set(LLVM_ENABLE_ZLIB "ON" CACHE STRING "Use zlib for compression/decompression if available. Can be ON, OFF, or FORCE_ON")
571
572set(LLVM_ENABLE_ZSTD "ON" CACHE STRING "Use zstd for compression/decompression if available. Can be ON, OFF, or FORCE_ON")
573
574set(LLVM_USE_STATIC_ZSTD FALSE CACHE BOOL "Use static version of zstd. Can be TRUE, FALSE")
575
576set(LLVM_ENABLE_CURL "OFF" CACHE STRING "Use libcurl for the HTTP client if available. Can be ON, OFF, or FORCE_ON")
577
578set(LLVM_HAS_LOGF128 "OFF" CACHE STRING "Use logf128 to constant fold fp128 logarithm calls. Can be ON, OFF, or FORCE_ON")
579
580set(LLVM_ENABLE_HTTPLIB "OFF" CACHE STRING "Use cpp-httplib HTTP server library if available. Can be ON, OFF, or FORCE_ON")
581
582set(LLVM_Z3_INSTALL_DIR "" CACHE STRING "Install directory of the Z3 solver.")
583
584option(LLVM_ENABLE_Z3_SOLVER
585  "Enable Support for the Z3 constraint solver in LLVM."
586  ${LLVM_ENABLE_Z3_SOLVER_DEFAULT}
587)
588
589if (LLVM_ENABLE_Z3_SOLVER)
590  find_package(Z3 4.8.9)
591
592  if (LLVM_Z3_INSTALL_DIR)
593    if (NOT Z3_FOUND)
594      message(FATAL_ERROR "Z3 >= 4.8.9 has not been found in LLVM_Z3_INSTALL_DIR: ${LLVM_Z3_INSTALL_DIR}.")
595    endif()
596  endif()
597
598  if (NOT Z3_FOUND)
599    message(FATAL_ERROR "LLVM_ENABLE_Z3_SOLVER cannot be enabled when Z3 is not available.")
600  endif()
601
602  set(LLVM_WITH_Z3 1)
603endif()
604
605set(LLVM_ENABLE_Z3_SOLVER_DEFAULT "${Z3_FOUND}")
606
607
608if( LLVM_TARGETS_TO_BUILD STREQUAL "all" )
609  set( LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS} )
610endif()
611
612if(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD STREQUAL "all")
613  set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ${LLVM_ALL_EXPERIMENTAL_TARGETS})
614endif()
615
616set(LLVM_TARGETS_TO_BUILD
617   ${LLVM_TARGETS_TO_BUILD}
618   ${LLVM_EXPERIMENTAL_TARGETS_TO_BUILD})
619list(REMOVE_DUPLICATES LLVM_TARGETS_TO_BUILD)
620
621if (NOT CMAKE_SYSTEM_NAME MATCHES "OS390")
622  option(LLVM_ENABLE_PIC "Build Position-Independent Code" ON)
623endif()
624option(LLVM_ENABLE_MODULES "Compile with C++ modules enabled." OFF)
625if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
626  option(LLVM_ENABLE_MODULE_DEBUGGING "Compile with -gmodules." ON)
627else()
628  option(LLVM_ENABLE_MODULE_DEBUGGING "Compile with -gmodules." OFF)
629endif()
630option(LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY "Compile with -fmodules-local-submodule-visibility." ON)
631option(LLVM_ENABLE_LIBCXX "Use libc++ if available." OFF)
632option(LLVM_ENABLE_LLVM_LIBC "Set to on to link all LLVM executables against LLVM libc, assuming it is accessible by the host compiler." OFF)
633option(LLVM_STATIC_LINK_CXX_STDLIB "Statically link the standard library." OFF)
634option(LLVM_ENABLE_LLD "Use lld as C and C++ linker." OFF)
635option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
636option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
637
638option(LLVM_ENABLE_DUMP "Enable dump functions even when assertions are disabled" OFF)
639option(LLVM_UNREACHABLE_OPTIMIZE "Optimize llvm_unreachable() as undefined behavior (default), guaranteed trap when OFF" ON)
640
641if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
642  option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)
643else()
644  option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
645endif()
646
647option(LLVM_ENABLE_EXPENSIVE_CHECKS "Enable expensive checks" OFF)
648
649# While adding scalable vector support to LLVM, we temporarily want to
650# allow an implicit conversion of TypeSize to uint64_t, and to allow
651# code to get the fixed number of elements from a possibly scalable vector.
652# This CMake flag enables a more strict mode where it asserts that the type
653# is not a scalable vector type.
654#
655# Enabling this flag makes it easier to find cases where the compiler makes
656# assumptions on the size being 'fixed size', when building tests for
657# SVE/SVE2 or other scalable vector architectures.
658option(LLVM_ENABLE_STRICT_FIXED_SIZE_VECTORS
659       "Enable assertions that type is not scalable in implicit conversion from TypeSize to uint64_t and calls to getNumElements" OFF)
660
661set(LLVM_ABI_BREAKING_CHECKS "WITH_ASSERTS" CACHE STRING
662  "Enable abi-breaking checks.  Can be WITH_ASSERTS, FORCE_ON or FORCE_OFF.")
663
664option(LLVM_FORCE_USE_OLD_TOOLCHAIN
665       "Set to ON to force using an old, unsupported host toolchain." OFF)
666
667set(LLVM_LOCAL_RPATH "" CACHE FILEPATH
668  "If set, an absolute path added as rpath on binaries that do not already contain an executable-relative rpath.")
669
670option(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN
671       "Set to ON to only warn when using a toolchain which is about to be deprecated, instead of emitting an error." OFF)
672
673option(LLVM_USE_INTEL_JITEVENTS
674  "Use Intel JIT API to inform Intel(R) VTune(TM) Amplifier XE 2011 about JIT code"
675  OFF)
676
677if( LLVM_USE_INTEL_JITEVENTS )
678  # Verify we are on a supported platform
679  if( NOT CMAKE_SYSTEM_NAME MATCHES "Windows" AND NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
680    message(FATAL_ERROR
681      "Intel JIT API support is available on Linux and Windows only.")
682  endif()
683endif( LLVM_USE_INTEL_JITEVENTS )
684
685option(LLVM_USE_OPROFILE
686  "Use opagent JIT interface to inform OProfile about JIT code" OFF)
687
688option(LLVM_EXTERNALIZE_DEBUGINFO
689  "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
690
691option(LLVM_ENABLE_EXPORTED_SYMBOLS_IN_EXECUTABLES
692  "Preserve exported symbols in executables" ON)
693
694set(LLVM_CODESIGNING_IDENTITY "" CACHE STRING
695  "Sign executables and dylibs with the given identity or skip if empty (Darwin Only)")
696
697# If enabled, verify we are on a platform that supports oprofile.
698if( LLVM_USE_OPROFILE )
699  if( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
700    message(FATAL_ERROR "OProfile support is available on Linux only.")
701  endif( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
702endif( LLVM_USE_OPROFILE )
703
704option(LLVM_USE_PERF
705  "Use perf JIT interface to inform perf about JIT code" OFF)
706
707# If enabled, verify we are on a platform that supports perf.
708if( LLVM_USE_PERF )
709  if( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
710    message(FATAL_ERROR "perf support is available on Linux only.")
711  endif( NOT CMAKE_SYSTEM_NAME MATCHES "Linux" )
712endif( LLVM_USE_PERF )
713
714set(LLVM_USE_SANITIZER "" CACHE STRING
715  "Define the sanitizer used to build binaries and tests.")
716option(LLVM_OPTIMIZE_SANITIZED_BUILDS "Pass -O1 on debug sanitizer builds" ON)
717set(LLVM_UBSAN_FLAGS
718    "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all"
719    CACHE STRING
720    "Compile flags set to enable UBSan. Only used if LLVM_USE_SANITIZER contains 'Undefined'.")
721set(LLVM_LIB_FUZZING_ENGINE "" CACHE PATH
722  "Path to fuzzing library for linking with fuzz targets")
723
724option(LLVM_USE_SPLIT_DWARF
725  "Use -gsplit-dwarf when compiling llvm and --gdb-index when linking." OFF)
726
727# Define an option controlling whether we should build for 32-bit on 64-bit
728# platforms, where supported.
729if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT (WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX"))
730  # TODO: support other platforms and toolchains.
731  option(LLVM_BUILD_32_BITS "Build 32 bits executables and libraries." OFF)
732endif()
733
734# Define the default arguments to use with 'lit', and an option for the user to
735# override.
736set(LIT_ARGS_DEFAULT "-sv")
737if (MSVC_IDE OR XCODE)
738  set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
739endif()
740if(LLVM_INDIVIDUAL_TEST_COVERAGE)
741   set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --per-test-coverage")
742endif()
743set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
744
745# On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
746if( WIN32 AND NOT CYGWIN )
747  set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
748endif()
749set(LLVM_NATIVE_TOOL_DIR "" CACHE PATH "Path to a directory containing prebuilt matching native tools (such as llvm-tblgen)")
750
751set(LLVM_ENABLE_RPMALLOC "" CACHE BOOL "Replace the CRT allocator with rpmalloc.")
752if(LLVM_ENABLE_RPMALLOC)
753  if(NOT (CMAKE_SYSTEM_NAME MATCHES "Windows|Linux"))
754    message(FATAL_ERROR "LLVM_ENABLE_RPMALLOC is only supported on Windows and Linux.")
755  endif()
756  if(LLVM_USE_SANITIZER)
757    message(FATAL_ERROR "LLVM_ENABLE_RPMALLOC cannot be used along with LLVM_USE_SANITIZER!")
758  endif()
759  if(WIN32)
760    if(CMAKE_CONFIGURATION_TYPES)
761      foreach(BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
762        string(TOUPPER "${BUILD_MODE}" uppercase_BUILD_MODE)
763        if(uppercase_BUILD_MODE STREQUAL "DEBUG")
764          message(WARNING "The Debug target isn't supported along with LLVM_ENABLE_RPMALLOC!")
765        endif()
766      endforeach()
767    else()
768      if(CMAKE_BUILD_TYPE AND uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
769        message(FATAL_ERROR "The Debug target isn't supported along with LLVM_ENABLE_RPMALLOC!")
770      endif()
771    endif()
772  endif()
773
774  # Override the C runtime allocator with the in-tree rpmalloc
775  set(LLVM_INTEGRATED_CRT_ALLOC "${CMAKE_CURRENT_SOURCE_DIR}/lib/Support")
776  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
777endif()
778
779set(LLVM_INTEGRATED_CRT_ALLOC "${LLVM_INTEGRATED_CRT_ALLOC}" CACHE PATH "Replace the Windows CRT allocator with any of {rpmalloc|mimalloc|snmalloc}. Only works with CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded.")
780if(LLVM_INTEGRATED_CRT_ALLOC)
781  if(NOT WIN32)
782    message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC is only supported on Windows.")
783  endif()
784  if(LLVM_USE_SANITIZER)
785    message(FATAL_ERROR "LLVM_INTEGRATED_CRT_ALLOC cannot be used along with LLVM_USE_SANITIZER!")
786  endif()
787  if(CMAKE_BUILD_TYPE AND uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
788    message(FATAL_ERROR "The Debug target isn't supported along with LLVM_INTEGRATED_CRT_ALLOC!")
789  endif()
790endif()
791
792# Define options to control the inclusion and default build behavior for
793# components which may not strictly be necessary (tools, examples, and tests).
794#
795# This is primarily to support building smaller or faster project files.
796option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
797option(LLVM_BUILD_TOOLS
798  "Build the LLVM tools. If OFF, just generate build targets." ON)
799
800option(LLVM_INCLUDE_UTILS "Generate build targets for the LLVM utils." ON)
801option(LLVM_BUILD_UTILS
802  "Build LLVM utility binaries. If OFF, just generate build targets." ON)
803
804option(LLVM_INCLUDE_RUNTIMES "Generate build targets for the LLVM runtimes." ON)
805option(LLVM_BUILD_RUNTIMES
806  "Build the LLVM runtimes. If OFF, just generate build targets." ON)
807
808option(LLVM_BUILD_RUNTIME
809  "Build the LLVM runtime libraries." ON)
810option(LLVM_BUILD_EXAMPLES
811  "Build the LLVM example programs. If OFF, just generate build targets." OFF)
812option(LLVM_INCLUDE_EXAMPLES "Generate build targets for the LLVM examples" ON)
813
814option(LLVM_BUILD_TESTS
815  "Build LLVM unit tests. If OFF, just generate build targets." OFF)
816option(LLVM_INCLUDE_TESTS "Generate build targets for the LLVM unit tests." ON)
817
818option(LLVM_INSTALL_GTEST
819  "Install the llvm gtest library.  This should be on if you want to do
820   stand-alone builds of the other projects and run their unit tests." OFF)
821
822option(LLVM_BUILD_BENCHMARKS "Add LLVM benchmark targets to the list of default
823targets. If OFF, benchmarks still could be built using Benchmarks target." OFF)
824option(LLVM_INCLUDE_BENCHMARKS "Generate benchmark targets. If OFF, benchmarks can't be built." ON)
825
826option (LLVM_BUILD_DOCS "Build the llvm documentation." OFF)
827option (LLVM_INCLUDE_DOCS "Generate build targets for llvm documentation." ON)
828option (LLVM_ENABLE_DOXYGEN "Use doxygen to generate llvm API documentation." OFF)
829option (LLVM_ENABLE_SPHINX "Use Sphinx to generate llvm documentation." OFF)
830option (LLVM_ENABLE_OCAMLDOC "Build OCaml bindings documentation." ON)
831option (LLVM_ENABLE_BINDINGS "Build bindings." ON)
832
833set(LLVM_INSTALL_DOXYGEN_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/doxygen-html"
834    CACHE STRING "Doxygen-generated HTML documentation install directory")
835set(LLVM_INSTALL_OCAMLDOC_HTML_DIR "${CMAKE_INSTALL_DOCDIR}/llvm/ocaml-html"
836    CACHE STRING "OCamldoc-generated HTML documentation install directory")
837
838option (LLVM_BUILD_EXTERNAL_COMPILER_RT
839  "Build compiler-rt as an external project." OFF)
840
841option (LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
842  "Show target and host info when tools are invoked with --version." ON)
843
844option(LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
845  "Show the optional build config flags when tools are invoked with --version." ON)
846
847# You can configure which libraries from LLVM you want to include in the
848# shared library by setting LLVM_DYLIB_COMPONENTS to a semi-colon delimited
849# list of LLVM components. All component names handled by llvm-config are valid.
850if(NOT DEFINED LLVM_DYLIB_COMPONENTS)
851  set(LLVM_DYLIB_COMPONENTS "all" CACHE STRING
852    "Semicolon-separated list of components to include in libLLVM, or \"all\".")
853endif()
854
855if(MSVC)
856  option(LLVM_BUILD_LLVM_C_DYLIB "Build LLVM-C.dll (Windows only)" ON)
857  if (BUILD_SHARED_LIBS)
858    message(FATAL_ERROR "BUILD_SHARED_LIBS options is not supported on Windows.")
859  endif()
860else()
861  option(LLVM_BUILD_LLVM_C_DYLIB "Build libllvm-c re-export library (Darwin only)" OFF)
862endif()
863
864# Used to test building the llvm shared library with explicit symbol visibility on
865# Windows and Linux. For ELF platforms default symbols visibility is set to hidden.
866set(LLVM_BUILD_LLVM_DYLIB_VIS FALSE CACHE BOOL "")
867mark_as_advanced(LLVM_BUILD_LLVM_DYLIB_VIS)
868
869set(CAN_BUILD_LLVM_DYLIB OFF)
870if(NOT MSVC OR LLVM_BUILD_LLVM_DYLIB_VIS)
871  set(CAN_BUILD_LLVM_DYLIB ON)
872endif()
873
874cmake_dependent_option(LLVM_LINK_LLVM_DYLIB "Link tools against the libllvm dynamic library" OFF
875                       "CAN_BUILD_LLVM_DYLIB" OFF)
876
877set(LLVM_BUILD_LLVM_DYLIB_default OFF)
878if(LLVM_LINK_LLVM_DYLIB OR LLVM_BUILD_LLVM_C_DYLIB)
879  set(LLVM_BUILD_LLVM_DYLIB_default ON)
880endif()
881cmake_dependent_option(LLVM_BUILD_LLVM_DYLIB "Build libllvm dynamic library" ${LLVM_BUILD_LLVM_DYLIB_default}
882                       "CAN_BUILD_LLVM_DYLIB" OFF)
883
884cmake_dependent_option(LLVM_DYLIB_EXPORT_INLINES "Force inline members of classes to be DLL exported when
885                       building with clang-cl so the libllvm DLL is compatible with MSVC"
886                       OFF
887                       "MSVC;LLVM_BUILD_LLVM_DYLIB_VIS" OFF)
888
889if(LLVM_BUILD_LLVM_DYLIB_VIS)
890  set(LLVM_BUILD_LLVM_DYLIB ON)
891endif()
892
893if (LLVM_LINK_LLVM_DYLIB AND BUILD_SHARED_LIBS)
894  message(FATAL_ERROR "Cannot enable BUILD_SHARED_LIBS with LLVM_LINK_LLVM_DYLIB.  We recommend disabling BUILD_SHARED_LIBS.")
895endif()
896
897option(LLVM_OPTIMIZED_TABLEGEN "Force TableGen to be built with optimization" OFF)
898if(CMAKE_CROSSCOMPILING OR (LLVM_OPTIMIZED_TABLEGEN AND (LLVM_ENABLE_ASSERTIONS OR CMAKE_CONFIGURATION_TYPES)))
899  set(LLVM_USE_HOST_TOOLS ON)
900endif()
901
902option(LLVM_OMIT_DAGISEL_COMMENTS "Do not add comments to DAG ISel" ON)
903if (CMAKE_BUILD_TYPE AND uppercase_CMAKE_BUILD_TYPE MATCHES "^(RELWITHDEBINFO|DEBUG)$")
904  set(LLVM_OMIT_DAGISEL_COMMENTS OFF)
905endif()
906
907if (MSVC_IDE)
908  option(LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION "Configure project to use Visual Studio native visualizers" TRUE)
909endif()
910
911if(NOT LLVM_INDIVIDUAL_TEST_COVERAGE)
912  if(LLVM_BUILD_INSTRUMENTED OR LLVM_BUILD_INSTRUMENTED_COVERAGE OR LLVM_ENABLE_IR_PGO)
913    if(NOT LLVM_PROFILE_MERGE_POOL_SIZE)
914      # A pool size of 1-2 is probably sufficient on an SSD. 3-4 should be fine
915      # for spinning disks. Anything higher may only help on slower mediums.
916      set(LLVM_PROFILE_MERGE_POOL_SIZE "4")
917    endif()
918    if(NOT LLVM_PROFILE_FILE_PATTERN)
919      if(NOT LLVM_PROFILE_DATA_DIR)
920        file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/profiles" LLVM_PROFILE_DATA_DIR)
921      endif()
922      file(TO_NATIVE_PATH "${LLVM_PROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_PROFILE_FILE_PATTERN)
923    endif()
924    if(NOT LLVM_CSPROFILE_FILE_PATTERN)
925      if(NOT LLVM_CSPROFILE_DATA_DIR)
926        file(TO_NATIVE_PATH "${LLVM_BINARY_DIR}/csprofiles" LLVM_CSPROFILE_DATA_DIR)
927      endif()
928      file(TO_NATIVE_PATH "${LLVM_CSPROFILE_DATA_DIR}/%${LLVM_PROFILE_MERGE_POOL_SIZE}m.profraw" LLVM_CSPROFILE_FILE_PATTERN)
929    endif()
930  endif()
931endif()
932
933if (LLVM_BUILD_STATIC)
934  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
935  # Remove shared library suffixes from use in find_library
936  foreach (shared_lib_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_IMPORT_LIBRARY_SUFFIX})
937    list(FIND CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix} shared_lib_suffix_idx)
938    if(NOT ${shared_lib_suffix_idx} EQUAL -1)
939      list(REMOVE_AT CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix_idx})
940    endif()
941  endforeach()
942endif()
943
944# Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
945if(CMAKE_HOST_APPLE AND APPLE)
946  include(UseLibtool)
947endif()
948
949# Override the default target with an environment variable named by LLVM_TARGET_TRIPLE_ENV.
950set(LLVM_TARGET_TRIPLE_ENV CACHE STRING "The name of environment variable to override default target. Disabled by blank.")
951mark_as_advanced(LLVM_TARGET_TRIPLE_ENV)
952
953if(CMAKE_SYSTEM_NAME MATCHES "BSD|Linux|OS390")
954  set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)
955else()
956  set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)
957endif()
958set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR ${LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default} CACHE BOOL
959  "Enable per-target runtimes directory")
960
961set(LLVM_PROFDATA_FILE "" CACHE FILEPATH
962  "Profiling data file to use when compiling in order to improve runtime performance.")
963
964if(LLVM_INCLUDE_TESTS)
965  # All LLVM Python files should be compatible down to this minimum version.
966  set(LLVM_MINIMUM_PYTHON_VERSION 3.8)
967else()
968  # FIXME: it is unknown if this is the actual minimum bound
969  set(LLVM_MINIMUM_PYTHON_VERSION 3.0)
970endif()
971
972# Find python before including config-ix, since it needs to be able to search
973# for python modules.
974find_package(Python3 ${LLVM_MINIMUM_PYTHON_VERSION} REQUIRED
975    COMPONENTS Interpreter)
976
977# All options referred to from HandleLLVMOptions have to be specified
978# BEFORE this include, otherwise options will not be correctly set on
979# first cmake run
980include(config-ix)
981
982# By default, we target the host, but this can be overridden at CMake
983# invocation time. Except on 64-bit AIX, where the system toolchain
984# expect 32-bit objects by default.
985if("${LLVM_HOST_TRIPLE}" MATCHES "^powerpc64-ibm-aix")
986  string(REGEX REPLACE "^powerpc64" "powerpc" LLVM_DEFAULT_TARGET_TRIPLE_DEFAULT "${LLVM_HOST_TRIPLE}")
987else()
988  # Only set default triple when native target is enabled.
989  if (LLVM_NATIVE_TARGET)
990    set(LLVM_DEFAULT_TARGET_TRIPLE_DEFAULT "${LLVM_HOST_TRIPLE}")
991  endif()
992endif()
993
994set(LLVM_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE_DEFAULT}" CACHE STRING
995    "Default target for which LLVM will generate code." )
996message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
997
998set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
999
1000if(WIN32 OR CYGWIN)
1001  if(BUILD_SHARED_LIBS OR LLVM_BUILD_LLVM_DYLIB)
1002    set(LLVM_ENABLE_PLUGINS_default ON)
1003  else()
1004    set(LLVM_ENABLE_PLUGINS_default OFF)
1005  endif()
1006else()
1007  set(LLVM_ENABLE_PLUGINS_default ${LLVM_ENABLE_PIC})
1008endif()
1009option(LLVM_ENABLE_PLUGINS "Enable plugin support" ${LLVM_ENABLE_PLUGINS_default})
1010
1011set(LLVM_ENABLE_NEW_PASS_MANAGER TRUE CACHE BOOL
1012  "Enable the new pass manager by default.")
1013if(NOT LLVM_ENABLE_NEW_PASS_MANAGER)
1014  message(FATAL_ERROR "Enabling the legacy pass manager on the cmake level is"
1015                      " no longer supported.")
1016endif()
1017
1018include(HandleLLVMOptions)
1019
1020######
1021
1022# Configure all of the various header file fragments LLVM uses which depend on
1023# configuration variables.
1024set(LLVM_ENUM_TARGETS "")
1025set(LLVM_ENUM_ASM_PRINTERS "")
1026set(LLVM_ENUM_ASM_PARSERS "")
1027set(LLVM_ENUM_DISASSEMBLERS "")
1028set(LLVM_ENUM_TARGETMCAS "")
1029set(LLVM_ENUM_EXEGESIS "")
1030foreach(t ${LLVM_TARGETS_TO_BUILD})
1031  set( td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t} )
1032
1033  # Make sure that any experimental targets were passed via
1034  # LLVM_EXPERIMENTAL_TARGETS_TO_BUILD, not LLVM_TARGETS_TO_BUILD.
1035  # We allow experimental targets that are not in LLVM_ALL_EXPERIMENTAL_TARGETS,
1036  # as long as they are passed via LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.
1037  if ( NOT "${t}" IN_LIST LLVM_ALL_TARGETS AND NOT "${t}" IN_LIST LLVM_EXPERIMENTAL_TARGETS_TO_BUILD )
1038    if( "${t}" IN_LIST LLVM_ALL_EXPERIMENTAL_TARGETS )
1039      message(FATAL_ERROR "The target `${t}' is experimental and must be passed "
1040        "via LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.")
1041    else()
1042      message(FATAL_ERROR "The target `${t}' is not a core tier target. It may be "
1043        "experimental, if so it must be passed via "
1044        "LLVM_EXPERIMENTAL_TARGETS_TO_BUILD.\n"
1045        "Core tier targets: ${LLVM_ALL_TARGETS}\n"
1046        "Known experimental targets: ${LLVM_ALL_EXPERIMENTAL_TARGETS}")
1047    endif()
1048  else()
1049    set(LLVM_ENUM_TARGETS "${LLVM_ENUM_TARGETS}LLVM_TARGET(${t})\n")
1050    string(TOUPPER ${t} T_UPPER)
1051    set(LLVM_HAS_${T_UPPER}_TARGET 1)
1052  endif()
1053
1054  file(GLOB asmp_file "${td}/*AsmPrinter.cpp")
1055  if( asmp_file )
1056    set(LLVM_ENUM_ASM_PRINTERS
1057      "${LLVM_ENUM_ASM_PRINTERS}LLVM_ASM_PRINTER(${t})\n")
1058  endif()
1059  if( EXISTS ${td}/AsmParser/CMakeLists.txt )
1060    set(LLVM_ENUM_ASM_PARSERS
1061      "${LLVM_ENUM_ASM_PARSERS}LLVM_ASM_PARSER(${t})\n")
1062  endif()
1063  if( EXISTS ${td}/Disassembler/CMakeLists.txt )
1064    set(LLVM_ENUM_DISASSEMBLERS
1065      "${LLVM_ENUM_DISASSEMBLERS}LLVM_DISASSEMBLER(${t})\n")
1066  endif()
1067  if( EXISTS ${td}/MCA/CMakeLists.txt )
1068    set(LLVM_ENUM_TARGETMCAS
1069      "${LLVM_ENUM_TARGETMCAS}LLVM_TARGETMCA(${t})\n")
1070  endif()
1071  if( EXISTS ${LLVM_MAIN_SRC_DIR}/tools/llvm-exegesis/lib/${t}/CMakeLists.txt )
1072    set(LLVM_ENUM_EXEGESIS
1073      "${LLVM_ENUM_EXEGESIS}LLVM_EXEGESIS(${t})\n")
1074  endif()
1075endforeach(t)
1076
1077# Provide an LLVM_ namespaced alias for use in #cmakedefine.
1078set(LLVM_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
1079
1080# Produce the target definition files, which provide a way for clients to easily
1081# include various classes of targets.
1082configure_file(
1083  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmPrinters.def.in
1084  ${LLVM_INCLUDE_DIR}/llvm/Config/AsmPrinters.def
1085  )
1086configure_file(
1087  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmParsers.def.in
1088  ${LLVM_INCLUDE_DIR}/llvm/Config/AsmParsers.def
1089  )
1090configure_file(
1091  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Disassemblers.def.in
1092  ${LLVM_INCLUDE_DIR}/llvm/Config/Disassemblers.def
1093  )
1094configure_file(
1095  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
1096  ${LLVM_INCLUDE_DIR}/llvm/Config/Targets.def
1097  )
1098configure_file(
1099  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/TargetMCAs.def.in
1100  ${LLVM_INCLUDE_DIR}/llvm/Config/TargetMCAs.def
1101  )
1102configure_file(
1103  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/TargetExegesis.def.in
1104  ${LLVM_INCLUDE_DIR}/llvm/Config/TargetExegesis.def
1105  )
1106
1107# They are not referenced. See set_output_directory().
1108set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR} )
1109set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_LIBRARY_DIR} )
1110set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_LIBRARY_DIR} )
1111
1112# For up-to-date instructions for installing the TFLite dependency, refer to
1113# the bot setup script: https://github.com/google/ml-compiler-opt/blob/main/buildbot/buildbot_init.sh
1114set(LLVM_HAVE_TFLITE "" CACHE BOOL "Use tflite")
1115if (LLVM_HAVE_TFLITE)
1116  find_package(tensorflow-lite REQUIRED)
1117endif()
1118
1119# For up-to-date instructions for installing the Tensorflow dependency, refer to
1120# the bot setup script: https://github.com/google/ml-compiler-opt/blob/main/buildbot/buildbot_init.sh
1121# Specifically, assuming python3 is installed:
1122# python3 -m pip install --upgrade pip && python3 -m pip install --user tf_nightly==2.3.0.dev20200528
1123# Then set TENSORFLOW_AOT_PATH to the package install - usually it's ~/.local/lib/python3.7/site-packages/tensorflow
1124#
1125set(TENSORFLOW_AOT_PATH "" CACHE PATH "Path to TensorFlow pip install dir")
1126
1127if (NOT TENSORFLOW_AOT_PATH STREQUAL "")
1128  set(LLVM_HAVE_TF_AOT "ON" CACHE BOOL "Tensorflow AOT available")
1129  set(TENSORFLOW_AOT_COMPILER
1130    "${TENSORFLOW_AOT_PATH}/../../../../bin/saved_model_cli"
1131    CACHE PATH "Path to the Tensorflow AOT compiler")
1132  include_directories(${TENSORFLOW_AOT_PATH}/include)
1133  add_subdirectory(${TENSORFLOW_AOT_PATH}/xla_aot_runtime_src
1134    ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/tf_runtime)
1135  install(TARGETS tf_xla_runtime EXPORT LLVMExports
1136    ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
1137  install(TARGETS tf_xla_runtime EXPORT LLVMDevelopmentExports
1138    ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX} COMPONENT tf_xla_runtime)
1139  set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS tf_xla_runtime)
1140  # Once we add more modules, we should handle this more automatically.
1141  if (DEFINED LLVM_OVERRIDE_MODEL_HEADER_INLINERSIZEMODEL)
1142    set(LLVM_INLINER_MODEL_PATH "none")
1143  elseif(NOT DEFINED LLVM_INLINER_MODEL_PATH
1144      OR "${LLVM_INLINER_MODEL_PATH}" STREQUAL ""
1145      OR "${LLVM_INLINER_MODEL_PATH}" STREQUAL "autogenerate")
1146    set(LLVM_INLINER_MODEL_PATH "autogenerate")
1147    set(LLVM_INLINER_MODEL_AUTOGENERATED 1)
1148  endif()
1149  if (DEFINED LLVM_OVERRIDE_MODEL_HEADER_REGALLOCEVICTMODEL)
1150    set(LLVM_RAEVICT_MODEL_PATH "none")
1151  elseif(NOT DEFINED LLVM_RAEVICT_MODEL_PATH
1152      OR "${LLVM_RAEVICT_MODEL_PATH}" STREQUAL ""
1153      OR "${LLVM_RAEVICT_MODEL_PATH}" STREQUAL "autogenerate")
1154    set(LLVM_RAEVICT_MODEL_PATH "autogenerate")
1155    set(LLVM_RAEVICT_MODEL_AUTOGENERATED 1)
1156  endif()
1157
1158endif()
1159
1160# Configure the three LLVM configuration header files.
1161configure_file(
1162  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/config.h.cmake
1163  ${LLVM_INCLUDE_DIR}/llvm/Config/config.h)
1164configure_file(
1165  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/llvm-config.h.cmake
1166  ${LLVM_INCLUDE_DIR}/llvm/Config/llvm-config.h)
1167configure_file(
1168  ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/abi-breaking.h.cmake
1169  ${LLVM_INCLUDE_DIR}/llvm/Config/abi-breaking.h)
1170
1171if(APPLE AND DARWIN_LTO_LIBRARY)
1172  set(CMAKE_EXE_LINKER_FLAGS
1173    "${CMAKE_EXE_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
1174  set(CMAKE_SHARED_LINKER_FLAGS
1175    "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
1176  set(CMAKE_MODULE_LINKER_FLAGS
1177    "${CMAKE_MODULE_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")
1178endif()
1179
1180# Build with _XOPEN_SOURCE on AIX, as stray macros in _ALL_SOURCE mode tend to
1181# break things. In this case we need to enable the large-file API as well.
1182if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
1183          add_compile_definitions(_XOPEN_SOURCE=700)
1184          add_compile_definitions(_LARGE_FILE_API)
1185
1186  # Modules should be built with -shared -Wl,-G, so we can use runtime linking
1187  # with plugins.
1188  string(APPEND CMAKE_MODULE_LINKER_FLAGS " -shared -Wl,-G")
1189
1190  # Also set the correct flags for building shared libraries.
1191  string(APPEND CMAKE_SHARED_LINKER_FLAGS " -shared")
1192endif()
1193
1194# Build with _XOPEN_SOURCE on z/OS.
1195if (CMAKE_SYSTEM_NAME MATCHES "OS390")
1196  add_compile_definitions(_XOPEN_SOURCE=600)
1197  add_compile_definitions(_OPEN_SYS) # Needed for process information.
1198  add_compile_definitions(_OPEN_SYS_FILE_EXT) # Needed for EBCDIC I/O.
1199  add_compile_definitions(_EXT) # Needed for file data.
1200  add_compile_definitions(_UNIX03_THREADS) # Multithreading support.
1201  # Need to build LLVM as ASCII application.
1202  # This can't be a global setting because other projects may
1203  # need to be built in EBCDIC mode.
1204  append("-fzos-le-char-mode=ascii" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
1205  append("-m64" CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
1206endif()
1207
1208# Build with _FILE_OFFSET_BITS=64 on Solaris to match g++ >= 9.
1209if (UNIX AND ${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
1210          add_compile_definitions(_FILE_OFFSET_BITS=64)
1211endif()
1212
1213set(CMAKE_INCLUDE_CURRENT_DIR ON)
1214
1215include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
1216
1217# when crosscompiling import the executable targets from a file
1218if(LLVM_USE_HOST_TOOLS)
1219  include(CrossCompile)
1220  llvm_create_cross_target(LLVM NATIVE "" Release)
1221endif(LLVM_USE_HOST_TOOLS)
1222if(LLVM_TARGET_IS_CROSSCOMPILE_HOST)
1223# Dummy use to avoid CMake Warning: Manually-specified variables were not used
1224# (this is a variable that CrossCompile sets on recursive invocations)
1225endif()
1226
1227if( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
1228   # special hack for Solaris to handle crazy system sys/regset.h
1229   include_directories("${LLVM_MAIN_INCLUDE_DIR}/llvm/Support/Solaris")
1230endif( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
1231
1232# Make sure we don't get -rdynamic in every binary. For those that need it,
1233# use EXPORT_SYMBOLS argument.
1234set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
1235
1236include(AddLLVM)
1237include(TableGen)
1238
1239include(LLVMDistributionSupport)
1240
1241if( MINGW AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" )
1242  # People report that -O3 is unreliable on MinGW. The traditional
1243  # build also uses -O2 for that reason:
1244  llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
1245endif()
1246
1247if(LLVM_INCLUDE_TESTS)
1248  umbrella_lit_testsuite_begin(check-all)
1249endif()
1250
1251# Put this before tblgen. Else we have a circular dependence.
1252add_subdirectory(lib/Demangle)
1253add_subdirectory(lib/Support)
1254add_subdirectory(lib/TableGen)
1255
1256add_subdirectory(utils/TableGen)
1257
1258add_subdirectory(include)
1259
1260add_subdirectory(lib)
1261
1262if( LLVM_INCLUDE_UTILS )
1263  add_subdirectory(utils/FileCheck)
1264  add_subdirectory(utils/PerfectShuffle)
1265  add_subdirectory(utils/count)
1266  add_subdirectory(utils/not)
1267  add_subdirectory(utils/UnicodeData)
1268  add_subdirectory(utils/yaml-bench)
1269  add_subdirectory(utils/split-file)
1270  add_subdirectory(utils/mlgo-utils)
1271  if( LLVM_INCLUDE_TESTS )
1272    set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")
1273    add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest ${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest)
1274    set(LLVM_SUBPROJECT_TITLE)
1275  endif()
1276else()
1277  if ( LLVM_INCLUDE_TESTS )
1278    message(FATAL_ERROR "Including tests when not building utils will not work.
1279    Either set LLVM_INCLUDE_UTILS to On, or set LLVM_INCLUDE_TESTS to Off.")
1280  endif()
1281endif()
1282
1283# Use LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION instead of LLVM_INCLUDE_UTILS because it is not really a util
1284if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
1285  add_subdirectory(utils/LLVMVisualizers)
1286endif()
1287
1288foreach( binding ${LLVM_BINDINGS_LIST} )
1289  if( EXISTS "${LLVM_MAIN_SRC_DIR}/bindings/${binding}/CMakeLists.txt" )
1290    add_subdirectory(bindings/${binding})
1291  endif()
1292endforeach()
1293
1294add_subdirectory(projects)
1295
1296if( LLVM_INCLUDE_TOOLS )
1297  add_subdirectory(tools)
1298endif()
1299
1300if( LLVM_INCLUDE_RUNTIMES )
1301  add_subdirectory(runtimes)
1302endif()
1303
1304if( LLVM_INCLUDE_EXAMPLES )
1305  add_subdirectory(examples)
1306endif()
1307
1308if( LLVM_INCLUDE_TESTS )
1309  set(LLVM_GTEST_RUN_UNDER
1310    "" CACHE STRING
1311    "Define the wrapper program that LLVM unit tests should be run under.")
1312  if(EXISTS ${LLVM_MAIN_SRC_DIR}/projects/test-suite AND TARGET clang)
1313    include(LLVMExternalProjectUtils)
1314    llvm_ExternalProject_Add(test-suite ${LLVM_MAIN_SRC_DIR}/projects/test-suite
1315      USE_TOOLCHAIN
1316      EXCLUDE_FROM_ALL
1317      NO_INSTALL
1318      ALWAYS_CLEAN)
1319  endif()
1320  add_subdirectory(utils/lit)
1321  add_subdirectory(test)
1322  add_subdirectory(unittests)
1323
1324  if (WIN32)
1325    # This utility is used to prevent crashing tests from calling Dr. Watson on
1326    # Windows.
1327    add_subdirectory(utils/KillTheDoctor)
1328  endif()
1329
1330  umbrella_lit_testsuite_end(check-all)
1331  get_property(LLVM_ALL_LIT_DEPENDS GLOBAL PROPERTY LLVM_ALL_LIT_DEPENDS)
1332  get_property(LLVM_ALL_ADDITIONAL_TEST_DEPENDS
1333      GLOBAL PROPERTY LLVM_ALL_ADDITIONAL_TEST_DEPENDS)
1334  add_custom_target(test-depends)
1335  if(LLVM_ALL_LIT_DEPENDS OR LLVM_ALL_ADDITIONAL_TEST_DEPENDS)
1336    add_dependencies(test-depends ${LLVM_ALL_LIT_DEPENDS} ${LLVM_ALL_ADDITIONAL_TEST_DEPENDS})
1337  endif()
1338  set_target_properties(test-depends PROPERTIES FOLDER "LLVM/Tests")
1339  add_dependencies(check-all test-depends)
1340endif()
1341
1342if (LLVM_INCLUDE_DOCS)
1343  add_subdirectory(docs)
1344endif()
1345
1346add_subdirectory(cmake/modules)
1347
1348# Do this last so that all lit targets have already been created.
1349if (LLVM_INCLUDE_UTILS)
1350  add_subdirectory(utils/llvm-lit)
1351endif()
1352
1353if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
1354  install(DIRECTORY include/llvm include/llvm-c
1355    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1356    COMPONENT llvm-headers
1357    FILES_MATCHING
1358    PATTERN "*.def"
1359    PATTERN "*.h"
1360    PATTERN "*.td"
1361    PATTERN "*.inc"
1362    PATTERN "LICENSE.TXT"
1363    )
1364
1365  install(DIRECTORY ${LLVM_INCLUDE_DIR}/llvm ${LLVM_INCLUDE_DIR}/llvm-c
1366    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1367    COMPONENT llvm-headers
1368    FILES_MATCHING
1369    PATTERN "*.def"
1370    PATTERN "*.h"
1371    PATTERN "*.gen"
1372    PATTERN "*.inc"
1373    # Exclude include/llvm/CMakeFiles/intrinsics_gen.dir, matched by "*.def"
1374    PATTERN "CMakeFiles" EXCLUDE
1375    PATTERN "config.h" EXCLUDE
1376    )
1377
1378  if (LLVM_INSTALL_MODULEMAPS)
1379    install(DIRECTORY include
1380            DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1381            COMPONENT llvm-headers
1382            FILES_MATCHING
1383            PATTERN "module.modulemap"
1384            )
1385    install(FILES include/module.install.modulemap
1386            DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
1387            COMPONENT llvm-headers
1388            RENAME "module.extern.modulemap"
1389            )
1390  endif(LLVM_INSTALL_MODULEMAPS)
1391
1392  # Installing the headers needs to depend on generating any public
1393  # tablegen'd headers.
1394  add_custom_target(llvm-headers DEPENDS intrinsics_gen omp_gen)
1395  set_target_properties(llvm-headers PROPERTIES FOLDER "LLVM/Resources")
1396
1397  if (NOT LLVM_ENABLE_IDE)
1398    add_llvm_install_targets(install-llvm-headers
1399                             DEPENDS llvm-headers
1400                             COMPONENT llvm-headers)
1401  endif()
1402
1403  # Custom target to install all libraries.
1404  add_custom_target(llvm-libraries)
1405  set_target_properties(llvm-libraries PROPERTIES FOLDER "LLVM/Resources")
1406
1407  if (NOT LLVM_ENABLE_IDE)
1408    add_llvm_install_targets(install-llvm-libraries
1409                             DEPENDS llvm-libraries
1410                             COMPONENT llvm-libraries)
1411  endif()
1412
1413  get_property(LLVM_LIBS GLOBAL PROPERTY LLVM_LIBS)
1414  if(LLVM_LIBS)
1415    list(REMOVE_DUPLICATES LLVM_LIBS)
1416    foreach(lib ${LLVM_LIBS})
1417      add_dependencies(llvm-libraries ${lib})
1418      if (NOT LLVM_ENABLE_IDE)
1419        add_dependencies(install-llvm-libraries install-${lib})
1420        add_dependencies(install-llvm-libraries-stripped install-${lib}-stripped)
1421      endif()
1422    endforeach()
1423  endif()
1424endif()
1425
1426# This must be at the end of the LLVM root CMakeLists file because it must run
1427# after all targets are created.
1428llvm_distribution_add_targets()
1429process_llvm_pass_plugins(GEN_CONFIG)
1430include(CoverageReport)
1431
1432# This allows us to deploy the Universal CRT DLLs by passing -DCMAKE_INSTALL_UCRT_LIBRARIES=ON to CMake
1433if (MSVC AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_INSTALL_UCRT_LIBRARIES)
1434  include(InstallRequiredSystemLibraries)
1435endif()
1436
1437if (LLVM_INCLUDE_BENCHMARKS)
1438  # Override benchmark defaults so that when the library itself is updated these
1439  # modifications are not lost.
1440  set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Disable benchmark testing" FORCE)
1441  set(BENCHMARK_ENABLE_EXCEPTIONS OFF CACHE BOOL "Disable benchmark exceptions" FORCE)
1442  set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Don't install benchmark" FORCE)
1443  set(BENCHMARK_DOWNLOAD_DEPENDENCIES OFF CACHE BOOL "Don't download dependencies" FORCE)
1444  set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE BOOL "Disable Google Test in benchmark" FORCE)
1445  set(BENCHMARK_ENABLE_WERROR ${LLVM_ENABLE_WERROR} CACHE BOOL
1446    "Handle -Werror for Google Benchmark based on LLVM_ENABLE_WERROR" FORCE)
1447  # Since LLVM requires C++11 it is safe to assume that std::regex is available.
1448  set(HAVE_STD_REGEX ON CACHE BOOL "OK" FORCE)
1449  add_subdirectory(${LLVM_THIRD_PARTY_DIR}/benchmark
1450    ${CMAKE_CURRENT_BINARY_DIR}/third-party/benchmark)
1451  set_target_properties(benchmark PROPERTIES FOLDER "Third-Party/Google Benchmark")
1452  set_target_properties(benchmark_main PROPERTIES FOLDER "Third-Party/Google Benchmark")
1453  add_subdirectory(benchmarks)
1454endif()
1455
1456if (LLVM_INCLUDE_UTILS AND LLVM_INCLUDE_TOOLS)
1457  add_subdirectory(utils/llvm-locstats)
1458endif()
1459
1460if (XCODE)
1461  # For additional targets that you would like to add schemes, specify e.g:
1462  #
1463  #   -DLLVM_XCODE_EXTRA_TARGET_SCHEMES="TargetParserTests;SupportTests"
1464  #
1465  # at CMake configure time.
1466  set(LLVM_XCODE_EXTRA_TARGET_SCHEMES "" CACHE STRING "Specifies an extra list of targets to turn into schemes")
1467
1468  foreach(target ${LLVM_XCODE_EXTRA_TARGET_SCHEMES})
1469    set_target_properties(${target} PROPERTIES XCODE_GENERATE_SCHEME ON)
1470  endforeach()
1471endif()
1472