| 1 |
// SPDX-License-Identifier: GPL-3.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use fmt; |
| 5 |
use hare::module; |
| 6 |
use hare::unparse; |
| 7 |
use io; |
| 8 |
use memio; |
| 9 |
use os; |
| 10 |
use path; |
| 11 |
use strings; |
| 12 |
|
| 13 |
export fn main() void = { |
| 14 |
if (len(os::args) > 1 && os::args[1] == "-h") { |
| 15 |
fmt::errorln("usage:", os::args[0], "[<tag>...]")!; |
| 16 |
os::exit(0); |
| 17 |
}; |
| 18 |
|
| 19 |
const ctx = module::context { |
| 20 |
harepath = os::tryenv("HAREPATH", "."), |
| 21 |
harecache = os::tryenv("HARECACHE", ".cache"), |
| 22 |
tags = os::args[1..], |
| 23 |
}; |
| 24 |
let mods: []module::module = []; |
| 25 |
defer module::free_slice(mods); |
| 26 |
module::gather(&ctx, &mods, ["rt"])!; |
| 27 |
module::gather(&ctx, &mods, ["cmd", "hare"])!; |
| 28 |
|
| 29 |
let ids: [](str, str) = []; |
| 30 |
defer free(ids); |
| 31 |
defer for (let id .. ids) { |
| 32 |
free(id.0); |
| 33 |
free(id.1); |
| 34 |
}; |
| 35 |
|
| 36 |
let with_libc = for (let tag .. ctx.tags) { |
| 37 |
if (tag == "libc") { |
| 38 |
break true; |
| 39 |
}; |
| 40 |
} else false; |
| 41 |
|
| 42 |
let tds = memio::dynamic(); |
| 43 |
defer io::close(&tds)!; |
| 44 |
let objs = memio::dynamic(); |
| 45 |
defer io::close(&objs)!; |
| 46 |
for (let i = 0z; i < len(mods); i += 1) { |
| 47 |
append(ids, (strings::join("_", mods[i].ns...)!, |
| 48 |
unparse::identstr(mods[i].ns)))!; |
| 49 |
fmt::fprintf(&tds, ` HARE_TD_{}=$(HARECACHE)/{}.td`, |
| 50 |
ids[i].1, ids[i].0)!; |
| 51 |
fmt::fprintf(&objs, ` $(HARECACHE)/{}.o`, ids[i].0)!; |
| 52 |
}; |
| 53 |
|
| 54 |
let cwd = os::getcwd(); |
| 55 |
let buf = path::init(mods[0].srcs.sc[0])!; |
| 56 |
fmt::println(`# generated by cmd/genbootstrap`)!; |
| 57 |
fmt::println(`# DO NOT EDIT BY HAND. run 'make bootstrap' to update`)!; |
| 58 |
fmt::printfln(`TDENV = env{}`, memio::string(&tds)!)!; |
| 59 |
fmt::printfln(`RTSCRIPT = {}`, path::trimprefix(&buf, cwd)!)!; |
| 60 |
fmt::printfln(`OBJS ={}`, memio::string(&objs)!)!; |
| 61 |
|
| 62 |
let deps = memio::dynamic(); |
| 63 |
defer io::close(&deps)!; |
| 64 |
let sources = memio::dynamic(); |
| 65 |
defer io::close(&sources)!; |
| 66 |
for (let i = 0z; i < len(mods); i += 1) { |
| 67 |
memio::reset(&deps); |
| 68 |
for (let j = 0z; j < len(mods[i].deps); j += 1) { |
| 69 |
fmt::fprintf(&deps, ` $(HARECACHE)/{}.td`, |
| 70 |
ids[mods[i].deps[j].0].0)!; |
| 71 |
}; |
| 72 |
memio::reset(&sources); |
| 73 |
for (let j = 0z; j < len(mods[i].srcs.ha); j += 1) { |
| 74 |
path::set(&buf, mods[i].srcs.ha[j])!; |
| 75 |
fmt::fprint(&sources, "", path::trimprefix(&buf, cwd)!)!; |
| 76 |
}; |
| 77 |
|
| 78 |
fmt::println()!; |
| 79 |
fmt::printfln(`{}_ha ={}`, ids[i].0, memio::string(&sources)!)!; |
| 80 |
fmt::printfln(`$(HARECACHE)/{}.ssa: $({}_ha){}`, |
| 81 |
ids[i].0, ids[i].0, memio::string(&deps)!)!; |
| 82 |
fmt::println("\t" `@mkdir -p -- "$(HARECACHE)"`)!; |
| 83 |
fmt::println("\t" `@printf 'HAREC\t%s\n' "$@"`)!; |
| 84 |
fmt::printfln("\t" `@$(TDENV) $(HAREC) $(HARECFLAGS) {}-o $@ -t $(HARECACHE)/{}.td.tmp {} {} $({}_ha)`, |
| 85 |
if (i == len(mods) - 1 && with_libc) `-N "" ` else ``, |
| 86 |
ids[i].0, |
| 87 |
if (i == len(mods) - 1) `$(HARE_DEFINES)` else `-N`, |
| 88 |
if (i == len(mods) - 1) `` else ids[i].1, ids[i].0)!; |
| 89 |
if (len(mods[i].srcs.s) == 0) { |
| 90 |
continue; |
| 91 |
}; |
| 92 |
|
| 93 |
memio::reset(&sources); |
| 94 |
for (let s .. mods[i].srcs.s) { |
| 95 |
path::set(&buf, s)!; |
| 96 |
fmt::fprint(&sources, "", path::trimprefix(&buf, cwd)!)!; |
| 97 |
}; |
| 98 |
|
| 99 |
fmt::println()!; |
| 100 |
fmt::printfln(`{}_s = $(HARECACHE)/{}.s{}`, |
| 101 |
ids[i].0, ids[i].0, memio::string(&sources)!)!; |
| 102 |
fmt::printfln(`$(HARECACHE)/{}.o: $({}_s)`, ids[i].0, ids[i].0)!; |
| 103 |
fmt::println("\t" `@printf 'AS\t%s\n' "$@"`)!; |
| 104 |
fmt::printfln("\t" `@$(AS) $(ASFLAGS) -o $@ $({}_s)`, ids[i].0)!; |
| 105 |
}; |
| 106 |
}; |