2022-10-13 16:07:52 +03:00
|
|
|
# You can customize a build by specifying CMake options. An option may be
|
2022-10-13 09:44:23 +03:00
|
|
|
# given in the -Dvariable=value form. For a boolean variable, `ON` or `1`
|
|
|
|
# means true while `OFF` or `0` means false.
|
|
|
|
#
|
|
|
|
# Here are a couple of common cmake options:
|
|
|
|
#
|
|
|
|
# -DCMAKE_C_COMPILER=<command-name>
|
|
|
|
#
|
2022-10-13 11:29:06 +03:00
|
|
|
# Specifies a C compiler name to use. The default value is `cc`.
|
2022-10-13 09:44:23 +03:00
|
|
|
#
|
|
|
|
# -DCMAKE_CXX_COMPILER=<command-name>
|
|
|
|
#
|
2022-10-13 11:29:06 +03:00
|
|
|
# Specifies a C++ compiler name to use. The default value is `c++`.
|
2022-10-13 09:44:23 +03:00
|
|
|
#
|
|
|
|
# -DCMAKE_INSTALL_PREFIX=<directory>
|
|
|
|
#
|
2022-10-13 16:07:52 +03:00
|
|
|
# Specifies an install target directory. The default value is `/usr/local`.
|
2022-10-13 09:44:23 +03:00
|
|
|
#
|
|
|
|
# -DCMAKE_BUILD_TYPE=[Debug | Release | RelWithDebInfo | MinSizeRel]
|
|
|
|
#
|
|
|
|
# Specifies a build type. The default is `Release` which is the right
|
|
|
|
# option unless you are debugging mold.
|
|
|
|
#
|
|
|
|
# An example of a cmake command line is shown below:
|
|
|
|
#
|
|
|
|
# $ cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_CXX_COMPILER=g++-12 ..
|
|
|
|
#
|
|
|
|
# where `..` refers this directory.
|
|
|
|
#
|
2022-10-13 16:07:52 +03:00
|
|
|
# With cmake, you may run `cmake --install .` instead of `make install` to
|
|
|
|
# install build artifacts to system directories. If you want to install
|
|
|
|
# artifacts to a temporary target directory, run `cmake --install . --prefix
|
|
|
|
# <dir>`.
|
|
|
|
#
|
2022-10-13 11:29:06 +03:00
|
|
|
# You can see the current cmake variables and their values by running
|
|
|
|
# `cmake -N -L .` in a build directory.
|
2022-10-13 09:44:23 +03:00
|
|
|
#
|
|
|
|
# Note that in this file, we provide various dials and knobs to configure
|
|
|
|
# how to build mold. However, as a policy, we do not provide a way to
|
|
|
|
# enable/disable any individual mold's feature. In other words, we do not
|
2022-10-13 16:07:52 +03:00
|
|
|
# provide options like `--enable-foo` or `--disable-foo`. The motivation
|
|
|
|
# behind it is build reproducibility. We want to guarantees that all builds
|
|
|
|
# of the mold linker of the same version will have the exactly same set of
|
|
|
|
# features and behave exactly the same.
|
2022-10-13 09:44:23 +03:00
|
|
|
|
2022-09-24 08:26:38 +03:00
|
|
|
cmake_minimum_required(VERSION 3.13)
|
2023-07-26 13:40:11 +03:00
|
|
|
project(mold VERSION 2.0.0)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
|
|
|
include(CMakeDependentOption)
|
|
|
|
include(GNUInstallDirs)
|
|
|
|
|
2022-11-01 04:09:54 +03:00
|
|
|
# Build mold itself using mold if -DMOLD_USE_MOLD=ON
|
2022-09-24 08:26:38 +03:00
|
|
|
option(MOLD_USE_MOLD "Use mold to build mold" OFF)
|
2022-08-08 07:07:47 +03:00
|
|
|
if(MOLD_USE_MOLD)
|
2023-01-09 07:46:01 +03:00
|
|
|
add_link_options(-fuse-ld=mold -Wl,--gdb-index)
|
2022-08-08 07:07:47 +03:00
|
|
|
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-14 08:12:45 +03:00
|
|
|
|
2022-08-20 11:07:14 +03:00
|
|
|
if(NOT "${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC")
|
2022-08-14 08:12:45 +03:00
|
|
|
target_compile_options(mold PRIVATE
|
|
|
|
-fno-exceptions
|
|
|
|
-fno-unwind-tables
|
2022-09-14 10:11:28 +03:00
|
|
|
-fno-asynchronous-unwind-tables
|
|
|
|
-Wno-sign-compare
|
2023-01-07 12:26:04 +03:00
|
|
|
-Wno-unused-function
|
|
|
|
-ggnu-pubnames)
|
2022-08-10 23:48:24 +03:00
|
|
|
endif()
|
2022-08-06 16:17:29 +03:00
|
|
|
|
2023-01-21 07:00:05 +03:00
|
|
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|
|
|
target_compile_options(mold PRIVATE -D_GLIBCXX_ASSERTIONS)
|
|
|
|
endif()
|
|
|
|
|
2023-06-13 06:10:03 +03:00
|
|
|
if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
|
|
|
|
set(OPENBSD ON)
|
|
|
|
endif()
|
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# Build mold with -flto if -DMOLD_LTO=ON
|
2022-08-08 08:30:14 +03:00
|
|
|
option(MOLD_LTO "Build mold with link-time optimization enabled")
|
|
|
|
if(MOLD_LTO)
|
|
|
|
set_property(TARGET mold PROPERTY INTERPROCEDURAL_OPTIMIZATION ON)
|
|
|
|
endif()
|
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# Enable AddressSanitizer if -DMOLD_USE_ASAN=ON
|
2022-08-08 07:30:56 +03:00
|
|
|
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-10-13 09:44:23 +03:00
|
|
|
# Enabled ThreadSanitizer if -DMOLD_USE_TSAN=ON
|
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-12-07 08:38:22 +03:00
|
|
|
# Statically-link libstdc++ and libcrypto if -DMOLD_MOSTLY_STATIC=ON.
|
2022-10-13 09:44:23 +03:00
|
|
|
#
|
|
|
|
# This option is intended to be used by `./dist.sh` script to create a
|
|
|
|
# mold binary that works on various Linux distros. You probably don't
|
|
|
|
# need nor want to set this to ON.
|
2022-08-08 12:11:32 +03:00
|
|
|
option(MOLD_MOSTLY_STATIC "Statically link libstdc++ and libcrypto" OFF)
|
|
|
|
if(MOLD_MOSTLY_STATIC)
|
2022-12-07 08:38:22 +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-10-13 09:44:23 +03:00
|
|
|
# Find zlib. If libz.so is not found, we compile a bundled one and
|
|
|
|
# statically-link it to mold.
|
2022-08-08 08:01:52 +03:00
|
|
|
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-14 08:12:16 +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
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# Find zstd compression library. Just like zlib, if libzstd.so is not
|
|
|
|
# found, we compile a bundled one and statically-link it to mold.
|
2022-09-13 11:21:22 +03:00
|
|
|
include(CheckIncludeFile)
|
|
|
|
check_include_file(zstd.h HAVE_ZSTD_H)
|
|
|
|
|
|
|
|
if(HAVE_ZSTD_H)
|
|
|
|
target_link_libraries(mold PRIVATE zstd)
|
2022-09-13 09:40:11 +03:00
|
|
|
else()
|
|
|
|
add_subdirectory(third-party/zstd/build/cmake EXCLUDE_FROM_ALL)
|
|
|
|
target_compile_definitions(libzstd_static PRIVATE
|
|
|
|
ZSTD_BUILD_STATIC=1
|
|
|
|
ZSTD_BUILD_SHARED=0
|
|
|
|
ZSTD_BUILD_PROGRAMS=0
|
|
|
|
ZSTD_MULTITHREAD_SUPPORT=0
|
|
|
|
ZSTD_BUILD_TESTS=0)
|
2022-09-13 10:09:14 +03:00
|
|
|
target_include_directories(mold PUBLIC third-party/zstd/lib)
|
2022-09-13 09:40:11 +03:00
|
|
|
target_link_libraries(mold PRIVATE libzstd_static)
|
|
|
|
endif()
|
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# Find mimalloc. mimalloc is an alternative malloc implementation
|
|
|
|
# optimized for multi-threaded applications.
|
|
|
|
#
|
2022-10-13 16:07:52 +03:00
|
|
|
# If you want to use the usual libc's malloc, pass -DMOLD_USE_MIMALLOC=OFF.
|
2022-11-06 10:55:23 +03:00
|
|
|
#
|
|
|
|
# We enable mimalloc by default for 64-bit targets. It doesn't seem to
|
|
|
|
# be stable on 32-bit targets.
|
|
|
|
include(CheckCXXSourceCompiles)
|
|
|
|
check_cxx_source_compiles(
|
|
|
|
"static_assert(sizeof(void *) == 4); int main() {}" P32)
|
2022-09-21 06:05:34 +03:00
|
|
|
|
2022-08-08 07:30:56 +03:00
|
|
|
cmake_dependent_option(MOLD_USE_MIMALLOC "Use mimalloc" ON
|
2023-06-13 06:10:03 +03:00
|
|
|
"NOT APPLE; NOT ANDROID; NOT OPENBSD; NOT P32" OFF)
|
2022-08-08 07:30:56 +03:00
|
|
|
cmake_dependent_option(
|
|
|
|
MOLD_USE_SYSTEM_MIMALLOC "Use system or vendored mimalloc" OFF
|
|
|
|
MOLD_USE_MIMALLOC OFF)
|
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# By default, we build a bundled mimalloc and statically-link it to
|
|
|
|
# mold. If you want to dynamically link to the system's
|
|
|
|
# libmimalloc.so, pass -DMOLD_USE_SYSTEM_MIMALLOC=ON.
|
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_link_libraries(mold PRIVATE mimalloc)
|
2022-09-02 19:20:37 +03:00
|
|
|
target_compile_definitions(mold PRIVATE USE_SYSTEM_MIMALLOC)
|
2022-08-08 06:04:08 +03:00
|
|
|
else()
|
2022-08-19 14:26:06 +03:00
|
|
|
function(mold_add_mimalloc)
|
2022-11-21 20:03:08 +03:00
|
|
|
set(MI_BUILD_STATIC ON CACHE INTERNAL "")
|
|
|
|
set(MI_BUILD_TESTS OFF CACHE INTERNAL "")
|
2022-08-19 14:26:06 +03:00
|
|
|
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()
|
2022-08-08 06:04:08 +03:00
|
|
|
endif()
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
2022-10-13 09:44:23 +03:00
|
|
|
# Find TBB. TBB (OneTBB or Intel TBB) is a high-level threading library.
|
|
|
|
# Use of this library is mandatory.
|
|
|
|
#
|
|
|
|
# By default, we build a bundled one and statically-link the library
|
|
|
|
# to mold. If you want to link to the system's libtbb2.so, pass
|
|
|
|
# -DMOLD_USE_SYSTEM_TBB=ON.
|
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-12-12 11:00:59 +03:00
|
|
|
# Check if this is a commercial version of mold (a.k.a. "sold")
|
|
|
|
if(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE.md")
|
|
|
|
set(MOLD_IS_SOLD ON)
|
|
|
|
endif()
|
|
|
|
|
2022-11-01 04:09:54 +03:00
|
|
|
# We always use Clang to build mold on Windows. MSVC can't compile mold.
|
2022-08-14 08:12:16 +03:00
|
|
|
if(WIN32)
|
2023-01-09 21:56:05 +03:00
|
|
|
if(MSVC AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
2022-08-16 07:02:53 +03:00
|
|
|
message(FATAL_ERROR
|
|
|
|
"Your compiler is not supported; install Clang from Visual Studio Installer and re-run cmake with '-T clangcl'")
|
2022-08-20 11:07:14 +03:00
|
|
|
endif()
|
2022-08-16 07:02:53 +03:00
|
|
|
|
2022-08-14 08:12:16 +03:00
|
|
|
target_compile_definitions(mold PRIVATE NOGDI NOMINMAX)
|
2023-01-09 21:56:05 +03:00
|
|
|
if(MINGW)
|
|
|
|
target_compile_definitions(mold PRIVATE _WIN32_WINNT=0xA00)
|
|
|
|
target_link_libraries(mold PRIVATE bcrypt)
|
|
|
|
endif()
|
2022-08-14 08:12:16 +03:00
|
|
|
else()
|
2022-08-08 06:04:08 +03:00
|
|
|
include(CheckLibraryExists)
|
|
|
|
check_library_exists(m pow "" LIBM_FOUND)
|
|
|
|
if(LIBM_FOUND)
|
|
|
|
target_link_libraries(mold PRIVATE m)
|
|
|
|
endif()
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
2022-11-01 04:09:54 +03:00
|
|
|
# Build mold-wrapper.so
|
2022-08-17 11:17:50 +03:00
|
|
|
if(NOT APPLE AND 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)
|
|
|
|
endif()
|
|
|
|
|
2022-08-20 11:07:14 +03:00
|
|
|
if(NOT APPLE AND NOT WIN32 AND NOT MOLD_MOSTLY_STATIC)
|
2022-08-10 23:48:24 +03:00
|
|
|
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()
|
|
|
|
|
2022-10-31 16:21:45 +03:00
|
|
|
# If atomics doesn't work by default, add -latomic.
|
|
|
|
# We need the flag on riscv, armv6 and m68k.
|
2022-11-01 04:09:54 +03:00
|
|
|
include(CheckCXXSourceCompiles)
|
2022-11-27 11:16:53 +03:00
|
|
|
check_cxx_source_compiles("#include <atomic>
|
2022-10-31 16:21:45 +03:00
|
|
|
int main() {
|
2022-12-25 03:30:55 +03:00
|
|
|
std::atomic_uint8_t a;
|
|
|
|
std::atomic_uint16_t b;
|
|
|
|
std::atomic_uint32_t c;
|
|
|
|
std::atomic_uint64_t d;
|
|
|
|
return ++a + ++b + ++c + ++d;
|
2022-11-27 11:16:53 +03:00
|
|
|
}" HAVE_FULL_ATOMIC_SUPPORT)
|
|
|
|
|
|
|
|
if(NOT HAVE_FULL_ATOMIC_SUPPORT)
|
2022-08-08 06:04:08 +03:00
|
|
|
target_link_libraries(mold PRIVATE atomic)
|
2022-07-14 19:42:02 +03:00
|
|
|
endif()
|
|
|
|
|
2022-12-25 06:47:30 +03:00
|
|
|
# Add -pthread
|
2023-01-09 21:56:05 +03:00
|
|
|
if(NOT APPLE AND NOT MSVC)
|
2022-12-25 06:47:30 +03:00
|
|
|
target_compile_options(mold PRIVATE -pthread)
|
|
|
|
target_link_options(mold PRIVATE -pthread)
|
|
|
|
endif()
|
|
|
|
|
2022-09-23 10:36:59 +03:00
|
|
|
# Create a .cc file containing the current git hash for `mold --version`.
|
2022-08-20 07:16:21 +03:00
|
|
|
add_custom_target(git_hash
|
|
|
|
COMMAND ${CMAKE_COMMAND}
|
2022-08-20 08:27:42 +03:00
|
|
|
-DSOURCE_DIR=${CMAKE_SOURCE_DIR}
|
2022-08-20 07:16:21 +03:00
|
|
|
-DOUTPUT_FILE=${CMAKE_BINARY_DIR}/git-hash.cc
|
2023-01-16 10:35:24 +03:00
|
|
|
-P ${CMAKE_SOURCE_DIR}/common/update-git-hash.cmake
|
|
|
|
DEPENDS common/update-git-hash.cmake
|
2022-08-17 11:38:13 +03:00
|
|
|
BYPRODUCTS git-hash.cc
|
|
|
|
VERBATIM)
|
|
|
|
|
2022-08-20 07:16:21 +03:00
|
|
|
add_dependencies(mold git_hash)
|
2022-07-14 19:42:02 +03:00
|
|
|
|
2022-10-22 09:30:12 +03:00
|
|
|
# Create config.h file
|
2023-01-16 10:35:24 +03:00
|
|
|
configure_file(common/config.h.in config.h)
|
2022-10-22 09:30:12 +03:00
|
|
|
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
|
|
|
|
2022-09-23 10:36:59 +03:00
|
|
|
# Almost all functions are template in mold which take a target type
|
|
|
|
# (e.g. X86_64) as its type parameter. Since we suport more than 10
|
|
|
|
# targets, compiling a single source file for all the targets is very
|
|
|
|
# slow.
|
|
|
|
#
|
|
|
|
# As a workaround, we create a .cc file for each target and spawn many
|
2022-11-01 04:09:54 +03:00
|
|
|
# compiler instances. This is hacky but greatly reduces compile time
|
|
|
|
# on a multicore machine.
|
2022-09-23 10:36:59 +03:00
|
|
|
list(APPEND MOLD_ELF_TARGETS
|
2023-07-07 12:19:35 +03:00
|
|
|
X86_64 I386 ARM64 ARM32 RV32LE RV32BE RV64LE RV64BE PPC32 PPC64V1 PPC64V2
|
2023-07-30 22:49:24 +03:00
|
|
|
S390X SPARC64 M68K SH4 ALPHA MIPS64LE MIPS64BE LOONGARCH32 LOONGARCH64)
|
2022-09-23 10:36:59 +03:00
|
|
|
|
|
|
|
list(APPEND MOLD_ELF_TEMPLATE_FILES
|
2023-08-10 19:14:24 +03:00
|
|
|
elf/arch-loongarch.cc
|
|
|
|
elf/arch-riscv.cc
|
2022-08-08 06:04:08 +03:00
|
|
|
elf/cmdline.cc
|
|
|
|
elf/dwarf.cc
|
|
|
|
elf/gc-sections.cc
|
|
|
|
elf/icf.cc
|
|
|
|
elf/input-files.cc
|
|
|
|
elf/input-sections.cc
|
2023-03-17 09:32:12 +03:00
|
|
|
elf/jobs.cc
|
2022-08-08 06:04:08 +03:00
|
|
|
elf/linker-script.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
elf/lto.cc
|
2023-03-13 11:23:36 +03:00
|
|
|
elf/main.cc
|
2022-08-08 06:04:08 +03:00
|
|
|
elf/mapfile.cc
|
|
|
|
elf/output-chunks.cc
|
|
|
|
elf/passes.cc
|
|
|
|
elf/relocatable.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
elf/subprocess.cc
|
2022-09-01 10:32:09 +03:00
|
|
|
elf/thunks.cc
|
2023-01-07 13:24:29 +03:00
|
|
|
elf/tls.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
list(APPEND MOLD_MACHO_TARGETS X86_64 ARM64)
|
|
|
|
|
|
|
|
list(APPEND MOLD_MACHO_TEMPLATE_FILES
|
2022-08-08 06:04:08 +03:00
|
|
|
macho/cmdline.cc
|
|
|
|
macho/dead-strip.cc
|
|
|
|
macho/input-files.cc
|
|
|
|
macho/input-sections.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
macho/lto.cc
|
2023-03-13 11:23:36 +03:00
|
|
|
macho/main.cc
|
2022-08-08 06:04:08 +03:00
|
|
|
macho/mapfile.cc
|
|
|
|
macho/output-chunks.cc
|
|
|
|
macho/tapi.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
function(mold_instantiate_templates SOURCE TARGET)
|
|
|
|
set(PATH ${CMAKE_BINARY_DIR}/${SOURCE}.${TARGET}.cc)
|
|
|
|
file(WRITE ${PATH} "#define MOLD_${TARGET} 1
|
|
|
|
#define MOLD_TARGET ${TARGET}
|
|
|
|
#include \"${CMAKE_SOURCE_DIR}/${SOURCE}\"
|
|
|
|
")
|
|
|
|
target_sources(mold PRIVATE ${PATH})
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
foreach (SOURCE IN LISTS MOLD_ELF_TEMPLATE_FILES)
|
|
|
|
foreach(TARGET IN LISTS MOLD_ELF_TARGETS)
|
|
|
|
mold_instantiate_templates(${SOURCE} ${TARGET})
|
|
|
|
endforeach()
|
|
|
|
endforeach()
|
|
|
|
|
2022-12-12 11:11:45 +03:00
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
foreach (SOURCE IN LISTS MOLD_MACHO_TEMPLATE_FILES)
|
|
|
|
foreach(TARGET IN LISTS MOLD_MACHO_TARGETS)
|
|
|
|
mold_instantiate_templates(${SOURCE} ${TARGET})
|
|
|
|
endforeach()
|
2022-09-23 10:36:59 +03:00
|
|
|
endforeach()
|
2022-12-12 11:11:45 +03:00
|
|
|
endif()
|
2022-09-23 10:36:59 +03:00
|
|
|
|
|
|
|
# Add other non-template source files.
|
|
|
|
target_sources(mold PRIVATE
|
2023-01-16 10:26:52 +03:00
|
|
|
common/compress.cc
|
|
|
|
common/demangle.cc
|
|
|
|
common/filepath.cc
|
|
|
|
common/glob.cc
|
|
|
|
common/hyperloglog.cc
|
|
|
|
common/main.cc
|
|
|
|
common/multi-glob.cc
|
|
|
|
common/perf.cc
|
|
|
|
common/tar.cc
|
|
|
|
common/uuid.cc
|
2022-12-27 08:47:53 +03:00
|
|
|
elf/arch-alpha.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
elf/arch-arm32.cc
|
|
|
|
elf/arch-arm64.cc
|
|
|
|
elf/arch-i386.cc
|
2022-10-30 06:06:15 +03:00
|
|
|
elf/arch-m68k.cc
|
2023-07-07 12:19:35 +03:00
|
|
|
elf/arch-mips64.cc
|
2022-10-15 10:54:42 +03:00
|
|
|
elf/arch-ppc32.cc
|
2022-10-08 05:33:56 +03:00
|
|
|
elf/arch-ppc64v1.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
elf/arch-ppc64v2.cc
|
2022-10-06 04:10:28 +03:00
|
|
|
elf/arch-s390x.cc
|
2022-12-26 02:45:42 +03:00
|
|
|
elf/arch-sh4.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
elf/arch-sparc64.cc
|
|
|
|
elf/arch-x86-64.cc
|
2022-12-29 12:39:39 +03:00
|
|
|
elf/elf.cc
|
2022-09-23 10:36:59 +03:00
|
|
|
git-hash.cc
|
2022-08-08 15:01:32 +03:00
|
|
|
third-party/rust-demangle/rust-demangle.c
|
2022-08-14 08:45:25 +03:00
|
|
|
)
|
2022-08-10 23:48:24 +03:00
|
|
|
|
2022-12-12 11:11:45 +03:00
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
target_sources(mold PRIVATE
|
|
|
|
macho/arch-arm64.cc
|
|
|
|
macho/arch-x86-64.cc
|
|
|
|
macho/yaml.cc
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2022-10-17 11:58:42 +03:00
|
|
|
# Add frequently included header files for pre-compiling.
|
2022-10-22 09:30:12 +03:00
|
|
|
# target_precompile_headers is supported by CMake 3.16.0 or newer.
|
|
|
|
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.16.0")
|
2022-12-12 11:11:45 +03:00
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
target_precompile_headers(mold PRIVATE
|
|
|
|
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/elf/mold.h>"
|
|
|
|
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/macho/mold.h>")
|
|
|
|
else()
|
|
|
|
target_precompile_headers(mold PRIVATE
|
|
|
|
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_SOURCE_DIR}/elf/mold.h>")
|
|
|
|
endif()
|
2022-11-05 07:41:21 +03:00
|
|
|
|
|
|
|
# ccache needs this flag along with `sloppiness = pch_defines,time_macros`
|
|
|
|
# to enable caching
|
|
|
|
target_compile_options(mold PRIVATE -fpch-preprocess)
|
2022-10-17 11:58:42 +03:00
|
|
|
endif()
|
|
|
|
|
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
|
2022-08-13 11:54:38 +03:00
|
|
|
if(NOT WIN32)
|
|
|
|
add_custom_command(
|
|
|
|
TARGET mold POST_BUILD
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
|
2022-12-12 11:11:45 +03:00
|
|
|
BYPRODUCTS ld
|
2022-08-13 11:54:38 +03:00
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
|
|
|
VERBATIM)
|
2022-12-12 11:11:45 +03:00
|
|
|
|
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
add_custom_command(
|
|
|
|
TARGET mold POST_BUILD
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld64
|
|
|
|
BYPRODUCTS ld64
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
|
|
|
VERBATIM)
|
|
|
|
endif()
|
2022-08-13 11:54:38 +03:00
|
|
|
endif()
|
2022-08-08 06:04:08 +03:00
|
|
|
|
|
|
|
if(${APPLE})
|
2022-12-12 11:11:45 +03:00
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
add_subdirectory(test/macho)
|
|
|
|
endif()
|
2022-08-08 06:04:08 +03:00
|
|
|
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-09-29 12:49:57 +03:00
|
|
|
install(TARGETS mold RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
2022-08-13 08:20:32 +03:00
|
|
|
install(FILES docs/mold.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1/)
|
Respect prefix set by `cmake --install`
Currently, `CMAKE_INSTALL_FULL_{LIBEXEC,BIN,MAN}DIR`
are used for symlink creation. The problem with
that is it only works when the prefix is not
overridden by `cmake --install <build-dir> --prefix <prefix>`,
because their values are evaluated and hard-coded
into `cmake_install.cmake` during configuration
time, rather than installation time.
While it would be nice if we can get their
overridden values during installation, only
`CMAKE_INSTALL_PREFIX` reflects the overriden
value. (See "INSTALL A PROJECT", cmake(1)).
Therefore, the logic for symlink creation is
rewritten and extracted into a function named
`mold_install_symlink`. Now only `CMAKE_INSTALL_PREFIX`
is used during installation.
A new problem with this change is, if environment
variable `DESTDIR` is not set and `CMAKE_INSTALL_PREFIX`
is set to a relative path (which is perfectly
valid), a slash `/` would be incorrectly
prepended. To avoid that, and maintain
consistency with CMake's behaviour, the install
prefix is first turned into absolute path, stored
in `PREFIX_ABS`.
By the way, it seems okay to use the
`MAKE_DIRECTORY` and `CREATE_LINK` subcommands
under command `file`. So, it shouldn't be needed
to invoke a subprocess.
Signed-off-by: Zhong Ruoyu <zhongruoyu@outlook.com>
2022-11-01 21:42:16 +03:00
|
|
|
|
2023-01-07 09:28:08 +03:00
|
|
|
if(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE")
|
|
|
|
install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
|
|
|
endif()
|
2023-07-27 03:10:22 +03:00
|
|
|
if(EXISTS "${CMAKE_SOURCE_DIR}/LICENSE.third-party")
|
|
|
|
install(FILES "LICENSE.third-party" DESTINATION "${CMAKE_INSTALL_DOCDIR}")
|
|
|
|
endif()
|
2023-01-07 09:28:08 +03:00
|
|
|
|
2022-11-02 13:00:14 +03:00
|
|
|
function(mold_install_relative_symlink OLD NEW)
|
Respect prefix set by `cmake --install`
Currently, `CMAKE_INSTALL_FULL_{LIBEXEC,BIN,MAN}DIR`
are used for symlink creation. The problem with
that is it only works when the prefix is not
overridden by `cmake --install <build-dir> --prefix <prefix>`,
because their values are evaluated and hard-coded
into `cmake_install.cmake` during configuration
time, rather than installation time.
While it would be nice if we can get their
overridden values during installation, only
`CMAKE_INSTALL_PREFIX` reflects the overriden
value. (See "INSTALL A PROJECT", cmake(1)).
Therefore, the logic for symlink creation is
rewritten and extracted into a function named
`mold_install_symlink`. Now only `CMAKE_INSTALL_PREFIX`
is used during installation.
A new problem with this change is, if environment
variable `DESTDIR` is not set and `CMAKE_INSTALL_PREFIX`
is set to a relative path (which is perfectly
valid), a slash `/` would be incorrectly
prepended. To avoid that, and maintain
consistency with CMake's behaviour, the install
prefix is first turned into absolute path, stored
in `PREFIX_ABS`.
By the way, it seems okay to use the
`MAKE_DIRECTORY` and `CREATE_LINK` subcommands
under command `file`. So, it shouldn't be needed
to invoke a subprocess.
Signed-off-by: Zhong Ruoyu <zhongruoyu@outlook.com>
2022-11-01 21:42:16 +03:00
|
|
|
install(CODE "
|
2022-11-17 07:24:44 +03:00
|
|
|
get_filename_component(PREFIX_ABS \${CMAKE_INSTALL_PREFIX}/ ABSOLUTE)
|
2022-11-02 13:00:14 +03:00
|
|
|
get_filename_component(OLD_ABS ${OLD} ABSOLUTE BASE_DIR \${PREFIX_ABS})
|
|
|
|
get_filename_component(NEW_ABS ${NEW} ABSOLUTE BASE_DIR \${PREFIX_ABS})
|
|
|
|
get_filename_component(NEW_DIR \${NEW_ABS} DIRECTORY)
|
|
|
|
file(RELATIVE_PATH OLD_REL \${NEW_DIR} \${OLD_ABS})
|
2022-11-17 07:25:41 +03:00
|
|
|
message(STATUS \"Installing symlink: \$ENV{DESTDIR}\${NEW_ABS} -> \${OLD_REL}\")
|
|
|
|
file(MAKE_DIRECTORY \$ENV{DESTDIR}\${NEW_DIR})
|
|
|
|
file(CREATE_LINK \${OLD_REL} \$ENV{DESTDIR}\${NEW_ABS} SYMBOLIC)")
|
Respect prefix set by `cmake --install`
Currently, `CMAKE_INSTALL_FULL_{LIBEXEC,BIN,MAN}DIR`
are used for symlink creation. The problem with
that is it only works when the prefix is not
overridden by `cmake --install <build-dir> --prefix <prefix>`,
because their values are evaluated and hard-coded
into `cmake_install.cmake` during configuration
time, rather than installation time.
While it would be nice if we can get their
overridden values during installation, only
`CMAKE_INSTALL_PREFIX` reflects the overriden
value. (See "INSTALL A PROJECT", cmake(1)).
Therefore, the logic for symlink creation is
rewritten and extracted into a function named
`mold_install_symlink`. Now only `CMAKE_INSTALL_PREFIX`
is used during installation.
A new problem with this change is, if environment
variable `DESTDIR` is not set and `CMAKE_INSTALL_PREFIX`
is set to a relative path (which is perfectly
valid), a slash `/` would be incorrectly
prepended. To avoid that, and maintain
consistency with CMake's behaviour, the install
prefix is first turned into absolute path, stored
in `PREFIX_ABS`.
By the way, it seems okay to use the
`MAKE_DIRECTORY` and `CREATE_LINK` subcommands
under command `file`. So, it shouldn't be needed
to invoke a subprocess.
Signed-off-by: Zhong Ruoyu <zhongruoyu@outlook.com>
2022-11-01 21:42:16 +03:00
|
|
|
endfunction()
|
2022-11-18 12:00:01 +03:00
|
|
|
|
2023-01-09 21:56:05 +03:00
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold${CMAKE_EXECUTABLE_SUFFIX}
|
|
|
|
${CMAKE_INSTALL_LIBEXECDIR}/mold/ld${CMAKE_EXECUTABLE_SUFFIX})
|
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold${CMAKE_EXECUTABLE_SUFFIX}
|
|
|
|
${CMAKE_INSTALL_BINDIR}/ld.mold${CMAKE_EXECUTABLE_SUFFIX})
|
2022-11-02 13:00:14 +03:00
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_MANDIR}/man1/mold.1
|
|
|
|
${CMAKE_INSTALL_MANDIR}/man1/ld.mold.1)
|
2022-11-18 12:00:01 +03:00
|
|
|
|
2022-12-12 11:11:45 +03:00
|
|
|
if(MOLD_IS_SOLD)
|
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold
|
|
|
|
${CMAKE_INSTALL_BINDIR}/ld64.mold)
|
2022-11-18 12:00:01 +03:00
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold
|
2022-12-12 16:25:32 +03:00
|
|
|
${CMAKE_INSTALL_BINDIR}/ld.sold)
|
2022-12-12 16:27:25 +03:00
|
|
|
mold_install_relative_symlink(${CMAKE_INSTALL_BINDIR}/mold
|
|
|
|
${CMAKE_INSTALL_BINDIR}/ld64.sold)
|
2022-11-18 12:00:01 +03:00
|
|
|
endif()
|
2022-08-06 16:17:29 +03:00
|
|
|
endif()
|