Skip to content

Commit 757114a

Browse files
committed
Small dmd.vsoptions refactoring
1 parent e436cb0 commit 757114a

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

dmd/vsoptions.d

+28-49
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,8 @@ import dmd.root.outbuffer;
2727
import dmd.root.rmem;
2828
import dmd.root.string;
2929

30-
version (IN_LLVM)
31-
{
32-
enum supportedPre2017Versions = ["14.0".ptr];
33-
}
34-
else
35-
{
36-
enum supportedPre2017Versions = ["14.0".ptr, "12.0", "11.0", "10.0", "9.0"];
37-
}
30+
version (IN_LLVM) enum supportedPre2017Versions = ["14.0".ptr];
31+
else enum supportedPre2017Versions = ["14.0".ptr, "12.0", "11.0", "10.0", "9.0"];
3832

3933
extern(C++) struct VSOptions
4034
{
@@ -361,15 +355,7 @@ public:
361355
*/
362356
const(char)* getVCBinDir(bool x64, out const(char)* addpath) const
363357
{
364-
static const(char)* linkExists(scope const(char)*[] dirs...)
365-
{
366-
auto p = buildPathWithSuffix("link.exe", dirs);
367-
const(char)* result = null;
368-
if (FileName.exists(p))
369-
result = FileName.path(p);
370-
mem.xfree(p);
371-
return result;
372-
}
358+
alias linkExists = returnDirIfContainsFile!"link.exe";
373359

374360
const bool isHost64 = isWin64Host();
375361
if (VCToolsInstallDir !is null)
@@ -478,15 +464,7 @@ public:
478464
{
479465
if (WindowsSdkDir)
480466
{
481-
static const(char)* kernel32Exists(scope const(char)*[] dirs...)
482-
{
483-
auto p = buildPathWithSuffix("kernel32.lib", dirs);
484-
const(char)* result = null;
485-
if (FileName.exists(p))
486-
result = FileName.path(p);
487-
mem.xfree(p);
488-
return result;
489-
}
467+
alias kernel32Exists = returnDirIfContainsFile!"kernel32.lib";
490468

491469
const(char)* arch = x64 ? "x64" : "x86";
492470
auto sdk = FileName.combine(WindowsSdkDir, "lib");
@@ -685,49 +663,50 @@ char* getenv(wstring name)
685663
return null;
686664
}
687665

688-
char* buildPathWithSuffix(string suffix, const(char)*[] dirs...)
666+
char* buildPath(const(char)*[] fragments...)
689667
{
690-
assert(dirs.length);
668+
assert(fragments.length);
691669

692-
const(char)[][] ddirs = (cast(const(char)[]*) mem.xmalloc_noscan(dirs.length * string.sizeof))[0 .. dirs.length];
693-
scope(exit) mem.xfree(ddirs.ptr);
670+
const(char)[][] f = (cast(const(char)[]*) mem.xmalloc_noscan(fragments.length * string.sizeof))[0 .. fragments.length];
671+
scope(exit) mem.xfree(f.ptr);
694672

695673
size_t size;
696-
foreach (i, dir; dirs)
674+
foreach (i, fragment; fragments)
697675
{
698-
ddirs[i] = dir.toDString;
699-
size += ddirs[i].length + 1;
676+
f[i] = fragment.toDString;
677+
size += f[i].length + 1;
700678
}
701-
if (suffix.length)
702-
size += suffix.length + 1;
703679

704680
char* p = cast(char*) mem.xmalloc_noscan(size);
705681
size_t length;
706-
foreach (dir; ddirs)
682+
foreach (fragment; f)
707683
{
708-
assert(dir.length);
709-
p[length .. length + dir.length] = dir;
710-
length += dir.length;
684+
assert(fragment.length);
685+
p[length .. length + fragment.length] = fragment;
686+
length += fragment.length;
711687
const last = p[length - 1];
712688
if (last != '\\' && last != '/')
713689
p[length++] = '\\';
714690
}
715691

716-
if (suffix.length)
717-
{
718-
p[length .. length + suffix.length] = suffix;
719-
length += suffix.length;
720-
p[length] = 0;
721-
}
722-
else
723-
p[--length] = 0; // overwrite last '\' with null terminator
692+
// overwrite last '\' with null terminator
693+
p[--length] = 0;
724694

725695
return p;
726696
}
727697

728-
char* buildPath(const(char)*[] fragments...)
698+
char* returnDirIfContainsFile(string fileName)(scope const(char)*[] dirs...)
729699
{
730-
return buildPathWithSuffix(null, fragments);
700+
auto dirPath = buildPath(dirs);
701+
702+
auto filePath = FileName.combine(dirPath, fileName);
703+
scope(exit) FileName.free(filePath);
704+
705+
if (FileName.exists(filePath))
706+
return dirPath;
707+
708+
mem.xfree(dirPath);
709+
return null;
731710
}
732711

733712
extern (C) int _waccess(const(wchar)* _FileName, int _AccessMode);

0 commit comments

Comments
 (0)