first class support for compiling C code

New CLI parameter: --c-source [options] [file]

It even works with `--cache on` when there are transitive dependencies.

Instead of `builder.addCExecutable`, use `builder.addExecutable` and pass
`null` for the root source file. Then use `builder.addCSourceFile`,
which takes the path to the C code, and a list of C compiler args.
Be sure to linkSystemLibrary("c") if you want libc headers to be
available.

Merge TestStep into LibExeObjStep. That was long overdue.
master
Andrew Kelley 2019-02-25 11:37:54 -05:00
parent e5d4862e14
commit e76ce2c1d0
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
8 changed files with 379 additions and 723 deletions

View File

@ -3,10 +3,10 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const obj = b.addObject("base64", "base64.zig");
const exe = b.addCExecutable("test");
exe.addCompileFlags([][]const u8{"-std=c99"});
exe.addSourceFile("test.c");
const exe = b.addExecutable("test", null);
exe.addCSourceFile("test.c",[][]const u8{"-std=c99"});
exe.addObject(obj);
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);

View File

@ -3,10 +3,10 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
const exe = b.addCExecutable("test");
exe.addCompileFlags([][]const u8{"-std=c99"});
exe.addSourceFile("test.c");
const exe = b.addExecutable("test", null);
exe.addCSourceFile("test.c", [][]const u8{"-std=c99"});
exe.linkLibrary(lib);
exe.linkSystemLibrary("c");
b.default_step.dependOn(&exe.step);

View File

@ -1611,6 +1611,11 @@ enum ValgrindSupport {
ValgrindSupportEnabled,
};
struct CFile {
ZigList<const char *> args;
const char *source_path;
};
// When adding fields, check if they should be added to the hash computation in build_with_cache
struct CodeGen {
//////////////////////////// Runtime State
@ -1788,6 +1793,7 @@ struct CodeGen {
bool verbose_ir;
bool verbose_llvm_ir;
bool verbose_cimport;
bool verbose_cc;
bool error_during_imports;
bool generate_error_name_table;
bool enable_cache;
@ -1805,6 +1811,7 @@ struct CodeGen {
ZigList<Buf *> forbidden_libs;
ZigList<Buf *> link_objects;
ZigList<Buf *> assembly_files;
ZigList<CFile *> c_source_files;
ZigList<const char *> lib_dirs;
ZigLibCInstallation *libc;

View File

@ -7885,8 +7885,8 @@ static void detect_libc(CodeGen *g) {
fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
exit(1);
}
if (rename(buf_ptr(native_libc_tmp), buf_ptr(native_libc_txt)) == -1) {
fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(native_libc_txt), strerror(errno));
if ((err = os_rename(native_libc_tmp, native_libc_txt))) {
fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(native_libc_txt), err_str(err));
exit(1);
}
}
@ -8123,6 +8123,162 @@ static void gen_global_asm(CodeGen *g) {
}
}
static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
Error err;
Buf *c_source_file = buf_create_from_str(c_file->source_path);
Buf *c_source_basename = buf_alloc();
os_path_split(c_source_file, nullptr, c_source_basename);
Buf *out_obj_name = buf_sprintf("%s%s", buf_ptr(c_source_basename), target_o_file_ext(g->zig_target));
Buf *out_obj_path = buf_alloc();
os_path_join(&g->cache_dir, out_obj_name, out_obj_path);
Buf *out_dep_name = buf_sprintf("%s.d", buf_ptr(c_source_file));
Buf *out_dep_path = buf_alloc();
os_path_join(&g->cache_dir, out_dep_name, out_dep_path);
Termination term;
ZigList<const char *> args = {};
args.append("cc");
if (g->enable_cache) {
args.append("-MD");
args.append("-MF");
args.append(buf_ptr(out_dep_path));
}
args.append("-isystem");
args.append(buf_ptr(g->zig_c_headers_dir));
if (g->libc != nullptr) {
args.append("-isystem");
args.append(buf_ptr(&g->libc->include_dir));
}
if (g->zig_target->is_native) {
args.append("-march=native");
} else {
args.append("-target");
args.append(buf_ptr(&g->triple_str));
}
if (!g->strip_debug_symbols) {
args.append("-g");
}
switch (g->build_mode) {
case BuildModeDebug:
if (g->libc_link_lib != nullptr) {
args.append("-fstack-protector-strong");
args.append("--param");
args.append("ssp-buffer-size=4");
} else {
args.append("-fno-stack-protector");
}
break;
case BuildModeSafeRelease:
args.append("-O2");
if (g->libc_link_lib != nullptr) {
args.append("-D_FORTIFY_SOURCE=2");
args.append("-fstack-protector-strong");
args.append("--param");
args.append("ssp-buffer-size=4");
} else {
args.append("-fno-stack-protector");
}
break;
case BuildModeFastRelease:
args.append("-O2");
args.append("-fno-stack-protector");
break;
case BuildModeSmallRelease:
args.append("-Os");
args.append("-fno-stack-protector");
break;
}
args.append("-o");
args.append(buf_ptr(out_obj_path));
args.append("-c");
args.append(buf_ptr(c_source_file));
if (!g->disable_pic) {
args.append("-fPIC");
}
for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) {
args.append(g->clang_argv[arg_i]);
}
for (size_t arg_i = 0; arg_i < c_file->args.length; arg_i += 1) {
args.append(c_file->args.at(arg_i));
}
if (g->verbose_cc) {
for (size_t arg_i = 0; arg_i < args.length; arg_i += 1) {
fprintf(stderr, "%s ", args.at(arg_i));
}
fprintf(stderr, "\n");
}
os_spawn_process(buf_ptr(self_exe_path), args, &term);
if (term.how != TerminationIdClean || term.code != 0) {
fprintf(stderr, "`zig cc` failed\n");
exit(1);
}
g->link_objects.append(out_obj_path);
// add the files depended on to the cache system
if (g->enable_cache) {
Buf *contents = buf_alloc();
if ((err = os_fetch_file_path(out_dep_path, contents, false))) {
fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
exit(1);
}
if ((err = cache_add_file(&g->cache_hash, c_source_file))) {
fprintf(stderr, "unable to add %s to cache: %s\n", buf_ptr(c_source_file), err_str(err));
exit(1);
}
SplitIterator it = memSplit(buf_to_slice(contents), str("\n"));
// skip first line
SplitIterator_next(&it);
for (;;) {
Optional<Slice<uint8_t>> opt_line = SplitIterator_next(&it);
if (!opt_line.is_some)
break;
if (opt_line.value.len == 0)
continue;
SplitIterator line_it = memSplit(opt_line.value, str(" \t"));
Slice<uint8_t> filename;
if (!SplitIterator_next(&line_it).unwrap(&filename))
continue;
Buf *filename_buf = buf_create_from_slice(filename);
if ((err = cache_add_file(&g->cache_hash, filename_buf))) {
fprintf(stderr, "unable to add %s to cache: %s\n", buf_ptr(c_source_file), err_str(err));
exit(1);
}
}
}
}
static void gen_c_objects(CodeGen *g) {
Error err;
if (g->c_source_files.length == 0)
return;
Buf *self_exe_path = buf_alloc();
if ((err = os_self_exe_path(self_exe_path))) {
fprintf(stderr, "Unable to get self exe path: %s\n", err_str(err));
exit(1);
}
for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
CFile *c_file = g->c_source_files.at(c_file_i);
gen_c_object(g, self_exe_path, c_file);
}
}
void codegen_add_object(CodeGen *g, Buf *object_path) {
g->link_objects.append(object_path);
}
@ -8637,6 +8793,13 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);
cache_list_of_file(ch, g->link_objects.items, g->link_objects.length);
cache_list_of_file(ch, g->assembly_files.items, g->assembly_files.length);
for (size_t c_file_i = 0; c_file_i < g->c_source_files.length; c_file_i += 1) {
CFile *c_file = g->c_source_files.at(c_file_i);
cache_file(ch, buf_create_from_str(c_file->source_path));
for (size_t opt_i = 0; opt_i < c_file->args.length; opt_i += 1) {
cache_buf(ch, buf_create_from_str(c_file->args.at(opt_i)));
}
}
cache_int(ch, g->emit_file_type);
cache_int(ch, g->build_mode);
cache_int(ch, g->out_type);
@ -8788,6 +8951,7 @@ void codegen_build_and_link(CodeGen *g) {
gen_global_asm(g);
gen_root_source(g);
gen_c_objects(g);
if (g->enable_cache) {
if ((err = cache_final(&g->cache_hash, &digest))) {
@ -8805,16 +8969,25 @@ void codegen_build_and_link(CodeGen *g) {
resolve_out_paths(g);
codegen_add_time_event(g, "Code Generation");
do_code_gen(g);
codegen_add_time_event(g, "LLVM Emit Output");
zig_llvm_emit_output(g);
if (g->out_type == OutTypeObj && g->c_source_files.length == 1) {
assert(g->link_objects.length == 1);
if ((err = os_rename(g->link_objects.pop(), &g->o_file_output_path))) {
fprintf(stderr, "unable to move object to '%s': %s\n",
buf_ptr(&g->o_file_output_path), err_str(err));
exit(1);
}
} else {
do_code_gen(g);
codegen_add_time_event(g, "LLVM Emit Output");
zig_llvm_emit_output(g);
if (g->out_h_path != nullptr) {
codegen_add_time_event(g, "Generate .h");
gen_h_file(g);
}
if (g->out_type != OutTypeObj && g->emit_file_type == EmitFileTypeBinary) {
codegen_link(g);
if (g->out_h_path != nullptr) {
codegen_add_time_event(g, "Generate .h");
gen_h_file(g);
}
if (g->out_type != OutTypeObj && g->emit_file_type == EmitFileTypeBinary) {
codegen_link(g);
}
}
}

View File

@ -18,7 +18,7 @@
#include <stdio.h>
static int print_error_usage(const char *arg0) {
fprintf(stderr, "See `%s help` for detailed usage information\n", arg0);
fprintf(stderr, "See `%s --help` for detailed usage information\n", arg0);
return EXIT_FAILURE;
}
@ -34,7 +34,6 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
" builtin show the source code of that @import(\"builtin\")\n"
" cc C compiler\n"
" fmt parse files and render in canonical zig format\n"
" help show this usage information\n"
" id print the base64-encoded compiler id\n"
" init-exe initialize a `zig build` application in the cwd\n"
" init-lib initialize a `zig build` library in the cwd\n"
@ -48,6 +47,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
"\n"
"Compile Options:\n"
" --assembly [source] add assembly file to build\n"
" --c-source [options] [file] compile C source code\n"
" --cache-dir [path] override the cache directory\n"
" --cache [auto|off|on] build in global cache, print out paths to stdout\n"
" --color [auto|off|on] enable or disable colored error messages\n"
@ -77,6 +77,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
" --verbose-ir enable compiler debug output for Zig IR\n"
" --verbose-llvm-ir enable compiler debug output for LLVM IR\n"
" --verbose-cimport enable compiler debug output for C imports\n"
" --verbose-cc enable compiler debug output for C compilation\n"
" -dirafter [dir] same as -isystem but do it last\n"
" -isystem [dir] add additional search path for other .h files\n"
" -mllvm [arg] forward an arg to LLVM's option processing\n"
@ -181,7 +182,6 @@ enum Cmd {
CmdNone,
CmdBuild,
CmdBuiltin,
CmdHelp,
CmdRun,
CmdTargets,
CmdTest,
@ -385,6 +385,7 @@ int main(int argc, char **argv) {
bool verbose_ir = false;
bool verbose_llvm_ir = false;
bool verbose_cimport = false;
bool verbose_cc = false;
ErrColor color = ErrColorAuto;
CacheOpt enable_cache = CacheOptAuto;
const char *libc_txt = nullptr;
@ -404,6 +405,7 @@ int main(int argc, char **argv) {
ZigList<const char *> rpath_list = {0};
bool each_lib_rpath = false;
ZigList<const char *> objects = {0};
ZigList<CFile *> c_source_files = {0};
ZigList<const char *> asm_files = {0};
const char *test_filter = nullptr;
const char *test_name_prefix = nullptr;
@ -512,6 +514,7 @@ int main(int argc, char **argv) {
" --verbose-ir Enable compiler debug output for Zig IR\n"
" --verbose-llvm-ir Enable compiler debug output for LLVM IR\n"
" --verbose-cimport Enable compiler debug output for C imports\n"
" --verbose-cc Enable compiler debug output for C compilation\n"
"\n"
, zig_exe_path);
return EXIT_SUCCESS;
@ -521,7 +524,7 @@ int main(int argc, char **argv) {
"No 'build.zig' file found.\n"
"Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,\n"
"or build an executable directly with `zig build-exe $FILENAME.zig`.\n"
"See: `zig build --help` or `zig help` for more options.\n"
"See: `zig build --help` or `zig --help` for more options.\n"
);
return EXIT_FAILURE;
}
@ -587,9 +590,9 @@ int main(int argc, char **argv) {
build_mode = BuildModeSmallRelease;
} else if (strcmp(arg, "--help") == 0) {
if (cmd == CmdLibC) {
return print_libc_usage(arg0, stderr, EXIT_FAILURE);
return print_libc_usage(arg0, stdout, EXIT_SUCCESS);
} else {
return print_full_usage(arg0, stderr, EXIT_FAILURE);
return print_full_usage(arg0, stdout, EXIT_SUCCESS);
}
} else if (strcmp(arg, "--strip") == 0) {
strip = true;
@ -607,6 +610,8 @@ int main(int argc, char **argv) {
verbose_llvm_ir = true;
} else if (strcmp(arg, "--verbose-cimport") == 0) {
verbose_cimport = true;
} else if (strcmp(arg, "--verbose-cc") == 0) {
verbose_cc = true;
} else if (strcmp(arg, "-rdynamic") == 0) {
rdynamic = true;
} else if (strcmp(arg, "--each-lib-rpath") == 0) {
@ -714,6 +719,19 @@ int main(int argc, char **argv) {
forbidden_link_libs.append(argv[i]);
} else if (strcmp(arg, "--object") == 0) {
objects.append(argv[i]);
} else if (strcmp(arg, "--c-source") == 0) {
CFile *c_file = allocate<CFile>(1);
for (;;) {
if (argv[i][0] == '-') {
c_file->args.append(argv[i]);
i += 1;
continue;
} else {
c_file->source_path = argv[i];
c_source_files.append(c_file);
break;
}
}
} else if (strcmp(arg, "--assembly") == 0) {
asm_files.append(argv[i]);
} else if (strcmp(arg, "--cache-dir") == 0) {
@ -792,8 +810,6 @@ int main(int argc, char **argv) {
} else if (strcmp(arg, "build-lib") == 0) {
cmd = CmdBuild;
out_type = OutTypeLib;
} else if (strcmp(arg, "help") == 0) {
cmd = CmdHelp;
} else if (strcmp(arg, "run") == 0) {
cmd = CmdRun;
out_type = OutTypeExe;
@ -835,7 +851,6 @@ int main(int argc, char **argv) {
}
break;
case CmdBuiltin:
case CmdHelp:
case CmdVersion:
case CmdZen:
case CmdTargets:
@ -910,15 +925,43 @@ int main(int argc, char **argv) {
case CmdTranslateC:
case CmdTest:
{
if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0) {
fprintf(stderr, "Expected source file argument or at least one --object or --assembly argument.\n");
if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 &&
c_source_files.length == 0)
{
fprintf(stderr,
"Expected at least one of these things:\n"
" * Zig root source file argument\n"
" * --object argument\n"
" * --assembly argument\n"
" * --c-source argument\n");
return print_error_usage(arg0);
} else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) {
fprintf(stderr, "Expected source file argument.\n");
return print_error_usage(arg0);
} else if (cmd == CmdBuild && out_type == OutTypeObj && objects.length != 0) {
fprintf(stderr, "When building an object file, --object arguments are invalid.\n");
return print_error_usage(arg0);
} else if (cmd == CmdBuild && out_type == OutTypeObj) {
if (objects.length != 0) {
fprintf(stderr,
"When building an object file, --object arguments are invalid.\n"
"Consider building a static library instead.\n");
return print_error_usage(arg0);
}
size_t zig_root_src_count = in_file ? 1 : 0;
if (zig_root_src_count + c_source_files.length > 1) {
fprintf(stderr,
"When building an object file, only one of these allowed:\n"
" * Zig root source file argument\n"
" * --c-source argument\n"
"Consider building a static library instead.\n");
return print_error_usage(arg0);
}
if (c_source_files.length != 0 && asm_files.length != 0) {
fprintf(stderr,
"When building an object file, only one of these allowed:\n"
" * --assembly argument\n"
" * --c-source argument\n"
"Consider building a static library instead.\n");
return print_error_usage(arg0);
}
}
assert(cmd != CmdBuild || out_type != OutTypeUnknown);
@ -945,6 +988,13 @@ int main(int argc, char **argv) {
}
}
if (need_name && buf_out_name == nullptr && c_source_files.length == 1) {
Buf basename = BUF_INIT;
os_path_split(buf_create_from_str(c_source_files.at(0)->source_path), nullptr, &basename);
buf_out_name = buf_alloc();
os_path_extname(&basename, buf_out_name, nullptr);
}
if (need_name && buf_out_name == nullptr) {
fprintf(stderr, "--name [name] not provided and unable to infer\n\n");
return print_error_usage(arg0);
@ -996,6 +1046,7 @@ int main(int argc, char **argv) {
g->verbose_ir = verbose_ir;
g->verbose_llvm_ir = verbose_llvm_ir;
g->verbose_cimport = verbose_cimport;
g->verbose_cc = verbose_cc;
codegen_set_errmsg_color(g, color);
g->system_linker_hack = system_linker_hack;
@ -1048,6 +1099,7 @@ int main(int argc, char **argv) {
add_package(g, cur_pkg, g->root_package);
if (cmd == CmdBuild || cmd == CmdRun || cmd == CmdTest) {
g->c_source_files = c_source_files;
for (size_t i = 0; i < objects.length; i += 1) {
codegen_add_object(g, buf_create_from_str(objects.at(i)));
}
@ -1147,8 +1199,6 @@ int main(int argc, char **argv) {
zig_unreachable();
}
}
case CmdHelp:
return print_full_usage(arg0, stdout, EXIT_SUCCESS);
case CmdVersion:
printf("%s\n", ZIG_VERSION_STRING);
return EXIT_SUCCESS;
@ -1158,7 +1208,6 @@ int main(int argc, char **argv) {
case CmdTargets:
return print_target_list(stdout);
case CmdNone:
fprintf(stderr, "Zig programming language\n");
return print_error_usage(arg0);
return print_full_usage(arg0, stderr, EXIT_FAILURE);
}
}

View File

@ -32,6 +32,10 @@ pub const BufSet = struct {
}
}
pub fn exists(self: BufSet, key: []const u8) bool {
return self.hash_map.get(key) != null;
}
pub fn delete(self: *BufSet, key: []const u8) void {
const entry = self.hash_map.remove(key) orelse return;
self.free(entry.key);

File diff suppressed because it is too large Load Diff

View File

@ -3,9 +3,10 @@ const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const rel_opts = b.standardReleaseOptions();
const c_obj = b.addCObject("cfuncs", "cfuncs.c");
const c_obj = b.addObject("cfuncs", null);
c_obj.addCSourceFile("cfuncs.c", [][]const u8{"-std=c99"});
c_obj.setBuildMode(rel_opts);
c_obj.setNoStdLib(true);
c_obj.linkSystemLibrary("c");
const main = b.addTest("main.zig");
main.setBuildMode(rel_opts);