From 757114a9af669b7f64209cd180cd6663919a219b Mon Sep 17 00:00:00 2001 From: Martin Kinkelin Date: Fri, 1 May 2020 02:34:28 +0200 Subject: [PATCH] Small dmd.vsoptions refactoring --- dmd/vsoptions.d | 77 ++++++++++++++++++------------------------------- 1 file changed, 28 insertions(+), 49 deletions(-) diff --git a/dmd/vsoptions.d b/dmd/vsoptions.d index 55c010980bd..df5134e6745 100644 --- a/dmd/vsoptions.d +++ b/dmd/vsoptions.d @@ -27,14 +27,8 @@ import dmd.root.outbuffer; import dmd.root.rmem; import dmd.root.string; -version (IN_LLVM) -{ - enum supportedPre2017Versions = ["14.0".ptr]; -} -else -{ - enum supportedPre2017Versions = ["14.0".ptr, "12.0", "11.0", "10.0", "9.0"]; -} +version (IN_LLVM) enum supportedPre2017Versions = ["14.0".ptr]; +else enum supportedPre2017Versions = ["14.0".ptr, "12.0", "11.0", "10.0", "9.0"]; extern(C++) struct VSOptions { @@ -361,15 +355,7 @@ public: */ const(char)* getVCBinDir(bool x64, out const(char)* addpath) const { - static const(char)* linkExists(scope const(char)*[] dirs...) - { - auto p = buildPathWithSuffix("link.exe", dirs); - const(char)* result = null; - if (FileName.exists(p)) - result = FileName.path(p); - mem.xfree(p); - return result; - } + alias linkExists = returnDirIfContainsFile!"link.exe"; const bool isHost64 = isWin64Host(); if (VCToolsInstallDir !is null) @@ -478,15 +464,7 @@ public: { if (WindowsSdkDir) { - static const(char)* kernel32Exists(scope const(char)*[] dirs...) - { - auto p = buildPathWithSuffix("kernel32.lib", dirs); - const(char)* result = null; - if (FileName.exists(p)) - result = FileName.path(p); - mem.xfree(p); - return result; - } + alias kernel32Exists = returnDirIfContainsFile!"kernel32.lib"; const(char)* arch = x64 ? "x64" : "x86"; auto sdk = FileName.combine(WindowsSdkDir, "lib"); @@ -685,49 +663,50 @@ char* getenv(wstring name) return null; } -char* buildPathWithSuffix(string suffix, const(char)*[] dirs...) +char* buildPath(const(char)*[] fragments...) { - assert(dirs.length); + assert(fragments.length); - const(char)[][] ddirs = (cast(const(char)[]*) mem.xmalloc_noscan(dirs.length * string.sizeof))[0 .. dirs.length]; - scope(exit) mem.xfree(ddirs.ptr); + const(char)[][] f = (cast(const(char)[]*) mem.xmalloc_noscan(fragments.length * string.sizeof))[0 .. fragments.length]; + scope(exit) mem.xfree(f.ptr); size_t size; - foreach (i, dir; dirs) + foreach (i, fragment; fragments) { - ddirs[i] = dir.toDString; - size += ddirs[i].length + 1; + f[i] = fragment.toDString; + size += f[i].length + 1; } - if (suffix.length) - size += suffix.length + 1; char* p = cast(char*) mem.xmalloc_noscan(size); size_t length; - foreach (dir; ddirs) + foreach (fragment; f) { - assert(dir.length); - p[length .. length + dir.length] = dir; - length += dir.length; + assert(fragment.length); + p[length .. length + fragment.length] = fragment; + length += fragment.length; const last = p[length - 1]; if (last != '\\' && last != '/') p[length++] = '\\'; } - if (suffix.length) - { - p[length .. length + suffix.length] = suffix; - length += suffix.length; - p[length] = 0; - } - else - p[--length] = 0; // overwrite last '\' with null terminator + // overwrite last '\' with null terminator + p[--length] = 0; return p; } -char* buildPath(const(char)*[] fragments...) +char* returnDirIfContainsFile(string fileName)(scope const(char)*[] dirs...) { - return buildPathWithSuffix(null, fragments); + auto dirPath = buildPath(dirs); + + auto filePath = FileName.combine(dirPath, fileName); + scope(exit) FileName.free(filePath); + + if (FileName.exists(filePath)) + return dirPath; + + mem.xfree(dirPath); + return null; } extern (C) int _waccess(const(wchar)* _FileName, int _AccessMode);