Skip to content

Commit

Permalink
[buildkit] Improve source translation facilities
Browse files Browse the repository at this point in the history
  • Loading branch information
osdeverr committed Jun 21, 2024
1 parent a7a1aaf commit f5bb278
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
12 changes: 12 additions & 0 deletions buildkit/re/build/default_build_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,22 @@ namespace re

Info(style, " - Running pre-build actions\n");

for (auto &dep : mEnv->GetSingleTargetDepSet(desc.pBuildTarget))
{
mEnv->RunActionsCategorized(dep, &desc, "pre-source-translate");
mEnv->RunAutomaticStructuredTasks(dep, &desc, "pre-source-translate");
}

for (auto &dep : mEnv->GetSingleTargetDepSet(desc.pBuildTarget))
{
for (auto &[key, object] : dep->features)
object->ProcessTargetPreBuild(*dep);
}

for (auto &dep : mEnv->GetSingleTargetDepSet(desc.pBuildTarget))
{
mEnv->RunActionsCategorized(dep, &desc, "post-source-translate");
mEnv->RunAutomaticStructuredTasks(dep, &desc, "post-source-translate");

mEnv->RunActionsCategorized(dep, &desc, "pre-build");
mEnv->RunAutomaticStructuredTasks(dep, &desc, "pre-build");
Expand Down
3 changes: 3 additions & 0 deletions buildkit/re/buildenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ namespace re

void BuildEnv::RunAutomaticStructuredTasks(Target *target, const NinjaBuildDesc *desc, std::string_view stage)
{
if (target->parent)
RunAutomaticStructuredTasks(target->parent, desc, stage);

if (auto tasks = (target->resolved_config ? target->resolved_config : target->config)["tasks"])
{
for (auto kv : tasks)
Expand Down
64 changes: 50 additions & 14 deletions buildkit/re/langs/cxx/features/source_translation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,51 @@ namespace re
if (!supported)
continue;

// fmt::print("translating {} -> {}\n", source.path.generic_u8string(), out_path.generic_u8string());

target.unused_sources.push_back(source);

source.path = out_path;
source.extension = out_ext;

in_path = out_path;

num_steps++;
}
}
}

void SourceTranslation::ProcessTargetPreBuild(Target &target)
{
auto out_dir =
target.build_var_scope->GetVar("source-translation-temp-dir").value_or(".re-cache/source-translation");
auto out_root = target.root_path / out_dir / target.module;

for (auto &source : target.unused_sources)
{
auto in_path = source.path;
auto base_path = source.path.lexically_relative(target.path).u8string();

int num_steps = 0;

for (auto step : target.resolved_config["source-translation-steps"])
{
auto vars = LocalVarScope{&*target.build_var_scope, "translation_exec"};

auto step_suffix = fmt::format("_st_step{}", num_steps);

auto step_name = step["name"];

auto out_ext_node = step["out-extension"];
auto out_ext = out_ext_node ? out_ext_node.Scalar() : source.extension;

auto out_path = out_root / (base_path + step_suffix + "." + out_ext);

vars.SetVar("source-file", in_path.generic_u8string());
vars.SetVar("out-file", out_path.generic_u8string());

auto extensions = step["extensions"];

ulib::string sc = step["command"].Scalar();
ulib::list<ulib::string> command = sc.split(" ");

Expand All @@ -71,22 +116,13 @@ namespace re

fs::create_directories(out_path.parent_path());

if (target.build_var_scope->GetVar("building-sources").value_or("false") == "true" ||
target.build_var_scope->GetVar("do-source-translation").value_or("false") == "true")
{
std::vector<std::string> ccommand;
for (auto& str : command)
ccommand.push_back(str);

re::RunProcessOrThrow(step_name ? step_name.Scalar() : step_suffix.substr(1), {}, ccommand, true, true);
}

// fmt::print("translating {} -> {}\n", source.path.generic_u8string(), out_path.generic_u8string());
std::vector<std::string> ccommand;
for (auto &str : command)
ccommand.push_back(str);

target.unused_sources.push_back(source);
re::RunProcessOrThrow(step_name ? step_name.Scalar() : step_suffix.substr(1), {}, ccommand, true, true);

source.path = out_path;
source.extension = out_ext;
// fmt::print("translating {} -> {}\n", source.path.generic_u8string(), out_path.generic_u8string());

in_path = out_path;

Expand Down
1 change: 1 addition & 0 deletions buildkit/re/langs/cxx/features/source_translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace re
static constexpr auto kFeatureName = "source-translation";

void ProcessTargetPostInit(Target &target);
void ProcessTargetPreBuild(Target &target);
};
}

0 comments on commit f5bb278

Please sign in to comment.