1
1
mirror of https://github.com/rui314/mold.git synced 2024-11-13 09:39:13 +03:00

Static-link oneTBB by default

oneTBB API is changing rapidly, so even if a oneTBB is installed
into a system already, it is likely that that is not a compatible
version with mold. This makes program building unnecessarily hard.

This change intends to solve the problem once and for all by
including oneTBB as a subdirectory and static-link against it at
build-time.

oneTBB is released under the Apache License 2.0, which is compatible
with AGPL. Therefore, we can still distribute the binary of mold
under AGPL.

If you do not want to static-link oneTBB, pass `SYSTEM_TBB=1`
to `make`.

This change also ports mold from oneTBB v2020.3 to v2021.3.0.

Fixes https://github.com/rui314/mold/issues/66
This commit is contained in:
Rui Ueyama 2021-07-02 23:11:48 +09:00
parent a32c81f94c
commit 3e3d58b6f1
6 changed files with 28 additions and 30 deletions

View File

@ -4,12 +4,11 @@ CXX = clang++
GIT_HASH ?= $(shell [ -d .git ] && git rev-parse HEAD)
CPPFLAGS = -g -Imimalloc/include -pthread -std=c++20 \
-Wno-deprecated-volatile \
-DMOLD_VERSION=\"0.9.1\" \
-DGIT_HASH=\"$(GIT_HASH)\" \
$(EXTRA_CPPFLAGS)
LDFLAGS += $(EXTRA_LDFLAGS)
LIBS = -Wl,-as-needed -lcrypto -pthread -ltbb -lz -lxxhash -ldl
LIBS = -Wl,-as-needed -lcrypto -pthread -lz -lxxhash -ldl
OBJS = main.o object_file.o input_sections.o output_chunks.o \
mapfile.o perf.o linker_script.o archive_file.o output_file.o \
subprocess.o gc_sections.o icf.o symbols.o cmdline.o filepath.o \
@ -53,9 +52,17 @@ ifeq ($(TSAN), 1)
LDFLAGS += -fsanitize=thread
endif
ifdef SYSTEM_TBB
LIBS += -ltbb
else
TBB_LIB = oneTBB/out/libs/libtbb.a
LIBS += $(TBB_LIB)
CPPFLAGS += -IoneTBB/include
endif
all: mold mold-wrapper.so
mold: $(OBJS) $(MIMALLOC_LIB)
mold: $(OBJS) $(MIMALLOC_LIB) $(TBB_LIB)
$(CXX) $(CXXFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(LIBS)
mold-wrapper.so: mold-wrapper.c Makefile
@ -68,6 +75,12 @@ $(MIMALLOC_LIB):
(cd mimalloc/out/release; CFLAGS=-DMI_USE_ENVIRON=0 cmake ../..)
$(MAKE) -C mimalloc/out/release mimalloc-static
$(TBB_LIB):
mkdir -p oneTBB/out
(cd oneTBB/out; cmake -DBUILD_SHARED_LIBS=OFF -DTBB_TEST=OFF -DCMAKE_CXX_FLAGS=-D__TBB_DYNAMIC_LOAD_ENABLED=0 ..)
$(MAKE) -C oneTBB/out
(cd oneTBB/out; ln -sf *_relwithdebinfo libs)
test tests check: all
$(MAKE) -C test --output-sync --no-print-directory

View File

@ -22,19 +22,6 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/*
EOF
if ! [ -d oneTBB ]; then
git clone https://github.com/oneapi-src/oneTBB.git
(cd oneTBB; git checkout --quiet v2020.3)
fi
docker run -it --rm -v `pwd`/oneTBB:/oneTBB -u $(id -u):$(id -g) \
mold-build-ubuntu20 \
make -C /oneTBB -j$(nproc) extra_inc=big_iron.inc
tbb_bindir=$(ls -d oneTBB/build/linux_intel64_*_release)
docker run -it --rm -v `pwd`:/mold -u $(id -u):$(id -g) \
mold-build-ubuntu20 \
make -C /mold -j$(nproc) \
EXTRA_CPPFLAGS=-IoneTBB/include \
EXTRA_LDFLAGS="-fuse-ld=lld -static -L$tbb_bindir"
make -C /mold -j$(nproc) EXTRA_LDFLAGS='-fuse-ld=lld -static'

View File

@ -7,9 +7,6 @@
#include <tbb/concurrent_vector.h>
#include <tbb/parallel_for_each.h>
template <typename E>
using Feeder = tbb::parallel_do_feeder<InputSection<E> *>;
template <typename E>
static bool is_init_fini(const InputSection<E> &isec) {
return isec.shdr.sh_type == SHT_INIT_ARRAY ||
@ -28,7 +25,7 @@ static bool mark_section(InputSection<E> *isec) {
template <typename E>
static void visit(Context<E> &ctx, InputSection<E> *isec,
Feeder<E> &feeder, i64 depth) {
tbb::feeder<InputSection<E> *> &feeder, i64 depth) {
assert(isec->is_visited);
// A relocation can refer either a section fragment (i.e. a piece of
@ -139,7 +136,8 @@ static void mark(Context<E> &ctx,
tbb::concurrent_vector<InputSection<E> *> &roots) {
Timer t(ctx, "mark");
tbb::parallel_do(roots, [&](InputSection<E> *isec, Feeder<E> &feeder) {
tbb::parallel_for_each(roots, [&](InputSection<E> *isec,
tbb::feeder<InputSection<E> *> &feeder) {
visit(ctx, isec, feeder, 0);
});
}

4
icf.cc
View File

@ -65,8 +65,8 @@ static constexpr i64 HASH_SIZE = 16;
typedef std::array<u8, HASH_SIZE> Digest;
namespace tbb {
template<> struct tbb_hash<Digest> {
namespace std {
template<> struct hash<Digest> {
size_t operator()(const Digest &k) const {
return *(i64 *)&k[0];
}

1
mold.h
View File

@ -25,6 +25,7 @@
#include <tbb/enumerable_thread_specific.h>
#include <tbb/spin_mutex.h>
#include <tbb/task_group.h>
#include <unistd.h>
#include <unordered_set>
#include <vector>
#include <xxh3.h>

View File

@ -3,7 +3,6 @@
#include <functional>
#include <map>
#include <regex>
#include <tbb/parallel_do.h>
#include <tbb/parallel_for_each.h>
#include <tbb/parallel_scan.h>
#include <tbb/partitioner.h>
@ -99,9 +98,9 @@ void resolve_symbols(Context<E> &ctx) {
if (!file->is_alive.exchange(true) && !file->is_dso)
live_objs.push_back((ObjectFile<E> *)file);
tbb::parallel_do(live_objs,
tbb::parallel_for_each(live_objs,
[&](ObjectFile<E> *file,
tbb::parallel_do_feeder<ObjectFile<E> *> &feeder) {
tbb::feeder<ObjectFile<E> *> &feeder) {
file->mark_live_objects(ctx, [&](ObjectFile<E> *obj) { feeder.add(obj); });
});
@ -133,9 +132,9 @@ void resolve_symbols(Context<E> &ctx) {
std::vector<SharedFile<E> *> live_dsos = ctx.dsos;
erase(live_dsos, [](SharedFile<E> *file) { return !file->is_alive; });
tbb::parallel_do(live_dsos,
[&](SharedFile<E> *file,
tbb::parallel_do_feeder<SharedFile<E> *> &feeder) {
tbb::parallel_for_each(live_dsos,
[&](SharedFile<E> *file,
tbb::feeder<SharedFile<E> *> &feeder) {
for (Symbol<E> *sym : file->globals)
if (sym->file && sym->file != file && sym->file->is_dso &&
!sym->file->is_alive.exchange(true))