mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 13:10:12 +03:00

When DMD is used as a Dub dependency and generates the DMD version as a pre generate command. Dub will execute this command in the current working directory, which usually is the root package working directory, instead of the DMD package directory. This will result in the wrong version, that is, it will pick up the git tag of the root package instead of the DMD package. This looks related: https://github.com/dlang/dub/pull/659.
84 lines
2.2 KiB
D
84 lines
2.2 KiB
D
/+
|
|
dub.sdl:
|
|
name "config"
|
|
targetPath "generated/dub"
|
|
+/
|
|
/**
|
|
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);
|
|
}
|