authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-08 01:11:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-09 09:28:05-07:00
log4056bb92e6d6ca3d2ab8f49b4ac83c01fb25bd11
tree3992ca209d9759c7a041c1c9f58e8572081051da
parent472ee184862415d1f2651d81d248f3000032932d

stage2: more progress moving `zig cc` to stage2

* std.cache_hash exposes Hasher type * std.cache_hash makes hasher_init a global const * std.cache_hash supports cloning so that clones can share the same open manifest dir handle as well as fork from shared hasher state * start to populate the cache_hash for stage2 builds * remove a footgun from std.cache_hash add function * get rid of std.Target.ObjectFormat.unknown * rework stage2 logic for resolving output artifact names by adding object_format as an optional parameter to std.zig.binNameAlloc * support -Denable-llvm in stage2 tests * Module supports the use case when there are no .zig files * introduce c_object_table and failed_c_objects to Module * propagate many new kinds of data from CLI into Module and into linker.Options * introduce -fLLVM, -fLLD, -fClang and their -fno- counterparts. closes #6251. - add logic for choosing when to use LLD or zig's self-hosted linker * stub code for implementing invoking Clang to build C objects * add -femit-h, -femit-h=foo, and -fno-emit-h CLI options

13 files changed, 595 insertions(+), 129 deletions(-)

build.zig+1
......@@ -139,6 +139,7 @@ pub fn build(b: *Builder) !void {
139139 const is_wasmtime_enabled = b.option(bool, "enable-wasmtime", "Use Wasmtime to enable and run WASI libstd tests") orelse false;
140140 const glibc_multi_dir = b.option([]const u8, "enable-foreign-glibc", "Provide directory with glibc installations to run cross compiled tests that link glibc");
141141
142 test_stage2.addBuildOption(bool, "have_llvm", enable_llvm);
142143 test_stage2.addBuildOption(bool, "enable_qemu", is_qemu_enabled);
143144 test_stage2.addBuildOption(bool, "enable_wine", is_wine_enabled);
144145 test_stage2.addBuildOption(bool, "enable_wasmtime", is_wasmtime_enabled);
lib/std/cache_hash.zig+73-35
......@@ -5,7 +5,6 @@
55// and substantial portions of the software.
66const std = @import("std.zig");
77const crypto = std.crypto;
8const Hasher = crypto.auth.siphash.SipHash128(1, 3); // provides enough collision resistance for the CacheHash use cases, while being one of our fastest options right now
98const fs = std.fs;
109const base64 = std.base64;
1110const ArrayList = std.ArrayList;
......@@ -23,6 +22,14 @@ const BASE64_DIGEST_LEN = base64.Base64Encoder.calcSize(BIN_DIGEST_LEN);
2322
2423const MANIFEST_FILE_SIZE_MAX = 50 * 1024 * 1024;
2524
25/// The type used for hashing file contents. Currently, this is SipHash128(1, 3), because it
26/// provides enough collision resistance for the CacheHash use cases, while being one of our
27/// fastest options right now.
28pub const Hasher = crypto.auth.siphash.SipHash128(1, 3);
29
30/// Initial state, that can be copied.
31pub const hasher_init: Hasher = Hasher.init(&[_]u8{0} ** Hasher.minimum_key_length);
32
2633pub const File = struct {
2734 path: ?[]const u8,
2835 max_file_size: ?usize,
......@@ -45,52 +52,82 @@ pub const File = struct {
4552
4653/// CacheHash manages project-local `zig-cache` directories.
4754/// This is not a general-purpose cache.
48/// It was designed to be fast and simple, not to withstand attacks using specially-crafted input.
55/// It is designed to be fast and simple, not to withstand attacks using specially-crafted input.
4956pub const CacheHash = struct {
5057 allocator: *Allocator,
51 hasher_init: Hasher, // initial state, that can be copied
52 hasher: Hasher, // current state for incremental hashing
58 /// Current state for incremental hashing.
59 hasher: Hasher,
5360 manifest_dir: fs.Dir,
5461 manifest_file: ?fs.File,
5562 manifest_dirty: bool,
63 owns_manifest_dir: bool,
5664 files: ArrayList(File),
5765 b64_digest: [BASE64_DIGEST_LEN]u8,
5866
5967 /// Be sure to call release after successful initialization.
6068 pub fn init(allocator: *Allocator, dir: fs.Dir, manifest_dir_path: []const u8) !CacheHash {
61 const hasher_init = Hasher.init(&[_]u8{0} ** Hasher.minimum_key_length);
6269 return CacheHash{
6370 .allocator = allocator,
64 .hasher_init = hasher_init,
6571 .hasher = hasher_init,
6672 .manifest_dir = try dir.makeOpenPath(manifest_dir_path, .{}),
6773 .manifest_file = null,
6874 .manifest_dirty = false,
75 .owns_manifest_dir = true,
76 .files = ArrayList(File).init(allocator),
77 .b64_digest = undefined,
78 };
79 }
80
81 /// Allows one to fork a CacheHash instance into another one, which does not require an additional
82 /// directory handle to be opened. The new instance inherits the hash state.
83 pub fn clone(self: CacheHash) CacheHash {
84 assert(self.manifest_file == null);
85 assert(files.items.len == 0);
86 return .{
87 .allocator = self.allocator,
88 .hasher = self.hasher,
89 .manifest_dir = self.manifest_dir,
90 .manifest_file = null,
91 .manifest_dirty = false,
92 .owns_manifest_dir = false,
6993 .files = ArrayList(File).init(allocator),
7094 .b64_digest = undefined,
7195 };
7296 }
7397
7498 /// Record a slice of bytes as an dependency of the process being cached
75 pub fn addSlice(self: *CacheHash, val: []const u8) void {
99 pub fn addBytes(self: *CacheHash, bytes: []const u8) void {
76100 assert(self.manifest_file == null);
77101
78 self.hasher.update(val);
79 self.hasher.update(&[_]u8{0});
102 self.hasher.update(mem.asBytes(&bytes.len));
103 self.hasher.update(bytes);
80104 }
81105
82 /// Convert the input value into bytes and record it as a dependency of the
83 /// process being cached
84 pub fn add(self: *CacheHash, val: anytype) void {
106 pub fn addListOfBytes(self: *CacheHash, list_of_bytes: []const []const u8) void {
85107 assert(self.manifest_file == null);
86108
87 const valPtr = switch (@typeInfo(@TypeOf(val))) {
88 .Int => &val,
89 .Pointer => val,
90 else => &val,
91 };
109 self.add(list_of_bytes.items.len);
110 for (list_of_bytes) |bytes| self.addBytes(bytes);
111 }
112
113 /// Convert the input value into bytes and record it as a dependency of the process being cached.
114 pub fn add(self: *CacheHash, x: anytype) void {
115 assert(self.manifest_file == null);
116
117 switch (@TypeOf(x)) {
118 std.builtin.Version => {
119 self.add(x.major);
120 self.add(x.minor);
121 self.add(x.patch);
122 return;
123 },
124 else => {},
125 }
92126
93 self.addSlice(mem.asBytes(valPtr));
127 switch (@typeInfo(@TypeOf(x))) {
128 .Bool, .Int, .Enum, .Array => self.addBytes(mem.asBytes(&x)),
129 else => @compileError("unable to hash type " ++ @typeName(@TypeOf(x))),
130 }
94131 }
95132
96133 /// Add a file as a dependency of process being cached. When `CacheHash.hit` is
......@@ -122,7 +159,7 @@ pub const CacheHash = struct {
122159 .bin_digest = undefined,
123160 };
124161
125 self.addSlice(resolved_path);
162 self.addBytes(resolved_path);
126163
127164 return idx;
128165 }
......@@ -143,7 +180,7 @@ pub const CacheHash = struct {
143180
144181 base64_encoder.encode(self.b64_digest[0..], &bin_digest);
145182
146 self.hasher = self.hasher_init;
183 self.hasher = hasher_init;
147184 self.hasher.update(&bin_digest);
148185
149186 const manifest_file_path = try fmt.allocPrint(self.allocator, "{}.txt", .{self.b64_digest});
......@@ -244,7 +281,7 @@ pub const CacheHash = struct {
244281 }
245282
246283 var actual_digest: [BIN_DIGEST_LEN]u8 = undefined;
247 try hashFile(this_file, &actual_digest, self.hasher_init);
284 try hashFile(this_file, &actual_digest);
248285
249286 if (!mem.eql(u8, &cache_hash_file.bin_digest, &actual_digest)) {
250287 cache_hash_file.bin_digest = actual_digest;
......@@ -262,7 +299,7 @@ pub const CacheHash = struct {
262299 // cache miss
263300 // keep the manifest file open
264301 // reset the hash
265 self.hasher = self.hasher_init;
302 self.hasher = hasher_init;
266303 self.hasher.update(&bin_digest);
267304
268305 // Remove files not in the initial hash
......@@ -310,7 +347,7 @@ pub const CacheHash = struct {
310347
311348 // Hash while reading from disk, to keep the contents in the cpu cache while
312349 // doing hashing.
313 var hasher = self.hasher_init;
350 var hasher = hasher_init;
314351 var off: usize = 0;
315352 while (true) {
316353 // give me everything you've got, captain
......@@ -323,7 +360,7 @@ pub const CacheHash = struct {
323360
324361 ch_file.contents = contents;
325362 } else {
326 try hashFile(file, &ch_file.bin_digest, self.hasher_init);
363 try hashFile(file, &ch_file.bin_digest);
327364 }
328365
329366 self.hasher.update(&ch_file.bin_digest);
......@@ -435,11 +472,12 @@ pub const CacheHash = struct {
435472 file.deinit(self.allocator);
436473 }
437474 self.files.deinit();
438 self.manifest_dir.close();
475 if (self.owns_manifest_dir)
476 self.manifest_dir.close();
439477 }
440478};
441479
442fn hashFile(file: fs.File, bin_digest: []u8, hasher_init: anytype) !void {
480fn hashFile(file: fs.File, bin_digest: []u8) !void {
443481 var buf: [1024]u8 = undefined;
444482
445483 var hasher = hasher_init;
......@@ -509,7 +547,7 @@ test "cache file and then recall it" {
509547
510548 ch.add(true);
511549 ch.add(@as(u16, 1234));
512 ch.add("1234");
550 ch.addBytes("1234");
513551 _ = try ch.addFile(temp_file, null);
514552
515553 // There should be nothing in the cache
......@@ -523,7 +561,7 @@ test "cache file and then recall it" {
523561
524562 ch.add(true);
525563 ch.add(@as(u16, 1234));
526 ch.add("1234");
564 ch.addBytes("1234");
527565 _ = try ch.addFile(temp_file, null);
528566
529567 // Cache hit! We just "built" the same file
......@@ -577,7 +615,7 @@ test "check that changing a file makes cache fail" {
577615 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
578616 defer ch.release();
579617
580 ch.add("1234");
618 ch.addBytes("1234");
581619 const temp_file_idx = try ch.addFile(temp_file, 100);
582620
583621 // There should be nothing in the cache
......@@ -594,7 +632,7 @@ test "check that changing a file makes cache fail" {
594632 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
595633 defer ch.release();
596634
597 ch.add("1234");
635 ch.addBytes("1234");
598636 const temp_file_idx = try ch.addFile(temp_file, 100);
599637
600638 // A file that we depend on has been updated, so the cache should not contain an entry for it
......@@ -628,7 +666,7 @@ test "no file inputs" {
628666 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
629667 defer ch.release();
630668
631 ch.add("1234");
669 ch.addBytes("1234");
632670
633671 // There should be nothing in the cache
634672 testing.expectEqual(@as(?[BASE64_DIGEST_LEN]u8, null), try ch.hit());
......@@ -639,7 +677,7 @@ test "no file inputs" {
639677 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
640678 defer ch.release();
641679
642 ch.add("1234");
680 ch.addBytes("1234");
643681
644682 digest2 = (try ch.hit()).?;
645683 }
......@@ -674,7 +712,7 @@ test "CacheHashes with files added after initial hash work" {
674712 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
675713 defer ch.release();
676714
677 ch.add("1234");
715 ch.addBytes("1234");
678716 _ = try ch.addFile(temp_file1, null);
679717
680718 // There should be nothing in the cache
......@@ -688,7 +726,7 @@ test "CacheHashes with files added after initial hash work" {
688726 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
689727 defer ch.release();
690728
691 ch.add("1234");
729 ch.addBytes("1234");
692730 _ = try ch.addFile(temp_file1, null);
693731
694732 digest2 = (try ch.hit()).?;
......@@ -707,7 +745,7 @@ test "CacheHashes with files added after initial hash work" {
707745 var ch = try CacheHash.init(testing.allocator, cwd, temp_manifest_dir);
708746 defer ch.release();
709747
710 ch.add("1234");
748 ch.addBytes("1234");
711749 _ = try ch.addFile(temp_file1, null);
712750
713751 // A file that we depend on has been updated, so the cache should not contain an entry for it
lib/std/target.zig-2
......@@ -465,8 +465,6 @@ pub const Target = struct {
465465 };
466466
467467 pub const ObjectFormat = enum {
468 /// TODO Get rid of this one.
469 unknown,
470468 coff,
471469 pe,
472470 elf,
lib/std/zig.zig+44-9
......@@ -71,17 +71,52 @@ pub fn binNameAlloc(
7171 target: std.Target,
7272 output_mode: std.builtin.OutputMode,
7373 link_mode: ?std.builtin.LinkMode,
74 object_format: ?std.Target.ObjectFormat,
7475) error{OutOfMemory}![]u8 {
75 switch (output_mode) {
76 .Exe => return std.fmt.allocPrint(allocator, "{}{}", .{ root_name, target.exeFileExt() }),
77 .Lib => {
78 const suffix = switch (link_mode orelse .Static) {
79 .Static => target.staticLibSuffix(),
80 .Dynamic => target.dynamicLibSuffix(),
81 };
82 return std.fmt.allocPrint(allocator, "{}{}{}", .{ target.libPrefix(), root_name, suffix });
76 switch (object_format orelse target.getObjectFormat()) {
77 .coff, .pe => switch (output_mode) {
78 .Exe => {
79 const suffix = switch (target.os.tag) {
80 .uefi => ".efi",
81 else => ".exe",
82 };
83 return std.fmt.allocPrint(allocator, "{}{}", .{ root_name, suffix });
84 },
85 .Lib => {
86 const suffix = switch (link_mode orelse .Static) {
87 .Static => ".lib",
88 .Dynamic => ".dll",
89 };
90 return std.fmt.allocPrint(allocator, "{}{}{}", .{ target.libPrefix(), root_name, suffix });
91 },
92 .Obj => return std.fmt.allocPrint(allocator, "{}.obj", .{root_name}),
93 },
94 .elf => switch (output_mode) {
95 .Exe => return allocator.dupe(u8, root_name),
96 .Lib => {
97 const suffix = switch (link_mode orelse .Static) {
98 .Static => ".a",
99 .Dynamic => ".so",
100 };
101 return std.fmt.allocPrint(allocator, "{}{}{}", .{ target.libPrefix(), root_name, suffix });
102 },
103 .Obj => return std.fmt.allocPrint(allocator, "{}.o", .{root_name}),
104 },
105 .macho => switch (output_mode) {
106 .Exe => return allocator.dupe(u8, root_name),
107 .Lib => {
108 const suffix = switch (link_mode orelse .Static) {
109 .Static => ".a",
110 .Dynamic => ".dylib",
111 };
112 return std.fmt.allocPrint(allocator, "{}{}{}", .{ target.libPrefix(), root_name, suffix });
113 },
114 .Obj => return std.fmt.allocPrint(allocator, "{}.o", .{root_name}),
83115 },
84 .Obj => return std.fmt.allocPrint(allocator, "{}{}", .{ root_name, target.oFileExt() }),
116 .wasm => return std.fmt.allocPrint(allocator, "{}.wasm", .{root_name}),
117 .c => return std.fmt.allocPrint(allocator, "{}.c", .{root_name}),
118 .hex => return std.fmt.allocPrint(allocator, "{}.ihex", .{root_name}),
119 .raw => return std.fmt.allocPrint(allocator, "{}.bin", .{root_name}),
85120 }
86121}
87122
src-self-hosted/Module.zig+319-34
......@@ -22,11 +22,12 @@ const trace = @import("tracy.zig").trace;
2222const liveness = @import("liveness.zig");
2323const astgen = @import("astgen.zig");
2424const zir_sema = @import("zir_sema.zig");
25const build_options = @import("build_options");
2526
2627/// General-purpose allocator. Used for both temporary and long-term storage.
2728gpa: *Allocator,
28/// Pointer to externally managed resource.
29root_pkg: *Package,
29/// Pointer to externally managed resource. `null` if there is no zig file being compiled.
30root_pkg: ?*Package,
3031/// Module owns this resource.
3132/// The `Scope` is either a `Scope.ZIRModule` or `Scope.File`.
3233root_scope: *Scope,
......@@ -48,22 +49,26 @@ export_owners: std.AutoArrayHashMapUnmanaged(*Decl, []*Export) = .{},
4849/// Maps fully qualified namespaced names to the Decl struct for them.
4950decl_table: std.ArrayHashMapUnmanaged(Scope.NameHash, *Decl, Scope.name_hash_hash, Scope.name_hash_eql, false) = .{},
5051
52c_object_table: std.AutoArrayHashMapUnmanaged(*CObject, void) = .{},
53
5154link_error_flags: link.File.ErrorFlags = .{},
5255
5356work_queue: std.fifo.LinearFifo(WorkItem, .Dynamic),
5457
5558/// We optimize memory usage for a compilation with no compile errors by storing the
5659/// error messages and mapping outside of `Decl`.
57/// The ErrorMsg memory is owned by the decl, using Module's allocator.
60/// The ErrorMsg memory is owned by the decl, using Module's general purpose allocator.
5861/// Note that a Decl can succeed but the Fn it represents can fail. In this case,
5962/// a Decl can have a failed_decls entry but have analysis status of success.
6063failed_decls: std.AutoArrayHashMapUnmanaged(*Decl, *ErrorMsg) = .{},
6164/// Using a map here for consistency with the other fields here.
62/// The ErrorMsg memory is owned by the `Scope`, using Module's allocator.
65/// The ErrorMsg memory is owned by the `Scope`, using Module's general purpose allocator.
6366failed_files: std.AutoArrayHashMapUnmanaged(*Scope, *ErrorMsg) = .{},
6467/// Using a map here for consistency with the other fields here.
65/// The ErrorMsg memory is owned by the `Export`, using Module's allocator.
68/// The ErrorMsg memory is owned by the `Export`, using Module's general purpose allocator.
6669failed_exports: std.AutoArrayHashMapUnmanaged(*Export, *ErrorMsg) = .{},
70/// The ErrorMsg memory is owned by the `CObject`, using Module's general purpose allocator.
71failed_c_objects: std.AutoArrayHashMapUnmanaged(*CObject, *ErrorMsg) = .{},
6772
6873/// Incrementing integer used to compare against the corresponding Decl
6974/// field to determine whether a Decl's status applies to an ongoing update, or a
......@@ -79,10 +84,15 @@ deletion_set: std.ArrayListUnmanaged(*Decl) = .{},
7984/// Owned by Module.
8085root_name: []u8,
8186keep_source_files_loaded: bool,
87use_clang: bool,
8288
8389/// Error tags and their values, tag names are duped with mod.gpa.
8490global_error_set: std.StringHashMapUnmanaged(u16) = .{},
8591
92c_source_files: []const []const u8,
93clang_argv: []const []const u8,
94cache: std.cache_hash.CacheHash,
95
8696pub const InnerError = error{ OutOfMemory, AnalysisFail };
8797
8898const WorkItem = union(enum) {
......@@ -95,6 +105,9 @@ const WorkItem = union(enum) {
95105 /// The source file containing the Decl has been updated, and so the
96106 /// Decl may need its line number information updated in the debug info.
97107 update_line_number: *Decl,
108 /// Invoke the Clang compiler to create an object file, which gets linked
109 /// with the Module.
110 c_object: *CObject,
98111};
99112
100113pub const Export = struct {
......@@ -230,6 +243,7 @@ pub const Decl = struct {
230243 const src_decl = module.decls[self.src_index];
231244 return src_decl.inst.src;
232245 },
246 .none => unreachable,
233247 .file, .block => unreachable,
234248 .gen_zir => unreachable,
235249 .local_val => unreachable,
......@@ -282,6 +296,30 @@ pub const Decl = struct {
282296 }
283297};
284298
299pub const CObject = struct {
300 /// Relative to cwd. Owned by arena.
301 src_path: []const u8,
302 /// Owned by arena.
303 extra_flags: []const []const u8,
304 arena: std.heap.ArenaAllocator.State,
305 status: union(enum) {
306 new,
307 /// This is the output object path. Owned by gpa.
308 success: []u8,
309 /// There will be a corresponding ErrorMsg in Module.failed_c_objects.
310 /// This is the C source file contents (used for printing error messages). Owned by gpa.
311 failure: []u8,
312 },
313
314 pub fn destroy(self: *CObject, gpa: *Allocator) void {
315 switch (self.status) {
316 .new => {},
317 .failure, .success => |data| gpa.free(data),
318 }
319 self.arena.promote(gpa).deinit();
320 }
321};
322
285323/// Fn struct memory is owned by the Decl's TypedValue.Managed arena allocator.
286324pub const Fn = struct {
287325 /// This memory owned by the Decl's TypedValue.Managed arena allocator.
......@@ -361,6 +399,7 @@ pub const Scope = struct {
361399 .zir_module => return &self.cast(ZIRModule).?.contents.module.arena.allocator,
362400 .file => unreachable,
363401 .container => unreachable,
402 .none => unreachable,
364403 }
365404 }
366405
......@@ -376,6 +415,7 @@ pub const Scope = struct {
376415 .zir_module => null,
377416 .file => null,
378417 .container => null,
418 .none => unreachable,
379419 };
380420 }
381421
......@@ -390,6 +430,7 @@ pub const Scope = struct {
390430 .decl => return self.cast(DeclAnalysis).?.decl.scope,
391431 .file => return &self.cast(File).?.root_container.base,
392432 .zir_module, .container => return self,
433 .none => unreachable,
393434 }
394435 }
395436
......@@ -406,6 +447,7 @@ pub const Scope = struct {
406447 .file => unreachable,
407448 .zir_module => return self.cast(ZIRModule).?.fullyQualifiedNameHash(name),
408449 .container => return self.cast(Container).?.fullyQualifiedNameHash(name),
450 .none => unreachable,
409451 }
410452 }
411453
......@@ -414,6 +456,7 @@ pub const Scope = struct {
414456 switch (self.tag) {
415457 .file => return self.cast(File).?.contents.tree,
416458 .zir_module => unreachable,
459 .none => unreachable,
417460 .decl => return self.cast(DeclAnalysis).?.decl.scope.cast(Container).?.file_scope.contents.tree,
418461 .block => return self.cast(Block).?.decl.scope.cast(Container).?.file_scope.contents.tree,
419462 .gen_zir => return self.cast(GenZIR).?.decl.scope.cast(Container).?.file_scope.contents.tree,
......@@ -434,6 +477,7 @@ pub const Scope = struct {
434477 .zir_module => unreachable,
435478 .file => unreachable,
436479 .container => unreachable,
480 .none => unreachable,
437481 };
438482 }
439483
......@@ -444,6 +488,7 @@ pub const Scope = struct {
444488 .container => return @fieldParentPtr(Container, "base", base).file_scope.sub_file_path,
445489 .file => return @fieldParentPtr(File, "base", base).sub_file_path,
446490 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).sub_file_path,
491 .none => unreachable,
447492 .block => unreachable,
448493 .gen_zir => unreachable,
449494 .local_val => unreachable,
......@@ -456,6 +501,7 @@ pub const Scope = struct {
456501 switch (base.tag) {
457502 .file => return @fieldParentPtr(File, "base", base).unload(gpa),
458503 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).unload(gpa),
504 .none => {},
459505 .block => unreachable,
460506 .gen_zir => unreachable,
461507 .local_val => unreachable,
......@@ -470,6 +516,7 @@ pub const Scope = struct {
470516 .container => return @fieldParentPtr(Container, "base", base).file_scope.getSource(module),
471517 .file => return @fieldParentPtr(File, "base", base).getSource(module),
472518 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).getSource(module),
519 .none => unreachable,
473520 .gen_zir => unreachable,
474521 .local_val => unreachable,
475522 .local_ptr => unreachable,
......@@ -483,6 +530,7 @@ pub const Scope = struct {
483530 switch (base.tag) {
484531 .container => return @fieldParentPtr(Container, "base", base).removeDecl(child),
485532 .zir_module => return @fieldParentPtr(ZIRModule, "base", base).removeDecl(child),
533 .none => unreachable,
486534 .file => unreachable,
487535 .block => unreachable,
488536 .gen_zir => unreachable,
......@@ -505,6 +553,10 @@ pub const Scope = struct {
505553 scope_zir_module.deinit(gpa);
506554 gpa.destroy(scope_zir_module);
507555 },
556 .none => {
557 const scope_none = @fieldParentPtr(None, "base", base);
558 gpa.destroy(scope_none);
559 },
508560 .block => unreachable,
509561 .gen_zir => unreachable,
510562 .local_val => unreachable,
......@@ -527,6 +579,8 @@ pub const Scope = struct {
527579 zir_module,
528580 /// .zig source code.
529581 file,
582 /// There is no .zig or .zir source code being compiled in this Module.
583 none,
530584 /// struct, enum or union, every .file contains one of these.
531585 container,
532586 block,
......@@ -622,7 +676,7 @@ pub const Scope = struct {
622676 pub fn getSource(self: *File, module: *Module) ![:0]const u8 {
623677 switch (self.source) {
624678 .unloaded => {
625 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(
679 const source = try module.root_pkg.?.root_src_dir.readFileAllocOptions(
626680 module.gpa,
627681 self.sub_file_path,
628682 std.math.maxInt(u32),
......@@ -638,6 +692,12 @@ pub const Scope = struct {
638692 }
639693 };
640694
695 /// For when there is no top level scope because there are no .zig files being compiled.
696 pub const None = struct {
697 pub const base_tag: Tag = .none;
698 base: Scope = Scope{ .tag = base_tag },
699 };
700
641701 pub const ZIRModule = struct {
642702 pub const base_tag: Tag = .zir_module;
643703 base: Scope = Scope{ .tag = base_tag },
......@@ -720,7 +780,7 @@ pub const Scope = struct {
720780 pub fn getSource(self: *ZIRModule, module: *Module) ![:0]const u8 {
721781 switch (self.source) {
722782 .unloaded => {
723 const source = try module.root_pkg.root_src_dir.readFileAllocOptions(
783 const source = try module.root_pkg.?.root_src_dir.readFileAllocOptions(
724784 module.gpa,
725785 self.sub_file_path,
726786 std.math.maxInt(u32),
......@@ -855,20 +915,81 @@ pub const AllErrors = struct {
855915pub const InitOptions = struct {
856916 target: std.Target,
857917 root_name: []const u8,
858 root_pkg: *Package,
918 root_pkg: ?*Package,
859919 output_mode: std.builtin.OutputMode,
860920 bin_file_dir: ?std.fs.Dir = null,
861921 bin_file_path: []const u8,
922 emit_h: ?[]const u8 = null,
862923 link_mode: ?std.builtin.LinkMode = null,
863924 object_format: ?std.builtin.ObjectFormat = null,
864925 optimize_mode: std.builtin.Mode = .Debug,
865926 keep_source_files_loaded: bool = false,
927 clang_argv: []const []const u8 = &[0][]const u8{},
928 lib_dirs: []const []const u8 = &[0][]const u8{},
929 rpath_list: []const []const u8 = &[0][]const u8{},
930 c_source_files: []const []const u8 = &[0][]const u8{},
931 link_objects: []const []const u8 = &[0][]const u8{},
932 framework_dirs: []const []const u8 = &[0][]const u8{},
933 frameworks: []const []const u8 = &[0][]const u8{},
934 system_libs: []const []const u8 = &[0][]const u8{},
935 have_libc: bool = false,
936 have_libcpp: bool = false,
937 want_pic: ?bool = null,
938 want_sanitize_c: ?bool = null,
939 use_llvm: ?bool = null,
940 use_lld: ?bool = null,
941 use_clang: ?bool = null,
942 rdynamic: bool = false,
943 strip: bool = false,
944 linker_script: ?[]const u8 = null,
945 version_script: ?[]const u8 = null,
946 disable_c_depfile: bool = false,
947 override_soname: ?[]const u8 = null,
948 linker_optimization: ?[]const u8 = null,
949 linker_gc_sections: ?bool = null,
950 linker_allow_shlib_undefined: ?bool = null,
951 linker_bind_global_refs_locally: ?bool = null,
952 linker_z_nodelete: bool = false,
953 linker_z_defs: bool = false,
954 stack_size_override: u64 = 0,
955 compiler_id: [16]u8,
866956};
867957
868958pub fn init(gpa: *Allocator, options: InitOptions) !Module {
869959 const root_name = try gpa.dupe(u8, options.root_name);
870960 errdefer gpa.free(root_name);
871961
962 const ofmt = options.object_format orelse options.target.getObjectFormat();
963
964 // Make a decision on whether to use LLD or our own linker.
965 const use_lld = if (options.use_lld) |explicit| explicit else blk: {
966 if (!build_options.have_llvm)
967 break :blk false;
968
969 if (ofmt == .c)
970 break :blk false;
971
972 // Our linker can't handle objects or most advanced options yet.
973 if (options.link_objects.len != 0 or
974 options.c_source_files.len != 0 or
975 options.frameworks.len != 0 or
976 options.system_libs.len != 0 or
977 options.have_libc or options.have_libcpp or
978 options.linker_script != null or options.version_script != null)
979 {
980 break :blk true;
981 }
982 break :blk false;
983 };
984
985 // Make a decision on whether to use LLVM or our own backend.
986 const use_llvm = if (options.use_llvm) |explicit| explicit else blk: {
987 // We would want to prefer LLVM for release builds when it is available, however
988 // we don't have an LLVM backend yet :)
989 // We would also want to prefer LLVM for architectures that we don't have self-hosted support for too.
990 break :blk false;
991 };
992
872993 const bin_file_dir = options.bin_file_dir orelse std.fs.cwd();
873994 const bin_file = try link.File.openPath(gpa, bin_file_dir, options.bin_file_path, .{
874995 .root_name = root_name,
......@@ -876,39 +997,130 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
876997 .target = options.target,
877998 .output_mode = options.output_mode,
878999 .link_mode = options.link_mode orelse .Static,
879 .object_format = options.object_format orelse options.target.getObjectFormat(),
1000 .object_format = ofmt,
8801001 .optimize_mode = options.optimize_mode,
1002 .use_lld = use_lld,
1003 .use_llvm = use_llvm,
1004 .objects = options.link_objects,
1005 .frameworks = options.frameworks,
1006 .framework_dirs = options.framework_dirs,
1007 .system_libs = options.system_libs,
1008 .lib_dirs = options.lib_dirs,
1009 .rpath_list = options.rpath_list,
1010 .strip = options.strip,
8811011 });
8821012 errdefer bin_file.destroy();
8831013
8841014 const root_scope = blk: {
885 if (mem.endsWith(u8, options.root_pkg.root_src_path, ".zig")) {
886 const root_scope = try gpa.create(Scope.File);
887 root_scope.* = .{
888 .sub_file_path = options.root_pkg.root_src_path,
889 .source = .{ .unloaded = {} },
890 .contents = .{ .not_available = {} },
891 .status = .never_loaded,
892 .root_container = .{
893 .file_scope = root_scope,
1015 if (options.root_pkg) |root_pkg| {
1016 if (mem.endsWith(u8, root_pkg.root_src_path, ".zig")) {
1017 const root_scope = try gpa.create(Scope.File);
1018 root_scope.* = .{
1019 .sub_file_path = root_pkg.root_src_path,
1020 .source = .{ .unloaded = {} },
1021 .contents = .{ .not_available = {} },
1022 .status = .never_loaded,
1023 .root_container = .{
1024 .file_scope = root_scope,
1025 .decls = .{},
1026 },
1027 };
1028 break :blk &root_scope.base;
1029 } else if (mem.endsWith(u8, root_pkg.root_src_path, ".zir")) {
1030 const root_scope = try gpa.create(Scope.ZIRModule);
1031 root_scope.* = .{
1032 .sub_file_path = root_pkg.root_src_path,
1033 .source = .{ .unloaded = {} },
1034 .contents = .{ .not_available = {} },
1035 .status = .never_loaded,
8941036 .decls = .{},
895 },
896 };
897 break :blk &root_scope.base;
898 } else if (mem.endsWith(u8, options.root_pkg.root_src_path, ".zir")) {
899 const root_scope = try gpa.create(Scope.ZIRModule);
900 root_scope.* = .{
901 .sub_file_path = options.root_pkg.root_src_path,
902 .source = .{ .unloaded = {} },
903 .contents = .{ .not_available = {} },
904 .status = .never_loaded,
905 .decls = .{},
906 };
907 break :blk &root_scope.base;
1037 };
1038 break :blk &root_scope.base;
1039 } else {
1040 unreachable;
1041 }
9081042 } else {
909 unreachable;
1043 const root_scope = try gpa.create(Scope.None);
1044 root_scope.* = .{};
1045 break :blk &root_scope.base;
1046 }
1047 };
1048
1049 // We put everything into the cache hash except for the root source file, because we want to
1050 // find the same binary and incrementally update it even if the file contents changed.
1051 const cache_dir = if (options.root_pkg) |root_pkg| root_pkg.root_src_dir else std.fs.cwd();
1052 var cache = try std.cache_hash.CacheHash.init(gpa, cache_dir, "zig-cache");
1053 errdefer cache.release();
1054
1055 // Now we will prepare hash state initializations to avoid redundantly computing hashes.
1056 // First we add common things between things that apply to zig source and all c source files.
1057 cache.add(options.compiler_id);
1058 cache.add(options.optimize_mode);
1059 cache.add(options.target.cpu.arch);
1060 cache.addBytes(options.target.cpu.model.name);
1061 cache.add(options.target.cpu.features.ints);
1062 cache.add(options.target.os.tag);
1063 switch (options.target.os.tag) {
1064 .linux => {
1065 cache.add(options.target.os.version_range.linux.range.min);
1066 cache.add(options.target.os.version_range.linux.range.max);
1067 cache.add(options.target.os.version_range.linux.glibc);
1068 },
1069 .windows => {
1070 cache.add(options.target.os.version_range.windows.min);
1071 cache.add(options.target.os.version_range.windows.max);
1072 },
1073 .freebsd,
1074 .macosx,
1075 .ios,
1076 .tvos,
1077 .watchos,
1078 .netbsd,
1079 .openbsd,
1080 .dragonfly,
1081 => {
1082 cache.add(options.target.os.version_range.semver.min);
1083 cache.add(options.target.os.version_range.semver.max);
1084 },
1085 else => {},
1086 }
1087 cache.add(options.target.abi);
1088 cache.add(ofmt);
1089 // TODO PIC (see detect_pic from codegen.cpp)
1090 cache.add(bin_file.options.link_mode);
1091 cache.add(options.strip);
1092
1093 // Make a decision on whether to use Clang for translate-c and compiling C files.
1094 const use_clang = if (options.use_clang) |explicit| explicit else blk: {
1095 if (build_options.have_llvm) {
1096 // Can't use it if we don't have it!
1097 break :blk false;
9101098 }
1099 // It's not planned to do our own translate-c or C compilation.
1100 break :blk true;
9111101 };
1102 var c_object_table = std.AutoArrayHashMapUnmanaged(*CObject, void){};
1103 errdefer {
1104 for (c_object_table.items()) |entry| entry.key.destroy(gpa);
1105 c_object_table.deinit(gpa);
1106 }
1107 // Add a `CObject` for each `c_source_files`.
1108 try c_object_table.ensureCapacity(gpa, options.c_source_files.len);
1109 for (options.c_source_files) |c_source_file| {
1110 var local_arena = std.heap.ArenaAllocator.init(gpa);
1111 errdefer local_arena.deinit();
1112
1113 const c_object = try local_arena.allocator.create(CObject);
1114 const src_path = try local_arena.allocator.dupe(u8, c_source_file);
1115
1116 c_object.* = .{
1117 .status = .{ .new = {} },
1118 .src_path = src_path,
1119 .extra_flags = &[0][]const u8{},
1120 .arena = local_arena.state,
1121 };
1122 c_object_table.putAssumeCapacityNoClobber(c_object, {});
1123 }
9121124
9131125 return Module{
9141126 .gpa = gpa,
......@@ -920,6 +1132,11 @@ pub fn init(gpa: *Allocator, options: InitOptions) !Module {
9201132 .bin_file = bin_file,
9211133 .work_queue = std.fifo.LinearFifo(WorkItem, .Dynamic).init(gpa),
9221134 .keep_source_files_loaded = options.keep_source_files_loaded,
1135 .use_clang = use_clang,
1136 .clang_argv = options.clang_argv,
1137 .c_source_files = options.c_source_files,
1138 .cache = cache,
1139 .c_object_table = c_object_table,
9231140 };
9241141}
9251142
......@@ -935,11 +1152,21 @@ pub fn deinit(self: *Module) void {
9351152 }
9361153 self.decl_table.deinit(gpa);
9371154
1155 for (self.c_object_table.items()) |entry| {
1156 entry.key.destroy(gpa);
1157 }
1158 self.c_object_table.deinit(gpa);
1159
9381160 for (self.failed_decls.items()) |entry| {
9391161 entry.value.destroy(gpa);
9401162 }
9411163 self.failed_decls.deinit(gpa);
9421164
1165 for (self.failed_c_objects.items()) |entry| {
1166 entry.value.destroy(gpa);
1167 }
1168 self.failed_c_objects.deinit(gpa);
1169
9431170 for (self.failed_files.items()) |entry| {
9441171 entry.value.destroy(gpa);
9451172 }
......@@ -969,6 +1196,7 @@ pub fn deinit(self: *Module) void {
9691196 gpa.free(entry.key);
9701197 }
9711198 self.global_error_set.deinit(gpa);
1199 self.cache.release();
9721200 self.* = undefined;
9731201}
9741202
......@@ -995,7 +1223,15 @@ pub fn update(self: *Module) !void {
9951223
9961224 self.generation += 1;
9971225
998 // TODO Use the cache hash file system to detect which source files changed.
1226 // For compiling C objects, we rely on the cache hash system to avoid duplicating work.
1227 // TODO Look into caching this data in memory to improve performance.
1228 // Add a WorkItem for each C object.
1229 try self.work_queue.ensureUnusedCapacity(self.c_object_table.items().len);
1230 for (self.c_object_table.items()) |entry| {
1231 self.work_queue.writeItemAssumeCapacity(.{ .c_object = entry.key });
1232 }
1233
1234 // TODO Detect which source files changed.
9991235 // Until then we simulate a full cache miss. Source files could have been loaded for any reason;
10001236 // to force a refresh we unload now.
10011237 if (self.root_scope.cast(Scope.File)) |zig_file| {
......@@ -1053,6 +1289,7 @@ pub fn makeBinFileWritable(self: *Module) !void {
10531289
10541290pub fn totalErrorCount(self: *Module) usize {
10551291 const total = self.failed_decls.items().len +
1292 self.failed_c_objects.items().len +
10561293 self.failed_files.items().len +
10571294 self.failed_exports.items().len;
10581295 return if (total == 0) @boolToInt(self.link_error_flags.no_entry_point_found) else total;
......@@ -1065,6 +1302,12 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
10651302 var errors = std.ArrayList(AllErrors.Message).init(self.gpa);
10661303 defer errors.deinit();
10671304
1305 for (self.failed_c_objects.items()) |entry| {
1306 const c_object = entry.key;
1307 const err_msg = entry.value;
1308 const source = c_object.status.failure;
1309 try AllErrors.add(&arena, &errors, c_object.src_path, source, err_msg.*);
1310 }
10681311 for (self.failed_files.items()) |entry| {
10691312 const scope = entry.key;
10701313 const err_msg = entry.value;
......@@ -1085,8 +1328,14 @@ pub fn getAllErrorsAlloc(self: *Module) !AllErrors {
10851328 }
10861329
10871330 if (errors.items.len == 0 and self.link_error_flags.no_entry_point_found) {
1331 const global_err_src_path = blk: {
1332 if (self.root_pkg) |root_pkg| break :blk root_pkg.root_src_path;
1333 if (self.c_source_files.len != 0) break :blk self.c_source_files[0];
1334 if (self.bin_file.options.objects.len != 0) break :blk self.bin_file.options.objects[0];
1335 break :blk "(no file)";
1336 };
10881337 try errors.append(.{
1089 .src_path = self.root_pkg.root_src_path,
1338 .src_path = global_err_src_path,
10901339 .line = 0,
10911340 .column = 0,
10921341 .byte_offset = 0,
......@@ -1175,6 +1424,41 @@ pub fn performAllTheWork(self: *Module) error{OutOfMemory}!void {
11751424 decl.analysis = .codegen_failure_retryable;
11761425 };
11771426 },
1427 .c_object => |c_object| {
1428 // Free the previous attempt.
1429 switch (c_object.status) {
1430 .new => {},
1431 .success => |o_file_path| {
1432 self.gpa.free(o_file_path);
1433 c_object.status = .{ .new = {} };
1434 },
1435 .failure => |source| {
1436 self.failed_c_objects.removeAssertDiscard(c_object);
1437 self.gpa.free(source);
1438
1439 c_object.status = .{ .new = {} };
1440 },
1441 }
1442 if (!build_options.have_llvm) {
1443 try self.failed_c_objects.ensureCapacity(self.gpa, self.failed_c_objects.items().len + 1);
1444 self.failed_c_objects.putAssumeCapacityNoClobber(c_object, try ErrorMsg.create(
1445 self.gpa,
1446 0,
1447 "clang not available: compiler not built with LLVM extensions enabled",
1448 .{},
1449 ));
1450 c_object.status = .{ .failure = "" };
1451 continue;
1452 }
1453 try self.failed_c_objects.ensureCapacity(self.gpa, self.failed_c_objects.items().len + 1);
1454 self.failed_c_objects.putAssumeCapacityNoClobber(c_object, try ErrorMsg.create(
1455 self.gpa,
1456 0,
1457 "TODO: implement invoking clang to compile C source files",
1458 .{},
1459 ));
1460 c_object.status = .{ .failure = "" };
1461 },
11781462 };
11791463}
11801464
......@@ -3161,6 +3445,7 @@ fn failWithOwnedErrorMsg(self: *Module, scope: *Scope, src: usize, err_msg: *Err
31613445 zir_module.status = .loaded_sema_failure;
31623446 self.failed_files.putAssumeCapacityNoClobber(scope, err_msg);
31633447 },
3448 .none => unreachable,
31643449 .file => unreachable,
31653450 .container => unreachable,
31663451 }
src-self-hosted/link.zig+26-5
......@@ -16,14 +16,36 @@ pub const Options = struct {
1616 object_format: std.builtin.ObjectFormat,
1717 optimize_mode: std.builtin.Mode,
1818 root_name: []const u8,
19 root_pkg: *const Package,
19 root_pkg: ?*const Package,
2020 /// Used for calculating how much space to reserve for symbols in case the binary file
2121 /// does not already have a symbol table.
2222 symbol_count_hint: u64 = 32,
2323 /// Used for calculating how much space to reserve for executable program code in case
24 /// the binary file deos not already have such a section.
24 /// the binary file does not already have such a section.
2525 program_code_size_hint: u64 = 256 * 1024,
2626 entry_addr: ?u64 = null,
27 /// Set to `true` to omit debug info.
28 strip: bool = false,
29 /// If this is true then this link code is responsible for outputting an object
30 /// file and then using LLD to link it together with the link options and other objects.
31 /// Otherwise (depending on `use_llvm`) this link code directly outputs and updates the final binary.
32 use_lld: bool = false,
33 /// If this is true then this link code is responsible for making an LLVM IR Module,
34 /// outputting it to an object file, and then linking that together with link options and
35 /// other objects.
36 /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary.
37 use_llvm: bool = false,
38
39 objects: []const []const u8 = &[0][]const u8{},
40 framework_dirs: []const []const u8 = &[0][]const u8{},
41 frameworks: []const []const u8 = &[0][]const u8{},
42 system_libs: []const []const u8 = &[0][]const u8{},
43 lib_dirs: []const []const u8 = &[0][]const u8{},
44 rpath_list: []const []const u8 = &[0][]const u8{},
45
46 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
47 return if (options.use_lld) .Obj else options.output_mode;
48 }
2749};
2850
2951pub const File = struct {
......@@ -67,14 +89,13 @@ pub const File = struct {
6789 /// and does not cause Illegal Behavior. This operation is not atomic.
6890 pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: Options) !*File {
6991 switch (options.object_format) {
70 .unknown => unreachable,
7192 .coff, .pe => return Coff.openPath(allocator, dir, sub_path, options),
7293 .elf => return Elf.openPath(allocator, dir, sub_path, options),
7394 .macho => return MachO.openPath(allocator, dir, sub_path, options),
7495 .wasm => return Wasm.openPath(allocator, dir, sub_path, options),
7596 .c => return C.openPath(allocator, dir, sub_path, options),
76 .hex => return error.TODOImplementHex,
77 .raw => return error.TODOImplementRaw,
97 .hex => return error.HexObjectFormatUnimplemented,
98 .raw => return error.RawObjectFormatUnimplemented,
7899 }
79100 }
80101
src-self-hosted/link/C.zig+3
......@@ -25,6 +25,9 @@ error_msg: *Module.ErrorMsg = undefined,
2525pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*File {
2626 assert(options.object_format == .c);
2727
28 if (options.use_llvm) return error.LLVM_HasNoCBackend;
29 if (options.use_lld) return error.LLD_HasNoCBackend;
30
2831 const file = try dir.createFile(sub_path, .{ .truncate = true, .read = true, .mode = link.determineMode(options) });
2932 errdefer file.close();
3033
src-self-hosted/link/Coff.zig+3
......@@ -113,6 +113,9 @@ pub const SrcFn = void;
113113pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {
114114 assert(options.object_format == .coff);
115115
116 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForCoff; // TODO
117 if (options.use_lld) return error.LLD_LinkingIsTODO_ForCoff; // TODO
118
116119 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options) });
117120 errdefer file.close();
118121
src-self-hosted/link/Elf.zig+12-9
......@@ -219,6 +219,9 @@ pub const SrcFn = struct {
219219pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*File {
220220 assert(options.object_format == .elf);
221221
222 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForELF; // TODO
223 if (options.use_lld) return error.LLD_LinkingIsTODOForELF; // TODO
224
222225 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options) });
223226 errdefer file.close();
224227
......@@ -235,7 +238,7 @@ pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, option
235238
236239/// Returns error.IncrFailed if incremental update could not be performed.
237240fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Elf {
238 switch (options.output_mode) {
241 switch (options.effectiveOutputMode()) {
239242 .Exe => {},
240243 .Obj => {},
241244 .Lib => return error.IncrFailed,
......@@ -264,7 +267,7 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Elf {
264267/// Truncates the existing file contents and overwrites the contents.
265268/// Returns an error if `file` is not already open with +read +write +seek abilities.
266269fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Elf {
267 switch (options.output_mode) {
270 switch (options.effectiveOutputMode()) {
268271 .Exe => {},
269272 .Obj => {},
270273 .Lib => return error.TODOImplementWritingLibFiles,
......@@ -861,8 +864,8 @@ pub fn flush(self: *Elf, module: *Module) !void {
861864 },
862865 }
863866 // Write the form for the compile unit, which must match the abbrev table above.
864 const name_strp = try self.makeDebugString(self.base.options.root_pkg.root_src_path);
865 const comp_dir_strp = try self.makeDebugString(self.base.options.root_pkg.root_src_dir_path);
867 const name_strp = try self.makeDebugString(self.base.options.root_pkg.?.root_src_path);
868 const comp_dir_strp = try self.makeDebugString(self.base.options.root_pkg.?.root_src_dir_path);
866869 const producer_strp = try self.makeDebugString(link.producer_string);
867870 // Currently only one compilation unit is supported, so the address range is simply
868871 // identical to the main program header virtual address and memory size.
......@@ -1031,7 +1034,7 @@ pub fn flush(self: *Elf, module: *Module) !void {
10311034 0, // include_directories (none except the compilation unit cwd)
10321035 });
10331036 // file_names[0]
1034 di_buf.appendSliceAssumeCapacity(self.base.options.root_pkg.root_src_path); // relative path name
1037 di_buf.appendSliceAssumeCapacity(self.base.options.root_pkg.?.root_src_path); // relative path name
10351038 di_buf.appendSliceAssumeCapacity(&[_]u8{
10361039 0, // null byte for the relative path name
10371040 0, // directory_index
......@@ -1195,7 +1198,7 @@ pub fn flush(self: *Elf, module: *Module) !void {
11951198 }
11961199 self.shdr_table_dirty = false;
11971200 }
1198 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
1201 if (self.entry_addr == null and self.base.options.effectiveOutputMode() == .Exe) {
11991202 log.debug("flushing. no_entry_point_found = true\n", .{});
12001203 self.error_flags.no_entry_point_found = true;
12011204 } else {
......@@ -1255,7 +1258,7 @@ fn writeElfHeader(self: *Elf) !void {
12551258
12561259 assert(index == 16);
12571260
1258 const elf_type = switch (self.base.options.output_mode) {
1261 const elf_type = switch (self.base.options.effectiveOutputMode()) {
12591262 .Exe => elf.ET.EXEC,
12601263 .Obj => elf.ET.REL,
12611264 .Lib => switch (self.base.options.link_mode) {
......@@ -2430,8 +2433,8 @@ fn dbgLineNeededHeaderBytes(self: Elf) u32 {
24302433 directory_count * 8 + file_name_count * 8 +
24312434 // These are encoded as DW.FORM_string rather than DW.FORM_strp as we would like
24322435 // because of a workaround for readelf and gdb failing to understand DWARFv5 correctly.
2433 self.base.options.root_pkg.root_src_dir_path.len +
2434 self.base.options.root_pkg.root_src_path.len);
2436 self.base.options.root_pkg.?.root_src_dir_path.len +
2437 self.base.options.root_pkg.?.root_src_path.len);
24352438}
24362439
24372440fn dbgInfoNeededHeaderBytes(self: Elf) u32 {
src-self-hosted/link/MachO.zig+3
......@@ -137,6 +137,9 @@ pub const SrcFn = struct {
137137pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*File {
138138 assert(options.object_format == .macho);
139139
140 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForMachO; // TODO
141 if (options.use_lld) return error.LLD_LinkingIsTODO_ForMachO; // TODO
142
140143 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = link.determineMode(options) });
141144 errdefer file.close();
142145
src-self-hosted/link/Wasm.zig+3
......@@ -52,6 +52,9 @@ funcs: std.ArrayListUnmanaged(*Module.Decl) = .{},
5252pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {
5353 assert(options.object_format == .wasm);
5454
55 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForWasm; // TODO
56 if (options.use_lld) return error.LLD_LinkingIsTODO_ForWasm; // TODO
57
5558 // TODO: read the file and keep vaild parts instead of truncating
5659 const file = try dir.createFile(sub_path, .{ .truncate = true, .read = true });
5760 errdefer file.close();
src-self-hosted/main.zig+87-19
......@@ -13,6 +13,7 @@ const Package = @import("Package.zig");
1313const zir = @import("zir.zig");
1414const build_options = @import("build_options");
1515const warn = std.log.warn;
16const introspect = @import("introspect.zig");
1617
1718fn fatal(comptime format: []const u8, args: anytype) noreturn {
1819 std.log.emerg(format, args);
......@@ -231,7 +232,6 @@ pub fn buildOutputType(
231232 var root_src_file: ?[]const u8 = null;
232233 var version: std.builtin.Version = .{ .major = 0, .minor = 0, .patch = 0 };
233234 var strip = false;
234 var emit_h = true;
235235 var watch = false;
236236 var debug_tokenize = false;
237237 var debug_ast_tree = false;
......@@ -248,6 +248,7 @@ pub fn buildOutputType(
248248 var target_dynamic_linker: ?[]const u8 = null;
249249 var target_ofmt: ?[]const u8 = null;
250250 var output_mode: std.builtin.OutputMode = undefined;
251 var emit_h: Emit = undefined;
251252 var ensure_libc_on_non_freestanding = false;
252253 var ensure_libcpp_on_non_freestanding = false;
253254 var have_libc = false;
......@@ -269,6 +270,9 @@ pub fn buildOutputType(
269270 var linker_z_nodelete = false;
270271 var linker_z_defs = false;
271272 var stack_size_override: u64 = 0;
273 var use_llvm: ?bool = null;
274 var use_lld: ?bool = null;
275 var use_clang: ?bool = null;
272276
273277 var system_libs = std.ArrayList([]const u8).init(gpa);
274278 defer system_libs.deinit();
......@@ -296,6 +300,10 @@ pub fn buildOutputType(
296300
297301 if (arg_mode == .build) {
298302 output_mode = arg_mode.build;
303 emit_h = switch (output_mode) {
304 .Exe => .no,
305 .Obj, .Lib => .yes_default_path,
306 };
299307
300308 const args = all_args[2..];
301309 var i: usize = 0;
......@@ -416,6 +424,18 @@ pub fn buildOutputType(
416424 want_pic = true;
417425 } else if (mem.eql(u8, arg, "-fno-PIC")) {
418426 want_pic = false;
427 } else if (mem.eql(u8, arg, "-fLLVM")) {
428 use_llvm = true;
429 } else if (mem.eql(u8, arg, "-fno-LLVM")) {
430 use_llvm = false;
431 } else if (mem.eql(u8, arg, "-fLLD")) {
432 use_lld = true;
433 } else if (mem.eql(u8, arg, "-fno-LLD")) {
434 use_lld = false;
435 } else if (mem.eql(u8, arg, "-fClang")) {
436 use_clang = true;
437 } else if (mem.eql(u8, arg, "-fno-Clang")) {
438 use_clang = false;
419439 } else if (mem.eql(u8, arg, "-rdynamic")) {
420440 rdynamic = true;
421441 } else if (mem.eql(u8, arg, "-femit-bin")) {
......@@ -430,6 +450,12 @@ pub fn buildOutputType(
430450 emit_zir = .{ .yes = arg["-femit-zir=".len..] };
431451 } else if (mem.eql(u8, arg, "-fno-emit-zir")) {
432452 emit_zir = .no;
453 } else if (mem.eql(u8, arg, "-femit-h")) {
454 emit_h = .yes_default_path;
455 } else if (mem.startsWith(u8, arg, "-femit-h=")) {
456 emit_h = .{ .yes = arg["-femit-h=".len..] };
457 } else if (mem.eql(u8, arg, "-fno-emit-h")) {
458 emit_h = .no;
433459 } else if (mem.eql(u8, arg, "-dynamic")) {
434460 link_mode = .Dynamic;
435461 } else if (mem.eql(u8, arg, "-static")) {
......@@ -491,7 +517,7 @@ pub fn buildOutputType(
491517 }
492518 }
493519 } else {
494 emit_h = false;
520 emit_h = .no;
495521 strip = true;
496522 ensure_libc_on_non_freestanding = true;
497523 ensure_libcpp_on_non_freestanding = arg_mode == .cpp;
......@@ -874,14 +900,6 @@ pub fn buildOutputType(
874900 }
875901 }
876902
877 if (system_libs.items.len != 0) {
878 fatal("linking against system libraries not yet supported", .{});
879 }
880
881 const src_path = root_src_file orelse {
882 fatal("expected at least one file argument", .{});
883 };
884
885903 const object_format: ?std.Target.ObjectFormat = blk: {
886904 const ofmt = target_ofmt orelse break :blk null;
887905 if (mem.eql(u8, ofmt, "elf")) {
......@@ -909,11 +927,14 @@ pub fn buildOutputType(
909927 .no => {
910928 fatal("-fno-emit-bin not supported yet", .{});
911929 },
912 .yes_default_path => if (object_format != null and object_format.? == .c)
913 try std.fmt.allocPrint(arena, "{}.c", .{root_name})
914 else
915 try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),
916
930 .yes_default_path => try std.zig.binNameAlloc(
931 arena,
932 root_name,
933 target_info.target,
934 output_mode,
935 link_mode,
936 object_format,
937 ),
917938 .yes => |p| p,
918939 };
919940
......@@ -930,10 +951,25 @@ pub fn buildOutputType(
930951 .yes => |p| p,
931952 };
932953
933 const root_pkg = try Package.create(gpa, fs.cwd(), ".", src_path);
934 defer root_pkg.destroy();
954 const root_pkg = if (root_src_file) |src_path| try Package.create(gpa, fs.cwd(), ".", src_path) else null;
955 defer if (root_pkg) |pkg| pkg.destroy();
935956
936 var module = try Module.init(gpa, .{
957 const emit_h_path: ?[]const u8 = switch (emit_h) {
958 .yes => |p| p,
959 .no => null,
960 .yes_default_path => try std.fmt.allocPrint(arena, "{}.h", .{root_name}),
961 };
962
963 // TODO Remove this, we'll have this error emitted lazily only if the features would end
964 // up actually getting used.
965 //if (!build_options.have_llvm) {
966 // if ((use_llvm orelse false) or (use_lld orelse false) or (use_clang orelse false))
967 // fatal("-fLLVM, -fLLD, and -fClang unavailable: compiler not built with LLVM extensions enabled", .{});
968 //}
969
970 const compiler_id = try introspect.resolveCompilerId(gpa);
971
972 var module = Module.init(gpa, .{
937973 .root_name = root_name,
938974 .target = target_info.target,
939975 .output_mode = output_mode,
......@@ -944,7 +980,39 @@ pub fn buildOutputType(
944980 .object_format = object_format,
945981 .optimize_mode = build_mode,
946982 .keep_source_files_loaded = zir_out_path != null,
947 });
983 .clang_argv = clang_argv.items,
984 .lib_dirs = lib_dirs.items,
985 .rpath_list = rpath_list.items,
986 .c_source_files = c_source_files.items,
987 .link_objects = link_objects.items,
988 .framework_dirs = framework_dirs.items,
989 .frameworks = frameworks.items,
990 .system_libs = system_libs.items,
991 .emit_h = emit_h_path,
992 .have_libc = have_libc,
993 .have_libcpp = have_libcpp,
994 .want_pic = want_pic,
995 .want_sanitize_c = want_sanitize_c,
996 .use_llvm = use_llvm,
997 .use_lld = use_lld,
998 .use_clang = use_clang,
999 .rdynamic = rdynamic,
1000 .linker_script = linker_script,
1001 .version_script = version_script,
1002 .disable_c_depfile = disable_c_depfile,
1003 .override_soname = override_soname,
1004 .linker_optimization = linker_optimization,
1005 .linker_gc_sections = linker_gc_sections,
1006 .linker_allow_shlib_undefined = linker_allow_shlib_undefined,
1007 .linker_bind_global_refs_locally = linker_bind_global_refs_locally,
1008 .linker_z_nodelete = linker_z_nodelete,
1009 .linker_z_defs = linker_z_defs,
1010 .stack_size_override = stack_size_override,
1011 .compiler_id = compiler_id,
1012 .strip = strip,
1013 }) catch |err| {
1014 fatal("unable to initialize module: {}", .{@errorName(err)});
1015 };
9481016 defer module.deinit();
9491017
9501018 const stdin = std.io.getStdIn().inStream();
src-self-hosted/test.zig+21-16
......@@ -9,6 +9,7 @@ const enable_qemu: bool = build_options.enable_qemu;
99const enable_wine: bool = build_options.enable_wine;
1010const enable_wasmtime: bool = build_options.enable_wasmtime;
1111const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
12const introspect = @import("introspect.zig");
1213
1314const cheader = @embedFile("link/cbe.h");
1415
......@@ -435,7 +436,10 @@ pub const TestContext = struct {
435436 const root_pkg = try Package.create(allocator, tmp.dir, ".", tmp_src_path);
436437 defer root_pkg.destroy();
437438
438 const bin_name = try std.zig.binNameAlloc(arena, "test_case", target, case.output_mode, null);
439 const ofmt: ?std.builtin.ObjectFormat = if (case.cbe) .c else null;
440 const bin_name = try std.zig.binNameAlloc(arena, "test_case", target, case.output_mode, null, ofmt);
441
442 const compiler_id = try introspect.resolveCompilerId(arena);
439443
440444 var module = try Module.init(allocator, .{
441445 .root_name = "test_case",
......@@ -450,7 +454,8 @@ pub const TestContext = struct {
450454 .bin_file_path = bin_name,
451455 .root_pkg = root_pkg,
452456 .keep_source_files_loaded = true,
453 .object_format = if (case.cbe) .c else null,
457 .object_format = ofmt,
458 .compiler_id = compiler_id,
454459 });
455460 defer module.deinit();
456461
......@@ -693,23 +698,23 @@ pub const TestContext = struct {
693698 }
694699
695700 var interpreter = spu.Interpreter(struct {
696 RAM: [0x10000]u8 = undefined,
701 RAM: [0x10000]u8 = undefined,
697702
698 pub fn read8(bus: @This(), addr: u16) u8 {
699 return bus.RAM[addr];
700 }
701 pub fn read16(bus: @This(), addr: u16) u16 {
702 return std.mem.readIntLittle(u16, bus.RAM[addr..][0..2]);
703 }
703 pub fn read8(bus: @This(), addr: u16) u8 {
704 return bus.RAM[addr];
705 }
706 pub fn read16(bus: @This(), addr: u16) u16 {
707 return std.mem.readIntLittle(u16, bus.RAM[addr..][0..2]);
708 }
704709
705 pub fn write8(bus: *@This(), addr: u16, val: u8) void {
706 bus.RAM[addr] = val;
707 }
710 pub fn write8(bus: *@This(), addr: u16, val: u8) void {
711 bus.RAM[addr] = val;
712 }
708713
709 pub fn write16(bus: *@This(), addr: u16, val: u16) void {
710 std.mem.writeIntLittle(u16, bus.RAM[addr..][0..2], val);
711 }
712 }){
714 pub fn write16(bus: *@This(), addr: u16, val: u16) void {
715 std.mem.writeIntLittle(u16, bus.RAM[addr..][0..2], val);
716 }
717 }){
713718 .bus = .{},
714719 };
715720