-
Notifications
You must be signed in to change notification settings - Fork 37
/
configure
executable file
·751 lines (643 loc) · 19.5 KB
/
configure
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#!/usr/bin/env dub
/+ dub.sdl:
name "configure"
+/
/**
* This file implements a configuration script that will setup the correct flags
* to link with libclang.
*
* The script will try to automatically detect the location of libclang by
* searching through a number of preset library search paths for different
* platforms.
*
* If the script fails to find libclang or fails to find the correct version,
* this script provides a flag,`--llvm-config`, which can be used by manually
* executing this script (`./configure`) and specifying the path to the LLVM
* configuration binary, `llvm-config`. The LLVM configuration binary will then
* be used to find the location of libclang.
*
* The result of invoking this configuration script is a file,
* `linker_flags.txt`, which will be created. This file contains the necessary
* linker flags which will be read by the linker when building DStep.
*
* This script is only intended to be used on the Posix platforms.
*/
module configure;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.file;
import std.format;
import std.getopt;
import std.path;
import std.process;
import std.range;
import std.string;
import std.uni;
import std.traits;
import std.typecons : Tuple;
version (OSX) {}
else version (linux) {}
else version (FreeBSD) {}
else version (Windows) {}
else
static assert("The current platform is not supported");
/**
* The options of the application.
*
* When parsing the command line arguments, these fields will be set.
*/
struct Options
{
/// Print extra information.
bool verbose = true;
/// Indicates if help/usage information was requested.
bool help = false;
/// The specified path to the LLVM/Clang root directory.
string llvmPath;
/// Indicates if libclang should be statically or dynamically linked.
bool staticallyLinkClang = false;
/**
* Indicates if the whole binary, including the C standard library, should
* be statically linked.
*/
bool staticallyLinkBinary = false;
/// Only generate configuration if needed.
bool ifNeeded = false;
/// The path to the LLVM/Clang library directory.
string llvmLibPath ()
{
return llvmPath.empty ? "" : buildPath(llvmPath, "lib");
}
}
/// This struct contains the name and filename of a library.
struct LibraryName
{
/**
* The name of the library.
*
* Used in error message and similar.
*/
string name;
/// The filename of the library.
string filename;
}
/// Default configuration and paths.
struct DefaultConfig
{
static:
version (D_Ddoc)
{
/// The name of the Clang dynamic library.
enum clangLib = "";
/**
* A list of default paths where to look for the LLVM and Clang
* libraries.
*/
immutable string[] llvmLibPaths = [];
/**
* The name of the C++ standard library.
*
* Used when statically linking libclang.
*/
enum cppLib = "c++";
}
else version (OSX)
{
enum clangLib = "libclang.dylib";
enum standardPaths = [
"/usr/local/lib",
"/usr/lib"
];
enum macPortsPaths = [
"/opt/local/libexec/llvm-14/lib",
"/opt/local/libexec/llvm-10/lib",
"/opt/local/libexec/llvm-9.0/lib",
"/opt/local/libexec/llvm-8.0/lib",
"/opt/local/libexec/llvm-7.0/lib",
"/opt/local/libexec/llvm-6.0/lib",
"/opt/local/libexec/llvm-5.0/lib",
"/opt/local/libexec/llvm-4.0/lib",
"/opt/local/libexec/llvm-3.9/lib",
"/opt/local/libexec/llvm-3.8/lib",
"/opt/local/libexec/llvm-3.7/lib"
];
enum homebrewPaths = [
"/usr/local/opt/llvm@14/lib",
"/usr/local/opt/llvm@10/lib",
"/usr/local/opt/llvm@9/lib",
"/usr/local/opt/llvm@8/lib",
"/usr/local/opt/llvm@7/lib",
"/usr/local/opt/llvm@6/lib",
"/usr/local/opt/llvm@5/lib",
"/usr/local/opt/llvm40/lib",
"/usr/local/opt/llvm39/lib",
"/usr/local/opt/llvm38/lib",
"/usr/local/opt/llvm37/lib"
];
static assert(macPortsPaths.length == homebrewPaths.length);
enum llvmLibPaths = macPortsPaths
.zip(homebrewPaths)
.map!((Tuple!(string, string) t) => [t[0], t[1]])
.joiner
.chain(standardPaths)
.array;
enum cppLib = "c++";
}
else version (linux)
{
enum clangLib = "libclang.so";
enum standardPaths = [
"/usr/lib",
"/usr/local/lib",
"/usr/lib/x86_64-linux-gnu", // Debian
"/usr/lib64", // Fedora
"/usr/lib32", // Fedora
"/data/data/com.termux/files/usr/lib", // Termux
];
enum debianPaths = [
"/usr/lib/llvm-14/lib",
"/usr/lib/llvm-10/lib",
"/usr/lib/llvm-9/lib",
"/usr/lib/llvm-8/lib",
"/usr/lib/llvm-7/lib",
"/usr/lib/llvm-6.0/lib",
"/usr/lib/llvm-5.0/lib",
"/usr/lib/llvm-4.0/lib",
"/usr/lib/llvm-3.9/lib",
"/usr/lib/llvm-3.8/lib",
"/usr/lib/llvm-3.7/lib"
];
enum centOsPaths = [
"/usr/lib64/llvm",
"/usr/lib32/llvm"
];
immutable llvmLibPaths = debianPaths ~ centOsPaths ~ standardPaths;
enum cppLib = "stdc++";
}
else version (FreeBSD)
{
enum clangLib = "libclang.so";
enum standardPaths = [
"/usr/lib",
"/usr/local/lib"
];
immutable llvmLibPaths = [
"/usr/local/llvm14/lib",
"/usr/local/llvm90/lib",
"/usr/local/llvm80/lib",
"/usr/local/llvm70/lib",
"/usr/local/llvm60/lib",
"/usr/local/llvm50/lib",
] ~ standardPaths;
enum cppLib = "c++";
}
else version (Windows)
{
enum clangLib = "clang.dll";
immutable string[] llvmLibPaths = [];
enum cppLib = "";
}
else
static assert(false, "Unsupported platform");
/// The name of the LLVM configure binary.
enum llvmConfigExecutable = "llvm-config";
}
/**
* This class represents a path to a file, like a library or an executable.
*
* It's the abstract base class for the `LibraryPath` and `LLVMConfigPath`
* subclasses.
*/
class Path
{
private
{
/**
* The name of the file this path represents.
*
* This is a name for the file that is used in error messages.
*/
string name;
/**
* A set of standard paths to which to search for the file this path
* represents.
*/
const(string)[] standardPaths;
/**
* The custom path that was specified when invoking this configuration
* script, or `null` if no custom path was specified.
*/
string specifiedPath;
/// The actual file to look for in `standardPaths` and `specifiedPath`.
string fileToCheck;
/// Local cache for the full path to the file.
string path_;
}
alias path this;
/**
* Constructs a new instance of this class.
*
* Params:
* name = the name of the file this path represents
*
* standardPaths = a set of standard paths to which to search for the file
* this path represents
*
* specifiedPath = the custom path that was specified when invoking this
* configuration script, or `null` if no custom path was specified
*
* fileToCheck = the actual file to look for in `standardPaths` and
* `specifiedPath`
*/
this(string name, const(string)[] standardPaths,
string specifiedPath, string fileToCheck)
{
this.name = name;
this.standardPaths = standardPaths;
this.specifiedPath = specifiedPath;
this.fileToCheck = fileToCheck;
}
/**
* Returns the full path to the file this path represents as a string.
*
* If `specifiedPath` is non-empty, `fileToCheck` will be searched for in
* `specifiedPath`. Otherwise `fileToCheck` will be searched for in
* `standardPaths`.
*
* Returns: the full path to the file this path represents
*/
string path()
{
if (path_.ptr)
return path_;
return path_ = specifiedPath.empty ? standardPath : customPath;
}
override string toString()
{
return path;
}
/**
* Returns the full path of `fileToCheck` by searching in `standardPaths`.
*
* Returns: the full path of `fileToCheck` by searching in `standardPaths`
*
* Throws: an `Exception` if `fileToCheck` cannot be found in any of the
* paths in `standardPath`
*/
string standardPath()
{
auto errorMessage = format("Could not find %s in any of the standard " ~
"paths for %s: \n%s\nPlease specify a path manually using " ~
"'./configure --%s-path=<path>'.",
fileToCheck, name, standardPaths.join('\n'), name.toLower
);
auto result = standardPaths.
find!(exists).
find!(e => e.buildPath(fileToCheck).exists);
enforce(!result.empty, errorMessage);
return result.front.absolutePath;
}
private:
/**
* Returns the full path of `fileToCheck` by searching in `specifiedPath`
* and the `PATH` environment variable.
*
* If `fileToCheck` cannot be found in `specifiedPath` it will search for
* `fileToCheck` in the `PATH` environment variable. If that fails, an
* exception is thrown.
*
* Returns: the full path of `fileToCheck`
*
* Throws: an `Exception` if `fileToCheck` cannot be found in
* `specifiedPath` or the `PATH` environment variable
*/
string customPath()
{
auto path = specifiedPath.asAbsolutePath.asNormalizedPath.to!string;
auto errorMessage = format("The specified library %s in path '%s' " ~
"does not exist.", name, path);
if (path.exists)
return path;
path = searchPath(specifiedPath);
enforce(path.exists, errorMessage);
return path;
}
}
/// The name of the file where the configuration is written.
enum configPath = "linker_flags.txt";
/**
* This mixin template contains shared logic to generate the actual
* configuration.
*/
mixin template BaseConfigurator()
{
private
{
/// The options that were the result of parsing the command line flags.
Options options;
/// The default configuration.
DefaultConfig defaultConfig;
/// The LLVM/Clang library path.
Path llvmLibPath;
}
/**
* Initializes the receiver with the given arguments. This method acts as
* the shared constructor.
*
* Params:
* options = the options
* defaultConfig = the default configuration
*/
void initialize(Options options, DefaultConfig defaultConfig)
{
this.options = options;
this.defaultConfig = defaultConfig;
llvmLibPath = new Path(
"llvm",
defaultConfig.llvmLibPaths,
options.llvmLibPath,
defaultConfig.clangLib
);
}
private:
/**
* Writes given configuration to the config file.
*
* Params:
* config = the configuration to write, that is, the linker flags
*/
void writeConfig(string config)
{
write(configPath, config);
}
/// Returns: the configuration, that is, the linker flags.
string config()
{
return flags.filter!(e => !e.empty).join("\n") ~ '\n';
}
}
/**
* This struct contains the logic for generating the configuration for static
* linking.
*/
struct StaticConfigurator
{
mixin BaseConfigurator;
private
{
version (D_Ddoc)
{
/**
* Contains the `--start-group` flag on non-macOS platforms.
*
* Used on non-macOS platforms to group the LLVM and Clang
* libraries to be searched repeatedly to resolve undefined symbols.
*/
enum startGroupFlag = "";
/**
* Contains the `--end-group` flag on non-macOS platforms.
*
* Used on non-macOS platforms to group the LLVM and Clang
* libraries to be searched repeatedly to resolve undefined symbols.
*/
enum endGroupFlag = "";
}
else version (OSX)
{
enum startGroupFlag = "".only;
enum endGroupFlag = "".only;
}
else version (Windows)
{
enum startGroupFlag = "".only;
enum endGroupFlag = "".only;
}
else
{
enum startGroupFlag = "--start-group".only;
enum endGroupFlag = "-Wl,--end-group".only;
}
}
/**
* Constructs a new instance of this struct with the given arguments.
*
* Params:
* options = the options
* defaultConfig = the default configuration
*/
this(Options options, DefaultConfig defaultConfig)
{
initialize(options, defaultConfig);
}
/**
* Generates the actual configuration.
*
* This will locate all required libraries, build a set of linker flags and
* write the result to the configuration file.
*/
void generateConfig()
{
writeConfig(config);
}
private:
/// Return: a range of all the necessary linker flags.
auto flags()
{
return chain(
startGroupFlag,
libclangFlags,
llvmFlags,
endGroupFlag,
cppFlags,
extraFlags
);
}
/**
* Returns: a range of linker flags necessary to link with the standard C++
* library.
*/
auto cppFlags()
{
return format("-l%s", DefaultConfig.cppLib).only;
}
/**
* Returns: a range of linker flags necessary to link with the LLVM
* libraries.
*/
auto llvmFlags()
{
const result = dirEntries(llvmLibPath, "*LLVM*.{a,lib}", SpanMode.shallow)
.map!(e => e.name)
// exclude import library which causes a dynamic link on Windows
.filter!(e => !e.endsWith("LLVM-C.lib"))
.array;
const findAllSymbolsPath = llvmLibPath.buildPath("libfindAllSymbols.a");
return findAllSymbolsPath.exists ? result ~ findAllSymbolsPath : result;
}
/**
* Returns: a range of linker flags necessary to link with the Clang
* libraries.
*/
auto libclangFlags()
{
return dirEntries(llvmLibPath, "{libclang,clang}*.{a,lib}", SpanMode.shallow);
}
auto extraFlags()
{
return (options.staticallyLinkBinary ? "-static" : "").only;
}
}
/**
* This struct contains the logic for generating the configuration for dynamic
* linking.
*/
struct DynamicConfigurator
{
mixin BaseConfigurator;
/**
* Constructs a new instance of this struct with the given arguments.
*
* Params:
* options = the options
* defaultConfig = the default configuration
*/
this(Options options, DefaultConfig defaultConfig)
{
initialize(options, defaultConfig);
}
/**
* Generates the actual configuration.
*
* This will locate all required libraries, build a set of linker flags and
* write the result to the configuration file.
*/
void generateConfig()
{
enforceLibrariesExist("libclang", llvmLibPath, DefaultConfig.clangLib);
writeConfig(config);
}
private:
/// Return: a range of all the necessary linker flags.
auto flags()
{
return format("-L%1$s\n-lclang\n-Xlinker -rpath %1$s", llvmLibPath)
.only;
}
}
/// The main entry point of this script.
void main(string[] args)
{
auto options = parseArguments(args);
if (options.help)
return;
if (options.ifNeeded && exists(configPath))
return;
if (options.staticallyLinkClang)
StaticConfigurator(options, DefaultConfig()).generateConfig();
else
DynamicConfigurator(options, DefaultConfig()).generateConfig();
}
private:
/**
* Parses the command line arguments given to the application.
*
* Params:
* args = the command line arguments to parse
*
* Returns: the options set while parsing the arguments
*/
Options parseArguments(string[] args)
{
import std.typecons : tuple;
Options options;
auto defaultGetoptArgs = tuple(
args,
"llvm-path", "The path to the LLVM/Clang root directory.", &options.llvmPath,
// "ncurses-lib-path", "The path to the ncurses library.", &options.ncursesLibPath,
"statically-link-clang", "Statically link libclang. Defaults to no.", &options.staticallyLinkClang,
"statically-link-binary", "Completely statically link the binary. Defaults to no.", &options.staticallyLinkBinary,
"if-needed", "Only generate the configuration if needed. Defaults to no.", &options.ifNeeded
);
version (OSX)
auto getoptArgs = defaultGetoptArgs;
else
{
auto getoptArgs = tuple(
defaultGetoptArgs.tupleof,
"statically-link-binary", "Completely statically link the binary. Defaults to no.", &options.staticallyLinkBinary
);
}
auto help = getopt(defaultGetoptArgs.tupleof);
postProcessArguments(help, options);
return options;
}
/**
* Post processes the arguments.
*
* This will:
* $(UL
* $(LI Print the help/usage information, if that was requested)
* $(LI
* Set the `help` field of the `options` struct to `true`, if help was
* requested
* )
* $(LI
* Set `staticallyLinkClang` to `true` if `staticallyLinkBinary` is
* true
* )
* )
*
* Params:
* result = the result value from the call to `getopt`
* options = the struct containing the parsed arguments
*/
void postProcessArguments(GetoptResult result, ref Options options)
{
if (options.staticallyLinkBinary)
options.staticallyLinkClang = true;
if (!result.helpWanted)
return;
options.help = true;
defaultGetoptPrinter("Usage: ./configure [options]\n\nOptions:",
result.options);
}
/**
* Enforces that a given set of libraries exist.
*
* Params:
* name = a textual representation of the set of libraries to check for.
* Will be used in error messages
*
* path = the path to the directory where to look for the libraries
* libraries = the actual libraries to look for
*
* Throws: Exception if any of the given libraries don't exist
*/
void enforceLibrariesExist(string name, string path,
const(string)[] libraries ...)
{
auto errorMessage = format("All required %s libraries could not be " ~
"found in the path '%s'.\nRequired libraries are:\n%s", name, path,
libraries.join("\n"));
alias libraryExists = library => path.buildPath(library).exists;
enforce(libraries.all!(libraryExists), errorMessage);
}
/**
* Searches the `PATH` environment variable for the given filename.
*
* Params:
* filename = the filename to search for in the `PATH`
*
* Return: the full path to the given filename if found, otherwise `null`
*/
string searchPath(string filename)
{
auto path =
environment.get("PATH", "").
split(':').
map!(path => path.buildPath(filename)).
find!(exists);
return path.empty ? null : path.front;
}