1{ stdenv, icu, expat, zlib, bzip2, python, fixDarwinDylibNames
2, toolset ? if stdenv.isDarwin then "clang" else null
3, enableRelease ? true
4, enableDebug ? false
5, enableSingleThreaded ? false
6, enableMultiThreaded ? true
7, enableShared ? true
8, enableStatic ? false
9, enablePIC ? false
10, enableExceptions ? false
11, taggedLayout ? ((enableRelease && enableDebug) || (enableSingleThreaded && enableMultiThreaded) || (enableShared && enableStatic))
12, patches ? null
13, mpi ? null
14
15# Attributes inherit from specific versions
16, version, src
17, ...
18}:
19
20# We must build at least one type of libraries
21assert !enableShared -> enableStatic;
22
23with stdenv.lib;
24let
25
26 variant = concatStringsSep ","
27 (optional enableRelease "release" ++
28 optional enableDebug "debug");
29
30 threading = concatStringsSep ","
31 (optional enableSingleThreaded "single" ++
32 optional enableMultiThreaded "multi");
33
34 link = concatStringsSep ","
35 (optional enableShared "shared" ++
36 optional enableStatic "static");
37
38 runtime-link = if enableShared then "shared" else "static";
39
40 # To avoid library name collisions
41 layout = if taggedLayout then "tagged" else "system";
42
43 cflags = if enablePIC && enableExceptions then
44 "cflags=\"-fPIC -fexceptions\" cxxflags=-fPIC linkflags=-fPIC"
45 else if enablePIC then
46 "cflags=-fPIC cxxflags=-fPIC linkflags=-fPIC"
47 else if enableExceptions then
48 "cflags=-fexceptions"
49 else
50 "";
51
52 withToolset = stdenv.lib.optionalString (toolset != null) "--with-toolset=${toolset}";
53
54 genericB2Flags = [
55 "--includedir=$dev/include"
56 "--libdir=$lib/lib"
57 "-j$NIX_BUILD_CORES"
58 "--layout=${layout}"
59 "variant=${variant}"
60 "threading=${threading}"
61 "runtime-link=${runtime-link}"
62 "link=${link}"
63 "${cflags}"
64 ] ++ optional (variant == "release") "debug-symbols=off";
65
66 nativeB2Flags = [
67 "-sEXPAT_INCLUDE=${expat}/include"
68 "-sEXPAT_LIBPATH=${expat}/lib"
69 ] ++ optional (toolset != null) "toolset=${toolset}"
70 ++ optional (mpi != null) "--user-config=user-config.jam";
71 nativeB2Args = concatStringsSep " " (genericB2Flags ++ nativeB2Flags);
72
73 crossB2Flags = [
74 "-sEXPAT_INCLUDE=${expat.crossDrv}/include"
75 "-sEXPAT_LIBPATH=${expat.crossDrv}/lib"
76 "--user-config=user-config.jam"
77 "toolset=gcc-cross"
78 "--without-python"
79 ];
80 crossB2Args = concatMapStringsSep " " (genericB2Flags ++ crossB2Flags);
81
82 builder = b2Args: ''
83 ./b2 ${b2Args}
84 '';
85
86 installer = b2Args: ''
87 # boostbook is needed by some applications
88 mkdir -p $dev/share/boostbook
89 cp -a tools/boostbook/{xsl,dtd} $dev/share/boostbook/
90
91 # Let boost install everything else
92 ./b2 ${b2Args} install
93
94 # Create a derivation which encompasses everything, making buildInputs nicer
95 mkdir -p $out/nix-support
96 echo "$dev $lib" > $out/nix-support/propagated-native-build-inputs
97 '';
98
99 commonConfigureFlags = [
100 "--includedir=$(dev)/include"
101 "--libdir=$(lib)/lib"
102 ];
103
104 fixup = ''
105 # Make boost header paths relative so that they are not runtime dependencies
106 (
107 cd "$dev"
108 find include \( -name '*.hpp' -or -name '*.h' -or -name '*.ipp' \) \
109 -exec sed '1i#line 1 "{}"' -i '{}' \;
110 )
111 '';
112
113in
114
115stdenv.mkDerivation {
116 name = "boost-${version}";
117
118 inherit src patches;
119
120 meta = {
121 homepage = "http://boost.org/";
122 description = "Collection of C++ libraries";
123 license = stdenv.lib.licenses.boost;
124
125 platforms = platforms.unix;
126 maintainers = with maintainers; [ simons wkennington ];
127 };
128
129 preConfigure = ''
130 NIX_LDFLAGS="$(echo $NIX_LDFLAGS | sed "s,$out,$lib,g")"
131 if test -f tools/build/src/tools/clang-darwin.jam ; then
132 substituteInPlace tools/build/src/tools/clang-darwin.jam \
133 --replace '$(<[1]:D=)' "$lib/lib/\$(<[1]:D=)";
134 fi;
135 '' + optionalString (mpi != null) ''
136 cat << EOF > user-config.jam
137 using mpi : ${mpi}/bin/mpiCC ;
138 EOF
139 '';
140
141 NIX_CFLAGS_LINK = stdenv.lib.optionalString stdenv.isDarwin
142 "-headerpad_max_install_names";
143
144 enableParallelBuilding = true;
145
146 buildInputs = [ icu expat zlib bzip2 python ]
147 ++ stdenv.lib.optional stdenv.isDarwin fixDarwinDylibNames;
148
149 configureScript = "./bootstrap.sh";
150 configureFlags = commonConfigureFlags ++ [
151 "--with-icu=${icu}"
152 "--with-python=${python.interpreter}"
153 ] ++ optional (toolset != null) "--with-toolset=${toolset}";
154
155 buildPhase = builder nativeB2Args;
156
157 installPhase = installer nativeB2Args;
158
159 postFixup = fixup;
160
161 outputs = [ "out" "dev" "lib" ];
162
163 crossAttrs = rec {
164 buildInputs = [ expat.crossDrv zlib.crossDrv bzip2.crossDrv ];
165 # all buildInputs set previously fell into propagatedBuildInputs, as usual, so we have to
166 # override them.
167 propagatedBuildInputs = buildInputs;
168 # We want to substitute the contents of configureFlags, removing thus the
169 # usual --build and --host added on cross building.
170 preConfigure = ''
171 export configureFlags="--without-icu ${concatStringsSep " " commonConfigureFlags}"
172 set -x
173 cat << EOF > user-config.jam
174 using gcc : cross : $crossConfig-g++ ;
175 EOF
176 '';
177 buildPhase = builder crossB2Args;
178 installPhase = installer crossB2Args;
179 postFixup = fixup;
180 };
181}