| author | |
| committer | |
| log | b35c74ea4c9d93d6a8d90812d2066a78b9abb64e |
| tree | 2bd1b9b8f353718c385fd6b800c1b7372840f99f |
| parent | 2bf1b6840d33a27614630ddb34f53a859fc87345 |
| signature |
to canonicalize imports.
This means that softlinks can represent different files,
but referencing the same absolute path different ways
still references the same import.3 files changed, 30 insertions(+), 44 deletions(-)
src/analyze.cpp+6-6| ... | ... | @@ -4236,9 +4236,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) { |
| 4236 | 4236 | node->data.use.value = result; |
| 4237 | 4237 | } |
| 4238 | 4238 | |
| 4239 | ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code) { | |
| 4239 | ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *resolved_path, Buf *source_code) { | |
| 4240 | 4240 | if (g->verbose_tokenize) { |
| 4241 | fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(abs_full_path)); | |
| 4241 | fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path)); | |
| 4242 | 4242 | fprintf(stderr, "----------------\n"); |
| 4243 | 4243 | fprintf(stderr, "%s\n", buf_ptr(source_code)); |
| 4244 | 4244 | |
| ... | ... | @@ -4250,7 +4250,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a |
| 4250 | 4250 | tokenize(source_code, &tokenization); |
| 4251 | 4251 | |
| 4252 | 4252 | if (tokenization.err) { |
| 4253 | ErrorMsg *err = err_msg_create_with_line(abs_full_path, tokenization.err_line, tokenization.err_column, | |
| 4253 | ErrorMsg *err = err_msg_create_with_line(resolved_path, tokenization.err_line, tokenization.err_column, | |
| 4254 | 4254 | source_code, tokenization.line_offsets, tokenization.err); |
| 4255 | 4255 | |
| 4256 | 4256 | print_err_msg(err, g->err_color); |
| ... | ... | @@ -4268,7 +4268,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a |
| 4268 | 4268 | import_entry->package = package; |
| 4269 | 4269 | import_entry->source_code = source_code; |
| 4270 | 4270 | import_entry->line_offsets = tokenization.line_offsets; |
| 4271 | import_entry->path = abs_full_path; | |
| 4271 | import_entry->path = resolved_path; | |
| 4272 | 4272 | |
| 4273 | 4273 | import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color); |
| 4274 | 4274 | assert(import_entry->root); |
| ... | ... | @@ -4278,10 +4278,10 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a |
| 4278 | 4278 | |
| 4279 | 4279 | Buf *src_dirname = buf_alloc(); |
| 4280 | 4280 | Buf *src_basename = buf_alloc(); |
| 4281 | os_path_split(abs_full_path, src_dirname, src_basename); | |
| 4281 | os_path_split(resolved_path, src_dirname, src_basename); | |
| 4282 | 4282 | |
| 4283 | 4283 | import_entry->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname)); |
| 4284 | g->import_table.put(abs_full_path, import_entry); | |
| 4284 | g->import_table.put(resolved_path, import_entry); | |
| 4285 | 4285 | g->import_queue.append(import_entry); |
| 4286 | 4286 | |
| 4287 | 4287 | import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr, nullptr, import_entry); |
src/codegen.cpp+16-21| ... | ... | @@ -6866,19 +6866,16 @@ static void define_builtin_compile_vars(CodeGen *g) { |
| 6866 | 6866 | ensure_cache_dir(g); |
| 6867 | 6867 | os_write_file(builtin_zig_path, contents); |
| 6868 | 6868 | |
| 6869 | int err; | |
| 6870 | Buf *abs_full_path = buf_alloc(); | |
| 6871 | if ((err = os_path_real(builtin_zig_path, abs_full_path))) { | |
| 6872 | fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err)); | |
| 6873 | exit(1); | |
| 6874 | } | |
| 6869 | Buf *resolved_path = buf_alloc(); | |
| 6870 | Buf *resolve_paths[] = {builtin_zig_path}; | |
| 6871 | *resolved_path = os_path_resolve(resolve_paths, 1); | |
| 6875 | 6872 | |
| 6876 | 6873 | assert(g->root_package); |
| 6877 | 6874 | assert(g->std_package); |
| 6878 | 6875 | g->compile_var_package = new_package(buf_ptr(&g->cache_dir), builtin_zig_basename); |
| 6879 | 6876 | g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); |
| 6880 | 6877 | g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); |
| 6881 | g->compile_var_import = add_source_file(g, g->compile_var_package, abs_full_path, contents); | |
| 6878 | g->compile_var_import = add_source_file(g, g->compile_var_package, resolved_path, contents); | |
| 6882 | 6879 | scan_import(g, g->compile_var_import); |
| 6883 | 6880 | } |
| 6884 | 6881 | |
| ... | ... | @@ -7034,17 +7031,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package |
| 7034 | 7031 | Buf *code_basename = buf_create_from_str(basename); |
| 7035 | 7032 | Buf path_to_code_src = BUF_INIT; |
| 7036 | 7033 | os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src); |
| 7037 | Buf *abs_full_path = buf_alloc(); | |
| 7038 | int err; | |
| 7039 | if ((err = os_path_real(&path_to_code_src, abs_full_path))) { | |
| 7040 | zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err)); | |
| 7041 | } | |
| 7034 | ||
| 7035 | Buf *resolve_paths[] = {&path_to_code_src}; | |
| 7036 | Buf *resolved_path = buf_alloc(); | |
| 7037 | *resolved_path = os_path_resolve(resolve_paths, 1); | |
| 7042 | 7038 | Buf *import_code = buf_alloc(); |
| 7043 | if ((err = os_fetch_file_path(abs_full_path, import_code, false))) { | |
| 7039 | int err; | |
| 7040 | if ((err = os_fetch_file_path(resolved_path, import_code, false))) { | |
| 7044 | 7041 | zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err)); |
| 7045 | 7042 | } |
| 7046 | 7043 | |
| 7047 | return add_source_file(g, package, abs_full_path, import_code); | |
| 7044 | return add_source_file(g, package, resolved_path, import_code); | |
| 7048 | 7045 | } |
| 7049 | 7046 | |
| 7050 | 7047 | static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) { |
| ... | ... | @@ -7122,20 +7119,18 @@ static void gen_root_source(CodeGen *g) { |
| 7122 | 7119 | Buf *rel_full_path = buf_alloc(); |
| 7123 | 7120 | os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, rel_full_path); |
| 7124 | 7121 | |
| 7125 | Buf *abs_full_path = buf_alloc(); | |
| 7126 | int err; | |
| 7127 | if ((err = os_path_real(rel_full_path, abs_full_path))) { | |
| 7128 | fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err)); | |
| 7129 | exit(1); | |
| 7130 | } | |
| 7122 | Buf *resolved_path = buf_alloc(); | |
| 7123 | Buf *resolve_paths[] = {rel_full_path}; | |
| 7124 | *resolved_path = os_path_resolve(resolve_paths, 1); | |
| 7131 | 7125 | |
| 7132 | 7126 | Buf *source_code = buf_alloc(); |
| 7127 | int err; | |
| 7133 | 7128 | if ((err = os_fetch_file_path(rel_full_path, source_code, true))) { |
| 7134 | 7129 | fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err)); |
| 7135 | 7130 | exit(1); |
| 7136 | 7131 | } |
| 7137 | 7132 | |
| 7138 | g->root_import = add_source_file(g, g->root_package, abs_full_path, source_code); | |
| 7133 | g->root_import = add_source_file(g, g->root_package, resolved_path, source_code); | |
| 7139 | 7134 | |
| 7140 | 7135 | assert(g->root_out_name); |
| 7141 | 7136 | assert(g->out_type != OutTypeUnknown); |
src/ir.cpp+8-17| ... | ... | @@ -16131,29 +16131,20 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi |
| 16131 | 16131 | os_path_join(search_dir, import_target_path, &full_path); |
| 16132 | 16132 | |
| 16133 | 16133 | Buf *import_code = buf_alloc(); |
| 16134 | Buf *abs_full_path = buf_alloc(); | |
| 16135 | int err; | |
| 16136 | if ((err = os_path_real(&full_path, abs_full_path))) { | |
| 16137 | if (err == ErrorFileNotFound) { | |
| 16138 | ir_add_error_node(ira, source_node, | |
| 16139 | buf_sprintf("unable to find '%s'", buf_ptr(import_target_path))); | |
| 16140 | return ira->codegen->builtin_types.entry_invalid; | |
| 16141 | } else { | |
| 16142 | ira->codegen->error_during_imports = true; | |
| 16143 | ir_add_error_node(ira, source_node, | |
| 16144 | buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err))); | |
| 16145 | return ira->codegen->builtin_types.entry_invalid; | |
| 16146 | } | |
| 16147 | } | |
| 16134 | Buf *resolved_path = buf_alloc(); | |
| 16135 | ||
| 16136 | Buf *resolve_paths[] = { &full_path, }; | |
| 16137 | *resolved_path = os_path_resolve(resolve_paths, 1); | |
| 16148 | 16138 | |
| 16149 | auto import_entry = ira->codegen->import_table.maybe_get(abs_full_path); | |
| 16139 | auto import_entry = ira->codegen->import_table.maybe_get(resolved_path); | |
| 16150 | 16140 | if (import_entry) { |
| 16151 | 16141 | ConstExprValue *out_val = ir_build_const_from(ira, &import_instruction->base); |
| 16152 | 16142 | out_val->data.x_import = import_entry->value; |
| 16153 | 16143 | return ira->codegen->builtin_types.entry_namespace; |
| 16154 | 16144 | } |
| 16155 | 16145 | |
| 16156 | if ((err = os_fetch_file_path(abs_full_path, import_code, true))) { | |
| 16146 | int err; | |
| 16147 | if ((err = os_fetch_file_path(resolved_path, import_code, true))) { | |
| 16157 | 16148 | if (err == ErrorFileNotFound) { |
| 16158 | 16149 | ir_add_error_node(ira, source_node, |
| 16159 | 16150 | buf_sprintf("unable to find '%s'", buf_ptr(import_target_path))); |
| ... | ... | @@ -16164,7 +16155,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi |
| 16164 | 16155 | return ira->codegen->builtin_types.entry_invalid; |
| 16165 | 16156 | } |
| 16166 | 16157 | } |
| 16167 | ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, abs_full_path, import_code); | |
| 16158 | ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code); | |
| 16168 | 16159 | |
| 16169 | 16160 | scan_import(ira->codegen, target_import); |
| 16170 | 16161 |