2018-12-20 06:55:05 +01:00
if ( WITH_LLVM )
2023-07-11 20:40:30 +02:00
check_cxx_compiler_flag ( "-msse -msse2 -mcx16" COMPILER_X86 )
check_cxx_compiler_flag ( "-march=armv8-a+lse" COMPILER_ARM )
2021-12-30 17:39:18 +01:00
2023-03-11 20:08:27 +01:00
if ( BUILD_LLVM )
2018-09-18 12:07:33 +02:00
message ( STATUS "LLVM will be built from the submodule." )
2025-02-28 18:56:23 +01:00
if ( ANDROID )
if ( COMPILER_ARM )
set ( LLVM_TARGETS_TO_BUILD "AArch64" CACHE STRING "Semicolon-separated list of targets to build, or \" all\ "." )
set ( LLVM_TARGET_ARCH "${CMAKE_SYSTEM_PROCESSOR}-none-linux-android${ANDROID_NATIVE_API_LEVEL}" CACHE STRING "" )
else ( )
set ( LLVM_TARGETS_TO_BUILD "X86" CACHE STRING "Semicolon-separated list of targets to build, or \" all\ "." )
endif ( )
else ( )
set ( LLVM_TARGETS_TO_BUILD "AArch64;X86" CACHE STRING "Semicolon-separated list of targets to build, or \" all\ "." )
endif ( )
2025-01-16 18:17:13 +01:00
option ( LLVM_BUILD_RUNTIME "Build the LLVM runtime libraries." OFF )
option ( LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." OFF )
option ( LLVM_INCLUDE_BENCHMARKS "Generate benchmark targets. If OFF, benchmarks can't be built." OFF )
option ( LLVM_INCLUDE_DOCS "Generate build targets for llvm documentation." OFF )
option ( LLVM_INCLUDE_EXAMPLES "Generate build targets for the LLVM examples" OFF )
option ( LLVM_INCLUDE_TESTS "Generate build targets for the LLVM unit tests." OFF )
option ( LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." OFF )
option ( LLVM_INCLUDE_UTILS "Generate build targets for the LLVM utils." OFF )
option ( LLVM_CCACHE_BUILD "Set to ON for a ccache enabled build" OFF )
2023-07-28 11:25:18 +02:00
set ( LLVM_ENABLE_WARNINGS OFF CACHE BOOL "Enable compiler warnings." )
2018-09-18 12:07:33 +02:00
2024-10-15 17:53:31 +02:00
# For Windows x86 (not Windows AArch64).
# Check on MSVC is needed due to COMPILER_X86, COMPILER_ARM etc. are not set/supported by the MSVC compiler, if used.
# Furthermore, the MSVC compiler is not available/supported on Windows AArch64
if ( WIN32 AND ( COMPILER_X86 OR MSVC ) )
2021-12-24 18:33:32 +01:00
set ( LLVM_USE_INTEL_JITEVENTS ON )
endif ( )
2023-07-11 20:40:30 +02:00
if ( CMAKE_SYSTEM_NAME STREQUAL "Linux" )
2024-10-01 13:33:06 +02:00
if ( COMPILER_X86 )
set ( LLVM_USE_INTEL_JITEVENTS ON )
endif ( )
2021-12-24 18:33:32 +01:00
set ( LLVM_USE_PERF ON )
endif ( )
2018-09-18 12:07:33 +02:00
if ( MSVC )
2023-07-11 20:40:30 +02:00
add_compile_definitions ( "$<$<COMPILE_LANGUAGE:CXX>:_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS>" )
2018-09-18 12:07:33 +02:00
endif ( )
# LLVM needs to be built out-of-tree
2023-07-11 20:40:30 +02:00
add_subdirectory ( ${ CMAKE_CURRENT_SOURCE_DIR } /llvm/llvm ${ CMAKE_CURRENT_BINARY_DIR } /llvm_build EXCLUDE_FROM_ALL )
set ( LLVM_DIR "${CMAKE_CURRENT_BINARY_DIR}/llvm_build/lib/cmake/llvm/" )
2018-09-18 12:07:33 +02:00
2023-05-01 13:26:30 +02:00
set ( STATIC_LINK_LLVM ON CACHE BOOL "Link against LLVM statically. This will get set to ON if you build LLVM from the submodule." FORCE )
2018-09-18 12:07:33 +02:00
2025-01-25 16:17:40 +01:00
find_package ( LLVM 19.1 CONFIG )
2018-09-18 12:07:33 +02:00
if ( NOT LLVM_FOUND )
message ( FATAL_ERROR "Couldn't build LLVM from the submodule. You might need to run `git submodule update --init`" )
endif ( )
else ( )
2023-03-11 20:08:27 +01:00
message ( STATUS "Using prebuilt or system LLVM" )
2018-09-18 12:07:33 +02:00
if ( LLVM_DIR AND NOT IS_ABSOLUTE "${LLVM_DIR}" )
# change relative LLVM_DIR to be relative to the source dir
set ( LLVM_DIR ${ CMAKE_SOURCE_DIR } / ${ LLVM_DIR } )
endif ( )
2024-03-10 12:56:16 +01:00
find_package ( LLVM CONFIG )
2018-09-18 12:07:33 +02:00
if ( NOT LLVM_FOUND )
2025-06-13 02:05:15 +02:00
message ( FATAL_ERROR "Can't find LLVM libraries from the CMAKE_PREFIX_PATH path or LLVM_DIR. Enable BUILD_LLVM option to build LLVM from included as a git submodule." )
2018-09-18 12:07:33 +02:00
endif ( )
2025-01-24 04:46:09 +01:00
if ( LLVM_VERSION VERSION_LESS 18 )
2025-06-13 02:05:15 +02:00
message ( FATAL_ERROR "Found LLVM version ${LLVM_VERSION}. Required version 18 or above. Enable BUILD_LLVM option to build LLVM from included as a git submodule." )
2024-03-10 12:56:16 +01:00
endif ( )
2023-04-29 19:59:16 +02:00
endif ( )
2018-09-18 12:07:33 +02:00
2023-04-29 19:59:16 +02:00
if ( STATIC_LINK_LLVM )
if ( NOT DEFINED LLVM_TARGETS_TO_BUILD )
if ( COMPILER_ARM )
2025-01-24 04:46:09 +01:00
set ( LLVM_TARGETS_TO_BUILD "AArch64" CACHE STRING "Semicolon-separated list of targets to build, or \" all\ "." )
2023-04-29 19:59:16 +02:00
else ( )
set ( LLVM_TARGETS_TO_BUILD "X86" CACHE STRING "Semicolon-separated list of targets to build, or \" all\ "." )
endif ( )
endif ( )
2024-10-15 17:53:31 +02:00
# For Windows x86 (not Windows AArch64) only when BUILD_LLVM is enabled and
# for Linux x86 (not Linux AArch64) even if BUILD_LLVM is disabled (precompiled llvm used)
if ( LLVM_USE_INTEL_JITEVENTS OR ( CMAKE_SYSTEM_NAME STREQUAL "Linux" AND COMPILER_X86 ) )
2023-07-11 20:40:30 +02:00
list ( APPEND LLVM_ADDITIONAL_LIBS IntelJITEvents )
2023-04-29 19:59:16 +02:00
endif ( )
2024-10-15 17:53:31 +02:00
# For Linux even if BUILD_LLVM is disabled (precompiled llvm used)
2023-07-11 20:40:30 +02:00
if ( CMAKE_SYSTEM_NAME STREQUAL "Linux" )
list ( APPEND LLVM_ADDITIONAL_LIBS PerfJITEvents )
2023-04-29 19:59:16 +02:00
endif ( )
2024-10-15 17:53:31 +02:00
2023-04-29 19:59:16 +02:00
llvm_map_components_to_libnames ( LLVM_LIBS
$ { L L V M _ T A R G E T S _ T O _ B U I L D }
$ { L L V M _ A D D I T I O N A L _ L I B S }
C o r e
E x e c u t i o n E n g i n e
M C J I T
2023-11-29 19:53:38 +01:00
P a s s e s
2023-04-29 19:59:16 +02:00
)
else ( )
2023-04-07 23:50:10 +02:00
set ( LLVM_LIBS LLVM )
endif ( )
2018-09-18 12:07:33 +02:00
add_library ( 3rdparty_llvm INTERFACE )
target_link_libraries ( 3rdparty_llvm INTERFACE ${ LLVM_LIBS } )
target_include_directories ( 3rdparty_llvm INTERFACE ${ LLVM_INCLUDE_DIRS } )
2023-07-11 20:40:30 +02:00
separate_arguments ( LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${ LLVM_DEFINITIONS } )
target_compile_definitions ( 3rdparty_llvm INTERFACE ${ LLVM_DEFINITIONS_LIST } LLVM_AVAILABLE )
2018-09-18 12:07:33 +02:00
add_library ( 3rdparty::llvm ALIAS 3rdparty_llvm )
else ( )
add_library ( 3rdparty::llvm ALIAS 3rdparty_dummy_lib )
endif ( )