From 59b58ef336c58de195454ba1d7b929672e938c3e Mon Sep 17 00:00:00 2001 From: Denis Feklushkin Date: Wed, 7 Aug 2024 23:09:00 +0300 Subject: [PATCH] ldc2.conf: %%ldcconfigpath%% placeholder added --- CHANGELOG.md | 1 + driver/configfile.d | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 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..4d0b648d062 100644 --- a/driver/configfile.d +++ b/driver/configfile.d @@ -18,7 +18,7 @@ import core.stdc.stdio; import core.stdc.string; -string prepareBinDir(const(char)* binDir) +string normalizeSlashes(const(char)* binDir) { immutable len = strlen(binDir); auto res = binDir[0 .. len].dup; @@ -96,6 +96,26 @@ unittest assert(replace(test4, pattern, "word") == "a word, yet other words"); } +struct CfgPaths +{ + string cfgBaseDir; /// ldc2.conf directory + string ldcBinaryDir; /// ldc2.exe binary dir + + this(const(char)* cfPath, const(char)* binDir) + { + import dmd.root.filename: FileName; + + cfgBaseDir = normalizeSlashes(FileName.path(cfPath)); + ldcBinaryDir = normalizeSlashes(binDir); + } +} + +string replacePlaceholders(string str, CfgPaths cfgPaths) +{ + return str + .replace("%%ldcbinarypath%%", cfgPaths.ldcBinaryDir) + .replace("%%ldcconfigpath%%", cfgPaths.cfgBaseDir); +} extern(C++) struct ConfigFile { @@ -117,8 +137,7 @@ private: { switches.setDim(0); postSwitches.setDim(0); - - immutable dBinDir = prepareBinDir(binDir); + const cfgPaths = CfgPaths(cfPath, binDir); try { @@ -156,7 +175,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 +187,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; }