mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
hello world example working
This commit is contained in:
parent
925c805d4b
commit
fefbee166d
@ -31,6 +31,7 @@ set(ZIG_SOURCES
|
|||||||
"${CMAKE_SOURCE_DIR}/src/util.cpp"
|
"${CMAKE_SOURCE_DIR}/src/util.cpp"
|
||||||
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
|
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
|
||||||
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
|
"${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
|
||||||
|
"${CMAKE_SOURCE_DIR}/src/os.cpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
|
set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
#include "codegen.hpp"
|
#include "codegen.hpp"
|
||||||
#include "hash_map.hpp"
|
#include "hash_map.hpp"
|
||||||
#include "zig_llvm.hpp"
|
#include "zig_llvm.hpp"
|
||||||
|
#include "os.hpp"
|
||||||
|
|
||||||
#include <stdio.h>
|
#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,
|
LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple,
|
||||||
native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);
|
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/os.cpp
Normal file
33
src/os.cpp
Normal file
@ -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));
|
||||||
|
}
|
||||||
16
src/os.hpp
Normal file
16
src/os.hpp
Normal file
@ -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
|
||||||
@ -1,3 +1,3 @@
|
|||||||
pub fn add(a: int, b: int) -> int {
|
export fn add(a: i32, b: i32) -> i32 {
|
||||||
a + b
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user