Skip to content

Commit

Permalink
Update build procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed May 18, 2021
1 parent 394805d commit effadb0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
options.inc
core
ld
oneTBB
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
[submodule "oneTBB"]
path = oneTBB
url = https://github.com/oneapi-src/oneTBB.git
[submodule "mimalloc"]
path = mimalloc
url = https://github.com/microsoft/mimalloc.git
[submodule "xxHash"]
path = xxHash
url = https://github.com/Cyan4973/xxHash.git
54 changes: 23 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
CC=clang
CXX=clang++

CURRENT_DIR=$(shell pwd)
TBB_LIBDIR=$(wildcard $(CURRENT_DIR)/oneTBB/build/linux_intel64_*_release/)
MALLOC_LIBDIR=$(CURRENT_DIR)/mimalloc/out/release

CPPFLAGS=-g -IoneTBB/include -IxxHash -pthread -std=c++20 \
-Wno-deprecated-volatile -Wno-switch \
-DGIT_HASH=\"$(shell git rev-parse HEAD)\"
LDFLAGS=-fuse-ld=lld -L$(TBB_LIBDIR) -Wl,-rpath=$(TBB_LIBDIR) \
-L$(MALLOC_LIBDIR) -Wl,-rpath=$(MALLOC_LIBDIR) \
-L$(CURRENT_DIR)/xxHash -Wl,-rpath=$(CURRENT_DIR)/xxHash
LIBS=-lcrypto -pthread -ltbb -lmimalloc -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 glob.o passes.o tar.o compress.o \
arch_x86_64.o arch_i386.o
CC = clang
CXX = clang++

MIMALLOC_LIB = mimalloc/out/release/libmimalloc.a

CPPFLAGS = -g -Imimalloc/include -pthread -std=c++20 \
-Wno-deprecated-volatile -Wno-switch \
-DGIT_HASH=\"$(shell git rev-parse HEAD)\" \
$(EXTRA_CPPFLAGS)
LDFLAGS = $(EXTRA_LDFLAGS)
LIBS = -lcrypto -pthread -ltbb -lz -lxxhash \
-Wl,-as-needed -ldl -Wl,-no-as-needed
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 \
glob.o passes.o tar.o compress.o arch_x86_64.o arch_i386.o

PREFIX ?= /usr

DEBUG ?= 0
LTO ?= 0
ASAN ?= 0
TSAN ?= 0
STATIC ?= 1

ifeq ($(DEBUG), 1)
CPPFLAGS += -O0
Expand All @@ -39,36 +35,32 @@ endif
ifeq ($(ASAN), 1)
CPPFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address
STATIC = 0
else
# By default, we want to use mimalloc as a memory allocator.
# Since replacing the standard malloc is not compatible with ASAN,
# we do that only when ASAN is not enabled.
LDFLAGS += -Wl,-whole-archive $(MIMALLOC_LIB) -Wl,-no-whole-archive
endif

ifeq ($(TSAN), 1)
CPPFLAGS += -fsanitize=thread
LDFLAGS += -fsanitize=thread
STATIC = 0
endif

ifeq ($(STATIC), 1)
CPPFLAGS += -static
endif

all: mold mold-wrapper.so

mold: $(OBJS)
mold: $(OBJS) $(MIMALLOC_LIB)
$(CXX) $(CFLAGS) $(OBJS) -o $@ $(LDFLAGS) $(LIBS)

mold-wrapper.so: mold-wrapper.c Makefile
cc -fPIC -shared -o $@ $< -ldl

$(OBJS): mold.h elf.h Makefile

submodules:
$(MAKE) -C oneTBB
$(MAKE) -C oneTBB extra_inc=big_iron.inc
$(MIMALLOC_LIB):
mkdir -p mimalloc/out/release
(cd mimalloc/out/release; CFLAGS=-DMI_USE_ENVIRON=0 cmake ../..)
$(MAKE) -C mimalloc/out/release
$(MAKE) -C xxHash

test: all
for i in test/*.sh; do $$i || exit 1; done
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ Clang. I'm using Ubuntu 20.04 as a development platform. In that
environment, you can build mold by the following commands.

```
$ sudo apt-get install build-essential libstdc++-10-dev clang-10 libssl-dev zlib1g-dev cmake
$ sudo apt-get install build-essential libstdc++-10-dev cmake clang libssl-dev zlib1g-dev libxxhash-dev libtbb-dev
$ git clone --recursive https://github.com/rui314/mold.git
$ cd mold
$ make submodules
$ make
```

The last `make` command creates `mold` executable.

You can build a statically-linked mold executable by substituting the
last `make` with `make STATIC=1`. A statically-linked mold executable
can be copied to any Linux system and run there without copying other
libraries. This is useful if you want to use Ubuntu 20.04 only for
building mold.
If you don't have Ubuntu 20.04, or if for any reason `make` in the
above commands doesn't work for you, you can use Docker to build it in
a Docker environment. To do so, just run `./build-static.sh` in this
directory. The script creates a Ubuntu 20.04 Docker image, install
necessary tools and libraries to it and build mold as a static binary.

## How to use

Expand Down
33 changes: 33 additions & 0 deletions build-static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash -x

# This is a shell script to build a statically-linked mold
# executable using Docker, so that it is easy to build mold
# on non-Ubuntu 20.04 machines.
#
# A docker image used for building mold is persistent.
# Run `docker image rm mold-build-ubuntu20` to remove the image
# from disk.

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 \
libstdc++-10-dev libxxhash-dev zlib1g-dev libssl-dev && \
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

docker run -it --rm -v `pwd`/oneTBB:/oneTBB -v `pwd`:/mold \
-u $(id -u):$(id -g) mold-build-ubuntu20 \
make EXTRA_CPPFLAGS=-I/oneTBB/include \
EXTRA_LDFLAGS="-fuse-ld=lld -static -L/$(ls -d oneTBB/build/linux_intel64_*_release)" \
-j$(nproc) -C /mold
2 changes: 1 addition & 1 deletion mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#endif

#include "elf.h"
#include "xxHash/xxhash.h"

#include <atomic>
#include <cassert>
Expand All @@ -27,6 +26,7 @@
#include <tbb/task_group.h>
#include <unordered_set>
#include <vector>
#include <xxh3.h>

typedef uint8_t u8;
typedef uint16_t u16;
Expand Down
1 change: 0 additions & 1 deletion oneTBB
Submodule oneTBB deleted from eca91f
1 change: 0 additions & 1 deletion xxHash
Submodule xxHash deleted from ea5393

0 comments on commit effadb0

Please sign in to comment.