| ... | @@ -3,8 +3,7 @@ | ... | @@ -3,8 +3,7 @@ |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| 4 | const mem = std.mem; | 4 | const mem = std.mem; |
| 5 | const fs = std.fs; | 5 | const fs = std.fs; |
| 6 | | 6 | const CacheHash = std.cache_hash.CacheHash; |
| 7 | const warn = std.debug.warn; | | |
| 8 | | 7 | |
| 9 | /// Caller must free result | 8 | /// Caller must free result |
| 10 | pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 { | 9 | pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 { |
| ... | @@ -63,7 +62,7 @@ pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { | ... | @@ -63,7 +62,7 @@ pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { |
| 63 | | 62 | |
| 64 | pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 { | 63 | pub fn resolveZigLibDir(allocator: *mem.Allocator) ![]u8 { |
| 65 | return findZigLibDir(allocator) catch |err| { | 64 | return findZigLibDir(allocator) catch |err| { |
| 66 | warn( | 65 | std.debug.print( |
| 67 | \\Unable to find zig lib directory: {}. | 66 | \\Unable to find zig lib directory: {}. |
| 68 | \\Reinstall Zig or use --zig-install-prefix. | 67 | \\Reinstall Zig or use --zig-install-prefix. |
| 69 | \\ | 68 | \\ |
| ... | @@ -87,3 +86,50 @@ pub fn resolveGlobalCacheDir(allocator: *mem.Allocator) ![]u8 { | ... | @@ -87,3 +86,50 @@ pub fn resolveGlobalCacheDir(allocator: *mem.Allocator) ![]u8 { |
| 87 | | 86 | |
| 88 | return fs.getAppDataDir(allocator, appname); | 87 | return fs.getAppDataDir(allocator, appname); |
| 89 | } | 88 | } |
| | 89 | |
| | 90 | var compiler_id_mutex = std.Mutex{}; |
| | 91 | var compiler_id: [16]u8 = undefined; |
| | 92 | var compiler_id_computed = false; |
| | 93 | |
| | 94 | pub 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; |
| | 135 | } |