1{
2 gccStdenv,
3 lib,
4 pkgs,
5 git,
6 openssl,
7 autoconf,
8 coreutils,
9 src,
10 version,
11 git-version,
12 stampYmd ? 0,
13 stampHms ? 0,
14 gambit-support,
15 optimizationSetting ? "-O1",
16 gambit-params ? pkgs.gambit-support.stable-params,
17 rev ? git-version,
18}:
19
20# Note that according to a benchmark run by Marc Feeley on May 2018,
21# clang is 10x (with default settings) to 15% (with -O2) slower than GCC at compiling
22# Gambit output, producing code that is 3x slower. IIRC the benchmarks from Gambit@30,
23# the numbers were still heavily in favor of GCC in October 2019.
24# Thus we use GCC over clang, even on macOS.
25#
26# Also note that I (fare) just ran benchmarks from https://github.com/ecraven/r7rs-benchmarks
27# with Gambit 4.9.3 with -O1 vs -O2 vs -Os on Feb 2020. Which wins depends on the benchmark.
28# The fight is unclear between -O1 and -O2, where -O1 wins more often, by up to 17%,
29# but sometimes -O2 wins, once by up to 43%, so that overall -O2 is 5% faster.
30# However, -Os seems more consistent in winning slightly against both -O1 and -O2,
31# and is overall 15% faster than -O2. As for compile times, -O1 is fastest,
32# -Os is about 29%-33% slower than -O1, while -O2 is about 40%-50% slower than -O1.
33#
34# Overall, -Os seems like the best choice, but I care more about compile-time,
35# so I stick with -O1 (in the defaults above), which is also the default for Gambit.
36
37gccStdenv.mkDerivation rec {
38
39 pname = "gambit";
40 inherit src version git-version;
41 bootstrap = gambit-support.gambit-bootstrap;
42
43 passthru = {
44 inherit
45 src
46 version
47 git-version
48 rev
49 stampYmd
50 stampHms
51 optimizationSetting
52 openssl
53 ;
54 };
55
56 nativeBuildInputs = [
57 git
58 autoconf
59 ];
60
61 # TODO: if/when we can get all the library packages we depend on to have static versions,
62 # we could use something like (makeStaticLibraries openssl) to enable creation
63 # of statically linked binaries by gsc.
64 buildInputs = [ openssl ];
65
66 # TODO: patch gambit's source so it has the full path to sed, grep, fgrep? Is there more?
67 # Or wrap relevant programs to add a suitable PATH ?
68 #runtimeDeps = [ gnused gnugrep ];
69
70 configureFlags = [
71 "--enable-targets=${gambit-params.targets}"
72 "--enable-single-host"
73 "--enable-c-opt=${optimizationSetting}"
74 "--enable-c-opt-rts=-O2"
75 "--enable-gcc-opts"
76 "--enable-trust-c-tco"
77 "--enable-shared"
78 "--enable-absolute-shared-libs" # Yes, NixOS will want an absolute path, and fix it.
79 "--enable-openssl"
80 "--enable-dynamic-clib"
81 #"--enable-default-compile-options='(compactness 9)'" # Make life easier on the JS backend
82 "--enable-default-runtime-options=${gambit-params.defaultRuntimeOptions}"
83 # "--enable-rtlib-debug" # used by Geiser, but only on recent-enough gambit, and messes js runtime
84 # "--enable-debug" # Nope: enables plenty of good stuff, but also the costly console.log
85 # "--enable-multiple-versions" # Nope, NixOS already does version multiplexing
86 # "--enable-guide"
87 # "--enable-track-scheme"
88 # "--enable-high-res-timing"
89 # "--enable-max-processors=4"
90 # "--enable-multiple-vms"
91 # "--enable-dynamic-tls"
92 # "--enable-multiple-threaded-vms" # when SMP branch is merged in
93 # "--enable-thread-system=posix" # default when --enable-multiple-vms is on.
94 # "--enable-profile"
95 # "--enable-coverage"
96 # "--enable-inline-jumps"
97 # "--enable-char-size=1" # default is 4
98 # "--enable-march=native" # Nope, makes it not work on machines older than the builder
99 ]
100 ++ gambit-params.extraOptions
101 # TODO: pick an appropriate architecture to optimize on on x86-64?
102 # https://gcc.gnu.org/onlinedocs/gcc-4.8.4/gcc/i386-and-x86-64-Options.html#i386-and-x86-64-Options
103 # ++ lib.optional pkgs.stdenv.hostPlatform.isx86_64 "--enable-march=core-avx2"
104 # Do not enable poll on darwin due to https://github.com/gambit/gambit/issues/498
105 ++ lib.optional (!gccStdenv.hostPlatform.isDarwin) "--enable-poll";
106
107 configurePhase = ''
108 export CC=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}gcc \
109 CXX=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}g++ \
110 CPP=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}cpp \
111 CXXCPP=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}cpp \
112 LD=${gccStdenv.cc}/bin/${gccStdenv.cc.targetPrefix}ld \
113 XMKMF=${coreutils}/bin/false
114 unset CFLAGS LDFLAGS LIBS CPPFLAGS CXXFLAGS
115
116 ${gambit-params.fixStamp git-version stampYmd stampHms}
117
118 ./configure --prefix=$out/gambit ${builtins.concatStringsSep " " configureFlags}
119
120 # OS-specific paths are hardcoded in ./configure
121 substituteInPlace config.status \
122 ${
123 lib.optionalString (
124 gccStdenv.hostPlatform.isDarwin && !gambit-params.stable
125 ) ''--replace "/usr/local/opt/openssl@1.1" "${lib.getLib openssl}"''
126 } \
127 --replace "/usr/local/opt/openssl" "${lib.getLib openssl}"
128
129 ./config.status
130 '';
131
132 buildPhase = ''
133 # The MAKEFLAGS setting is a workaround for https://github.com/gambit/gambit/issues/833
134 export MAKEFLAGS="--output-sync=recurse"
135 echo "Make bootstrap compiler, from release bootstrap"
136 mkdir -p boot
137 cp -rp ${bootstrap}/gambit/. boot/.
138 chmod -R u+w boot
139 cd boot
140 cp ../gsc/makefile.in ../gsc/*.scm gsc/
141 echo > include/stamp.h # No stamp needed for the bootstrap compiler
142 ./configure
143 for i in lib gsi gsc ; do (cd $i ; make -j$NIX_BUILD_CORES) ; done
144 cd ..
145 cp boot/gsc/gsc gsc-boot
146
147 echo "Now use the bootstrap compiler to build the real thing!"
148 make -j$NIX_BUILD_CORES from-scratch
149 ${lib.optionalString gambit-params.modules "make -j$NIX_BUILD_CORES modules"}
150 '';
151
152 postInstall = ''
153 mkdir $out/bin
154 cd $out/bin
155 ln -s ../gambit/bin/* .
156 '';
157
158 doCheck = true;
159 dontStrip = true;
160
161 meta = gambit-support.meta;
162}