manual c function interop

This commit is contained in:
2026-06-11 22:46:12 +02:00
parent 2a79010a57
commit a5ceb727c1
20 changed files with 725 additions and 74 deletions
+46 -12
View File
@@ -1,25 +1,59 @@
package backend
import "../linker"
import "core:fmt"
import "core:mem"
import "core:os"
import "core:os/os2"
import "core:strings"
compile :: proc(llvm_path, output_path: string) -> bool {
append_owned :: proc(command: ^[dynamic]string, value: string, allocator: mem.Allocator) {
append(command, strings.clone(value, allocator))
}
build_command :: proc(
llvm_path, output_path: string,
link_arguments: []linker.Argument,
allocator := context.allocator,
) -> []string {
command: [dynamic]string
command.allocator = allocator
append_owned(&command, "/usr/bin/env", allocator)
append_owned(&command, "ZIG_LOCAL_CACHE_DIR=/tmp/brolang-zig-cache", allocator)
append_owned(&command, "ZIG_GLOBAL_CACHE_DIR=/tmp/brolang-zig-global-cache", allocator)
append_owned(&command, "zig", allocator)
append_owned(&command, "cc", allocator)
append_owned(&command, "-Wno-override-module", allocator)
append_owned(&command, llvm_path, allocator)
for argument in link_arguments {
switch argument.kind {
case .Input:
append_owned(&command, argument.value, allocator)
case .Library_Path:
append(&command, fmt.aprintf("-L%s", argument.value, allocator=allocator))
case .Library:
append(&command, fmt.aprintf("-l%s", argument.value, allocator=allocator))
}
}
append_owned(&command, "-o", allocator)
append_owned(&command, output_path, allocator)
return command[:]
}
destroy_command :: proc(command: []string, allocator := context.allocator) {
for argument in command {
delete(argument, allocator)
}
delete(command, allocator)
}
compile :: proc(llvm_path, output_path: string, link_arguments: []linker.Argument = nil) -> bool {
pid := os2.get_pid()
temporary_output := fmt.tprintf("%s.brolang-tmp-%d", output_path, pid)
defer _ = os.remove(temporary_output)
command := []string{
"/usr/bin/env",
"ZIG_LOCAL_CACHE_DIR=/tmp/brolang-zig-cache",
"ZIG_GLOBAL_CACHE_DIR=/tmp/brolang-zig-global-cache",
"zig",
"cc",
"-Wno-override-module",
llvm_path,
"-o",
temporary_output,
}
command := build_command(llvm_path, temporary_output, link_arguments)
defer destroy_command(command)
state, stdout, stderr, err := os2.process_exec(
os2.Process_Desc{command=command},
context.allocator,