This module contains helper dcg predicates to generate ninja build files akin to the ninja_syntax.py python module distributed by ninja.
You can use these predicates if you want to generate your own build.ninja build file.
Example usage:
:- use_module(prolog/ninja).
:- initialization(write_build(build_graph), main).
build_graph -->
rule(cp, "cp $in $out"),
build("input.txt", cp, "output.txt").
Then build.ninja contains the following build specification:
rule generate command = swipl $in generator = 1 build build.ninja: generate /home/kwon-young/prog/ninja/build.pl | /home/kwon-young/prog/ninja/build.pl /home/kwon-young/prog/ninja/prolog/ninja.pl rule cp command = cp $in $out build input.txt: cp output.txt
See the ninja build format documentation for generating more complex build files.
?- phrase(variable(name-"Value"), L), format("~s", [L]).
name = Value
L = [110, 97, 109, 101, 32, 61, 32, 86, 97|...].
?- phrase(variable(name, "Value"), L), format("~s", [L]).
name = Value
L = [110, 97, 109, 101, 32, 61, 32, 86, 97|...].
?- phrase(rule(cp, "cp $in $out"), L), format("~s", [L]).
rule cp
command = cp $in $out
L = [114, 117, 108, 101, 32, 99, 112, 10, 32|...].
?- phrase(rule(cp, "cp $in $out"), L), format("~s", [L]).
rule cp
command = cp $in $out
L = [114, 117, 108, 101, 32, 99, 112, 10, 32|...].
?- phrase(build(["input.txt"], cp, ["output.txt"]), L), format("~s", [L]).
build input.txt: cp output.txt
L = [98, 117, 105, 108, 100, 32, 105, 110, 112|...].
?- phrase(build(["input.txt"], cp, ["output.txt"],
[implicit_outs(["implicit_out.txt"]),
implicit_ins(["implicit_in.txt"]),
orderonly_ins(["orderonly_in.txt"]),
validations(["validation.txt"]),
variables([name-"value"])]), L), format("~s", [L]).
build input.txt | implicit_out.txt: cp output.txt | implicit_in.txt || orderonly_in.txt |@ validation.txt
name = value
L = `build input.txt | implici...alue\n`.
?- phrase(deps("ninja.pl"), L), format("~s", [L]).
ninja.pl /usr/lib64/swipl-9.0.4/library/dcg/basics.pl /usr/lib64/swipl-9.0.4/library/dcg/high_order.pl /usr/lib64/swipl-9.0.4/library/option.pl /usr/lib64/swipl-9.0.4/library/error.pl
L = `ninja.pl /usr/lib64/swipl...or.pl`.
?- phrase(generator("example.pl"), format("~s", [L]).
rule generate
command = swipl $in
generator = 1
build build.ninja: generate example.pl | example.pl /usr/lib64/swipl-9.0.4/library/dcg/basics.pl /usr/lib64/swipl-9.0.4/library/dcg/high_order.pl /home/kwon-young/prog/ninja/prolog/ninja.pl
?- phrase(generator("example.pl"), format("~s", [L]).
rule generate
command = swipl $in
generator = 1
build build.ninja: generate example.pl | example.pl /usr/lib64/swipl-9.0.4/library/dcg/basics.pl /usr/lib64/swipl-9.0.4/library/dcg/high_order.pl /home/kwon-young/prog/ninja/prolog/ninja.pl
?- phrase(seq([
build("foo.jpg", convert, "foo.pdf"),
build("foo.txt", ocr)]), L), format("~s", [L]).
build foo.jpg: convert foo.pdf
build foo.txt: ocr foo.jpg
Notice how "foo.jpg" is specified as input to the ocr build edge although we did not specified as input in the seq rule.
You can still specify options after the rule name in the build rule.
With the following prolog script "build.pl":
:- use_module(library(ninja)). :- initialization(write_build(build_graph), main). build_graph --> rule(cp, "cp $in $out"), build(["input.txt"], cp, ["output.txt"]).
When called on the command line like this:
$ swipl build.pl
Will generate the following "build.ninja" file:
rule generate command = swipl $in generator = 1 build build.ninja: generate /home/kwon-young/prog/ninja/build.pl | /home/kwon-young/prog/ninja/build.pl /home/kwon-young/prog/ninja/prolog/ninja.pl rule cp command = cp $in $out build input.txt: cp output.txt