Skip to content

Commit

Permalink
Fix cmake build
Browse files Browse the repository at this point in the history
You can now build mold with the following commands:

  $ mkdir -p out/debug
  $ cd out/debug
  $ cmake -GNinja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug ../..
  $ ninja

To run tests, use the following commands:

  $ cd out/debug
  $ ctest -j$(nproc)
  • Loading branch information
rui314 committed Jul 19, 2021
1 parent 4d81fdd commit fe26dad
Show file tree
Hide file tree
Showing 152 changed files with 530 additions and 434 deletions.
216 changes: 81 additions & 135 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
cmake_minimum_required(VERSION 3.13)

project(mold
LANGUAGES C CXX
VERSION 0.1.1)

###############################################################################
# Configuration options
###############################################################################
LANGUAGES C CXX
VERSION 0.9.2)

option(MOLD_ASAN "Use address sanitizer." OFF)
option(MOLD_TSAN "Use thread sanitizer." OFF)
Expand All @@ -25,17 +21,15 @@ if(Git_FOUND)
ERROR_QUIET)

if(NOT ${NOT_GIT_DIR})
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
endif()

###############################################################################
# Project-wide options
###############################################################################

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
Expand All @@ -53,152 +47,104 @@ endif()

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

###############################################################################
# Third-party dependencies
###############################################################################

# Strategy: search for the dependency using 'find_package' first. If the
# installed system version matches the one we want, use that.
# If there's a version mismatch, or if the library is not available,
# fetch the library using CMake, build and use it.

include(FetchContent)

# mimalloc
FetchContent_Declare(mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG dc6bce256d4f3ce87761f9337977dff3d8b1776c
GIT_PROGRESS TRUE)

FetchContent_GetProperties(mimalloc)

if(NOT mimalloc_POPULATED)
FetchContent_Populate(mimalloc)
set(MI_BUILD_TESTS OFF CACHE BOOL "Build mimalloc test executables.")
set(MI_BUILD_SHARED OFF CACHE BOOL "Build shared library")
set(MI_BUILD_OBJECT OFF CACHE BOOL "Build object library")
list(APPEND mi_cflags "-DMI_USE_ENVIRON=0")
add_subdirectory(${mimalloc_SOURCE_DIR} EXCLUDE_FROM_ALL)
endif()

# oneTBB
find_package(TBB QUIET COMPONENTS tbb)
if(NOT TBB_tbb_FOUND)
FetchContent_Declare(TBB
GIT_REPOSITORY https://github.com/oneapi-src/oneTBB.git
GIT_TAG eca91f16d7490a8abfdee652dadf457ec820cc37
GIT_PROGRESS TRUE)

FetchContent_GetProperties(TBB)
if(NOT TBB_POPULATED)
FetchContent_Populate(TBB)
include(${tbb_SOURCE_DIR}/cmake/TBBBuild.cmake)
tbb_build(TBB_ROOT ${tbb_SOURCE_DIR}
CONFIG_DIR ${tbb_BINARY_DIR})
find_package(TBB REQUIRED
CONFIG
PATHS ${tbb_SOURCE_DIR}/cmake)
endif()
else()
if(NOT TARGET TBB::tbb)
add_library(TBB::tbb ALIAS tbb)
endif()
endif()
option(MI_BUILD_SHARED "Build shared library" OFF)
option(MI_BUILD_OBJECT "Build object library" OFF)
option(MI_BUILD_TESTS "Build test executables" OFF)
add_subdirectory(mimalloc EXCLUDE_FROM_ALL)

set(BUILD_SHARED_LIBS OFF)
set(CMAKE_CXX_FLAGS "-D__TBB_DYNAMIC_LOAD_ENABLED=0")
option(TBB_TEST "Enable testing" OFF)
add_subdirectory(tbb EXCLUDE_FROM_ALL)

# pthreads
if(UNIX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
endif()

# xxHash
FetchContent_Declare(xxHash
GIT_REPOSITORY https://github.com/Cyan4973/xxHash.git
GIT_TAG 94e5f23e736f2bb67ebdf90727353e65344f9fc0
GIT_PROGRESS TRUE)

FetchContent_GetProperties(xxHash)

if(NOT xxHash_POPULATED)
FetchContent_Populate(xxHash)
add_subdirectory(${xxhash_SOURCE_DIR}/cmake_unofficial EXCLUDE_FROM_ALL)
endif()

# zlib
find_package(ZLIB 1.2 QUIET)
if(NOT ZLIB_FOUND)
FetchContent_Declare(zlib
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG cacf7f1d4e3d44d871b605da3b647f07d718623f)

FetchContent_GetProperties(zlib)
if(NOT zlib_POPULATED)
FetchContent_Populate(zlib)
add_subdirectory(${zlib_SOURCE_DIR} ${zlib_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()

###############################################################################
# Targets
###############################################################################

add_executable(mold)

target_sources(mold
PRIVATE
arch_i386.cc
arch_x86_64.cc
archive_file.cc
cmdline.cc
compress.cc
elf.h
filepath.cc
gc_sections.cc
glob.cc
icf.cc
input_sections.cc
linker_script.cc
main.cc
mapfile.cc
mold.h
object_file.cc
output_chunks.cc
output_file.cc
passes.cc
perf.cc
subprocess.cc
symbols.cc
tar.cc)

target_compile_definitions(mold
PRIVATE
"GIT_HASH=\"${GIT_HASH}\""
"MOLD_VERSION=\"${PROJECT_VERSION}\"")
"GIT_HASH=\"${GIT_HASH}\""
"MOLD_VERSION=\"${PROJECT_VERSION}\"")

target_compile_options(mold
target_sources(mold
PRIVATE
$<$<PLATFORM_ID:Linux>:-Wno-deprecated-volatile>)
arch_aarch64.cc
arch_i386.cc
archive_file.cc
arch_x86_64.cc
cmdline.cc
compress.cc
concurrent_map.cc
filepath.cc
gc_sections.cc
hyperloglog.cc
icf.cc
input_sections.cc
linker_script.cc
main.cc
mapfile.cc
memory_mapped_file.cc
object_file.cc
output_chunks.cc
output_file.cc
passes.cc
perf.cc
relocatable.cc
subprocess.cc
symbols.cc
tar.cc
)

target_include_directories(mold
PUBLIC
${PROJECT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/mimalloc/include
${CMAKE_CURRENT_SOURCE_DIR}/tbb/include
)

target_link_libraries(mold
PRIVATE
mimalloc-static
TBB::tbb
xxHash::xxhash
ZLIB::ZLIB
crypto
$<$<PLATFORM_ID:Linux>:dl>
$<$<PLATFORM_ID:Linux>:Threads::Threads>)
crypto
mimalloc-static
tbb
xxhash
z
$<$<PLATFORM_ID:Linux>:dl>
$<$<PLATFORM_ID:Linux>:Threads::Threads>)

add_library(mold-wrapper SHARED)

target_sources(mold-wrapper
PRIVATE
mold-wrapper.c)
target_sources(mold-wrapper PRIVATE mold-wrapper.c)

target_link_libraries(mold-wrapper
PRIVATE
$<$<PLATFORM_ID:Linux>:dl>)
$<$<PLATFORM_ID:Linux>:dl>)

# mold-wrapper.so is searched for by name, so disable the 'lib' prefix
set_target_properties(mold-wrapper
PROPERTIES PREFIX "")
set_target_properties(mold-wrapper PROPERTIES PREFIX "")

file(CREATE_LINK mold${CMAKE_EXECUTABLE_SUFFIX}
${CMAKE_CURRENT_BINARY_DIR}/ld SYMBOLIC)

# Tests

enable_testing()

file(GLOB TEST_SCRIPTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} test/*.sh)

foreach(testScript ${TEST_SCRIPTS})
add_test(${testScript} ${CMAKE_CURRENT_SOURCE_DIR}/${testScript}
${CMAKE_CURRENT_BINARY_DIR}/mold${CMAKE_EXECUTABLE_SUFFIX})
endforeach(testScript)

# Install

install(TARGETS mold DESTINATION bin)
install(TARGETS mold-wrapper DESTINATION lib/mold)
6 changes: 3 additions & 3 deletions build-static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ cat <<EOF | docker build -t mold-build-ubuntu20 -
FROM ubuntu:20.04
RUN apt-get update && \
TZ=Europe/London apt-get install -y tzdata && \
apt-get install -y build-essential git clang lld cmake \
apt-get install -y build-essential git clang lld cmake ninja-build \
libstdc++-10-dev libxxhash-dev zlib1g-dev libssl-dev && \
rm -rf /var/lib/apt/lists/*
EOF

docker run -it --rm -v `pwd`:/mold -u $(id -u):$(id -g) \
mold-build-ubuntu20 \
make -C /mold -j$(nproc) EXTRA_LDFLAGS='-fuse-ld=lld -static'
mold-build-ubuntu20 bash -c \
'cmake -GNinja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release -S /mold -B /mold/out/release; ninja -C /mold/out/release'
2 changes: 1 addition & 1 deletion tbb/src/tbb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ else()
if (BUILD_TYPE STREQUAL "debug")
set(TBB_LIB_NAME ${TBB_LIB_NAME}${CMAKE_DEBUG_POSTFIX})
endif()
configure_file(${CMAKE_SOURCE_DIR}/integration/pkg-config/tbb.pc.in ${CMAKE_CURRENT_BINARY_DIR}/${TBB_LIB_NAME}.pc @ONLY)
configure_file(${PROJECT_SOURCE_DIR}/integration/pkg-config/tbb.pc.in ${CMAKE_CURRENT_BINARY_DIR}/${TBB_LIB_NAME}.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${TBB_LIB_NAME}.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ TESTS = $(wildcard *.sh)
test: $(TESTS)

$(TESTS):
@./$@
@./$@ `pwd`/../mold

.PHONY: test $(TESTS)
3 changes: 2 additions & 1 deletion test/aarch64-hello-static.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
Expand All @@ -17,7 +18,7 @@ int main() {
}
EOF

aarch64-linux-gnu-gcc -B`pwd`/.. -o $t/exe $t/a.o -static
aarch64-linux-gnu-gcc -B`dirname $mold` -o $t/exe $t/a.o -static

readelf -p .comment $t/exe | grep -qw mold

Expand Down
7 changes: 4 additions & 3 deletions test/allow-multiple-definition.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
Expand All @@ -8,8 +9,8 @@ mkdir -p $t
echo 'int main() { return 0; }' > $t/a.c
echo 'int main() { return 0; }' > $t/b.c

! clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.c $t/b.c 2> /dev/null || false
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.c $t/b.c -Wl,-allow-multiple-definition
clang -fuse-ld=`pwd`/../mold -o $t/exe $t/a.c $t/b.c -Wl,-z,muldefs
! clang -fuse-ld=$mold -o $t/exe $t/a.c $t/b.c 2> /dev/null || false
clang -fuse-ld=$mold -o $t/exe $t/a.c $t/b.c -Wl,-allow-multiple-definition
clang -fuse-ld=$mold -o $t/exe $t/a.c $t/b.c -Wl,-z,muldefs

echo OK
3 changes: 2 additions & 1 deletion test/ar-alignment.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
Expand All @@ -24,6 +25,6 @@ EOF
rm -f $t/d.a
ar rcs $t/d.a $t/a.bin $t/b.o

clang -fuse-ld=`pwd`/../mold -o $t/exe $t/c.o $t/d.a
clang -fuse-ld=$mold -o $t/exe $t/c.o $t/d.a

echo OK
5 changes: 3 additions & 2 deletions test/as-needed-weak.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
Expand All @@ -21,13 +22,13 @@ cat <<EOF | cc -o $t/c.so -shared -fPIC -Wl,-soname,libbar.so -xc -
int fn2() { return 42; }
EOF

../mold -o $t/exe $t/a.o $t/b.so $t/c.so
$mold -o $t/exe $t/a.o $t/b.so $t/c.so

readelf --dynamic $t/exe > $t/readelf
fgrep -q 'Shared library: [libfoo.so]' $t/readelf
fgrep -q 'Shared library: [libbar.so]' $t/readelf

../mold -o $t/exe $t/a.o --as-needed $t/b.so $t/c.so
$mold -o $t/exe $t/a.o --as-needed $t/b.so $t/c.so

readelf --dynamic $t/exe > $t/readelf
! fgrep -q 'Shared library: [libfoo.so]' $t/readelf || false
Expand Down
5 changes: 3 additions & 2 deletions test/as-needed.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
set -e
mold=$1
cd $(dirname $0)
echo -n "Testing $(basename -s .sh $0) ... "
t=$(pwd)/tmp/$(basename -s .sh $0)
Expand All @@ -20,13 +21,13 @@ cat <<EOF | cc -o $t/c.so -shared -fPIC -Wl,-soname,libbar.so -xc -
int fn2() { return 42; }
EOF

../mold -o $t/exe $t/a.o $t/b.so $t/c.so
$mold -o $t/exe $t/a.o $t/b.so $t/c.so

readelf --dynamic $t/exe > $t/readelf
fgrep -q 'Shared library: [libfoo.so]' $t/readelf
fgrep -q 'Shared library: [libbar.so]' $t/readelf

../mold -o $t/exe $t/a.o --as-needed $t/b.so $t/c.so
$mold -o $t/exe $t/a.o --as-needed $t/b.so $t/c.so

readelf --dynamic $t/exe > $t/readelf
fgrep -q 'Shared library: [libfoo.so]' $t/readelf
Expand Down
Loading

0 comments on commit fe26dad

Please sign in to comment.