Skip to content

Commit

Permalink
parse_tagged_hier.sh -> parse_tagged_hier.d
Browse files Browse the repository at this point in the history
  • Loading branch information
denizzzka committed Jul 20, 2024
1 parent 388ebe4 commit 50f6bbf
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 82 deletions.
2 changes: 1 addition & 1 deletion druntime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ else
TAGS:=$(OS),posix_threads
endif

TGEN_CMD:=./parse_tagged_hier.sh $(TAGGED_SRCS_FILE) $(TAGGED_COPY_LIST_FILE) $(TAGGED_COPY_FILE) $(IMPDIR) $(TAGS) config > /dev/null
TGEN_CMD:=./parse_tagged_hier.d $(TAGGED_SRCS_FILE) $(TAGGED_COPY_LIST_FILE) $(TAGGED_COPY_FILE) $(IMPDIR) $(TAGS) config > /dev/null

TAGGED_SRCS:=$(shell $(TGEN_CMD) && cat $(TAGGED_SRCS_FILE))

Expand Down
115 changes: 115 additions & 0 deletions druntime/parse_tagged_hier.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env dub
/+
dub.sdl:
name "parse_tagged_hier"
+/

import std.algorithm;
import std.array;
import std.conv: to;
import std.file;
import std.exception: enforce;
import std.path;
import std.stdio;
import std.string: splitLines;
import std.typecons;

int main(in string[] args)
{
try
worker(args);
catch(Exception e)
{
stderr.writeln("Error: "~e.msg);
return 1;
}

return 0;
}

void worker(in string[] args)
{
enforce(args.length >= 7 && args.length <= 8, "need 6 or 7 CLI arguments");

immutable dstFile = args[1].buildNormalizedPath; /// i.e. GEN_SRCS file
immutable taggedImportsFile = args[2].buildNormalizedPath; /// i.e. mak/TAGGED_COPY
immutable dstCopyFile = args[3].buildNormalizedPath; /// i.e. GEN_COPY file, generated list of imports choised by tags
immutable impDir = args[4].buildNormalizedPath; /// path to druntime ./import/ dir
immutable tagsArg = args[5]; /// comma separated list of tags
immutable configDir = args[6]; /// path to druntime config/ dir where is placed tags implementations
immutable externalConfigDir = (args.length > 7) ? args[7] : null; /// path to additional (external) config/ dir

enforce(taggedImportsFile.isFile, `Tagged imports file '`~taggedImportsFile~`' not found`);
enforce(impDir.isDir, `DRuntime import/ dir '`~impDir~`' not found`);
enforce(configDir.isDir, `Tags implementations dir '`~configDir~`' not found`);

if(externalConfigDir !is null)
enforce(externalConfigDir.isDir, `Additional tags dir '`~externalConfigDir~`' not found`);

immutable string[] tags = tagsArg.split(",");

writeln("Tags will be applied: ", tagsArg);

immutable allConfigDirs = [configDir, externalConfigDir];

auto availTagsDirs = allConfigDirs
.map!(a => a.dirEntries(SpanMode.shallow))
.join
.filter!(a => a.isDir)
.map!(a => Tuple!(string, "base", string, "path")(a.name.baseName, a.name))
.array
.sort!((a, b) => a.base < b.base);

static struct SrcElem
{
string basePath; // ~/a/b/c/confing_dir/tag_1_name
string tag; // tag_1_name
string relPath; // core/internal/somemodule.d

string fullPath() const => basePath~"/"~relPath; // ~/a/b/c/confing_dir/tag_1_name/core/internal/somemodule.d
}

SrcElem[] resultSrcsList;

foreach(tag; tags)
{
auto foundSUbdirs = availTagsDirs.filter!(a => a.base == tag);

if(foundSUbdirs.empty)
{
stderr.writeln(`Warning: tag '`, tag, `' doesn't corresponds to any subdirectory inside of '`, allConfigDirs,`', skip`);
continue;
}

// tag matched, files from matching dirs should be added to list recursively
auto filesToAdd = foundSUbdirs.map!(
d => dirEntries(d.path, SpanMode.depth)
.filter!(a => a.isFile)
.map!(e => SrcElem(d.path, tag, e.name[d.path.length+1 .. $]))
).join;

foreach(f; filesToAdd)
{
auto found = resultSrcsList.find!((a, b) => a.relPath == b.relPath)(f);

enforce(found.empty, `File '`~f.fullPath~`' overrides already defined file '`~found.front.fullPath~`'`);

resultSrcsList ~= f;
}
}

auto taggedImportsList = taggedImportsFile.readText.replace(`\`, `/`).splitLines.sort.uniq.array;
auto importsToCopy = File(dstCopyFile, "w");

foreach(imp; taggedImportsList)
{
auto found = resultSrcsList.find!(a => a.relPath == imp);
enforce(!found.empty, `Required for import file '`~imp~`' is not found in tagged sources`);

importsToCopy.writeln(found.front.fullPath);
}

resultSrcsList.map!(a => a.fullPath).join("\n").toFile(dstFile);

writeln("All tags applied");
}
81 changes: 0 additions & 81 deletions druntime/parse_tagged_hier.sh

This file was deleted.

0 comments on commit 50f6bbf

Please sign in to comment.