2022-08-08 07:07:47 +03:00
|
|
|
cmake_minimum_required(VERSION 3.18)
|
2022-08-08 15:01:32 +03:00
|
|
|
project(mold VERSION 1.4.0)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
|
|
|
include(CMakeDependentOption)
|
|
|
|
|
2022-08-06 16:17:29 +03:00
|
|
|
# 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
|
2022-07-14 19:42:02 +03:00
|
|
|
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()
|
|
|
|
|
2022-08-06 16:17:29 +03:00
|
|
|
add_executable(mold)
|
|
|
|
target_compile_features(mold PRIVATE cxx_std_20)
|
2022-08-08 08:01:52 +03:00
|
|
|
target_link_libraries(mold PRIVATE ${CMAKE_DL_LIBS})
|
2022-08-10 23:48:24 +03:00
|
|
|
if(NOT MSVC)
|
|
|
|
target_compile_options(mold PRIVATE
|
|
|
|
-fno-exceptions
|
|
|
|
-fno-unwind-tables
|
|
|
|
-fno-asynchronous-unwind-tables)
|
|
|
|
endif()
|
2022-08-06 16:17:29 +03:00
|
|
|
|
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)
|
2022-08-08 08:01:52 +03:00
|
|
|
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()
|
|
|
|
|
2022-08-08 08:01:52 +03:00
|
|
|
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()
|
|
|
|
|
2022-08-08 08:01:52 +03:00
|
|
|
# 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)
|
2022-08-10 23:48:24 +03:00
|
|
|
target_include_directories(zlibstatic INTERFACE third-party/zlib $<TARGET_PROPERTY:zlibstatic,BINARY_DIR>)
|
2022-08-08 08:01:52 +03:00
|
|
|
target_link_libraries(mold PRIVATE zlibstatic)
|
|
|
|
endif()
|
2022-07-14 19:42:02 +03:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2022-07-14 19:42:02 +03:00
|
|
|
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()
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
|
|
|
# Setup TBB
|
2022-08-08 07:30:56 +03:00
|
|
|
option(MOLD_USE_SYSTEM_TBB "Use system or vendored TBB" OFF)
|
2022-07-14 19:42:02 +03:00
|
|
|
if(MOLD_USE_SYSTEM_TBB)
|
2022-08-08 06:04:08 +03:00
|
|
|
find_package(TBB REQUIRED)
|
|
|
|
target_link_libraries(mold PRIVATE TBB::tbb)
|
2022-07-14 19:42:02 +03:00
|
|
|
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-07-14 19:42:02 +03:00
|
|
|
|
2022-08-08 06:04:08 +03:00
|
|
|
mold_add_tbb()
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
2022-08-10 11:51:06 +03:00
|
|
|
# MOLD_X86_64_ONLY is a developer-only option. You should not use it
|
|
|
|
# for creating an executable for production use.
|
2022-08-10 11:43:58 +03:00
|
|
|
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)
|
2022-08-12 12:43:02 +03:00
|
|
|
add_library(mold-wrapper SHARED)
|
|
|
|
install(TARGETS mold-wrapper DESTINATION ${CMAKE_INSTALL_LIBDIR}/mold)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
2022-08-12 12:43:02 +03:00
|
|
|
# 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)
|
2022-08-12 12:43:02 +03:00
|
|
|
endif()
|
2022-07-14 19:42:02 +03:00
|
|
|
|
|
|
|
if(NOT MSVC)
|
2022-08-08 06:04:08 +03:00
|
|
|
include(CheckLibraryExists)
|
|
|
|
check_library_exists(m pow "" LIBM_FOUND)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
2022-08-08 06:04:08 +03:00
|
|
|
if(LIBM_FOUND)
|
|
|
|
target_link_libraries(mold PRIVATE m)
|
|
|
|
endif()
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
2022-08-10 23:48:24 +03:00
|
|
|
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)
|
2022-08-10 23:48:24 +03:00
|
|
|
endif()
|
|
|
|
|
|
|
|
if(WIN32)
|
|
|
|
target_compile_definitions(mold PRIVATE NOGDI NOMINMAX)
|
2022-07-14 19:42:02 +03:00
|
|
|
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)
|
2022-07-14 19:42:02 +03:00
|
|
|
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}\"")
|
2022-07-14 19:42:02 +03:00
|
|
|
|
2022-08-10 23:48:24 +03:00
|
|
|
find_package(Python3 REQUIRED)
|
|
|
|
|
2022-08-08 15:01:32 +03:00
|
|
|
add_custom_command(
|
2022-08-10 23:48:24 +03:00
|
|
|
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)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
|
|
|
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
|
2022-08-08 15:01:32 +03:00
|
|
|
third-party/rust-demangle/rust-demangle.c
|
2022-08-10 23:48:24 +03:00
|
|
|
git-hash.cc)
|
|
|
|
|
|
|
|
if(MSVC)
|
|
|
|
target_compile_options(mold PRIVATE /bigobj)
|
|
|
|
endif()
|
2022-07-14 19:43:06 +03:00
|
|
|
|
2022-07-14 20:53:26 +03:00
|
|
|
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()
|
2022-07-14 20:53:26 +03:00
|
|
|
endif()
|
|
|
|
|
2022-07-14 19:43:06 +03:00
|
|
|
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)")
|
2022-08-06 16:17:29 +03:00
|
|
|
endif()
|