| ... | @@ -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 | } |
| 259 | | 260 | |
| | 261 | static Buf saved_compiler_id = BUF_INIT; |
| | 262 | static 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 | |
| 260 | int main(int argc, char **argv) { | 310 | int 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 | } |
| 273 | | 323 | |
| 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 | } |
| 328 | | 331 | printf("%s\n", buf_ptr(compiler_id)); |
| 329 | | 332 | return EXIT_SUCCESS; |
| 330 | fprintf(stderr, "computed2: %s\n", buf_ptr(&exe_digest)); | | |
| 331 | return 0; | | |
| 332 | } | 333 | } |
| 333 | | 334 | |
| 334 | os_init(); | 335 | os_init(); |