From fefbee166d68d17dd0d5904fc52f72397fb51092 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 24 Nov 2015 13:51:36 -0700 Subject: [PATCH] hello world example working --- CMakeLists.txt | 1 + src/codegen.cpp | 14 +++++++++++++- src/os.cpp | 33 +++++++++++++++++++++++++++++++++ src/os.hpp | 16 ++++++++++++++++ test/add.zig | 4 ++-- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/os.cpp create mode 100644 src/os.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5310a16d7f..8c60947973 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ set(ZIG_SOURCES "${CMAKE_SOURCE_DIR}/src/util.cpp" "${CMAKE_SOURCE_DIR}/src/codegen.cpp" "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp" + "${CMAKE_SOURCE_DIR}/src/os.cpp" ) set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h") diff --git a/src/codegen.cpp b/src/codegen.cpp index 5669408be5..a9e7383321 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8,6 +8,7 @@ #include "codegen.hpp" #include "hash_map.hpp" #include "zig_llvm.hpp" +#include "os.hpp" #include @@ -422,7 +423,18 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) { LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple, native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault); - if (LLVMTargetMachineEmitToFile(target_machine, g->mod, strdup(out_file), LLVMObjectFile, &err_msg)) { + Buf out_file_o = {0}; + buf_init_from_str(&out_file_o, out_file); + buf_append_str(&out_file_o, ".o"); + + if (LLVMTargetMachineEmitToFile(target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) { zig_panic("unable to write object file: %s", err_msg); } + + ZigList args = {0}; + args.append("-o"); + args.append(out_file); + args.append((const char *)buf_ptr(&out_file_o)); + args.append("-lc"); + os_spawn_process("ld", args, false); } diff --git a/src/os.cpp b/src/os.cpp new file mode 100644 index 0000000000..25ce873772 --- /dev/null +++ b/src/os.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015 Andrew Kelley + * + * This file is part of zig, which is MIT licensed. + * See http://opensource.org/licenses/MIT + */ + +#include "os.hpp" +#include "util.hpp" + +#include +#include + +void os_spawn_process(const char *exe, ZigList &args, bool detached) { + pid_t pid = fork(); + if (pid == -1) + zig_panic("fork failed"); + if (pid != 0) + return; + if (detached) { + if (setsid() == -1) + zig_panic("process detach failed"); + } + + const char **argv = allocate(args.length + 2); + argv[0] = exe; + argv[args.length + 1] = nullptr; + for (int i = 0; i < args.length; i += 1) { + argv[i + 1] = args.at(i); + } + execvp(exe, const_cast(argv)); + zig_panic("execvp failed: %s", strerror(errno)); +} diff --git a/src/os.hpp b/src/os.hpp new file mode 100644 index 0000000000..90487e043b --- /dev/null +++ b/src/os.hpp @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2015 Andrew Kelley + * + * This file is part of zig, which is MIT licensed. + * See http://opensource.org/licenses/MIT + */ + +#ifndef ZIG_OS_HPP +#define ZIG_OS_HPP + +#include "list.hpp" +#include "buffer.hpp" + +void os_spawn_process(const char *exe, ZigList &args, bool detached); + +#endif diff --git a/test/add.zig b/test/add.zig index b25094575d..bb0175bff2 100644 --- a/test/add.zig +++ b/test/add.zig @@ -1,3 +1,3 @@ -pub fn add(a: int, b: int) -> int { - a + b +export fn add(a: i32, b: i32) -> i32 { + return a + b; }