manual c function interop
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user