1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-09 16:05:58 +03:00
mold/CMakeLists.txt

249 lines
7.2 KiB
CMake
Raw Normal View History

2022-08-08 07:07:47 +03:00
cmake_minimum_required(VERSION 3.18)
project(mold VERSION 1.4.0)
include(CMakeDependentOption)
# FIXME: this is for parity with the makefiles that install directly to
# <prefix>/lib
#
# NOTE: defining this before including GNUInstallDirs makes its platform
# detection a noop
set(CMAKE_INSTALL_LIBDIR lib CACHE PATH "")
include(GNUInstallDirs)
2022-08-08 07:30:56 +03:00
# Add -fuse-ld=mold if accepted by the compiler.
2022-08-08 06:11:22 +03:00
option(MOLD_USE_MOLD "Use mold to build mold" ON)
2022-08-08 07:07:47 +03:00
if(MOLD_USE_MOLD)
include(CheckLinkerFlag)
check_linker_flag(CXX -fuse-ld=mold CXX_SUPPORTS_FUSE_MOLD)
2022-08-08 08:30:14 +03:00
if(CXX_SUPPORTS_FUSE_MOLD)
2022-08-08 07:07:47 +03:00
add_link_options(-fuse-ld=mold)
endif()
endif()
add_executable(mold)
target_compile_features(mold PRIVATE cxx_std_20)
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
if(NOT MSVC)
target_compile_options(mold PRIVATE
-fno-exceptions
-fno-unwind-tables
-fno-asynchronous-unwind-tables)
endif()
2022-08-08 08:30:14 +03:00
# Build mold with -flto if MOLD_LTO=On
option(MOLD_LTO "Build mold with link-time optimization enabled")
if(MOLD_LTO)
set_property(TARGET mold PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
endif()
2022-08-08 07:30:56 +03:00
# Handle MOLD_USE_ASAN and MOLD_USE_TSAN
option(MOLD_USE_ASAN "Build mold with AddressSanitizer" OFF)
if(MOLD_USE_ASAN)
2022-08-09 17:25:32 +03:00
target_compile_options(mold PRIVATE -fsanitize=address -fsanitize=undefined)
target_link_options(mold PRIVATE -fsanitize=address -fsanitize=undefined)
2022-08-08 07:30:56 +03:00
endif()
option(MOLD_USE_TSAN "Build mold with ThreadSanitizer" OFF)
if(MOLD_USE_TSAN)
2022-08-08 07:30:56 +03:00
target_compile_options(mold PRIVATE -fsanitize=thread)
target_link_options(mold PRIVATE -fsanitize=thread)
endif()
2022-08-08 12:11:32 +03:00
# Static link libstdc++ and libcrypto if MOLD_MOSTLY_STATIC=On
option(MOLD_MOSTLY_STATIC "Statically link libstdc++ and libcrypto" OFF)
if(MOLD_MOSTLY_STATIC)
2022-08-08 12:30:53 +03:00
target_link_options(mold PRIVATE -static-libstdc++)
2022-08-08 12:11:32 +03:00
target_link_libraries(mold PRIVATE libcrypto.a)
endif()
# Setup zlib
find_package(ZLIB QUIET)
if(ZLIB_FOUND)
target_link_libraries(mold PRIVATE ZLIB::ZLIB)
else()
add_subdirectory(third-party/zlib EXCLUDE_FROM_ALL)
target_include_directories(zlibstatic INTERFACE third-party/zlib $<TARGET_PROPERTY:zlibstatic,BINARY_DIR>)
target_link_libraries(mold PRIVATE zlibstatic)
endif()
# Setup mimalloc
2022-08-08 07:30:56 +03:00
cmake_dependent_option(MOLD_USE_MIMALLOC "Use mimalloc" ON
"NOT APPLE;NOT ANDROID" OFF)
cmake_dependent_option(
MOLD_USE_SYSTEM_MIMALLOC "Use system or vendored mimalloc" OFF
MOLD_USE_MIMALLOC OFF)
if(MOLD_USE_MIMALLOC)
2022-08-08 06:04:08 +03:00
if(MOLD_USE_SYSTEM_MIMALLOC)
find_package(mimalloc REQUIRED)
target_compile_definitions(mimalloc INTERFACE USE_SYSTEM_MIMALLOC)
target_link_libraries(mold PRIVATE mimalloc)
else()
function(mold_add_mimalloc)
set(MI_BUILD_STATIC ON)
option(MI_BUILD_TESTS "Build test executables" OFF)
add_subdirectory(third-party/mimalloc EXCLUDE_FROM_ALL)
target_compile_definitions(mimalloc-static PRIVATE MI_USE_ENVIRON=0)
target_link_libraries(mold PRIVATE mimalloc-static)
endfunction()
mold_add_mimalloc()
endif()
endif()
# Setup TBB
2022-08-08 07:30:56 +03:00
option(MOLD_USE_SYSTEM_TBB "Use system or vendored TBB" OFF)
if(MOLD_USE_SYSTEM_TBB)
2022-08-08 06:04:08 +03:00
find_package(TBB REQUIRED)
target_link_libraries(mold PRIVATE TBB::tbb)
else()
2022-08-08 06:04:08 +03:00
function(mold_add_tbb)
set(BUILD_SHARED_LIBS OFF)
set(TBB_TEST OFF CACHE INTERNAL "")
set(TBB_STRICT OFF CACHE INTERNAL "")
add_subdirectory(third-party/tbb EXCLUDE_FROM_ALL)
target_compile_definitions(tbb PRIVATE __TBB_DYNAMIC_LOAD_ENABLED=0)
target_link_libraries(mold PRIVATE TBB::tbb)
endfunction()
2022-08-08 06:04:08 +03:00
mold_add_tbb()
endif()
# MOLD_X86_64_ONLY is a developer-only option. You should not use it
# for creating an executable for production use.
option(MOLD_X86_64_ONLY "Developer-only option" OFF)
if(MOLD_X86_64_ONLY)
set(CMAKE_BUILD_TYPE "Debug")
target_compile_options(mold PRIVATE -ffunction-sections -fdata-sections
-DMOLD_DEBUG_X86_64_ONLY)
target_link_options(mold PRIVATE -Wl,--gc-sections)
endif()
2022-08-12 12:52:35 +03:00
if(NOT WIN32)
add_library(mold-wrapper SHARED)
install(TARGETS mold-wrapper DESTINATION ${CMAKE_INSTALL_LIBDIR}/mold)
# Remove the default `lib` prefix
set_target_properties(mold-wrapper PROPERTIES PREFIX "")
target_link_libraries(mold-wrapper PRIVATE ${CMAKE_DL_LIBS})
target_sources(mold-wrapper PRIVATE elf/mold-wrapper.c)
2022-08-12 12:52:35 +03:00
target_sources(mold PRIVATE elf/subprocess.cc)
endif()
if(NOT MSVC)
2022-08-08 06:04:08 +03:00
include(CheckLibraryExists)
check_library_exists(m pow "" LIBM_FOUND)
2022-08-08 06:04:08 +03:00
if(LIBM_FOUND)
target_link_libraries(mold PRIVATE m)
endif()
endif()
if(NOT APPLE AND NOT MOLD_MOSTLY_STATIC AND NOT WIN32)
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
2022-08-08 06:04:08 +03:00
target_link_libraries(mold PRIVATE OpenSSL::Crypto)
endif()
if(WIN32)
target_compile_definitions(mold PRIVATE NOGDI NOMINMAX)
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES riscv64
OR CMAKE_SYSTEM_PROCESSOR MATCHES armv6)
2022-08-08 06:04:08 +03:00
target_link_libraries(mold PRIVATE atomic)
endif()
set_property(SOURCE main.cc elf/lto.cc macho/output-chunks.cc APPEND PROPERTY
2022-08-08 06:04:08 +03:00
COMPILE_DEFINITIONS "MOLD_VERSION=\"${CMAKE_PROJECT_VERSION}\"")
find_package(Python3 REQUIRED)
add_custom_command(
OUTPUT git-hash.cc
COMMAND ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/update-git-hash.py ${CMAKE_SOURCE_DIR} git-hash.cc
DEPENDS update-git-hash.py)
target_sources(mold PRIVATE
2022-08-08 06:04:08 +03:00
compress.cc
demangle.cc
filepath.cc
glob.cc
hyperloglog.cc
main.cc
multi-glob.cc
perf.cc
strerror.cc
tar.cc
uuid.cc
elf/arch-arm32.cc
elf/arch-arm64.cc
elf/arch-i386.cc
elf/arch-riscv.cc
elf/arch-x86-64.cc
elf/cmdline.cc
elf/dwarf.cc
elf/gc-sections.cc
elf/icf.cc
elf/input-files.cc
elf/input-sections.cc
elf/linker-script.cc
elf/lto.cc
elf/main.cc
elf/mapfile.cc
elf/output-chunks.cc
elf/passes.cc
elf/relocatable.cc
macho/arch-arm64.cc
macho/arch-x86-64.cc
macho/cmdline.cc
macho/dead-strip.cc
macho/input-files.cc
macho/input-sections.cc
macho/lto.cc
macho/main.cc
macho/mapfile.cc
macho/output-chunks.cc
macho/tapi.cc
macho/yaml.cc
third-party/rust-demangle/rust-demangle.c
git-hash.cc)
if(MSVC)
target_compile_options(mold PRIVATE /bigobj)
endif()
include(CTest)
if(BUILD_TESTING)
2022-08-08 06:04:08 +03:00
# Create the ld and ld64 symlinks required for testing
add_custom_command(
TARGET mold POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld64
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM)
if(${APPLE})
add_subdirectory(test/macho)
elseif(${UNIX})
add_subdirectory(test/elf)
endif()
endif()
if(NOT CMAKE_SKIP_INSTALL_RULES)
2022-08-08 06:04:08 +03:00
install(TARGETS mold)
2022-08-08 12:37:47 +03:00
install(CODE "
file(RELATIVE_PATH RELPATH
/${CMAKE_INSTALL_LIBEXECDIR}/mold /${CMAKE_INSTALL_BINDIR}/mold)
execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory
\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}/mold)
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink \${RELPATH}
\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}/mold/ld)
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink mold
\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/ld.mold)
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink mold
\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/ld64.mold)")
endif()