1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 08:37:28 +03:00

Merge pull request #571 from clemenswasser/cmake

CMake Port
This commit is contained in:
Rui Ueyama 2022-08-07 10:37:44 +08:00 committed by GitHub
commit d4ccc10ed4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 582 additions and 0 deletions

198
CMakeLists.txt Normal file
View File

@ -0,0 +1,198 @@
cmake_minimum_required(VERSION 3.14)
project(mold VERSION 1.3.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)
# Mold build options
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" ON
MOLD_USE_MIMALLOC OFF)
option(MOLD_USE_SYSTEM_TBB "Use system or vendored TBB" ON)
# Setup mimalloc
if(MOLD_USE_MIMALLOC)
if(MOLD_USE_SYSTEM_MIMALLOC)
find_package(mimalloc REQUIRED)
target_compile_definitions(mimalloc INTERFACE USE_SYSTEM_MIMALLOC)
else()
function(mold_add_mimalloc)
option(MI_BUILD_TESTS "Build test executables" OFF)
add_subdirectory(third-party/mimalloc EXCLUDE_FROM_ALL)
target_compile_definitions(mimalloc PRIVATE MI_USE_ENVIRON=0)
endfunction()
mold_add_mimalloc()
endif()
endif()
# Setup TBB
if(MOLD_USE_SYSTEM_TBB)
find_package(TBB REQUIRED)
else()
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)
endfunction()
mold_add_tbb()
endif()
add_library(mold-wrapper SHARED)
# 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)
add_executable(mold)
target_compile_features(mold PRIVATE cxx_std_20)
target_include_directories(mold SYSTEM PRIVATE third-party)
if(NOT MSVC)
include(CheckLibraryExists)
check_library_exists(m pow "" LIBM_FOUND)
if(LIBM_FOUND)
target_link_libraries(mold PRIVATE m)
endif()
endif()
find_package(ZLIB REQUIRED)
target_link_libraries(mold PRIVATE ZLIB::ZLIB TBB::tbb ${CMAKE_DL_LIBS})
if(MOLD_USE_MIMALLOC)
target_link_libraries(mold PRIVATE mimalloc)
endif()
if(NOT APPLE)
find_package(OpenSSL REQUIRED COMPONENTS Crypto)
target_link_libraries(mold PRIVATE OpenSSL::Crypto)
endif()
if(CMAKE_SYSTEM_PROCESSOR MATCHES riscv64
OR CMAKE_SYSTEM_PROCESSOR MATCHES armv6)
target_link_libraries(mold PRIVATE atomic)
endif()
set_property(SOURCE main.cc elf/lto.cc macho/output-chunks.cc APPEND PROPERTY
COMPILE_DEFINITIONS "MOLD_VERSION=\"${CMAKE_PROJECT_VERSION}\"")
if(EXISTS .git/HEAD)
file(STRINGS .git/HEAD GIT_HASH)
if(GIT_HASH MATCHES "ref: ")
string(REPLACE "ref: " "" GIT_HASH "${GIT_HASH}")
file(STRINGS .git/${GIT_HASH} GIT_HASH)
endif()
set_property(SOURCE main.cc APPEND PROPERTY
COMPILE_DEFINITIONS "GIT_HASH=\"${GIT_HASH}\"")
endif()
target_sources(mold PRIVATE
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-riscv64.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
elf/subprocess.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)
include(CTest)
if(BUILD_TESTING)
# Create the ld and ld64 symlinks required for testing
add_custom_command(
TARGET mold POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Creating ld symlink"
VERBATIM)
add_custom_command(
TARGET mold POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink mold ld64
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Creating ld64 symlink"
VERBATIM)
add_subdirectory(test)
include(ProcessorCount)
ProcessorCount(MOLD_NPROC)
add_custom_target(
check
COMMAND "${CMAKE_COMMAND}" --build "${mold_BINARY_DIR}" --config "\$<CONFIG>" -j "${MOLD_NPROC}"
COMMAND "${CMAKE_CTEST_COMMAND}" -C "\$<CONFIG>" -j "${MOLD_NPROC}"
VERBATIM
USES_TERMINAL)
endif()
if(NOT CMAKE_SKIP_INSTALL_RULES)
install(TARGETS mold)
install(TARGETS mold-wrapper DESTINATION ${CMAKE_INSTALL_LIBDIR}/mold)
install(CODE "\
file(REAL_PATH \${CMAKE_INSTALL_PREFIX} INSTALL_PREFIX)
file(RELATIVE_PATH\
REL_SYMLINK\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/mold\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}/mold)\n\
execute_process(\
COMMAND ${CMAKE_COMMAND} -E make_directory\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}/mold)\n\
execute_process(\
COMMAND ${CMAKE_COMMAND} -E create_symlink\
\${REL_SYMLINK}\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}/mold/ld)\n\
execute_process(\
COMMAND ${CMAKE_COMMAND} -E create_symlink\
mold\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/ld.mold)\n\
execute_process(\
COMMAND ${CMAKE_COMMAND} -E create_symlink\
mold\
\${INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/ld64.mold)")
endif()

30
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,30 @@
function(mold_add_arch_test NAME DIR TRIPLE MACHINE)
set(TEST_NAME "${NAME}_arch_${MACHINE}")
add_test(NAME ${TEST_NAME}
COMMAND ${DIR}/${NAME}.sh
WORKING_DIRECTORY ${mold_BINARY_DIR})
set(TEST_ENV
TEST_CC=${TRIPLE}-gcc
TEST_CXX=${TRIPLE}-g++
TEST_GCC=${TRIPLE}-gcc
TEST_GXX=${TRIPLE}-g++
OBJDUMP=${TRIPLE}-objdump
MACHINE=${MACHINE}
"QEMU=qemu-${MACHINE} -L /usr/${TRIPLE}")
set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${TEST_ENV}")
endfunction()
function(mold_add_arch_tests NAME DIR)
mold_add_arch_test(${NAME} ${DIR} x86_64-linux-gnu x86_64)
mold_add_arch_test(${NAME} ${DIR} i686-linux-gnu i386)
mold_add_arch_test(${NAME} ${DIR} aarch64-linux-gnu aarch64)
mold_add_arch_test(${NAME} ${DIR} arm-linux-gnueabihf arm)
mold_add_arch_test(${NAME} ${DIR} riscv64-linux-gnu riscv64)
endfunction()
if(CMAKE_SYSTEM_NAME MATCHES Linux)
add_subdirectory(elf)
elseif(CMAKE_SYSTEM_NAME MATCHES Darwin)
add_subdirectory(macho)
endif()

258
test/elf/CMakeLists.txt Normal file
View File

@ -0,0 +1,258 @@
set(MOLD_ELF_TESTS
absolute-symbols
allow-multiple-definition
ar-alignment
as-needed2
as-needed
as-needed-weak
auxiliary
basic
bno-symbolic
bsymbolic-functions
bsymbolic
bug178
build-id
canonical-plt
cmdline
color-diagnostics
comment
common-archive
common-ref
common
compress-debug-sections
compressed-debug-info-gnu
compressed-debug-info
copyrel-protected
copyrel-relro
copyrel
dead-debug-sections
debug-macro-section
default-symver
defsym
demangle-rust
demangle
dependency-file
disable-new-dtags
discard
dso-undef
dt-init
dt-needed
duplicate-error
dynamic-dt-debug
dynamic-linker
dynamic-list2
dynamic-list
dynamic
emit-relocs
empty-file
empty-input
empty-version
emulation-deduction
entry
exception-mcmodel-large
exception
exclude-libs2
exclude-libs3
exclude-libs
execstack-if-needed
execstack
export-dynamic
export-from-exe
fatal-warnings
filler
filter
func-addr
gc-sections
gdb-index-compress-output
gdb-index-dwarf2
gdb-index-dwarf3
gdb-index-dwarf4
gdb-index-dwarf5
gdb-index-empty
glibc-2.22-bug
gnu-hash
gnu-linkonce
gnu-retain
gnu-unique
gnu-warning
hello-dynamic
hello-static
help
hidden-undef
icf
icf-small
ifunc-dso
ifunc-dynamic
ifunc-export
ifunc-static-pie
ifunc-static
image-base
incompatible-libs2
incompatible-libs
incompatible-obj
init-array-priorities
init-array-readonly
init-array
initfirst
init-in-dso
init
interpose
invalid-version-script
large-alignment-dso
large-alignment
linker-script2
linker-script3
linker-script4
linker-script
link-order
lto-archive
lto-dso
lto-gcc
lto-llvm
lto-version-script
many-sections
mergeable-records
mergeable-strings
missing-but-ok
missing-error
mold-wrapper2
mold-wrapper
nocopyreloc
noinhibit-exec
non-canonical-plt
no-quick-exit
nostdlib
note2
note-property
note
now
oformat-binary
omagic
package-metadata
pack-dyn-relocs-relr
pie
plt-dso
pltgot
plt
preinit-array
print-dependencies
protected-dynsym
protected
push-pop-state
relax
relocatable-archive
relocatable
reloc-overflow
reloc-rodata
reloc
reloc-zero
relro
repro
require-defined
response-file
retain-symbols-file
reverse-sections
rodata-name
rosegment
rpath
run-clang
run
section-alignment
section-name
section-start
shared
shuffle-sections-seed
shuffle-sections
soname
start-lib
start-stop-symbol
static-archive
static-pie
stdout
strip
symbol-rank
symbol-version2
symbol-version3
symbol-version
symtab-dso
symtab-section-symbols
symtab
synthetic-symbols
sysroot2
sysroot-linker-script
sysroot
thin-archive
thread-count
tls-common
tlsdesc-import
tlsdesc
tlsdesc-static
tls-dso
tls-gd2
tls-gd-mcmodel-large
tls-gd-noplt
tls-gd
tls-ie
tls-large-tbss
tls-ld-mcmodel-large
tls-ld-noplt
tls-ld
tls-le
tls-module-base
tls-nopic
tls-pic
trace
trace-symbol
undefined
unique
unresolved-symbols
verbose
versioned-undef
version-script10
version-script11
version-script12
version-script13
version-script14
version-script15
version-script2
version-script3
version-script4
version-script5
version-script6
version-script7
version-script8
version-script9
version-script
version
visibility
warn-common
warn-execstack
warn-once
warn-shared-textrel
warn-textrel
warn-unresolved-symbols
weak-export-dso
weak-export-exe
weak-undef
whole-archive
wrap
z-cet-report
z-defs
z-ibtplt
z-ibt
z-max-page-size
z-nodefaultlib
z-nodump
z-now
z-origin
z-separate-code
z-shstk
z-text
z-unknown)
foreach(MOLD_ELF_TEST IN LISTS MOLD_ELF_TESTS)
add_test(NAME "${MOLD_ELF_TEST}_host"
COMMAND "${CMAKE_CURRENT_LIST_DIR}/${MOLD_ELF_TEST}.sh"
WORKING_DIRECTORY ${mold_BINARY_DIR})
mold_add_arch_tests(${MOLD_ELF_TEST} ${CMAKE_CURRENT_LIST_DIR})
endforeach()

96
test/macho/CMakeLists.txt Normal file
View File

@ -0,0 +1,96 @@
set(MOLD_ELF_TESTS
add-ast-path
add-empty-section
adhoc-codesign
all-load
application-extension
archive
baserel
basic
bss
bundle
comdat
common-alignment
common
cstring
data-reloc
dead-strip-dylibs2
dead-strip-dylibs
dead-strip
dependency-info
dlinfo
duplicate-error
dylib
entry
exception
export-dynamic
exported-symbols-list
filepath2
filepath
force-load
framework
headerpad-max-install-names
headerpad
hello2
hello3
hello4
hello5
hello
hidden-l
install-name
lc-build-version
lc-linker-option
lib1
libunwind
lto
macos-version-min
map
merge-scope
missing-error
needed-framework
needed-l
objc
object-path-lto
order-file
pagezero-size2
pagezero-size3
pagezero-size
platform-version
private-extern
reexport-l
reproducibility
response-file
rpath
search-dylibs-first
search-paths-first
sectcreate
stack-size
subsections-via-symbols
syslibroot
tbd-add
tbd-hide
tbd-install-name
tbd-previous
tbd-reexport
tbd
tls2
tls
undef
unexported-symbols-list
universal
unkown-tbd-target
U
uuid2
uuid
weak-def-dylib
weak-def-ref
weak-def
weak-l
weak-undef
Z)
foreach(MOLD_MACHO_TEST IN LISTS MOLD_MACHO_TESTS)
add_test(NAME ${MOLD_MACHO_TEST}
COMMAND ${CMAKE_CURRENT_LIST_DIR}/${MOLD_MACHO_TEST}.sh)
mold_add_arch_tests(${MOLD_MACHO_TEST} ${CMAKE_CURRENT_LIST_DIR})
endforeach()