summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-25 19:24:58 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-25 19:24:58 +0000
commit555141e35166c7f7d98c16f6f95fde8e57a651c2 (patch)
treee5cce20d7692b2ba99e68ed46ae545ea8923b94c
parentd5ad18ff3b97515c12a35f00d940e62bb34b7401 (diff)
Implement runtime OS detection and automatic versioningv0.1.2
-rw-r--r--.github/workflows/build-ape.yml2
-rw-r--r--Makefile6
-rw-r--r--src/main.c39
-rw-r--r--src/parser/parser_stmt.c26
-rw-r--r--src/repl/repl.c22
-rw-r--r--src/utils/utils.c30
-rw-r--r--src/zprep.h14
7 files changed, 91 insertions, 48 deletions
diff --git a/.github/workflows/build-ape.yml b/.github/workflows/build-ape.yml
index f5e9c85..ded68fe 100644
--- a/.github/workflows/build-ape.yml
+++ b/.github/workflows/build-ape.yml
@@ -30,7 +30,7 @@ jobs:
- name: Build APE
run: |
- # Ensure PATH is updated in this step
+ # Ensure PATH is updated and build with tag-aware versioning
export PATH="$HOME/cosmocc/bin:$PATH"
make clean
make ape
diff --git a/Makefile b/Makefile
index eaec55a..34b9a43 100644
--- a/Makefile
+++ b/Makefile
@@ -19,8 +19,9 @@ endif
# Default: gcc
# To build with clang: make CC=clang
# To build with zig: make CC="zig cc"
-CC = gcc
-CFLAGS = -Wall -Wextra -g -I./src -I./src/ast -I./src/parser -I./src/codegen -I./plugins -I./src/zen -I./src/utils -I./src/lexer -I./src/analysis -I./src/lsp
+# Version synchronization
+GIT_VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "0.1.0")
+CFLAGS = -Wall -Wextra -g -I./src -I./src/ast -I./src/parser -I./src/codegen -I./plugins -I./src/zen -I./src/utils -I./src/lexer -I./src/analysis -I./src/lsp -DZEN_VERSION=\"$(GIT_VERSION)\"
TARGET = zc$(EXE)
LIBS = -lm -lpthread -ldl
@@ -106,6 +107,7 @@ $(ZC_COM_BIN): $(ZC_ENTRY_O) $(SRCS)
PLUGINS= \
CC=$(COSMOCC) \
OBJ_DIR=obj-ape \
+ ZEN_VERSION="$(GIT_VERSION)" \
LIBS="$(abspath $(ZC_ENTRY_O)) -Wl,--wrap=main" \
TARGET="$(abspath $@)";
diff --git a/src/main.c b/src/main.c
index 3bf4832..e2b3cde 100644
--- a/src/main.c
+++ b/src/main.c
@@ -56,11 +56,14 @@ void print_usage()
int main(int argc, char **argv)
{
memset(&g_config, 0, sizeof(g_config));
-#ifdef _WIN32
- strcpy(g_config.cc, "gcc.exe");
-#else
- strcpy(g_config.cc, "gcc");
-#endif
+ if (z_is_windows())
+ {
+ strcpy(g_config.cc, "gcc.exe");
+ }
+ else
+ {
+ strcpy(g_config.cc, "gcc");
+ }
if (argc < 2)
{
@@ -346,14 +349,15 @@ int main(int argc, char **argv)
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)
+ if (z_is_windows())
{
- thread_flag = "";
+ // 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
@@ -386,11 +390,14 @@ 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
+ if (z_is_windows())
+ {
+ sprintf(run_cmd, "%s", outfile);
+ }
+ else
+ {
+ sprintf(run_cmd, "./%s", outfile);
+ }
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 aba6f5e..303f6ac 100644
--- a/src/parser/parser_stmt.c
+++ b/src/parser/parser_stmt.c
@@ -3179,11 +3179,14 @@ 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);
-#endif
+ if (z_is_windows())
+ {
+ sprintf(bin, "%s.exe", filename);
+ }
+ else
+ {
+ sprintf(bin, "%s.bin", filename);
+ }
sprintf(cmd, "%s %s -o %s", g_config.cc, filename, bin);
if (!g_config.verbose)
{
@@ -3199,11 +3202,14 @@ char *run_comptime_block(ParserContext *ctx, Lexer *l)
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 (z_is_windows())
+ {
+ sprintf(cmd, "%s > %s", bin, out_file);
+ }
+ else
+ {
+ sprintf(cmd, "./%s > %s", bin, out_file);
+ }
if (system(cmd) != 0)
{
diff --git a/src/repl/repl.c b/src/repl/repl.c
index 274c14c..cb63293 100644
--- a/src/repl/repl.c
+++ b/src/repl/repl.c
@@ -28,12 +28,10 @@ void run_repl(const char *self_path)
char history_path[512];
const char *home = getenv("HOME");
-#ifdef _WIN32
- if (!home)
+ if (z_is_windows() && !home)
{
home = getenv("USERPROFILE");
}
-#endif
if (home)
{
snprintf(history_path, sizeof(history_path), "%s/.zprep_history", home);
@@ -273,10 +271,14 @@ void run_repl(const char *self_path)
{
tmpdir = getenv("TMP");
}
- if (!tmpdir)
+ if (!tmpdir && !z_is_windows())
{
tmpdir = "/tmp";
}
+ if (!tmpdir)
+ {
+ tmpdir = ".";
+ }
snprintf(edit_path, sizeof(edit_path), "%s/zprep_edit_%d.zc", tmpdir, rand());
FILE *f = fopen(edit_path, "w");
if (f)
@@ -658,10 +660,14 @@ void run_repl(const char *self_path)
{
tmpdir = getenv("TMP");
}
- if (!tmpdir)
+ if (!tmpdir && !z_is_windows())
{
tmpdir = "/tmp";
}
+ if (!tmpdir)
+ {
+ tmpdir = ".";
+ }
snprintf(tmp_path, sizeof(tmp_path), "%s/zprep_repl_type_%d.zc", tmpdir,
rand());
FILE *f = fopen(tmp_path, "w");
@@ -752,10 +758,14 @@ void run_repl(const char *self_path)
{
tmpdir = getenv("TMP");
}
- if (!tmpdir)
+ if (!tmpdir && !z_is_windows())
{
tmpdir = "/tmp";
}
+ if (!tmpdir)
+ {
+ tmpdir = ".";
+ }
snprintf(tmp_path, sizeof(tmp_path), "%s/zprep_repl_time_%d.zc", tmpdir,
rand());
FILE *f = fopen(tmp_path, "w");
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 159326e..56a7690 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -671,14 +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 (z_is_windows())
+ {
+ // 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);
+ }
if (system(cmd) != 0)
{
zwarn("Failed to download %s", url);
@@ -693,11 +696,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
+ if (z_is_windows())
+ {
+ zwarn("pkg-config is usually not available on Windows. Build directive "
+ "'pkg-config:%s' might fail.",
+ libs);
+ }
char cmd[4096];
sprintf(cmd, "pkg-config --cflags %s", libs);
diff --git a/src/zprep.h b/src/zprep.h
index ea38927..e7004ee 100644
--- a/src/zprep.h
+++ b/src/zprep.h
@@ -7,9 +7,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
+
+#ifdef __COSMOPOLITAN__
+#include "libc/dce.h"
+#define z_is_windows() IsWindows()
+#else
+#ifdef _WIN32
+#define z_is_windows() 1
+#else
+#define z_is_windows() 0
+#endif
+#endif
// **ZEN VERSION**
+#ifndef ZEN_VERSION
#define ZEN_VERSION "0.1.0"
+#endif
// ** ANSI COLORS **
#define COLOR_RESET "\033[0m"