translate-c file emission

This commit is contained in:
2026-06-24 22:53:20 +02:00
parent f6fc25a899
commit 4cb0ad7f25
7 changed files with 512 additions and 4 deletions
+59
View File
@@ -4,6 +4,7 @@ import "./compiler"
import "./compiler/cimport"
import "./compiler/linker"
import "./compiler/target"
import "./compiler/translatec"
import "core:fmt"
import "core:os/os2"
@@ -85,9 +86,67 @@ print_usage :: proc() {
fmt.eprintln(
"usage: brolang <package-directory> -o <executable> [--target aarch64-macos] [--c-link <path> | --c-library-path <dir> | --c-library <name> | --c-include-path <dir> | --c-define <name[=value]>]...",
)
fmt.eprintln(
" brolang translate-c <header.h> [--target aarch64-macos] [--c-include-path <dir> | --c-define <name[=value]>]...",
)
}
// run_translate_c emits native brolang bindings for a C header to stdout, the
// offline counterpart of `native :: import "x.h"`.
run_translate_c :: proc(args: []string) -> int {
if len(args) < 3 {
print_usage()
return 2
}
header := args[2]
selected := target.DEFAULT
include_paths: [dynamic]string
defines: [dynamic]string
defer delete(include_paths)
defer delete(defines)
cursor := 3
for cursor < len(args) {
option := args[cursor]
cursor += 1
if cursor >= len(args) {
fmt.eprintfln("missing value for %s", option)
return 2
}
value := args[cursor]
cursor += 1
switch option {
case "--c-include-path":
append(&include_paths, value)
case "--c-define":
append(&defines, value)
case "--target":
parsed, ok := target.parse(value)
if !ok {
fmt.eprintfln("unknown target '%s'", value)
return 2
}
selected = parsed
case:
fmt.eprintfln("unknown option '%s'", option)
return 2
}
}
c_options := cimport.Options{include_paths=include_paths[:], defines=defines[:]}
result := cimport.import_header(c_options, header, selected)
defer cimport.destroy_result(&result)
if !result.available {
message := result.error_message if len(result.error_message) > 0 else "failed to import header"
fmt.eprintln(message)
return 1
}
fmt.print(translatec.emit(&result, header))
return 0
}
main :: proc() {
if len(os2.args) >= 2 && os2.args[1] == "translate-c" {
os2.exit(run_translate_c(os2.args))
}
options, valid := parse_cli_args(os2.args)
if !valid {
print_usage()