diff options
| author | iryuken <eshwarsajja20@gmail.com> | 2026-01-25 23:57:07 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-25 23:57:07 +0530 |
| commit | a2804ffe502ec31fc3bf561b7e59c25622b15e19 (patch) | |
| tree | 3fb732563d6a282e010bca2d3acb97eb351c925f /src | |
| parent | 123a961710aab45f30367cb8faa2c8ce00e49bdb (diff) | |
| parent | e3ab29bb4d7174cae65de2275f19105eb3d93d91 (diff) | |
Merge branch 'z-libs:main' into main
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 26 | ||||
| -rw-r--r-- | src/parser/parser_stmt.c | 17 | ||||
| -rw-r--r-- | src/repl/repl.c | 41 | ||||
| -rw-r--r-- | src/utils/utils.c | 59 |
4 files changed, 115 insertions, 28 deletions
@@ -56,7 +56,11 @@ void print_usage() int main(int argc, char **argv) { memset(&g_config, 0, sizeof(g_config)); +#ifdef __COSMOPOLITAN__ + strcpy(g_config.cc, "cosmocc"); +#else strcpy(g_config.cc, "gcc"); +#endif if (argc < 2) { @@ -339,9 +343,23 @@ int main(int argc, char **argv) char cmd[8192]; char *outfile = g_config.output_file ? g_config.output_file : "a.out"; - snprintf(cmd, sizeof(cmd), "%s %s %s %s %s -o %s %s -lm %s -I./src %s", g_config.cc, + const char *thread_flag = g_parser_ctx->has_async ? "-lpthread" : ""; + const char *math_flag = "-lm"; + +#ifdef _WIN32 + // Windows might use different flags or none for math/threads + math_flag = ""; + if (g_parser_ctx->has_async) + { + thread_flag = ""; + } +#endif + + // If using cosmocc, it handles these usually, but keeping them is okay for Linux targets + + snprintf(cmd, sizeof(cmd), "%s %s %s %s %s -o %s %s %s %s -I./src %s", g_config.cc, g_config.gcc_flags, g_cflags, g_config.is_freestanding ? "-ffreestanding" : "", "", - outfile, temp_source_file, g_parser_ctx->has_async ? "-lpthread" : "", g_link_flags); + outfile, temp_source_file, math_flag, thread_flag, g_link_flags); if (g_config.verbose) { @@ -368,7 +386,11 @@ int main(int argc, char **argv) if (g_config.mode_run) { char run_cmd[2048]; +#ifdef _WIN32 + sprintf(run_cmd, "%s", outfile); +#else sprintf(run_cmd, "./%s", outfile); +#endif ret = system(run_cmd); remove(outfile); zptr_plugin_mgr_cleanup(); diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c index 0c3885a..aba6f5e 100644 --- a/src/parser/parser_stmt.c +++ b/src/parser/parser_stmt.c @@ -3179,8 +3179,16 @@ char *run_comptime_block(ParserContext *ctx, Lexer *l) char cmd[4096]; char bin[1024]; +#ifdef _WIN32 + sprintf(bin, "%s.exe", filename); +#else sprintf(bin, "%s.bin", filename); - sprintf(cmd, "gcc %s -o %s > /dev/null 2>&1", filename, bin); +#endif + sprintf(cmd, "%s %s -o %s", g_config.cc, filename, bin); + if (!g_config.verbose) + { + strcat(cmd, " > /dev/null 2>&1"); + } int res = system(cmd); if (res != 0) { @@ -3189,7 +3197,14 @@ char *run_comptime_block(ParserContext *ctx, Lexer *l) char out_file[1024]; sprintf(out_file, "%s.out", filename); + + // Platform-neutral execution +#ifdef _WIN32 + sprintf(cmd, "%s > %s", bin, out_file); +#else sprintf(cmd, "./%s > %s", bin, out_file); +#endif + if (system(cmd) != 0) { zpanic_at(lexer_peek(l), "Comptime execution failed"); diff --git a/src/repl/repl.c b/src/repl/repl.c index 22fe95d..274c14c 100644 --- a/src/repl/repl.c +++ b/src/repl/repl.c @@ -28,6 +28,12 @@ void run_repl(const char *self_path) char history_path[512]; const char *home = getenv("HOME"); +#ifdef _WIN32 + if (!home) + { + home = getenv("USERPROFILE"); + } +#endif if (home) { snprintf(history_path, sizeof(history_path), "%s/.zprep_history", home); @@ -262,7 +268,16 @@ void run_repl(const char *self_path) } char edit_path[256]; - sprintf(edit_path, "/tmp/zprep_edit_%d.zc", rand()); + const char *tmpdir = getenv("TEMP"); + if (!tmpdir) + { + tmpdir = getenv("TMP"); + } + if (!tmpdir) + { + tmpdir = "/tmp"; + } + snprintf(edit_path, sizeof(edit_path), "%s/zprep_edit_%d.zc", tmpdir, rand()); FILE *f = fopen(edit_path, "w"); if (f) { @@ -638,7 +653,17 @@ void run_repl(const char *self_path) strcat(probe_code, "); }"); char tmp_path[256]; - sprintf(tmp_path, "/tmp/zprep_repl_type_%d.zc", rand()); + const char *tmpdir = getenv("TEMP"); + if (!tmpdir) + { + tmpdir = getenv("TMP"); + } + if (!tmpdir) + { + tmpdir = "/tmp"; + } + snprintf(tmp_path, sizeof(tmp_path), "%s/zprep_repl_type_%d.zc", tmpdir, + rand()); FILE *f = fopen(tmp_path, "w"); if (f) { @@ -722,7 +747,17 @@ void run_repl(const char *self_path) strcat(code, "}"); char tmp_path[256]; - sprintf(tmp_path, "/tmp/zprep_repl_time_%d.zc", rand()); + const char *tmpdir = getenv("TEMP"); + if (!tmpdir) + { + tmpdir = getenv("TMP"); + } + if (!tmpdir) + { + tmpdir = "/tmp"; + } + snprintf(tmp_path, sizeof(tmp_path), "%s/zprep_repl_time_%d.zc", tmpdir, + rand()); FILE *f = fopen(tmp_path, "w"); if (f) { diff --git a/src/utils/utils.c b/src/utils/utils.c index df7576f..159326e 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -640,7 +640,7 @@ void scan_build_directives(ParserContext *ctx, const char *src) int res = system(cmd); if (res != 0) { - zpanic("Shell directive failed: %s", cmd); + zwarn("Shell directive failed: %s", cmd); } } else if (strncmp(line, "get:", 4) == 0) @@ -671,12 +671,17 @@ void scan_build_directives(ParserContext *ctx, const char *src) { printf("[zprep] Downloading %s...\n", filename); char cmd[8192]; +#ifdef _WIN32 + // On Windows, try curl which is often built-in now + sprintf(cmd, "curl -s -L \"%s\" -o \"%s\"", url, filename); +#else // Try wget, then curl. sprintf(cmd, "wget -q \"%s\" -O \"%s\" || curl -s -L \"%s\" -o \"%s\"", url, filename, url, filename); +#endif if (system(cmd) != 0) { - zpanic("Failed to download %s", url); + zwarn("Failed to download %s", url); } } } @@ -688,6 +693,12 @@ void scan_build_directives(ParserContext *ctx, const char *src) libs++; } +#ifdef _WIN32 + zwarn("pkg-config is usually not available on Windows. Build directive " + "'pkg-config:%s' might fail.", + libs); +#endif + char cmd[4096]; sprintf(cmd, "pkg-config --cflags %s", libs); FILE *fp = popen(cmd, "r"); @@ -695,18 +706,20 @@ void scan_build_directives(ParserContext *ctx, const char *src) { char flags[4096]; flags[0] = 0; - fgets(flags, sizeof(flags), fp); - pclose(fp); - int len = strlen(flags); - if (len > 0 && flags[len - 1] == '\n') + if (fgets(flags, sizeof(flags), fp)) { - flags[len - 1] = 0; + int len = strlen(flags); + if (len > 0 && flags[len - 1] == '\n') + { + flags[len - 1] = 0; + } + if (strlen(g_cflags) > 0) + { + strcat(g_cflags, " "); + } + strcat(g_cflags, flags); } - if (strlen(g_cflags) > 0) - { - strcat(g_cflags, " "); - } - strcat(g_cflags, flags); + pclose(fp); } sprintf(cmd, "pkg-config --libs %s", libs); @@ -715,18 +728,20 @@ void scan_build_directives(ParserContext *ctx, const char *src) { char flags[4096]; flags[0] = 0; - fgets(flags, sizeof(flags), fp); - pclose(fp); - int len = strlen(flags); - if (len > 0 && flags[len - 1] == '\n') - { - flags[len - 1] = 0; - } - if (strlen(g_link_flags) > 0) + if (fgets(flags, sizeof(flags), fp)) { - strcat(g_link_flags, " "); + int len = strlen(flags); + if (len > 0 && flags[len - 1] == '\n') + { + flags[len - 1] = 0; + } + if (strlen(g_link_flags) > 0) + { + strcat(g_link_flags, " "); + } + strcat(g_link_flags, flags); } - strcat(g_link_flags, flags); + pclose(fp); } } |
