commit fefbee166d68d17dd0d5904fc52f72397fb51092 (tree)
parent 925c805d4b2431b1c3140ba9c9dd7fc81de8a7d7
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Tue, 24 Nov 2015 13:51:36 -0700
hello world example working
Diffstat:
5 files changed, 65 insertions(+), 3 deletions(-)
diff --git 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
@@ -8,6 +8,7 @@
#include "codegen.hpp"
#include "hash_map.hpp"
#include "zig_llvm.hpp"
+#include "os.hpp"
#include <stdio.h>
@@ -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<const char *> 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
@@ -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 <unistd.h>
+#include <errno.h>
+
+void os_spawn_process(const char *exe, ZigList<const char *> &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<const char *>(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<char * const *>(argv));
+ zig_panic("execvp failed: %s", strerror(errno));
+}
diff --git a/src/os.hpp 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<const char *> &args, bool detached);
+
+#endif
diff --git 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;
}