authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-17 18:57:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-17 18:57:34-07:00
loge26dda5308d5c853d195b65558454db4058ff218
treeb3af84ac22492e4b426068e3252a58f160fd93c4
parent044e3ca59222f26ae0a63be24510007c3e2cda82
parent4462c60639807502eca8a2db2bcf6adafea496ef

Merge branch 'Sergeeeek-master'

closes #5394 closes #4427

11 files changed, 165 insertions(+), 13 deletions(-)

CMakeLists.txt+7
......@@ -326,10 +326,15 @@ set(LIBUNWIND_FILES_DEST "${ZIG_LIB_DIR}/libunwind")
326326set(LIBCXX_FILES_DEST "${ZIG_LIB_DIR}/libcxx")
327327set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
328328set(ZIG_CONFIG_H_OUT "${CMAKE_BINARY_DIR}/config.h")
329set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")
329330configure_file (
330331 "${CMAKE_SOURCE_DIR}/src/config.h.in"
331332 "${ZIG_CONFIG_H_OUT}"
332333)
334configure_file (
335 "${CMAKE_SOURCE_DIR}/src/config.zig.in"
336 "${ZIG_CONFIG_ZIG_OUT}"
337)
333338
334339include_directories(
335340 ${CMAKE_SOURCE_DIR}
......@@ -472,6 +477,8 @@ set(BUILD_LIBSTAGE2_ARGS "build-lib"
472477 --bundle-compiler-rt
473478 -fPIC
474479 -lc
480 --pkg-begin build_options "${ZIG_CONFIG_ZIG_OUT}"
481 --pkg-end
475482)
476483
477484if("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
lib/std/cache_hash.zig+1-1
......@@ -193,7 +193,7 @@ pub const CacheHash = struct {
193193 const digest_str = iter.next() orelse return error.InvalidFormat;
194194 const file_path = iter.rest();
195195
196 cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, mtime_nsec_str, 10) catch return error.InvalidFormat;
196 cache_hash_file.stat.inode = fmt.parseInt(fs.File.INode, inode, 10) catch return error.InvalidFormat;
197197 cache_hash_file.stat.mtime = fmt.parseInt(i64, mtime_nsec_str, 10) catch return error.InvalidFormat;
198198 base64_decoder.decode(&cache_hash_file.bin_digest, digest_str) catch return error.InvalidFormat;
199199
src-self-hosted/introspect.zig+62-6
......@@ -3,8 +3,7 @@
33const std = @import("std");
44const mem = std.mem;
55const fs = std.fs;
6
7const warn = std.debug.warn;
6const CacheHash = std.cache_hash.CacheHash;
87
98/// Caller must free result
109pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 {
......@@ -63,7 +62,7 @@ pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 {
6362
6463pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 {
6564 return findZigLibDir(allocator) catch |err| {
66 warn(
65 std.debug.print(
6766 \\Unable to find zig lib directory: {}.
6867 \\Reinstall Zig or use --zig-install-prefix.
6968 \\
......@@ -73,7 +72,64 @@ pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 {
7372 };
7473}
7574
76/// Caller must free result
77pub fn resolveZigCacheDir(allocator: *mem.Allocator) ![]u8 {
78 return std.mem.dupe(allocator, u8, "zig-cache");
75/// Caller owns returned memory.
76pub fn resolveGlobalCacheDir(allocator: *mem.Allocator) ![]u8 {
77 const appname = "zig";
78
79 if (std.Target.current.os.tag != .windows) {
80 if (std.os.getenv("XDG_CACHE_HOME")) |cache_root| {
81 return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
82 } else if (std.os.getenv("HOME")) |home| {
83 return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });
84 }
85 }
86
87 return fs.getAppDataDir(allocator, appname);
88}
89
90var compiler_id_mutex = std.Mutex{};
91var compiler_id: [16]u8 = undefined;
92var compiler_id_computed = false;
93
94pub fn resolveCompilerId(gpa: *mem.Allocator) ![16]u8 {
95 const held = compiler_id_mutex.acquire();
96 defer held.release();
97
98 if (compiler_id_computed)
99 return compiler_id;
100 compiler_id_computed = true;
101
102 const global_cache_dir = try resolveGlobalCacheDir(gpa);
103 defer gpa.free(global_cache_dir);
104
105 // TODO Introduce openGlobalCacheDir which returns a dir handle rather than a string.
106 var cache_dir = try fs.cwd().openDir(global_cache_dir, .{});
107 defer cache_dir.close();
108
109 var ch = try CacheHash.init(gpa, cache_dir, "exe");
110 defer ch.release();
111
112 const self_exe_path = try fs.selfExePathAlloc(gpa);
113 defer gpa.free(self_exe_path);
114
115 _ = try ch.addFile(self_exe_path, null);
116
117 if (try ch.hit()) |digest| {
118 compiler_id = digest[0..16].*;
119 return compiler_id;
120 }
121
122 const libs = try std.process.getSelfExeSharedLibPaths(gpa);
123 defer {
124 for (libs) |lib| gpa.free(lib);
125 gpa.free(libs);
126 }
127
128 for (libs) |lib| {
129 try ch.addFilePost(lib);
130 }
131
132 const digest = ch.final();
133 compiler_id = digest[0..16].*;
134 return compiler_id;
79135}
src-self-hosted/main.zig+4-2
......@@ -30,6 +30,7 @@ const usage =
3030 \\ build-obj [source] Create object from source or assembly
3131 \\ fmt [source] Parse file and render in canonical zig format
3232 \\ targets List available compilation targets
33 \\ env Print lib path, std path, compiler id and version
3334 \\ version Print version number and exit
3435 \\ zen Print zen of zig and exit
3536 \\
......@@ -95,8 +96,9 @@ pub fn main() !void {
9596 const stdout = io.getStdOut().outStream();
9697 return @import("print_targets.zig").cmdTargets(arena, cmd_args, stdout, info.target);
9798 } else if (mem.eql(u8, cmd, "version")) {
98 std.io.getStdOut().writeAll(build_options.version ++ "\n") catch process.exit(1);
99 return;
99 try std.io.getStdOut().writeAll(build_options.version ++ "\n");
100 } else if (mem.eql(u8, cmd, "env")) {
101 try @import("print_env.zig").cmdEnv(arena, cmd_args, io.getStdOut().outStream());
100102 } else if (mem.eql(u8, cmd, "zen")) {
101103 try io.getStdOut().writeAll(info_zen);
102104 } else if (mem.eql(u8, cmd, "help")) {
src-self-hosted/print_env.zig created+47
......@@ -0,0 +1,47 @@
1const std = @import("std");
2const build_options = @import("build_options");
3const introspect = @import("introspect.zig");
4const Allocator = std.mem.Allocator;
5
6pub fn cmdEnv(gpa: *Allocator, args: []const []const u8, stdout: anytype) !void {
7 const zig_lib_dir = introspect.resolveZigLibDir(gpa) catch |err| {
8 std.debug.print("unable to find zig installation directory: {}\n", .{@errorName(err)});
9 std.process.exit(1);
10 };
11 defer gpa.free(zig_lib_dir);
12
13 const zig_std_dir = try std.fs.path.join(gpa, &[_][]const u8{ zig_lib_dir, "std" });
14 defer gpa.free(zig_std_dir);
15
16 const global_cache_dir = try introspect.resolveGlobalCacheDir(gpa);
17 defer gpa.free(global_cache_dir);
18
19 const compiler_id_digest = try introspect.resolveCompilerId(gpa);
20 var compiler_id_buf: [compiler_id_digest.len * 2]u8 = undefined;
21 const compiler_id = std.fmt.bufPrint(&compiler_id_buf, "{x}", .{compiler_id_digest}) catch unreachable;
22
23 var bos = std.io.bufferedOutStream(stdout);
24 const bos_stream = bos.outStream();
25
26 var jws = std.json.WriteStream(@TypeOf(bos_stream), 6).init(bos_stream);
27 try jws.beginObject();
28
29 try jws.objectField("lib_dir");
30 try jws.emitString(zig_lib_dir);
31
32 try jws.objectField("std_dir");
33 try jws.emitString(zig_std_dir);
34
35 try jws.objectField("id");
36 try jws.emitString(compiler_id);
37
38 try jws.objectField("global_cache_dir");
39 try jws.emitString(global_cache_dir);
40
41 try jws.objectField("version");
42 try jws.emitString(build_options.version);
43
44 try jws.endObject();
45 try bos_stream.writeByte('\n');
46 try bos.flush();
47}
src-self-hosted/print_targets.zig+1-1
......@@ -67,7 +67,7 @@ pub fn cmdTargets(
6767) !void {
6868 const available_glibcs = blk: {
6969 const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch |err| {
70 std.debug.warn("unable to find zig installation directory: {}\n", .{@errorName(err)});
70 std.debug.print("unable to find zig installation directory: {}\n", .{@errorName(err)});
7171 std.process.exit(1);
7272 };
7373 defer allocator.free(zig_lib_dir);
src-self-hosted/stage2.zig+29-3
......@@ -179,8 +179,7 @@ export fn stage2_fmt(argc: c_int, argv: [*]const [*:0]const u8) c_int {
179179 return 0;
180180}
181181
182fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void {
183 const allocator = std.heap.c_allocator;
182fn argvToArrayList(allocator: *Allocator, argc: c_int, argv: [*]const [*:0]const u8) !ArrayList([]const u8) {
184183 var args_list = std.ArrayList([]const u8).init(allocator);
185184 const argc_usize = @intCast(usize, argc);
186185 var arg_i: usize = 0;
......@@ -188,8 +187,16 @@ fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void {
188187 try args_list.append(mem.spanZ(argv[arg_i]));
189188 }
190189
191 const args = args_list.span()[2..];
190 return args_list;
191}
192192
193fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void {
194 const allocator = std.heap.c_allocator;
195
196 var args_list = try argvToArrayList(allocator, argc, argv);
197 defer args_list.deinit();
198
199 const args = args_list.span()[2..];
193200 return self_hosted_main.cmdFmt(allocator, args);
194201}
195202
......@@ -387,6 +394,25 @@ fn detectNativeCpuWithLLVM(
387394 return result;
388395}
389396
397export fn stage2_env(argc: c_int, argv: [*]const [*:0]const u8) c_int {
398 const allocator = std.heap.c_allocator;
399
400 var args_list = argvToArrayList(allocator, argc, argv) catch |err| {
401 std.debug.print("unable to parse arguments: {}\n", .{@errorName(err)});
402 return -1;
403 };
404 defer args_list.deinit();
405
406 const args = args_list.span()[2..];
407
408 @import("print_env.zig").cmdEnv(allocator, args, std.io.getStdOut().outStream()) catch |err| {
409 std.debug.print("unable to print info: {}\n", .{@errorName(err)});
410 return -1;
411 };
412
413 return 0;
414}
415
390416// ABI warning
391417export fn stage2_cmd_targets(
392418 zig_triple: ?[*:0]const u8,
src/config.zig.in created+3
......@@ -0,0 +1,3 @@
1pub const version: []const u8 = "@ZIG_VERSION@";
2pub const log_scopes: []const []const u8 = &[_][]const u8{};
3pub const enable_tracy = false;
src/main.cpp+3
......@@ -38,6 +38,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
3838 " builtin show the source code of @import(\"builtin\")\n"
3939 " cc use Zig as a drop-in C compiler\n"
4040 " c++ use Zig as a drop-in C++ compiler\n"
41 " env print lib path, std path, compiler id and version\n"
4142 " fmt parse files and render in canonical zig format\n"
4243 " id print the base64-encoded compiler id\n"
4344 " init-exe initialize a `zig build` application in the cwd\n"
......@@ -582,6 +583,8 @@ static int main0(int argc, char **argv) {
582583 return (term.how == TerminationIdClean) ? term.code : -1;
583584 } else if (argc >= 2 && strcmp(argv[1], "fmt") == 0) {
584585 return stage2_fmt(argc, argv);
586 } else if (argc >= 2 && strcmp(argv[1], "env") == 0) {
587 return stage2_env(argc, argv);
585588 } else if (argc >= 2 && (strcmp(argv[1], "cc") == 0 || strcmp(argv[1], "c++") == 0)) {
586589 emit_h = false;
587590 strip = true;
src/stage2.cpp+5
......@@ -27,6 +27,11 @@ void stage2_zen(const char **ptr, size_t *len) {
2727 stage2_panic(msg, strlen(msg));
2828}
2929
30int stage2_env(int argc, char** argv) {
31 const char *msg = "stage0 called stage2_env";
32 stage2_panic(msg, strlen(msg));
33}
34
3035void stage2_attach_segfault_handler(void) { }
3136
3237void stage2_panic(const char *ptr, size_t len) {
src/stage2.h+3
......@@ -141,6 +141,9 @@ ZIG_EXTERN_C void stage2_render_ast(struct Stage2Ast *ast, FILE *output_file);
141141// ABI warning
142142ZIG_EXTERN_C void stage2_zen(const char **ptr, size_t *len);
143143
144// ABI warning
145ZIG_EXTERN_C int stage2_env(int argc, char **argv);
146
144147// ABI warning
145148ZIG_EXTERN_C void stage2_attach_segfault_handler(void);
146149