stage1 caching: zig no longer uses zig-cache

master
Andrew Kelley 2018-09-10 17:30:45 -04:00
parent 32be6e9b2a
commit 5ee5933ade
No known key found for this signature in database
GPG Key ID: 4E7CD66038A4D47C
11 changed files with 187 additions and 129 deletions

View File

@ -411,6 +411,7 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"
"${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
"${CMAKE_SOURCE_DIR}/src/codegen.cpp"
"${CMAKE_SOURCE_DIR}/src/compiler.cpp"
"${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
"${CMAKE_SOURCE_DIR}/src/error.cpp"
"${CMAKE_SOURCE_DIR}/src/ir.cpp"

View File

@ -1671,7 +1671,6 @@ struct CodeGen {
Buf triple_str;
Buf global_asm;
Buf *out_h_path;
Buf cache_dir;
Buf artifact_dir;
Buf output_file_path;
Buf o_file_output_path;
@ -1731,7 +1730,6 @@ struct CodeGen {
ZigList<Buf *> assembly_files;
ZigList<const char *> lib_dirs;
Buf *compiler_id;
size_t version_major;
size_t version_minor;
size_t version_patch;

View File

@ -248,6 +248,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
int rc = blake2b_final(&ch->blake, bin_digest, 48);
assert(rc == 0);
if (ch->files.length == 0) {
buf_resize(out_digest, 64);
base64_encode(buf_to_slice(out_digest), {bin_digest, 48});
return ErrorNone;
}
Buf b64_digest = BUF_INIT;
buf_resize(&b64_digest, 64);
base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});
@ -458,5 +464,6 @@ Error cache_final(CacheHash *ch, Buf *out_digest) {
}
void cache_release(CacheHash *ch) {
assert(ch->manifest_file_path != nullptr);
os_file_close(ch->manifest_file);
}

View File

@ -8,6 +8,7 @@
#include "analyze.hpp"
#include "ast_render.hpp"
#include "codegen.hpp"
#include "compiler.hpp"
#include "config.h"
#include "errmsg.hpp"
#include "error.hpp"
@ -87,13 +88,12 @@ static const char *symbols_that_llvm_depends_on[] = {
};
CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
Buf *zig_lib_dir, Buf *compiler_id)
Buf *zig_lib_dir)
{
CodeGen *g = allocate<CodeGen>(1);
codegen_add_time_event(g, "Initialize");
g->compiler_id = compiler_id;
g->zig_lib_dir = zig_lib_dir;
g->zig_std_dir = buf_alloc();
@ -243,10 +243,6 @@ void codegen_set_out_name(CodeGen *g, Buf *out_name) {
g->root_out_name = out_name;
}
void codegen_set_cache_dir(CodeGen *g, Buf cache_dir) {
g->cache_dir = cache_dir;
}
void codegen_set_libc_lib_dir(CodeGen *g, Buf *libc_lib_dir) {
g->libc_lib_dir = libc_lib_dir;
}
@ -5728,13 +5724,6 @@ static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *na
return result;
}
static void ensure_cache_dir(CodeGen *g) {
int err;
if ((err = os_make_path(&g->cache_dir))) {
zig_panic("unable to make cache dir: %s", err_str(err));
}
}
static void report_errors_and_maybe_exit(CodeGen *g) {
if (g->errors.length != 0) {
for (size_t i = 0; i < g->errors.length; i += 1) {
@ -6824,36 +6813,84 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
return contents;
}
static void define_builtin_compile_vars(CodeGen *g) {
static Error define_builtin_compile_vars(CodeGen *g) {
if (g->std_package == nullptr)
return;
return ErrorNone;
Error err;
Buf *manifest_dir = buf_alloc();
os_path_join(get_stage1_cache_path(), buf_create_from_str("builtin"), manifest_dir);
CacheHash cache_hash;
cache_init(&cache_hash, manifest_dir);
Buf *compiler_id;
if ((err = get_compiler_id(&compiler_id)))
return err;
// Only a few things affect builtin.zig
cache_buf(&cache_hash, compiler_id);
cache_int(&cache_hash, g->build_mode);
cache_bool(&cache_hash, g->is_test_build);
cache_int(&cache_hash, g->zig_target.arch.arch);
cache_int(&cache_hash, g->zig_target.arch.sub_arch);
cache_int(&cache_hash, g->zig_target.vendor);
cache_int(&cache_hash, g->zig_target.os);
cache_int(&cache_hash, g->zig_target.env_type);
cache_int(&cache_hash, g->zig_target.oformat);
cache_bool(&cache_hash, g->have_err_ret_tracing);
cache_bool(&cache_hash, g->libc_link_lib != nullptr);
Buf digest = BUF_INIT;
buf_resize(&digest, 0);
if ((err = cache_hit(&cache_hash, &digest)))
return err;
// We should always get a cache hit because there are no
// files in the input hash.
assert(buf_len(&digest) != 0);
Buf *this_dir = buf_alloc();
os_path_join(manifest_dir, &digest, this_dir);
if ((err = os_make_path(this_dir)))
return err;
const char *builtin_zig_basename = "builtin.zig";
Buf *builtin_zig_path = buf_alloc();
os_path_join(&g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
os_path_join(this_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
Buf *contents = codegen_generate_builtin_source(g);
ensure_cache_dir(g);
os_write_file(builtin_zig_path, contents);
Buf *resolved_path = buf_alloc();
Buf *resolve_paths[] = {builtin_zig_path};
*resolved_path = os_path_resolve(resolve_paths, 1);
bool hit;
if ((err = os_file_exists(builtin_zig_path, &hit)))
return err;
Buf *contents;
if (hit) {
contents = buf_alloc();
if ((err = os_fetch_file_path(builtin_zig_path, contents, false))) {
fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
exit(1);
}
} else {
contents = codegen_generate_builtin_source(g);
os_write_file(builtin_zig_path, contents);
}
assert(g->root_package);
assert(g->std_package);
g->compile_var_package = new_package(buf_ptr(&g->cache_dir), builtin_zig_basename);
g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename);
g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
g->compile_var_import = add_source_file(g, g->compile_var_package, resolved_path, contents);
g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents);
scan_import(g, g->compile_var_import);
return ErrorNone;
}
static void init(CodeGen *g) {
if (g->module)
return;
if (g->llvm_argv_len > 0) {
const char **args = allocate_nonzero<const char *>(g->llvm_argv_len + 2);
args[0] = "zig (LLVM option parsing)";
@ -6960,7 +6997,11 @@ static void init(CodeGen *g) {
g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease;
define_builtin_fns(g);
define_builtin_compile_vars(g);
Error err;
if ((err = define_builtin_compile_vars(g))) {
fprintf(stderr, "Unable to create builtin.zig: %s\n", err_str(err));
exit(1);
}
}
void codegen_translate_c(CodeGen *g, Buf *full_path) {
@ -7668,6 +7709,10 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) {
static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
Error err;
Buf *compiler_id;
if ((err = get_compiler_id(&compiler_id)))
return err;
CacheHash *ch = &g->cache_hash;
cache_init(ch, manifest_dir);
@ -7675,7 +7720,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
if (g->linker_script != nullptr) {
cache_file(ch, buf_create_from_str(g->linker_script));
}
cache_buf(ch, g->compiler_id);
cache_buf(ch, compiler_id);
cache_buf(ch, g->root_out_name);
cache_list_of_link_lib(ch, g->link_libs_list.items, g->link_libs_list.length);
cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
@ -7766,13 +7811,7 @@ void codegen_build_and_link(CodeGen *g) {
codegen_add_time_event(g, "Check Cache");
Buf app_data_dir = BUF_INIT;
if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {
fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));
exit(1);
}
Buf *stage1_dir = buf_alloc();
os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
Buf *stage1_dir = get_stage1_cache_path();
Buf *manifest_dir = buf_alloc();
os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
@ -7820,6 +7859,7 @@ void codegen_build_and_link(CodeGen *g) {
}
// TODO hard link output_file_path to wanted_output_file_path
cache_release(&g->cache_hash);
codegen_add_time_event(g, "Done");
}

View File

@ -15,7 +15,7 @@
#include <stdio.h>
CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
Buf *zig_lib_dir, Buf *compiler_id);
Buf *zig_lib_dir);
void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
@ -46,7 +46,6 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script);
void codegen_set_test_filter(CodeGen *g, Buf *filter);
void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);
void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);
void codegen_set_cache_dir(CodeGen *g, Buf cache_dir);
void codegen_set_output_h_path(CodeGen *g, Buf *h_path);
void codegen_set_output_path(CodeGen *g, Buf *path);
void codegen_add_time_event(CodeGen *g, const char *name);

66
src/compiler.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "cache_hash.hpp"
#include <stdio.h>
static Buf saved_compiler_id = BUF_INIT;
static Buf saved_app_data_dir = BUF_INIT;
static Buf saved_stage1_path = BUF_INIT;
Buf *get_stage1_cache_path() {
if (saved_stage1_path.list.length != 0) {
return &saved_stage1_path;
}
Error err;
if ((err = os_get_app_data_dir(&saved_app_data_dir, "zig"))) {
fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));
exit(1);
}
os_path_join(&saved_app_data_dir, buf_create_from_str("stage1"), &saved_stage1_path);
return &saved_stage1_path;
}
Error get_compiler_id(Buf **result) {
if (saved_compiler_id.list.length != 0) {
*result = &saved_compiler_id;
return ErrorNone;
}
Error err;
Buf *stage1_dir = get_stage1_cache_path();
Buf *manifest_dir = buf_alloc();
os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
CacheHash cache_hash;
CacheHash *ch = &cache_hash;
cache_init(ch, manifest_dir);
Buf self_exe_path = BUF_INIT;
if ((err = os_self_exe_path(&self_exe_path)))
return err;
cache_file(ch, &self_exe_path);
buf_resize(&saved_compiler_id, 0);
if ((err = cache_hit(ch, &saved_compiler_id)))
return err;
if (buf_len(&saved_compiler_id) != 0) {
cache_release(ch);
*result = &saved_compiler_id;
return ErrorNone;
}
ZigList<Buf *> lib_paths = {};
if ((err = os_self_exe_shared_libs(lib_paths)))
return err;
for (size_t i = 0; i < lib_paths.length; i += 1) {
Buf *lib_path = lib_paths.at(i);
if ((err = cache_add_file(ch, lib_path)))
return err;
}
if ((err = cache_final(ch, &saved_compiler_id)))
return err;
cache_release(ch);
*result = &saved_compiler_id;
return ErrorNone;
}

17
src/compiler.hpp Normal file
View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2018 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_COMPILER_HPP
#define ZIG_COMPILER_HPP
#include "buffer.hpp"
#include "error.hpp"
Buf *get_stage1_cache_path();
Error get_compiler_id(Buf **result);
#endif

View File

@ -32,7 +32,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,
parent_gen->zig_lib_dir, parent_gen->compiler_id);
parent_gen->zig_lib_dir);
child_gen->want_h_file = false;
child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
@ -42,8 +42,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
child_gen->verbose_llvm_ir = parent_gen->verbose_llvm_ir;
child_gen->verbose_cimport = parent_gen->verbose_cimport;
codegen_set_cache_dir(child_gen, parent_gen->cache_dir);
codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
codegen_set_is_static(child_gen, parent_gen->is_static);

View File

@ -8,11 +8,11 @@
#include "ast_render.hpp"
#include "buffer.hpp"
#include "codegen.hpp"
#include "compiler.hpp"
#include "config.h"
#include "error.hpp"
#include "os.hpp"
#include "target.hpp"
#include "cache_hash.hpp"
#include <stdio.h>
@ -257,53 +257,6 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
}
}
static Buf saved_compiler_id = BUF_INIT;
static Error get_compiler_id(Buf **result) {
if (saved_compiler_id.list.length != 0) {
*result = &saved_compiler_id;
return ErrorNone;
}
Error err;
Buf app_data_dir = BUF_INIT;
if ((err = os_get_app_data_dir(&app_data_dir, "zig")))
return err;
Buf *stage1_dir = buf_alloc();
os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
Buf *manifest_dir = buf_alloc();
os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
CacheHash cache_hash;
CacheHash *ch = &cache_hash;
cache_init(ch, manifest_dir);
Buf self_exe_path = BUF_INIT;
if ((err = os_self_exe_path(&self_exe_path)))
return err;
cache_file(ch, &self_exe_path);
buf_resize(&saved_compiler_id, 0);
if ((err = cache_hit(ch, &saved_compiler_id)))
return err;
if (buf_len(&saved_compiler_id) != 0) {
*result = &saved_compiler_id;
return ErrorNone;
}
ZigList<Buf *> lib_paths = {};
if ((err = os_self_exe_shared_libs(lib_paths)))
return err;
for (size_t i = 0; i < lib_paths.length; i += 1) {
Buf *lib_path = lib_paths.at(i);
if ((err = cache_add_file(ch, lib_path)))
return err;
}
if ((err = cache_final(ch, &saved_compiler_id)))
return err;
*result = &saved_compiler_id;
return ErrorNone;
}
int main(int argc, char **argv) {
if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
@ -428,14 +381,7 @@ int main(int argc, char **argv) {
Buf *build_runner_path = buf_alloc();
os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);
Buf *compiler_id;
if ((err = get_compiler_id(&compiler_id))) {
fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
return EXIT_FAILURE;
}
CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf,
compiler_id);
CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf);
codegen_set_out_name(g, buf_create_from_str("build"));
Buf *build_file_buf = buf_create_from_str(build_file);
@ -452,8 +398,6 @@ int main(int argc, char **argv) {
full_cache_dir = os_path_resolve(&cache_dir_buf, 1);
}
codegen_set_cache_dir(g, full_cache_dir);
args.items[1] = buf_ptr(&build_file_dirname);
args.items[2] = buf_ptr(&full_cache_dir);
@ -795,7 +739,7 @@ int main(int argc, char **argv) {
switch (cmd) {
case CmdBuiltin: {
Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf, nullptr);
CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf);
Buf *builtin_source = codegen_generate_builtin_source(g);
if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));
@ -850,30 +794,16 @@ int main(int argc, char **argv) {
Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;
Buf full_cache_dir = BUF_INIT;
if (cmd == CmdRun && buf_out_name == nullptr) {
buf_out_name = buf_create_from_str("run");
}
{
Buf *resolve_paths = buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir);
full_cache_dir = os_path_resolve(&resolve_paths, 1);
}
Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
Buf *compiler_id;
if ((err = get_compiler_id(&compiler_id))) {
fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
return EXIT_FAILURE;
}
CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf,
compiler_id);
CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf);
codegen_set_out_name(g, buf_out_name);
codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
codegen_set_is_test(g, cmd == CmdTest);
codegen_set_linker_script(g, linker_script);
codegen_set_cache_dir(g, full_cache_dir);
if (each_lib_rpath)
codegen_set_each_lib_rpath(g, each_lib_rpath);

View File

@ -721,7 +721,7 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
#endif
}
int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
static const ssize_t buf_size = 0x2000;
buf_resize(out_buf, buf_size);
ssize_t actual_buf_len = 0;
@ -757,7 +757,7 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
if (amt_read != buf_size) {
if (feof(f)) {
buf_resize(out_buf, actual_buf_len);
return 0;
return ErrorNone;
} else {
return ErrorFileSystem;
}
@ -769,13 +769,13 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
zig_unreachable();
}
int os_file_exists(Buf *full_path, bool *result) {
Error os_file_exists(Buf *full_path, bool *result) {
#if defined(ZIG_OS_WINDOWS)
*result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES;
return 0;
return ErrorNone;
#else
*result = access(buf_ptr(full_path), F_OK) != -1;
return 0;
return ErrorNone;
#endif
}
@ -834,13 +834,15 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
os_fetch_file(stdout_f, out_stdout, false);
os_fetch_file(stderr_f, out_stderr, false);
Error err1 = os_fetch_file(stdout_f, out_stdout, false);
Error err2 = os_fetch_file(stderr_f, out_stderr, false);
fclose(stdout_f);
fclose(stderr_f);
return 0;
if (err1) return err1;
if (err2) return err2;
return ErrorNone;
}
}
#endif
@ -1064,7 +1066,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {
}
}
int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
FILE *f = fopen(buf_ptr(full_path), "rb");
if (!f) {
switch (errno) {
@ -1083,7 +1085,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
return ErrorFileSystem;
}
}
int result = os_fetch_file(f, out_contents, skip_shebang);
Error result = os_fetch_file(f, out_contents, skip_shebang);
fclose(f);
return result;
}

View File

@ -111,8 +111,8 @@ void os_file_close(OsFile file);
void os_write_file(Buf *full_path, Buf *contents);
int os_copy_file(Buf *src_path, Buf *dest_path);
int os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
int os_get_cwd(Buf *out_cwd);
@ -122,7 +122,7 @@ void os_stderr_set_color(TermColor color);
int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
int os_delete_file(Buf *path);
int os_file_exists(Buf *full_path, bool *result);
Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);
int os_rename(Buf *src_path, Buf *dest_path);
double os_get_time(void);