A Ninja-compatible build tool in MoonBit, verified against ninja 1.13.2
A drop-in replacement for Ninja 1.13.2, written in MoonBit. It is verified by porting ninja's own unit tests and by differential testing against the real ninja binary.
$ moon build --target js --release
$ node _build/js/release/build/cmd/cmd.js -C path/to/project
[1/3] CC foo.o
[2/3] CC bar.o
[3/3] LINK app| ninja 测试文件 | 用例 | moonja 测试文件 |
|---|---|---|
| lexer_test.cc | 7 | lexer_test.mbt |
| manifest_parser_test.cc | 53 | manifest_parser_test.mbt |
| depfile_parser_test.cc | 29 | depfile_parser_test.mbt |
| graph_test.cc | 51 | graph_test.mbt |
| build_log_test.cc | 11 | build_log_test.mbt |
| deps_log_test.cc | 9 | deps_log_test.mbt |
| dyndep_parser_test.cc | 42 | dyndep_parser_test.mbt(34 个测试块) |
| build_test.cc | 118 | plan_test.mbt build_test.mbt build_with_log_test.mbt deps_build_test.mbt dyndep_build_test.mbt |
| clean_test.cc | 19 | clean_test.mbt |
| missing_deps_test.cc / json_test.cc | 7 + 4 | missing_deps_test.mbt |
| edit_distance_test.cc | 4 | edit_distance_test.mbt |
| clparser_test.cc / includes_normalize_test.cc | 8 + 6 | clparser_test.mbt(Windows 用例注入工作目录,在任何平台上都能运行) |
| elide_middle_test.cc / util_test.cc(StripAnsi) | 3 + 2 | status_test.mbt |
| rapidhash | 341 个哈希向量 | rapidhash_vectors_test.mbt(向量取自真实 ninja 写出的 .ninja_log) |
$ moon build --target js
$ node scripts/difftest.mjs --ninja "$(which ninja)"
ok basic (7 runs)
...
16/16 cases identical, 162 runs compared| 场景 | ninja 1.13.2 | moonja(Node 25) | 倍数 |
|---|---|---|---|
| 只解析清单(-t targets all) | 42 ms | 213 ms | 5.0x |
| 全部最新的空构建 | 114 ms | 679 ms | 5.9x |
| 从零开始的空跑(-n) | 1207 ms | 2197 ms | 1.8x |
moonja(核心库,纯 MoonBit,无 I/O)
lexer / manifest_parser / eval_env 清单语言
graph / state / scan 依赖图、脏检查(DependencyScan)
build Plan + Builder(可恢复的状态机)
build_log / deps_log / dyndep 持久化格式
clean / missing_deps / status ... 子工具与输出
moonja/cli(ninja.cc 的主流程,纯 MoonBit)
flags / ninja / runner / tools 参数、构建循环、子工具;系统能力通过 Host 注入
moonja/cmd(Node.js 宿主)
fs / child_process / 终端 唯一和操作系统打交道的地方let state = @moonja.State::new()
let parser = @moonja.ManifestParser::without_files(state)
parser.parse_test("rule cc\n command = cc $in -o $out\nbuild a.o: cc a.c\n")
for edge in state.edges() {
println(edge.evaluate_command()) // cc a.c -o a.o
}pub(open) trait Disk {
fn stat(Self, String) -> Int64 raise NinjaError
fn make_dir(Self, String) -> Bool
fn write_file(Self, String, String) -> Bool
fn read_file(Self, String) -> String? raise NinjaError
fn read_bytes(Self, String) -> Bytes? raise NinjaError
fn remove_file(Self, String) -> Int
fn append_bytes(Self, String, Bytes) -> Unit raise NinjaError
fn rename_file(Self, String, String) -> Unit raise NinjaError
fn truncate_file(Self, String, Int) -> Unit raise NinjaError
}pub(open) trait Status {
fn edge_added_to_plan(Self, Edge) -> Unit
fn edge_removed_from_plan(Self, Edge) -> Unit
fn build_edge_started(Self, Edge, Int64) -> Unit
fn build_edge_finished(Self, Edge, Int64, Int64, ExitStatus, String) -> Unit
fn build_started(Self) -> Unit
fn build_finished(Self) -> Unit
fn info(Self, String) -> Unit
fn warning(Self, String) -> Unit
fn error(Self, String) -> Unit
}pub(open) trait Terminal {
fn write_out(Self, String) -> Unit
fn write_command_output(Self, String) -> Unit
fn write_err(Self, String) -> Unit
fn is_smart(Self) -> Bool
fn supports_color(Self) -> Bool
fn width(Self) -> Int
}pub struct BindingEnv {
bindings : Map[String, String]
rules : Map[String, Rule]
parent : BindingEnv?
}impl Env for BindingEnvfn[E : Env] BindingEnv::lookup_with_fallback(self : BindingEnv, name : String, eval : EvalString?, env : E) -> String raise NinjaErrorpub(all) struct BuildConfig {
verbosity : Verbosity
dry_run : Bool
parallelism : Int
failures_allowed : Int
max_load_average : Double
keep_depfile : Bool
keep_rsp : Bool
explain : Bool
}fn[D : Disk] BuildLog::load(self : BuildLog, disk : D, path : String) -> (LoadStatus, String) raise NinjaErrorfn[D : Disk] BuildLog::open_for_write(self : BuildLog, disk : D, path : String, is_path_dead : (String) -> Bool) -> Unit raise NinjaErrorfn[D : Disk] BuildLog::recompact(self : BuildLog, disk : D, path : String, is_path_dead : (String) -> Bool) -> Unit raise NinjaErrorfn[D : Disk] BuildLog::record_command(self : BuildLog, disk : D, edge : Edge, start_time : Int, end_time : Int, mtime? : Int64) -> Unit raise NinjaErrorpub struct Builder {
state : State
config : BuildConfig
plan : Plan
status : &Status
disk : &Disk
scan : DependencyScan
explanations : Explanations?
lock_file_path : String
start_time_millis : Int64
cwd : String
clock : () -> Int64
running_edges : Map[Int, Int64]
exit_code : ExitStatus
pending_commands : Int
failures_allowed : Int
}fn[R : WaitingCommandRunner + CommandRunner] Builder::build(self : Builder, runner : R) -> (ExitStatus, String)fn[R : CommandRunner] Builder::command_finished(self : Builder, runner : R, result : CommandResult?) -> BuildProgresspub struct Cleaner {
state : State
config : BuildConfig
disk : &Disk
reader : &FileReader?
terminal : &Terminal?
removed : Map[String, Unit]
cleaned : Map[Int, Unit]
cleaned_files_count : Int
status : Int
}fn Cleaner::new(state : State, config : BuildConfig, disk : &Disk, reader? : &FileReader, terminal? : &Terminal) -> Cleanerpub struct DependencyScan {
state : State
build_log : BuildLog?
disk : &Disk
dep_loader : ImplicitDepLoader
explanations : Explanations?
}fn DependencyScan::load_dyndeps(self : DependencyScan, node : Node, file? : DyndepFile) -> Unit raise NinjaErrorfn DependencyScan::new(state : State, disk : &Disk, build_log? : BuildLog, deps_log? : DepsLog, explanations? : Explanations) -> DependencyScanfn DependencyScan::recompute_dirty(self : DependencyScan, initial : Node, validation_nodes? : Array[Node]) -> Unit raise NinjaErrorfn DependencyScan::recompute_outputs_dirty(self : DependencyScan, edge : Edge, most_recent_input : Node?) -> Bool raise NinjaErrorfn[D : Disk] DepsLog::load(self : DepsLog, disk : D, path : String, state : State) -> (LoadStatus, String) raise NinjaErrorfn[D : Disk] DepsLog::open_for_write(self : DepsLog, disk : D, path : String) -> Unit raise NinjaErrorpub struct DyndepParser {
state : State
read : (String) -> String raise NinjaError?
file : DyndepFile
lexer : Lexer
env : BindingEnv
}fn DyndepParser::parse(self : DyndepParser, filename : String, input : String) -> Unit raise NinjaErrorpub struct Edge {
id : Int
rule : Rule
pool : Pool
inputs : Array[Node]
outputs : Array[Node]
validations : Array[Node]
env : BindingEnv
implicit_deps : Int
order_only_deps : Int
implicit_outs : Int
dyndep : Node?
platform : Platform
mark : VisitMark
outputs_ready : Bool
deps_loaded : Bool
deps_missing : Bool
critical_path_weight : Int64
command_start_time : Int64
prev_elapsed_time_millis : Int64
}pub struct GraphViz {
state : State
reader : &FileReader?
out : (String) -> Unit
warn : (String) -> Unit
windows : Bool
visited_nodes : Map[Int, Unit]
visited_edges : Map[Int, Unit]
}fn GraphViz::new(state : State, out : (String) -> Unit, reader? : &FileReader, warn? : (String) -> Unit) -> GraphVizfn ImplicitDepLoader::new(state : State, disk : &Disk, deps_log? : DepsLog, explanations? : Explanations, store? : Array[Node]) -> ImplicitDepLoaderfn IncludesNormalize::normalize(self : IncludesNormalize, input : String) -> String raise NinjaErrorpub struct Lexer {
filename : String
input : String
ofs : Int
last_token : Int
}pub struct LinePrinter {
terminal : &Terminal
smart : Bool
color : Bool
have_blank_line : Bool
console_locked : Bool
line_buffer : String
line_type : LineType
output_buffer : StringBuilder
}pub struct ManifestParser {
state : State
read : (String) -> String raise NinjaError?
options : ManifestParserOptions
env : BindingEnv
quiet : Bool
lexer : Lexer
warnings : Array[String]
}fn ManifestParser::load(self : ManifestParser, filename : String, parent? : Lexer) -> Unit raise NinjaErrorfn[R : FileReader] ManifestParser::new(state : State, reader? : R, options? : ManifestParserOptions) -> ManifestParserfn ManifestParser::parse(self : ManifestParser, filename : String, input : String) -> Unit raise NinjaErrorimpl Disk for MemoryDiskimpl FileReader for MemoryDiskpub struct MissingDependencyScanner {
on_missing_dep : (Node, String, Rule) -> Unit
deps_log : DepsLog
state : State
disk : &Disk
seen : Map[Int, Unit]
nodes_missing_deps : Map[Int, Node]
generated_nodes : Map[Int, Node]
generator_rules : Array[Rule]
missing_dep_path_count : Int
adjacency : Map[(Int, Int), Bool]
}fn MissingDependencyScanner::new(deps_log : DepsLog, state : State, disk : &Disk, on_missing_dep? : (Node, String, Rule) -> Unit) -> MissingDependencyScannerpub struct NullStatus {
}impl Status for NullStatusfn build_edge_finished(_ : NullStatus, _ : Edge, _ : Int64, _ : Int64, _ : ExitStatus, _ : String) -> Unitpub struct Plan {
want : Map[Int, Wanted]
ready : EdgePriorityQueue
targets : Array[Node]
wanted_edges : Int
command_edges : Int
status : &Status
dyndep_loader : (Node) -> Unit raise NinjaError?
}fn Plan::dyndeps_loaded(self : Plan, scan : DependencyScan, node : Node, file : DyndepFile) -> Unit raise NinjaErrortype SlidingRatefn State::add_out(self : State, edge : Edge, path : String, slash_bits : UInt64) -> Unit raise NinjaErrorpub struct StatusPrinter {
config : BuildConfig
printer : LinePrinter
format : String
explanations : Explanations?
started_edges : Int
finished_edges : Int
total_edges : Int
running_edges : Int
time_millis : Int64
cpu_time_millis : Int64
time_predicted_percentage : Double
eta_predictable_edges_total : Int
eta_predictable_cpu_time_total_millis : Int64
eta_predictable_edges_remaining : Int
eta_predictable_cpu_time_remaining_millis : Int64
eta_unpredictable_edges_remaining : Int
current_rate : SlidingRate
}impl Status for StatusPrinterfn build_edge_finished(self : StatusPrinter, edge : Edge, start : Int64, end : Int64, exit_code : ExitStatus, output : String) -> Unitfn StatusPrinter::format_progress_status(self : StatusPrinter, format : String, time_millis : Int64) -> String raise NinjaErrorfn StatusPrinter::new(config : BuildConfig, terminal : &Terminal, format? : String) -> StatusPrinterfn atoi(text : String) -> Intfn compare_lexical(a : String, b : String) -> Intfn edit_distance(s1 : String, s2 : String, allow_replacements? : Bool, max_edit_distance? : Int) -> Intfn elide_middle(text : String, max_width : Int) -> Stringfn encode_json_string(text : String) -> Stringfn filter_input_filename(line : String) -> Boolfn filter_show_includes(line : String, deps_prefix : String) -> Stringfn hash_command(command : String) -> UInt64fn is_system_include(path : String) -> Boolfn[R : FileReader] load_dyndeps(state : State, reader : R, node : Node, file? : DyndepFile) -> Unit raise NinjaErrorlet ninja_version : Stringfn parse_cl_output(output : String, deps_prefix : String, normalizer? : IncludesNormalize) -> ClOutput raise NinjaErrorfn strip_ansi_escape_codes(text : String) -> StringInstall
Download zipA Ninja-compatible build tool in MoonBit, verified against ninja 1.13.2