@cImport works with --cache on

We pass -MD -MF args to clang when doing `@cImport`, which
gives us a complete list of files that the C code read from.
Then we add these to the cache. So even when using `@cImport`
Zig's caching system remains perfect. This is a proof of concept
for the mechanism that the self-hosted compiler will use to
watch and rebuild files.
This commit is contained in:
Andrew Kelley 2019-02-25 14:03:36 -05:00
parent 525c2eaf5d
commit 0d4db8828a
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
9 changed files with 77 additions and 31 deletions

View File

@ -414,6 +414,39 @@ Error cache_add_file(CacheHash *ch, Buf *path) {
return cache_add_file_fetch(ch, resolved_path, nullptr); return cache_add_file_fetch(ch, resolved_path, nullptr);
} }
Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
Error err;
Buf *contents = buf_alloc();
if ((err = os_fetch_file_path(dep_file_path, contents, false))) {
if (verbose) {
fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
}
return ErrorReadingDepFile;
}
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(ch, filename_buf))) {
if (verbose) {
fprintf(stderr, "unable to add %s to cache: %s\n", buf_ptr(filename_buf), err_str(err));
}
return err;
}
}
return ErrorNone;
}
static Error write_manifest_file(CacheHash *ch) { static Error write_manifest_file(CacheHash *ch) {
Error err; Error err;
Buf contents = BUF_INIT; Buf contents = BUF_INIT;
@ -464,3 +497,4 @@ void cache_release(CacheHash *ch) {
os_file_close(ch->manifest_file); os_file_close(ch->manifest_file);
} }

View File

@ -56,6 +56,8 @@ Error ATTRIBUTE_MUST_USE cache_hit(CacheHash *ch, Buf *out_b64_digest);
// If you did not get a cache hit, call this function for every file // If you did not get a cache hit, call this function for every file
// that is depended on, and then finish with cache_final. // that is depended on, and then finish with cache_final.
Error ATTRIBUTE_MUST_USE cache_add_file(CacheHash *ch, Buf *path); Error ATTRIBUTE_MUST_USE cache_add_file(CacheHash *ch, Buf *path);
// This opens a file created by -MD -MF args to Clang
Error ATTRIBUTE_MUST_USE cache_add_dep_file(CacheHash *ch, Buf *path, bool verbose);
// This variant of cache_add_file returns the file contents. // This variant of cache_add_file returns the file contents.
// Also the file path argument must be already resolved. // Also the file path argument must be already resolved.

View File

@ -8218,8 +8218,9 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
} }
if (g->verbose_cc) { if (g->verbose_cc) {
fprintf(stderr, "zig");
for (size_t arg_i = 0; arg_i < args.length; arg_i += 1) { for (size_t arg_i = 0; arg_i < args.length; arg_i += 1) {
fprintf(stderr, "%s ", args.at(arg_i)); fprintf(stderr, " %s", args.at(arg_i));
} }
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@ -8232,35 +8233,15 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
g->link_objects.append(out_obj_path); g->link_objects.append(out_obj_path);
// add the files depended on to the cache system
if (g->enable_cache) { if (g->enable_cache) {
Buf *contents = buf_alloc(); // add the files depended on to the cache system
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))) { 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)); fprintf(stderr, "unable to add %s to cache: %s\n", buf_ptr(c_source_file), err_str(err));
exit(1); exit(1);
} }
SplitIterator it = memSplit(buf_to_slice(contents), str("\n")); if ((err = cache_add_dep_file(&g->cache_hash, out_dep_path, true))) {
// skip first line fprintf(stderr, "failed to add C source dependencies to cache: %s\n", err_str(err));
SplitIterator_next(&it); exit(1);
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);
}
} }
} }
} }

View File

@ -36,6 +36,7 @@ const char *err_str(Error err) {
case ErrorCacheUnavailable: return "cache unavailable"; case ErrorCacheUnavailable: return "cache unavailable";
case ErrorPathTooLong: return "path too long"; case ErrorPathTooLong: return "path too long";
case ErrorCCompilerCannotFindFile: return "C compiler cannot find file"; case ErrorCCompilerCannotFindFile: return "C compiler cannot find file";
case ErrorReadingDepFile: return "failed to read .d file";
} }
return "(invalid error)"; return "(invalid error)";
} }

View File

@ -38,6 +38,7 @@ enum Error {
ErrorCacheUnavailable, ErrorCacheUnavailable,
ErrorPathTooLong, ErrorPathTooLong,
ErrorCCompilerCannotFindFile, ErrorCCompilerCannotFindFile,
ErrorReadingDepFile,
}; };
const char *err_str(Error err); const char *err_str(Error err);

View File

@ -18670,12 +18670,6 @@ static IrInstruction *ir_analyze_instruction_type_name(IrAnalyze *ira, IrInstruc
} }
static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstructionCImport *instruction) { static IrInstruction *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstructionCImport *instruction) {
if (ira->codegen->enable_cache) {
ir_add_error(ira, &instruction->base,
buf_sprintf("TODO @cImport is incompatible with --cache on. The cache system currently is unable to detect subsequent changes in .h files."));
return ira->codegen->invalid_instruction;
}
AstNode *node = instruction->base.source_node; AstNode *node = instruction->base.source_node;
assert(node->type == NodeTypeFnCallExpr); assert(node->type == NodeTypeFnCallExpr);
AstNode *block_node = node->data.fn_call_expr.params.at(0); AstNode *block_node = node->data.fn_call_expr.params.at(0);

View File

@ -1232,6 +1232,18 @@ static Error os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_p
} }
#endif #endif
Buf *os_tmp_filename(Buf *prefix, Buf *suffix) {
Buf *result = buf_create_from_buf(prefix);
const char base64[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
assert(array_length(base64) == 64 + 1);
for (size_t i = 0; i < 12; i += 1) {
buf_append_char(result, base64[rand() % 64]);
}
buf_append_buf(result, suffix);
return result;
}
#if defined(ZIG_OS_WINDOWS) #if defined(ZIG_OS_WINDOWS)
static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) { static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
char tmp_dir[MAX_PATH + 1]; char tmp_dir[MAX_PATH + 1];

View File

@ -121,6 +121,7 @@ Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
bool os_stderr_tty(void); bool os_stderr_tty(void);
void os_stderr_set_color(TermColor color); void os_stderr_set_color(TermColor color);
Buf *os_tmp_filename(Buf *prefix, Buf *suffix);
Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path); Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
Error os_delete_file(Buf *path); Error os_delete_file(Buf *path);

View File

@ -4776,6 +4776,15 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
clang_argv.append("-x"); clang_argv.append("-x");
clang_argv.append("c"); clang_argv.append("c");
Buf *out_dep_path = nullptr;
if (codegen->enable_cache) {
Buf *prefix = buf_sprintf("%s" OS_SEP, buf_ptr(&codegen->cache_dir));
out_dep_path = os_tmp_filename(prefix, buf_create_from_str(".d"));
clang_argv.append("-MD");
clang_argv.append("-MF");
clang_argv.append(buf_ptr(out_dep_path));
}
if (c->codegen->zig_target->is_native) { if (c->codegen->zig_target->is_native) {
char *ZIG_PARSEC_CFLAGS = getenv("ZIG_NATIVE_PARSEC_CFLAGS"); char *ZIG_PARSEC_CFLAGS = getenv("ZIG_NATIVE_PARSEC_CFLAGS");
if (ZIG_PARSEC_CFLAGS) { if (ZIG_PARSEC_CFLAGS) {
@ -4912,6 +4921,17 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
return ErrorCCompileErrors; return ErrorCCompileErrors;
} }
if (codegen->enable_cache) {
Error err;
assert(out_dep_path != nullptr);
if ((err = cache_add_dep_file(&codegen->cache_hash, out_dep_path, codegen->verbose_cimport))) {
if (codegen->verbose_cimport) {
fprintf(stderr, "translate-c: aborting due to failed cache operation: %s\n", err_str(err));
}
return err;
}
}
c->ctx = ZigClangASTUnit_getASTContext(ast_unit); c->ctx = ZigClangASTUnit_getASTContext(ast_unit);
c->source_manager = ZigClangASTUnit_getSourceManager(ast_unit); c->source_manager = ZigClangASTUnit_getSourceManager(ast_unit);
c->root = trans_create_node(c, NodeTypeContainerDecl); c->root = trans_create_node(c, NodeTypeContainerDecl);