Skip to content

Commit

Permalink
Precompile headers
Browse files Browse the repository at this point in the history
This cuts 'make install -j6' on my laptop from 170s to 134s.
  • Loading branch information
edolstra committed Nov 7, 2019
1 parent 39a2e16 commit 5ff4d77
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ makefiles = \
tests/local.mk \
tests/plugins/local.mk

GLOBAL_CXXFLAGS += -g -Wall -include config.h

-include Makefile.config

include mk/lib.mk

GLOBAL_CXXFLAGS += -g -Wall -include config.h -std=c++17
1 change: 0 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ test "$localstatedir" = '${prefix}/var' && localstatedir=/nix/var
AC_PROG_CC
AC_PROG_CXX
AC_PROG_CPP
AX_CXX_COMPILE_STDCXX_17([noext], [mandatory])

AC_CHECK_TOOL([AR], [ar])

Expand Down
1 change: 1 addition & 0 deletions mk/lib.mk
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ include mk/jars.mk
include mk/patterns.mk
include mk/templates.mk
include mk/tests.mk
include mk/precompiled-headers.mk


# Include all sub-Makefiles.
Expand Down
4 changes: 2 additions & 2 deletions mk/patterns.mk
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
$(buildprefix)%.o: %.cc
@mkdir -p "$(dir $@)"
$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS_PCH) $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP

$(buildprefix)%.o: %.cpp
@mkdir -p "$(dir $@)"
$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS) $(GLOBAL_CXXFLAGS_PCH) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP
$(trace-cxx) $(CXX) -o $@ -c $< $(GLOBAL_CXXFLAGS_PCH) $(GLOBAL_CXXFLAGS) $(CXXFLAGS) $($@_CXXFLAGS) -MMD -MF $(call filename-to-dep, $@) -MP

$(buildprefix)%.o: %.c
@mkdir -p "$(dir $@)"
Expand Down
42 changes: 42 additions & 0 deletions mk/precompiled-headers.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
PRECOMPILE_HEADERS ?= 1

print-var-help += \
echo " PRECOMPILE_HEADERS ($(PRECOMPILE_HEADERS)): Whether to use precompiled headers to speed up the build";

GCH = $(buildprefix)precompiled-headers.h.gch

$(GCH): precompiled-headers.h
@rm -f $@
@mkdir -p "$(dir $@)"
$(trace-gen) $(CXX) -x c++-header -o $@ $^ $(GLOBAL_CXXFLAGS)

PCH = $(buildprefix)precompiled-headers.h.pch

$(PCH): precompiled-headers.h
@rm -f $@
@mkdir -p "$(dir $@)"
$(trace-gen) $(CXX) -x c++-header -o $@ $^ $(GLOBAL_CXXFLAGS)

clean-files += $(GCH) $(PCH)

ifeq ($(PRECOMPILE_HEADERS), 1)

ifeq ($(CXX), g++)

GLOBAL_CXXFLAGS_PCH += -include $(buildprefix)precompiled-headers.h -Winvalid-pch

GLOBAL_ORDER_AFTER += $(GCH)

else ifeq ($(CXX), clang++)

GLOBAL_CXXFLAGS_PCH += -include-pch $(PCH) -Winvalid-pch

GLOBAL_ORDER_AFTER += $(PCH)

else

$(error Don't know how to precompile headers on $(CXX))

endif

endif
71 changes: 71 additions & 0 deletions precompiled-headers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <functional>
#include <future>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <boost/format.hpp>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <pwd.h>
#include <sched.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/personality.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>

#include "util.hh"
#include "args.hh"

0 comments on commit 5ff4d77

Please sign in to comment.