-
Notifications
You must be signed in to change notification settings - Fork 21
/
config.d
executable file
·85 lines (66 loc) · 2.22 KB
/
config.d
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
#!/usr/bin/env dub
/+
dub.sdl:
name "config"
targetPath "generated/dub-gen"
+/
/**
Generates the compiler version, the version printed with `dmd --version`.
Outputs a file with the generated version which is imported as a string literal
in the compiler source code.
*/
module config;
void main(const string[] args)
{
import std.file : mkdirRecurse, readText;
import std.path : buildPath;
const outputDirectory = args[1];
const versionFile = args[2];
version (Posix)
const sysConfigDirectory = args[3];
mkdirRecurse(outputDirectory);
const version_ = generateVersion(versionFile);
updateIfChanged(buildPath(outputDirectory, "VERSION"), version_);
version (Posix)
{
const path = buildPath(outputDirectory, "SYSCONFDIR.imp");
updateIfChanged(path, sysConfigDirectory);
}
}
/**
Generates the version for the compiler.
If anything goes wrong in the process the contents of the file
`versionFile` will be returned.
Params:
versionFile = a file containing a version, used for backup if generating the
version fails
Returns: the generated version, or the content of `versionFile`
*/
string generateVersion(const string versionFile)
{
import std.process : execute;
import std.file : readText;
import std.path : dirName;
import std.string : strip;
enum workDir = __FILE_FULL_PATH__.dirName;
const result = execute(["git", "-C", workDir, "describe", "--dirty"]);
return result.status == 0 ? result.output.strip : versionFile.readText;
}
/**
Writes given the content to the given file.
The content will only be written to the file specified in `path` if that file
doesn't exist, or the content of the existing file is different from the given
content.
This makes sure the timestamp of the file is only updated when the
content has changed. This will avoid rebuilding when the content hasn't changed.
Params:
path = the path to the file to write the content to
content = the content to write to the file
*/
void updateIfChanged(const string path, const string content)
{
import std.file : exists, readText, write;
const existingContent = path.exists ? path.readText : "";
if (content != existingContent)
write(path, content);
}