Skip to content

Commit 6929bc7

Browse files
committed
Add amalgamation script
Sources are #included instead of inlined by default.
1 parent 9013ce7 commit 6929bc7

File tree

8 files changed

+5534
-2
lines changed

8 files changed

+5534
-2
lines changed

Makefile

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,27 @@ ifneq (Cygwin,$(UNAME))
172172
endif
173173
endif
174174

175+
AMALGAM ?= 0
176+
AMALGAM_INLINE ?= 0
177+
AMALGAM_SOURCE_INCLUDE := build/libsass-amalgam-include.cpp
178+
AMALGAM_SOURCE_INLINE := build/libsass-amalgam-inline.cpp
179+
180+
ifeq (1,$(AMALGAM_INLINE))
181+
AMALGAM_SOURCE := $(AMALGAM_SOURCE_INLINE)
182+
else
183+
AMALGAM_SOURCE := $(AMALGAM_SOURCE_INCLUDE)
184+
endif
185+
186+
AMALGAM_SOURCE_DIR := $(abspath $(dir $(AMALGAM_SOURCE)))
187+
175188
include Makefile.conf
176-
OBJECTS = $(addprefix src/,$(SOURCES:.cpp=.o))
177-
COBJECTS = $(addprefix src/,$(CSOURCES:.c=.o))
189+
ifeq (1,$(AMALGAM))
190+
OBJECTS = $(AMALGAM_SOURCE:.cpp=.o)
191+
COBJECTS =
192+
else
193+
OBJECTS = $(addprefix src/,$(SOURCES:.cpp=.o))
194+
COBJECTS = $(addprefix src/,$(CSOURCES:.c=.o))
195+
endif
178196
RCOBJECTS = $(RESOURCES:.rc=.o)
179197

180198
DEBUG_LVL ?= NONE
@@ -184,6 +202,7 @@ CLEANUPS += $(RCOBJECTS)
184202
CLEANUPS += $(COBJECTS)
185203
CLEANUPS += $(OBJECTS)
186204
CLEANUPS += $(LIBSASS_LIB)
205+
CLEANUPS += $(AMALGAM_SOURCE_INCLUDE) $(AMALGAM_SOURCE_INLINE)
187206

188207
all: $(BUILD)
189208

@@ -199,6 +218,28 @@ debug-shared: CFLAGS := -g -DDEBUG -DDEBUG_LVL="$(DEBUG_LVL)" $(filter-out -O2,$
199218
debug-shared: CXXFLAGS := -g -DDEBUG -DDEBUG_LVL="$(DEBUG_LVL)" $(filter-out -O2,$(CXXFLAGS))
200219
debug-shared: shared
201220

221+
AMALGAMATE_BIN := script/amalgamate/build/amalgamate
222+
223+
$(AMALGAMATE_BIN): script/amalgamate/amalgamate.cpp
224+
$(MAKE) -C script/amalgamate build/amalgamate
225+
226+
$(AMALGAM_SOURCE_DIR):
227+
$(MKDIR) $(AMALGAM_SOURCE_DIR)
228+
229+
ifeq (1,$(AMALGAM_INLINE))
230+
AMALGAM_INLINE_FLAG := true
231+
else
232+
AMALGAM_INLINE_FLAG := false
233+
endif
234+
235+
$(AMALGAM_SOURCE): $(AMALGAMATE_BIN) $(addprefix src/,$(SOURCES)) $(addprefix src/,$(CSOURCES)) | $(AMALGAM_SOURCE_DIR)
236+
$(AMALGAMATE_BIN) --out=$(AMALGAM_SOURCE) --inline=$(AMALGAM_INLINE_FLAG)
237+
238+
ifneq (1,$(AMALGAM_INLINE))
239+
$(AMALGAM_SOURCE:.cpp=.o): $(AMALGAM_SOURCE)
240+
$(CXX) $(CXXFLAGS) -I src -c -o $@ $<
241+
endif
242+
202243
lib:
203244
$(MKDIR) lib
204245

script/amalgamate/.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: Google

script/amalgamate/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CXX ?= c++
2+
3+
CXXFLAGS := -std=c++11
4+
CXXFLAGS_OPT := $(CXXFLAGS) -O2 -s
5+
CXXFLAGS_DBG := $(CXXFLAGS) -fsanitize=address -g -O1 -fno-omit-frame-pointer
6+
CXXFLAGS_FASTBUILD := $(CXXFLAGS) -O0 -s
7+
8+
build/amalgamate: amalgamate.cpp | build
9+
$(CXX) $(CXXFLAGS_FASTBUILD) -o build/amalgamate amalgamate.cpp
10+
11+
build:
12+
@mkdir build
13+
14+
clean: | build
15+
rm -rf build
16+
17+
.PHONY: amalgamate clean

script/amalgamate/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# LibSass amalgamation script
2+
3+
This script concatenates LibSass sources into a single file.
4+
5+
This reduces single-core compilation time by 50% and the output shared library size by 10%.
6+
7+
SQLite has a great writeup on amalgamation here:
8+
<https://www.sqlite.org/amalgamation.html>.
9+
10+
## Options
11+
12+
* `--inline`. Default: `true`\
13+
When `true`, all sources are inlined into the amalgam file (including headers.\
14+
When `false`, the amalgam file instead contains `#include` statements for each
15+
`.c,.cpp` file.
16+
* `--root`. Default: `$PWD`.\
17+
The root directory.
18+
By default, the `src/` subdirectory of root is searched and includes are
19+
resolved relative to it.
20+
* `--out`. Default: `/dev/stdout`.\
21+
Path to the amalgamated source output.
22+
23+
## Benchmarks
24+
25+
With amalgamation:
26+
27+
~~~bash
28+
rm -f script/amalgamate/build/amalgamate && make clean AMALGAM=1 && \
29+
time make lib/libsass.so AMALGAM=1 && du -sh lib/libsass.so
30+
~~~
31+
32+
Compilation time (1 core): 30s
33+
`lib/libsass.so` size: 3.0M
34+
35+
These numbers are the same affected for both inline and include-style amalgamation.
36+
37+
Without amalgamation:
38+
39+
~~~bash
40+
make clean AMALGAM=0 && time make -j`nproc` lib/libsass.so AMALGAM=0 && du -sh lib/libsass.so
41+
~~~
42+
43+
Compilation time (1 core): 60s
44+
Compilation time (8 cores): 16s
45+
`lib/libsass.so` size: 3.3M

0 commit comments

Comments
 (0)