authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-09 23:58:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-09 23:58:52-04:00
logc0bdcc7417a23d338dc1df6bd74b30fe6e3d1a1a
tree31772e18366a9e8dbf3e30f96190f2250d065b79
parent173fc842c4eb1da6ca07a4ab6026c4c62bc8c09b
signature Commit is signed but in an unrecognized format.

`zig id` command


3 files changed, 60 insertions(+), 60 deletions(-)

src/cache_hash.cpp-1
...@@ -377,7 +377,6 @@ static Error write_manifest_file(CacheHash *ch) {...@@ -377,7 +377,6 @@ static Error write_manifest_file(CacheHash *ch) {
377 buf_appendf(&contents, "%" ZIG_PRI_u64 " %" ZIG_PRI_u64 " %s %s\n",377 buf_appendf(&contents, "%" ZIG_PRI_u64 " %" ZIG_PRI_u64 " %s %s\n",
378 chf->mtime.sec, chf->mtime.nsec, encoded_digest, buf_ptr(chf->path));378 chf->mtime.sec, chf->mtime.nsec, encoded_digest, buf_ptr(chf->path));
379 }379 }
380 fprintf(stderr, "overwrite with\n%s\n", buf_ptr(&contents));
381 if ((err = os_file_overwrite(ch->manifest_file, &contents)))380 if ((err = os_file_overwrite(ch->manifest_file, &contents)))
382 return err;381 return err;
383382
src/main.cpp+57-56
...@@ -25,6 +25,7 @@ static int usage(const char *arg0) {...@@ -25,6 +25,7 @@ static int usage(const char *arg0) {
25 " build-lib [source] create library from source or object files\n"25 " build-lib [source] create library from source or object files\n"
26 " build-obj [source] create object from source or assembly\n"26 " build-obj [source] create object from source or assembly\n"
27 " builtin show the source code of that @import(\"builtin\")\n"27 " builtin show the source code of that @import(\"builtin\")\n"
28 " id print the base64-encoded compiler id\n"
28 " run [source] create executable and run immediately\n"29 " run [source] create executable and run immediately\n"
29 " translate-c [source] convert c code to zig code\n"30 " translate-c [source] convert c code to zig code\n"
30 " targets list available compilation targets\n"31 " targets list available compilation targets\n"
...@@ -257,6 +258,55 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {...@@ -257,6 +258,55 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
257 }258 }
258}259}
259260
261static Buf saved_compiler_id = BUF_INIT;
262static Error get_compiler_id(Buf **result) {
263 if (saved_compiler_id.list.length != 0) {
264 *result = &saved_compiler_id;
265 return ErrorNone;
266 }
267
268 Error err;
269 Buf app_data_dir = BUF_INIT;
270 if ((err = os_get_app_data_dir(&app_data_dir, "zig")))
271 return err;
272 Buf *stage1_dir = buf_alloc();
273 os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
274 Buf *manifest_dir = buf_alloc();
275 os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
276
277 if ((err = os_make_path(manifest_dir)))
278 return err;
279 CacheHash cache_hash;
280 CacheHash *ch = &cache_hash;
281 cache_init(ch, manifest_dir);
282 Buf self_exe_path = BUF_INIT;
283 if ((err = os_self_exe_path(&self_exe_path)))
284 return err;
285
286 cache_file(ch, &self_exe_path);
287
288 buf_resize(&saved_compiler_id, 0);
289 if ((err = cache_hit(ch, &saved_compiler_id)))
290 return err;
291 if (buf_len(&saved_compiler_id) != 0) {
292 *result = &saved_compiler_id;
293 return ErrorNone;
294 }
295 ZigList<Buf *> lib_paths = {};
296 if ((err = os_self_exe_shared_libs(lib_paths)))
297 return err;
298 for (size_t i = 0; i < lib_paths.length; i += 1) {
299 Buf *lib_path = lib_paths.at(i);
300 if ((err = cache_add_file(ch, lib_path)))
301 return err;
302 }
303 if ((err = cache_final(ch, &saved_compiler_id)))
304 return err;
305
306 *result = &saved_compiler_id;
307 return ErrorNone;
308}
309
260int main(int argc, char **argv) {310int main(int argc, char **argv) {
261 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {311 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
262 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",312 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
...@@ -271,64 +321,15 @@ int main(int argc, char **argv) {...@@ -271,64 +321,15 @@ int main(int argc, char **argv) {
271 return 0;321 return 0;
272 }322 }
273323
274 if (argc == 2 && strcmp(argv[1], "TEST") == 0) {324 if (argc == 2 && strcmp(argv[1], "id") == 0) {
275 Error err;325 Error err;
276 Buf app_data_dir = BUF_INIT;326 Buf *compiler_id;
277 if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {327 if ((err = get_compiler_id(&compiler_id))) {
278 fprintf(stderr, "get app dir: %s\n", err_str(err));328 fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
279 return 1;329 return EXIT_FAILURE;
280 }
281 Buf *stage1_dir = buf_alloc();
282 os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
283 Buf *manifest_dir = buf_alloc();
284 os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
285
286 if ((err = os_make_path(manifest_dir))) {
287 fprintf(stderr, "make path: %s\n", err_str(err));
288 return 1;
289 }
290 CacheHash cache_hash;
291 CacheHash *ch = &cache_hash;
292 cache_init(ch, manifest_dir);
293 Buf self_exe_path = BUF_INIT;
294 if ((err = os_self_exe_path(&self_exe_path))) {
295 fprintf(stderr, "self exe path: %s\n", err_str(err));
296 return 1;
297 }
298
299 cache_file(ch, &self_exe_path);
300
301 Buf exe_digest = BUF_INIT;
302 buf_resize(&exe_digest, 0);
303 if ((err = cache_hit(ch, &exe_digest))) {
304 fprintf(stderr, "cache hit error: %s\n", err_str(err));
305 return 1;
306 }
307 if (buf_len(&exe_digest) != 0) {
308 fprintf(stderr, "cache hit: %s\n", buf_ptr(&exe_digest));
309 return 0;
310 }
311 fprintf(stderr, "cache miss\n");
312 ZigList<Buf *> lib_paths = {};
313 if ((err = os_self_exe_shared_libs(lib_paths))) {
314 fprintf(stderr, "finding out shared libs: %s\n", err_str(err));
315 return 1;
316 }
317 for (size_t i = 0; i < lib_paths.length; i += 1) {
318 Buf *lib_path = lib_paths.at(i);
319 if ((err = cache_add_file(ch, lib_path))) {
320 fprintf(stderr, "cache add file %s: %s", buf_ptr(lib_path), err_str(err));
321 return 1;
322 }
323 }
324 if ((err = cache_final(ch, &exe_digest))) {
325 fprintf(stderr, "final: %s\n", err_str(err));
326 return 1;
327 }330 }
328331 printf("%s\n", buf_ptr(compiler_id));
329332 return EXIT_SUCCESS;
330 fprintf(stderr, "computed2: %s\n", buf_ptr(&exe_digest));
331 return 0;
332 }333 }
333334
334 os_init();335 os_init();
src/os.cpp+3-3
...@@ -1813,9 +1813,9 @@ Error os_file_read_all(OsFile file, Buf *contents) {...@@ -1813,9 +1813,9 @@ Error os_file_read_all(OsFile file, Buf *contents) {
1813 for (;;) {1813 for (;;) {
1814 size_t amt = buf_len(contents) - index;1814 size_t amt = buf_len(contents) - index;
18151815
1816 if (amt < 512) {1816 if (amt < 4096) {
1817 buf_resize(contents, buf_len(contents) + 512);1817 buf_resize(contents, buf_len(contents) + (4096 - amt));
1818 amt += 512;1818 amt = buf_len(contents) - index;
1819 }1819 }
18201820
1821 if ((err = os_file_read(file, buf_ptr(contents) + index, &amt)))1821 if ((err = os_file_read(file, buf_ptr(contents) + index, &amt)))