Skip to content

Commit f5fccc9

Browse files
committed
Update: Added archiver and linker information for redub
1 parent 3f2c0fb commit f5fccc9

File tree

7 files changed

+185
-32
lines changed

7 files changed

+185
-32
lines changed

source/redub/buildapi.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import redub.package_searching.api;
88

99

1010
///vX.X.X
11-
enum RedubVersionOnly = "v1.21.12";
11+
enum RedubVersionOnly = "v1.21.13";
1212
///Redub vX.X.X
1313
enum RedubVersionShort = "Redub "~RedubVersionOnly;
1414
///Redub vX.X.X - Description

source/redub/building/compile.d

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ CompilationResult execCompilation(immutable ThreadBuildData data, shared Project
128128

129129
if(!isDCompiler(compiler) && !ret.status) //Always requires link.
130130
{
131-
CompilationResult linkRes = link(cast()pack, hash.requirementHash, data, info, env);
132-
ret.status = linkRes.status;
133-
ret.output~= linkRes.message;
134-
res.compilationCommand~= linkRes.compilationCommand;
131+
string cmd ;
132+
auto archiverRes = executeArchiver(data, info, cmd);
133+
ret.status = archiverRes.status;
134+
ret.output~= archiverRes.output;
135+
res.compilationCommand~= cmd;
135136
}
136137

137138
copyDir(inDir, dirName(outDir));

source/redub/building/utils.d

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,27 @@ auto linkBase(const ThreadBuildData data, CompilingSession session, string rootH
5858
compilationCommand,
5959
session.compiler.isDCompiler,
6060
);
61+
}
62+
63+
auto executeArchiver(const ThreadBuildData data, CompilingSession s, out string command)
64+
{
65+
import std.process;
66+
import std.array;
67+
import redub.command_generators.commons;
68+
import redub.compiler_identification;
69+
Archiver a = s.compiler.archiver;
70+
71+
string cmd = a.bin;
72+
final switch(a.type) with(AcceptedArchiver)
73+
{
74+
case ar, llvmAr: cmd~= " rcs "; break;
75+
case libtool: cmd~= " -static -o "; break;
76+
case none: break;
77+
}
78+
79+
cmd~= getOutputName(data.cfg, s.os, s.isa);
80+
81+
command = join([cmd] ~ data.cfg.sourceFiles, " ");
82+
83+
return executeShell(command);
6184
}

source/redub/command_generators/automatic.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ string getLinkerBin(Compiler compiler)
5151
{
5252
if(compiler.isDCompiler)
5353
return compiler.binOrPath;
54-
return compiler.archiver;
54+
return compiler.linker.bin;
5555
}
5656

5757

@@ -64,7 +64,7 @@ string getLinkCommands(const ThreadBuildData data, CompilingSession s, string ma
6464

6565
if(s.compiler.isDCompiler)
6666
return escapeShellCommand(s.compiler.binOrPath) ~ " "~ processFlags(flags);
67-
return escapeShellCommand(s.compiler.archiver) ~ " " ~ processFlags(flags);
67+
return escapeShellCommand(s.compiler.linker.bin) ~ " " ~ processFlags(flags);
6868
}
6969

7070

source/redub/command_generators/d_compilers.d

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ string function(ValidDFlags) getFlagMapper(AcceptedCompiler comp)
143143
{
144144
case AcceptedCompiler.dmd: return &dmdFlags;
145145
case AcceptedCompiler.ldc2: return &ldcFlags;
146+
case AcceptedCompiler.gcc, AcceptedCompiler.gxx: return &gccFlags;
146147
default: throw new Exception("Compiler sent is not a D compiler.");
147148
}
148149
}
149150

150-
151151
string dmdFlags(ValidDFlags flag)
152152
{
153153
final switch(flag) with (ValidDFlags)
@@ -222,6 +222,43 @@ string ldcFlags(ValidDFlags flag)
222222
}
223223
}
224224

225+
string gccFlags(ValidDFlags flag)
226+
{
227+
final switch(flag) with (ValidDFlags)
228+
{
229+
case debugMode: return null;
230+
case debugInfo: return "-g";
231+
case releaseMode: return "-O3";
232+
case optimize: return "-O3";
233+
case inline: return "-finline";
234+
case noBoundsCheck: return null;
235+
case unittests: return null;
236+
case syntaxOnly: return "-S";
237+
case profile: return null;
238+
case profileGC: return null;
239+
case coverage: return null;
240+
case coverageCTFE: return null;
241+
case mixinFile: return null;
242+
case verbose: return "-v";
243+
case verboseCodeGen: return null;
244+
case timeTrace: return null;
245+
case timeTraceFile: return null;
246+
case enableColor: return null;
247+
case stringImportPaths: return "-I";
248+
case versions: return "-D";
249+
case debugVersions: return "-D";
250+
case importPaths: return "-I";
251+
case objectDir: return null;
252+
case outputFile: return "-o";
253+
case buildAsLibrary: return null;
254+
case buildAsShared: return "-shared";
255+
case compileOnly: return "-c";
256+
case arch: return null;
257+
case preserveNames: return null;
258+
case deps: return null;
259+
}
260+
}
261+
225262
// Determines whether the specified process is running under WOW64 or an Intel64 of x64 processor.
226263
version (Windows)
227264
private bool isWow64() {

source/redub/command_generators/linkers.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ string[] parseLinkConfiguration(const ThreadBuildData data, CompilingSession s,
1010
import redub.misc.path;
1111
import redub.building.cache;
1212
string[] commands;
13-
AcceptedLinker linker = s.compiler.linker;
13+
AcceptedLinker linker = s.compiler.linker.type;
1414
bool emitStartGroup = s.isa != ISA.webAssembly && linker != AcceptedLinker.ld64;
1515

1616

source/redub/compiler_identification.d

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public import redub.libs.semver;
33
import hipjson;
44

55

6-
enum AcceptedCompiler
6+
enum AcceptedCompiler : ubyte
77
{
88
invalid,
99
dmd,
@@ -12,21 +12,44 @@ enum AcceptedCompiler
1212
gxx
1313
}
1414

15-
enum AcceptedLinker
15+
enum AcceptedLinker : ubyte
1616
{
1717
unknown,
1818
gnuld,
1919
ld64,
2020
///I know there a plenty more, but still..
2121
}
2222

23+
enum AcceptedArchiver : ubyte
24+
{
25+
ar,
26+
llvmAr,
27+
libtool,
28+
/**
29+
* D compiler will be used for creating the library
30+
*/
31+
none
32+
}
33+
2334
enum UsesGnuLinker
2435
{
2536
unknown,
2637
yes,
2738
no
2839
}
2940

41+
42+
struct Archiver
43+
{
44+
AcceptedArchiver type;
45+
string bin;
46+
}
47+
struct Linker
48+
{
49+
AcceptedLinker type;
50+
string bin;
51+
}
52+
3053
AcceptedCompiler acceptedCompilerfromString(string str)
3154
{
3255
switch(str)
@@ -54,6 +77,35 @@ AcceptedLinker acceptedLinkerfromString(string str)
5477
}
5578
}
5679

80+
AcceptedArchiver acceptedArchiverFromString(string str)
81+
{
82+
switch(str)
83+
{
84+
static foreach(mem; __traits(allMembers, AcceptedArchiver))
85+
{
86+
case mem:
87+
return __traits(getMember, AcceptedArchiver, mem);
88+
}
89+
default:
90+
return AcceptedArchiver.none;
91+
}
92+
}
93+
94+
private Linker acceptedLinker(JSONValue v)
95+
{
96+
JSONValue* acc = "defaultLinker" in v;
97+
if(!acc)
98+
return Linker(AcceptedLinker.unknown);
99+
return Linker(acceptedLinkerfromString(acc.object["type"].str), acc.object["bin"].str);
100+
}
101+
102+
private Archiver acceptedArchiver(JSONValue v)
103+
{
104+
JSONValue* acc = "defaultArchiver" in v;
105+
if(!acc)
106+
return Archiver(AcceptedArchiver.none);
107+
return Archiver(acceptedArchiverFromString(acc.object["type"].str), acc.object["bin"].str);
108+
}
57109

58110

59111
/**
@@ -75,14 +127,21 @@ struct Compiler
75127
///Accepts both a complete path to an executable or a global environment search path name
76128
string binOrPath;
77129

78-
///Librarian tool
79-
string archiver = "llvm-ar";
130+
/**
131+
* For generating libraries, redub might use dmd/ldc2 by default since that simplifies the logic.
132+
* Although it is slightly slower, this is also a compromise one takes by using integration with C
133+
*/
134+
Archiver archiver = Archiver(AcceptedArchiver.none);
80135

136+
/**
137+
* Currently a flag that only affects Windows. Usually it is turned off since depending on the case, it might
138+
* make compilation slower
139+
*/
81140
bool usesIncremental = false;
82141

83142
///Currently unused. Was used before for checking whether --start-group should be emitted or not. Since it is emitted
84143
///by default, only on webAssembly which is not, it lost its usage for now.
85-
AcceptedLinker linker = AcceptedLinker.unknown;
144+
Linker linker = Linker(AcceptedLinker.unknown);
86145

87146

88147
string getCompilerString() const
@@ -239,7 +298,8 @@ Compiler getCompiler(string compilerOrPath = "dmd", string compilerAssumption =
239298
if(ret == Compiler.init)
240299
ret = inferCompiler(compilerOrPath, compilerAssumption, compilersInfo, isDefault, isGlobal);
241300

242-
ret.linker = acceptedLinkerfromString(compilersInfo["defaultLinker"].str);
301+
ret.linker = acceptedLinker(compilersInfo);
302+
ret.archiver = acceptedArchiver(compilersInfo);
243303

244304
//Checks for ldc.conf switches to see if it is using gnu linker by default
245305
///TODO: Might be reactivated if that issue shows again.
@@ -376,7 +436,7 @@ private Compiler getCompilerFromCache(JSONValue allCompilersInfo, string compile
376436
SemVer(arr[VERSION_].str),
377437
SemVer(arr[FRONTEND_VERSION].str),
378438
arr[VERSION_STRING].str,
379-
key, null, false, acceptedLinkerfromString(allCompilersInfo["defaultLinker"].str)
439+
key, acceptedArchiver(allCompilersInfo), false, acceptedLinker(allCompilersInfo)
380440
);
381441
}
382442
}
@@ -417,11 +477,24 @@ private void saveCompilerInfo(JSONValue allCompilersInfo, ref Compiler compiler,
417477
}
418478
if(!("version" in allCompilersInfo))
419479
allCompilersInfo["version"] = JSONValue(RedubVersionOnly);
480+
if(!("defaultArchiver" in allCompilersInfo))
481+
{
482+
JSONValue defaultArchiver = JSONValue.emptyObject;
483+
auto def = getDefaultArchiver();
484+
defaultArchiver["type"] = def.type.to!string;
485+
defaultArchiver["bin"] = def.bin;
486+
allCompilersInfo["defaultArchiver"] = defaultArchiver;
487+
}
420488

421489
if(!("defaultLinker" in allCompilersInfo))
422-
allCompilersInfo["defaultLinker"] = JSONValue(getDefaultLinker().to!string);
423-
424-
compiler.linker = acceptedLinkerfromString(allCompilersInfo["defaultLinker"].str);
490+
{
491+
JSONValue defaultLinker = JSONValue.emptyObject;
492+
auto def = getDefaultLinker();
493+
defaultLinker["type"] = def.type.to!string;
494+
defaultLinker["bin"] = def.bin;
495+
allCompilersInfo["defaultLinker"] = defaultLinker;
496+
}
497+
compiler.linker = acceptedLinker(allCompilersInfo);
425498

426499
if(!("compilers" in allCompilersInfo))
427500
allCompilersInfo["compilers"] = JSONValue.emptyObject;
@@ -475,23 +548,42 @@ private Compiler assumeCompiler(string compilerOrPath, string compilerAssumption
475548
}
476549

477550

478-
AcceptedLinker getDefaultLinker()
551+
Linker getDefaultLinker()
479552
{
480-
version(Posix)
553+
with(AcceptedLinker)
481554
{
482-
import std.process;
483-
import std.string;
484-
auto res = executeShell("ld -v");
485-
if(res.status != 0)
486-
return AcceptedLinker.unknown;
487-
488-
if(res.output.startsWith("GNU ld"))
489-
return AcceptedLinker.gnuld;
490-
else if(res.output.startsWith("@(#)PROGRAM:ld"))
491-
return AcceptedLinker.ld64;
555+
version(Posix)
556+
{
557+
import std.process;
558+
import std.string;
559+
auto res = executeShell("ld -v");
560+
if(res.status == 0)
561+
{
562+
if(res.output.startsWith("GNU ld"))
563+
return Linker(gnuld, "ld");
564+
else if(res.output.startsWith("@(#)PROGRAM:ld"))
565+
return Linker(ld64, "ld");
566+
}
567+
}
568+
return Linker(unknown);
492569
}
570+
}
493571

494-
return AcceptedLinker.unknown;
572+
573+
Archiver getDefaultArchiver()
574+
{
575+
import std.array:staticArray;
576+
import std.process;
577+
with(AcceptedArchiver)
578+
{
579+
foreach(Archiver v; [Archiver(ar, "ar"), Archiver(llvmAr, "llvm-ar"), Archiver(libtool, "libtool")].staticArray)
580+
{
581+
auto res = executeShell(v.bin~" --help");
582+
if(res.status == 0)
583+
return v;
584+
}
585+
return Archiver(none);
586+
}
495587
}
496588

497589
private bool tryInferLdc(string compilerOrPath, string vString, out Compiler comp)

0 commit comments

Comments
 (0)