-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.nix
324 lines (264 loc) · 10.6 KB
/
support.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{ stdenv, linkFarmFromDrvs }:
let
baseName = p: with builtins;
let
bp = baseNameOf ( toString p );
isBaseName = mb: ( baseNameOf mb ) == mb;
isNixStorePath = nsp:
let prefix = "/nix/store/"; plen = stringLength prefix; in
prefix == ( substring 0 plen ( toString nsp ) );
removeNixStorePrefix = nsp:
let m = match "/nix/store/[^-]+-(.*)" ( toString nsp ); in
if m == null then nsp else ( head m );
in baseNameOf ( removeNixStorePrefix ( p ) );
baseName' = p: builtins.unsafeDiscardStringContext ( baseName p );
baseNameOfDropExt = p: builtins.head ( builtins.split "\\." ( baseName' p ) );
objName = file: ( baseNameOfDropExt file ) + ".o";
objNames = sources: builtins.concatStringsSep " " ( map objName sources );
/* -------------------------------------------------------------------------- */
# Fixup flags that weren't properly split on spaces.
splitFlags' = f: builtins.filter builtins.isString ( builtins.split " " f );
splitFlags = fs: builtins.concatMap splitFlags' fs;
fixupWlPrefix = s:
if ( ( builtins.substring 0 4 s ) != "-Wl," ) then ( "-Wl," + s ) else s;
fixupLdFlag = flag: with builtins;
let cms = s: concatStringsSep "," ( filter isString ( split " +" s ) );
in fixupWlPrefix ( cms ( toString flag ) );
/* -------------------------------------------------------------------------- */
listFiles = dir:
let
inherit (builtins) readDir attrValues mapAttrs filter;
process = name: type: let bname = baseNameOf name; in
if ( type == "directory" ) then null
else ( ( toString dir ) + "/" + bname );
files = attrValues ( mapAttrs process ( readDir dir ) );
in filter ( x: x != null ) files;
/* -------------------------------------------------------------------------- */
findFileWithSuffix = dir: sfx:
let
fs = builtins.readDir dir;
slen = builtins.stringLength sfx;
suffstring = l: str: let sl = builtins.stringLength str; in
builtins.substring ( sl - l ) sl str;
hasSfx = s:
( slen <= ( builtins.stringLength s ) ) && ( suffstring slen s ) == sfx;
matches = builtins.filter hasSfx ( builtins.attrNames fs );
in ( toString dir ) + "/" + ( builtins.head matches );
/* -------------------------------------------------------------------------- */
cxxBin = findFileWithSuffix "${stdenv.cc}/bin" "g++";
arBin = findFileWithSuffix "${stdenv.cc.bintools.bintools_bin}/bin" "ar";
/* -------------------------------------------------------------------------- */
in rec {
inherit linkFarmFromDrvs stdenv listFiles;
ccDebugFlags = ["-O0" "-ggdb"];
ccOptFlags = ["-O2"];
/* -------------------------------------------------------------------------- */
compileCxx = flags: file: derivation {
name = objName file;
inherit (stdenv) system;
inherit flags file;
builder = cxxBin;
args = [
"-c" ( toString file )
"-o" ( builtins.placeholder "out" )
] ++ ( splitFlags flags );
preferLocalBuild = true;
};
/* -------------------------------------------------------------------------- */
linkCxxExecutable = {
name
, files # Must define `main'
# Passed to the CXX compiler.
# These are commonly flags like `-shared', `-f<...>', `-L<PATH>, or `-l<LIB>'.
, earlyCxxLinkFlags ? [] # Added before PIC archive.
, defaultCxxLinkFlags ? [] # Added before PIC archive.
, cxxLinkFlags ? [] # Added after PIC archive.
# Passed through to link-editor by CXX compiler.
# `-Wl,<FLAG>' is added if missing, and spaces will be replaced by commas
# rather than split into separate arguments.
, earlyLdFlags ? []
, defaultLdFlags ? []
, ldFlags ? []
}:
let
earlyCxxLinkFlags' = splitFlags earlyCxxLinkFlags;
defaultCxxLinkFlags' = splitFlags defaultCxxLinkFlags;
cxxLinkFlags' = splitFlags cxxLinkFlags;
earlyLdFlags' = map fixupLdFlag earlyLdFlags;
defaultLdFlags' = map fixupLdFlag defaultLdFlags;
ldFlags' = map fixupLdFlag ldFlags;
in derivation {
inherit (stdenv) system;
inherit name files;
inherit earlyCxxLinkFlags earlyLdFlags;
inherit defaultCxxLinkFlags defaultLdFlags;
inherit cxxLinkFlags ldFlags;
builder = cxxBin;
args = earlyCxxLinkFlags' ++ earlyLdFlags' ++
defaultCxxLinkFlags' ++ defaultLdFlags' ++
["-o" ( builtins.placeholder "out" )] ++
( map toString files )
++ cxxLinkFlags' ++ ldFlags';
};
/* -------------------------------------------------------------------------- */
mkArchive = name: files: derivation {
inherit name files;
inherit (stdenv) system;
builder = arBin;
args = ["crs" ( builtins.placeholder "out" )] ++ ( map toString files );
preferLocalBuild = true;
};
/* -------------------------------------------------------------------------- */
compileCxxArchive = name: flags: files:
mkArchive name ( map ( compileCxx flags ) files );
compileCxxStaticArchive = name: flags: files:
compileCxxArchive name ( flags ++ ["-static"] ) files;
compileCxxPicArchive = name: flags: files:
compileCxxArchive name ( flags ++ ["-shared" "-fPIC"] ) files;
mkCxxArchives = {
name
, src ? null
, includes ? [( src + "/include" )]
, flags ? []
, files ? listFiles ( src + "/src" )
}:
let
incFlags = map ( p: "-I" + p ) includes;
flags' = ( splitFlags flags ) ++ incFlags;
mkFlavor = suffix: fflags:
let
staticName = name + suffix + ".a";
picName = name + suffix + ".pic.a";
in {
static = compileCxxStaticArchive staticName ( flags' ++ fflags ) files;
pic = compileCxxPicArchive picName ( flags' ++ fflags ) files;
};
opt = mkFlavor "" ccOptFlags;
dbg = mkFlavor ".dbg" ccDebugFlags;
in {
inherit opt dbg;
all = linkFarmFromDrvs ( name + "-lib-archives" )
[opt.static opt.pic dbg.static dbg.pic];
};
/* -------------------------------------------------------------------------- */
sourceDrv = name: src: derivation {
inherit name;
inherit (stdenv) system;
builder = "${stdenv.cc.coreutils_bin}/bin/cp";
args = [
"-pr"
"--reflink=auto"
"--"
( toString src )
( builtins.placeholder "out" )
];
preferLocalBuild = true;
};
/* -------------------------------------------------------------------------- */
# FIXME: ".so" assumes ELF libraries on Linux, Darwin will shit the bed here.
# Flag ordering is:
# CXX ${earlyCxxLinkFlags} ${earlyLdFlags} \
# ${defaultCxxLinkFlags} ${defaultLdFlags} \
# -Wl,--push-state,--whole-archive \
# ${picArchive} -Wl,--pop-state \
# ${cxxLinkFlags} ${ldFlags} \
# -o $out
cxxSharedLibraryFromArchive = {
name ? ( baseNameOfDropExt picArchive.name ) + ".so"
, soname ? name
, picArchive
# Passed to the CXX compiler.
# These are commonly flags like `-shared', `-f<...>', `-L<PATH>, or `-l<LIB>'.
, earlyCxxLinkFlags ? [] # Added before PIC archive.
, defaultCxxLinkFlags ? ["-shared"] # Added before PIC archive.
, cxxLinkFlags ? [] # Added after PIC archive.
# Passed through to link-editor by CXX compiler.
# `-Wl,<FLAG>' is added if missing, and spaces will be replaced by commas
# rather than split into separate arguments.
, earlyLdFlags ? []
, defaultLdFlags ? ["-Wl,-soname,${soname}"]
, ldFlags ? ["-Wl,-z,defs,--no-allow-shlib-undefined"]
}:
let
earlyCxxLinkFlags' = splitFlags earlyCxxLinkFlags;
defaultCxxLinkFlags' = splitFlags defaultCxxLinkFlags;
cxxLinkFlags' = splitFlags cxxLinkFlags;
earlyLdFlags' = map fixupLdFlag earlyLdFlags;
defaultLdFlags' = map fixupLdFlag defaultLdFlags;
ldFlags' = map fixupLdFlag ldFlags;
in derivation {
inherit name soname picArchive;
inherit (stdenv) system;
builder = cxxBin;
args = earlyCxxLinkFlags' ++ earlyLdFlags' ++
defaultCxxLinkFlags' ++ defaultLdFlags' ++ [
"-o" ( builtins.placeholder "out" )
"-Wl,--push-state,--whole-archive"
( toString picArchive )
"-Wl,--pop-state"
] ++ cxxLinkFlags' ++ ldFlags';
};
cxxSharedLibsFromArchives = {
name ? ( baseNameOfDropExt optPicArchive.name )
, optPicArchive
, dbgPicArchive
, commonArgs ? { soname = name + ".so"; }
, optArgOverride ? ( prev: {} )
, dbgArgOverride ? ( prev: {} )
, defaultOptArgOverride ? ( prev: {
name = name + ".so";
picArchive = optPicArchive;
} )
, defaultDbgArgOverride ? ( prev: {
name = name + ".dbg.so";
picArchive = dbgPicArchive;
} )
}:
let
composeOvs = builtins.foldl' ( a: o: a // ( o a ) );
optArgs = composeOvs commonArgs [defaultOptArgOverride optArgOverride];
dbgArgs = composeOvs commonArgs [defaultDbgArgOverride dbgArgOverride];
opt = cxxSharedLibraryFromArchive optArgs;
dbg = cxxSharedLibraryFromArchive dbgArgs;
in {
inherit opt dbg;
all = linkFarmFromDrvs name [opt dbg];
};
/* -------------------------------------------------------------------------- */
mkLibOverride = libs: flavor:
let shLibs = if ( libs ? sharedLibs ) then libs.sharedLibs else libs;
in prev: {
cxxLinkFlags = ( prev.cxxLinkFlags or [] ) ++ [shLibs.${flavor}.outPath];
};
/* -------------------------------------------------------------------------- */
mkCxxLibs = args@{ name, src, ... }:
let
inherit (builtins) intersectAttrs functionArgs;
aflags = intersectAttrs ( functionArgs mkCxxArchives ) args;
archives = mkCxxArchives aflags;
sflags = intersectAttrs ( functionArgs cxxSharedLibsFromArchives ) args;
sharedLibs = cxxSharedLibsFromArchives ( sflags // {
optPicArchive = archives.opt.pic;
dbgPicArchive = archives.dbg.pic;
} );
in {
inherit archives sharedLibs;
all = linkFarmFromDrvs ( name + "-lib" ) [
archives.opt.static archives.opt.pic sharedLibs.opt
archives.dbg.static archives.dbg.pic sharedLibs.dbg
];
};
/* -------------------------------------------------------------------------- */
mkBinLink = name: path: derivation {
inherit name;
inherit (stdenv) system;
builder = "${stdenv.shell}";
PATH = "${stdenv.cc.coreutils_bin}/bin";
args = ["-c" ''mkdir -p "$out"; ln -s ${path} "$out/bin"''];
preferLocalBuild = true;
allowSubstitutes = false;
};
mkBinDir = name: bins:
mkBinLink name ( linkFarmFromDrvs ( name + "-bins" ) bins );
/* -------------------------------------------------------------------------- */
}