Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion source/dub/generators/ninja.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ module dub.generators.ninja;
import dub.compilers.compiler;
import dub.generators.generator;
import dub.project;
import dub.internal.vibecompat.inet.path;

import std.algorithm : map, startsWith;
import std.array : join, replace;
import std.path : stripExtension;
import std.stdio : File;
import std.file : exists, thisExePath;

class NinjaGenerator : ProjectGenerator
{
Expand Down Expand Up @@ -47,6 +49,22 @@ class NinjaGenerator : ProjectGenerator
f.writeln(" description = Archiving $out");
f.writeln();

const recipePath = m_project.rootPackage.recipePath.toNativeString();
auto selectionsPath = (m_project.rootPackage.path ~ "dub.selections.json").toNativeString();

f.writeln("rule regen");
f.writeln(" command = ", thisExePath(), " generate ninja");
f.writeln(" generator = 1");
f.writeln(" description = Regenerating build.ninja");
f.writeln();

string[] regenInputs = [recipePath];
if (exists(selectionsPath))
regenInputs ~= selectionsPath;

f.writeln("build build.ninja: regen ", regenInputs.map!(p => escapeNinjaPath(p)).join(" "));
f.writeln();

foreach (name, info; targets)
{
auto bs = info.buildSettings;
Expand All @@ -69,7 +87,7 @@ class NinjaGenerator : ProjectGenerator
foreach (src; bs.sourceFiles)
{
auto obj = objName(src);
f.writeln("build ", obj, ": dc ", src);
f.writeln("build ", obj, ": dc ", escapeNinjaPath(src));
if (flags.length)
f.writeln(" flags = ", flags);
objs ~= obj;
Expand Down Expand Up @@ -103,6 +121,11 @@ class NinjaGenerator : ProjectGenerator
}
}

private static string escapeNinjaPath(string path)
{
return path.replace(":", "$:").replace(" ", "$ ");
}

private static string objName(string src)
{
return stripExtension(src)
Expand Down
2 changes: 2 additions & 0 deletions test/ninja-generator-sdl/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name "ninja-generator-sdl"
targetType "executable"
1 change: 1 addition & 0 deletions test/ninja-generator-sdl/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void main() {}
64 changes: 64 additions & 0 deletions test/ninja-generator.script.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/+ dub.sdl:
name "ninja-generator-regen"
dependency "common" path="./common"
+/

module ninja_generator_regen;

import std.process : environment, execute, Config;
import std.path : buildPath, dirName;
import std.file : setTimes, readText, remove;
import std.datetime : Clock;
import std.algorithm : canFind;
import core.thread : Thread;
import core.time : seconds;

import common;

bool regenerated(string projDir, string touchedFile)
{
Thread.sleep(1.seconds);
setTimes(buildPath(projDir, touchedFile), Clock.currTime, Clock.currTime);
const result = execute(["ninja"], null, Config.none, size_t.max, projDir);
return result.status == 0 && result.output.canFind("Regenerating build.ninja");
Comment thread
atilaneves marked this conversation as resolved.
Outdated
}

int main()
{
const dub = environment.get("DUB", buildPath(__FILE_FULL_PATH__.dirName.dirName, "bin", "dub"));
const dc = environment.get("DC", "dmd");
const curr_dir = environment.get("CURR_DIR", buildPath(__FILE_FULL_PATH__.dirName));
const projDir = buildPath(curr_dir, "ninja-generator");

if (execute([dub, "generate", "ninja", "--compiler", dc], null, Config.none, size_t.max, projDir).status)
die("dub generate ninja failed");

execute(["ninja", "-t", "clean"], null, Config.none, size_t.max, projDir);
if (execute(["ninja"], null, Config.none, size_t.max, projDir).status)
die("initial ninja build failed");

if (!regenerated(projDir, "dub.json"))
die("no regen after touching dub.json");

const sdlProjDir = buildPath(curr_dir, "ninja-generator-sdl");

if (execute([dub, "generate", "ninja", "--compiler", dc], null, Config.none, size_t.max, sdlProjDir).status)
die("dub generate ninja failed for dub.sdl project");

const sdlBuildNinja = buildPath(sdlProjDir, "build.ninja");
if (!readText(sdlBuildNinja).canFind("regen") || !readText(sdlBuildNinja).canFind("dub.sdl"))
die("build.ninja regen edge missing dub.sdl dependency");

execute(["ninja", "-t", "clean"], null, Config.none, size_t.max, sdlProjDir);
if (execute(["ninja"], null, Config.none, size_t.max, sdlProjDir).status)
die("initial ninja build failed for dub.sdl project");

if (!regenerated(sdlProjDir, "dub.sdl"))
die("no regen after touching dub.sdl");

execute(["ninja", "-t", "clean"], null, Config.none, size_t.max, sdlProjDir);
remove(sdlBuildNinja);

log("PASS");
return 0;
}
Loading