1 { lib, stdenv, icu, expat, zlib, bzip2, python ? null, fixDarwinDylibNames, libiconv, libxcrypt
5 , toolset ? /**/ if stdenv.cc.isClang then "clang"
6 else if stdenv.cc.isGNU then "gcc"
10 , enableSingleThreaded ? false
11 , enableMultiThreaded ? true
12 , enableShared ? !(with stdenv.hostPlatform; isStatic || libc == "msvcrt") # problems for now
13 , enableStatic ? !enableShared
14 , enablePython ? false
16 , taggedLayout ? ((enableRelease && enableDebug) || (enableSingleThreaded && enableMultiThreaded) || (enableShared && enableStatic))
18 , boostBuildPatches ? []
23 # Attributes inherit from specific versions
28 # We must build at least one type of libraries
29 assert enableShared || enableStatic;
31 assert enableNumpy -> enablePython;
33 # Boost <1.69 can't be built on linux with clang >8, because pth was removed
34 assert with lib; (stdenv.isLinux && toolset == "clang" && versionAtLeast stdenv.cc.version "8.0.0") -> versionAtLeast version "1.69";
39 variant = concatStringsSep ","
40 (optional enableRelease "release" ++
41 optional enableDebug "debug");
43 threading = concatStringsSep ","
44 (optional enableSingleThreaded "single" ++
45 optional enableMultiThreaded "multi");
47 link = concatStringsSep ","
48 (optional enableShared "shared" ++
49 optional enableStatic "static");
51 runtime-link = if enableShared then "shared" else "static";
53 # To avoid library name collisions
54 layout = if taggedLayout then "tagged" else "system";
56 # Versions of b2 before 1.65 have job limits; specifically:
57 # - Versions before 1.58 support up to 64 jobs[0]
58 # - Versions before 1.65 support up to 256 jobs[1]
60 # [0]: https://github.com/boostorg/build/commit/0ef40cb86728f1cd804830fef89a6d39153ff632
61 # [1]: https://github.com/boostorg/build/commit/316e26ca718afc65d6170029284521392524e4f8
63 if versionOlder version "1.58" then
64 "$(($NIX_BUILD_CORES<=64 ? $NIX_BUILD_CORES : 64))"
65 else if versionOlder version "1.65" then
66 "$(($NIX_BUILD_CORES<=256 ? $NIX_BUILD_CORES : 256))"
70 needUserConfig = stdenv.hostPlatform != stdenv.buildPlatform || useMpi || (stdenv.isDarwin && enableShared);
72 b2Args = concatStringsSep " " ([
73 "--includedir=$dev/include"
78 "threading=${threading}"
80 "-sEXPAT_INCLUDE=${expat.dev}/include"
81 "-sEXPAT_LIBPATH=${expat.out}/lib"
83 # TODO: make this unconditional
84 ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform ||
85 # required on mips; see 61d9f201baeef4c4bb91ad8a8f5f89b747e0dfe4
86 (stdenv.hostPlatform.isMips && versionAtLeast version "1.79")) [
87 "address-model=${toString stdenv.hostPlatform.parsed.cpu.bits}"
88 "architecture=${if stdenv.hostPlatform.isMips64
89 then if versionOlder version "1.78" then "mips1" else "mips"
90 else if stdenv.hostPlatform.parsed.cpu.name == "s390x" then "s390x"
91 else toString stdenv.hostPlatform.parsed.cpu.family}"
92 "binary-format=${toString stdenv.hostPlatform.parsed.kernel.execFormat.name}"
93 "target-os=${toString stdenv.hostPlatform.parsed.kernel.name}"
95 # adapted from table in boost manual
96 # https://www.boost.org/doc/libs/1_66_0/libs/context/doc/html/context/architectures.html
97 "abi=${if stdenv.hostPlatform.parsed.cpu.family == "arm" then "aapcs"
98 else if stdenv.hostPlatform.isWindows then "ms"
99 else if stdenv.hostPlatform.isMips32 then "o32"
100 else if stdenv.hostPlatform.isMips64n64 then "n64"
102 ] ++ optional (link != "static") "runtime-link=${runtime-link}"
103 ++ optional (variant == "release") "debug-symbols=off"
104 ++ optional (toolset != null) "toolset=${toolset}"
105 ++ optional (!enablePython) "--without-python"
106 ++ optional needUserConfig "--user-config=user-config.jam"
107 ++ optional (stdenv.buildPlatform.isDarwin && stdenv.hostPlatform.isLinux) "pch=off"
108 ++ optionals (stdenv.hostPlatform.libc == "msvcrt") [
115 stdenv.mkDerivation {
123 ++ optional stdenv.isDarwin ./darwin-no-system-python.patch
124 # Fix boost-context segmentation faults on ppc64 due to ABI violation
125 ++ optional (versionAtLeast version "1.61" &&
126 versionOlder version "1.71") (fetchpatch {
127 url = "https://github.com/boostorg/context/commit/2354eca9b776a6739112833f64754108cc0d1dc5.patch";
128 sha256 = "067m4bjpmcanqvg28djax9a10avmdwhlpfx6gn73kbqqq70dnz29";
130 extraPrefix = "libs/context/";
132 # Fix compiler warning with GCC >= 8; TODO: patch may apply to older versions
133 ++ optional (versionAtLeast version "1.65" && versionOlder version "1.67")
135 url = "https://github.com/boostorg/mpl/commit/f48fd09d021db9a28bd7b8452c175897e1af4485.patch";
136 sha256 = "15d2a636hhsb1xdyp44x25dyqfcaws997vnp9kl1mhzvxjzz7hb0";
139 ++ optional (versionAtLeast version "1.65" && versionOlder version "1.70") (fetchpatch {
140 # support for Mips64n64 appeared in boost-context 1.70; this patch won't apply to pre-1.65 cleanly
141 url = "https://github.com/boostorg/context/commit/e3f744a1862164062d579d1972272d67bdaa9c39.patch";
142 sha256 = "sha256-qjQy1b4jDsIRrI+UYtcguhvChrMbGWO0UlEzEJHYzRI=";
144 extraPrefix = "libs/context/";
146 ++ optional (versionAtLeast version "1.70" && versionOlder version "1.73") ./cmake-paths.patch
147 ++ optional (versionAtLeast version "1.73") ./cmake-paths-173.patch
148 ++ optional (version == "1.77.0") (fetchpatch {
149 url = "https://github.com/boostorg/math/commit/7d482f6ebc356e6ec455ccb5f51a23971bf6ce5b.patch";
150 relative = "include";
151 sha256 = "sha256-KlmIbixcds6GyKYt1fx5BxDIrU7msrgDdYo9Va/KJR4=";
155 homepage = "http://boost.org/";
156 description = "Collection of C++ libraries";
157 license = licenses.boost;
158 platforms = platforms.unix ++ platforms.windows;
159 badPlatforms = optional (versionOlder version "1.59") "aarch64-linux"
160 ++ optional ((versionOlder version "1.57") || version == "1.58") "x86_64-darwin"
161 ++ optionals (versionOlder version "1.73") lib.platforms.riscv;
162 maintainers = with maintainers; [ hjones2199 ];
165 # boost-context lacks support for the N32 ABI on mips64. The build
166 # will succeed, but packages depending on boost-context will fail with
167 # a very cryptic error message.
168 stdenv.hostPlatform.isMips64n32 ||
169 # the patch above does not apply cleanly to pre-1.65 boost
170 (stdenv.hostPlatform.isMips64n64 && (versionOlder version "1.65"));
174 inherit boostBuildPatches;
177 preConfigure = optionalString useMpi ''
178 cat << EOF >> user-config.jam
179 using mpi : ${mpi}/bin/mpiCC ;
182 # On darwin we need to add the `$out/lib` to the libraries' rpath explicitly,
183 # otherwise the dynamic linker is unable to resolve the reference to @rpath
184 # when the boost libraries want to load each other at runtime.
185 + optionalString (stdenv.isDarwin && enableShared) ''
186 cat << EOF >> user-config.jam
187 using clang-darwin : : ${stdenv.cc.targetPrefix}c++
188 : <linkflags>"-rpath $out/lib/"
192 # b2 has trouble finding the correct compiler and tools for cross compilation
193 # since it apparently ignores $CC, $AR etc. Thus we need to set everything
194 # in user-config.jam. To keep things simple we just set everything in an
195 # uniform way for clang and gcc (which works thanks to our cc-wrapper).
196 # We pass toolset later which will make b2 invoke everything in the right
197 # way -- the other toolset in user-config.jam will be ignored.
198 + optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
199 cat << EOF >> user-config.jam
200 using gcc : cross : ${stdenv.cc.targetPrefix}c++
205 using clang : cross : ${stdenv.cc.targetPrefix}c++
212 NIX_CFLAGS_LINK = lib.optionalString stdenv.isDarwin
213 "-headerpad_max_install_names";
215 enableParallelBuilding = true;
217 nativeBuildInputs = [ which boost-build ]
218 ++ optional stdenv.hostPlatform.isDarwin fixDarwinDylibNames;
219 buildInputs = [ expat zlib bzip2 libiconv ]
220 ++ optional (stdenv.hostPlatform == stdenv.buildPlatform) icu
221 ++ optionals enablePython [ libxcrypt python ]
222 ++ optional enableNumpy python.pkgs.numpy;
224 configureScript = "./bootstrap.sh";
225 configurePlatforms = [];
226 dontDisableStatic = true;
227 dontAddStaticConfigureFlags = true;
229 "--includedir=$(dev)/include"
230 "--libdir=$(out)/lib"
231 "--with-bjam=b2" # prevent bootstrapping b2 in configurePhase
232 ] ++ optional enablePython "--with-python=${python.interpreter}"
233 ++ optional (toolset != null) "--with-toolset=${toolset}"
234 ++ [ (if stdenv.hostPlatform == stdenv.buildPlatform then "--with-icu=${icu.dev}" else "--without-icu") ];
245 # boostbook is needed by some applications
246 mkdir -p $dev/share/boostbook
247 cp -a tools/boostbook/{xsl,dtd} $dev/share/boostbook/
249 # Let boost install everything else
256 # Make boost header paths relative so that they are not runtime dependencies
257 cd "$dev" && find include \( -name '*.hpp' -or -name '*.h' -or -name '*.ipp' \) \
258 -exec sed '1s/^\xef\xbb\xbf//;1i#line 1 "{}"' -i '{}' \;
259 '' + optionalString (stdenv.hostPlatform.libc == "msvcrt") ''
260 $RANLIB "$out/lib/"*.a
263 outputs = [ "out" "dev" ];
264 setOutputFlags = false;