From 291c9fa5bc475bbee096d4dd1a07931de922c7fa Mon Sep 17 00:00:00 2001 From: Denis Feklushkin Date: Wed, 31 Jul 2024 18:38:47 +0300 Subject: [PATCH] ldc2.conf: %%ldcconfigpath%% placeholder added --- CHANGELOG.md | 1 + driver/configfile.d | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22cf17ed9f1..c8439f1f62d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ #### Big news - Android: NDK for prebuilt package bumped from r26d to r27. (#4711) +- ldc2.conf: %%ldcconfigpath%% placeholder added - specifies the directory where current configuration file is located. (#4717) #### Platform support diff --git a/driver/configfile.d b/driver/configfile.d index ed0eb9f4e37..e080016441a 100644 --- a/driver/configfile.d +++ b/driver/configfile.d @@ -96,6 +96,39 @@ unittest assert(replace(test4, pattern, "word") == "a word, yet other words"); } +const(char)* baseDir(const(char)* path) +{ + import dmd.root.filename: isDirSeparator; + + foreach_reverse (i; 0 .. strlen(path)) + { + if (path[i-1].isDirSeparator) + { + auto res = path[0 .. i].dup; + res[$-1] = '\0'; + + return &res[0]; + } + } + + assert (false); +} + +struct CfgPaths +{ + const(char)* cfPath; /// ldc2.conf path + const(char)* binDir; /// ldc2.exe binary dir +} + +string replacePlaceholders(string str, CfgPaths cfgPaths) +{ + immutable dBinDir = prepareBinDir(cfgPaths.binDir); + immutable dCfgDir = prepareBinDir(cfgPaths.cfPath.baseDir); + + return str + .replace("%%ldcbinarypath%%", dBinDir) + .replace("%%ldcconfigpath%%", dCfgDir); +} extern(C++) struct ConfigFile { @@ -117,8 +150,7 @@ private: { switches.setDim(0); postSwitches.setDim(0); - - immutable dBinDir = prepareBinDir(binDir); + const CfgPaths cfgPaths = { cfPath: cfPath, binDir: binDir }; try { @@ -156,7 +188,7 @@ private: output.reserve(input.vals.length); foreach (sw; input.vals) { - const finalSwitch = sw.replace("%%ldcbinarypath%%", dBinDir) ~ '\0'; + const finalSwitch = sw.replacePlaceholders(cfgPaths) ~ '\0'; output.push(finalSwitch.ptr); } } @@ -168,7 +200,7 @@ private: applyArray(_libDirs, libDirs); if (auto rpath = findScalarSetting(sections, "rpath")) - this.rpathcstr = (rpath.val.replace("%%ldcbinarypath%%", dBinDir) ~ '\0').ptr; + this.rpathcstr = (rpath.val.replacePlaceholders(cfgPaths) ~ '\0').ptr; return true; }