commit 13785b79e76b06eecefed529804859c26f927c39
parent 44d688f073c97dcfca80411c8d4b3416d6c0edd4
Author: finwo <finwo@pm.me>
Date: Sun, 15 Mar 2026 21:16:45 +0100
Compile on macos
Diffstat:
3 files changed, 68 insertions(+), 46 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
/lib/
*.o
/dep
+/license.c
diff --git a/Makefile b/Makefile
@@ -1,11 +1,9 @@
CC?=clang
FIND=$(shell which gfind find | head -1)
-OBJCOPY?=objcopy
+UNAME_S:=$(shell uname -s)
UNAME_M:=$(shell uname -m)
-ARCH_TARGET?=$(shell echo $(UNAME_M) | sed -e 's/x86_64/elf64-x86-64/' -e 's/arm64/elf64-littleaarch64/' -e 's/aarch64/elf64-littleaarch64/')
-ARCH_BIN?=$(shell echo $(UNAME_M) | sed -e 's/x86_64/i386/' -e 's/arm64/aarch64/' -e 's/aarch64/aarch64/')
SRC:=
INCLUDES:=
@@ -13,6 +11,20 @@ CFLAGS:=
LDFLAGS:=
DESTDIR?=/usr/local
+ifeq ($(UNAME_S),Darwin)
+ OBJCOPY:=$(shell echo "echo 'Not using objcopy'")
+ LD:=$(shell which ld)
+ LICENSE_OBJ:=license.c
+ LDFLAGS+=-lobjc
+else
+ OBJCOPY?=objcopy
+ OBJCOPY:=$(shell which objcopy)
+ LICENSE_OBJ:=license.o
+endif
+
+ARCH_TARGET?=$(shell echo $(UNAME_M) | sed -e 's/x86_64/elf64-x86-64/' -e 's/arm64/elf64-littleaarch64/' -e 's/aarch64/elf64-littleaarch64/')
+ARCH_BIN?=$(shell echo $(UNAME_M) | sed -e 's/x86_64/i386/' -e 's/arm64/aarch64/' -e 's/aarch64/aarch64/')
+
SRC+=$(shell $(FIND) src/ -type f -name '*.c')
INCLUDES+=-Isrc
INCLUDES+=-Ilib/.dep/include
@@ -40,7 +52,7 @@ SRC+=lib/tidwall/json.c/json.c
OBJ:=$(SRC:.c=.o)
OBJ:=$(OBJ:.cc=.o)
-OBJ+=license.o
+OBJ+=$(LICENSE_OBJ)
CFLAGS+=${INCLUDES}
@@ -50,6 +62,12 @@ default: dep
license.o: LICENSE.md
$(OBJCOPY) --input binary --output $(ARCH_TARGET) --binary-architecture $(ARCH_BIN) $< $@
+license.c: LICENSE.md
+ (echo "static const unsigned char _license_md_data[] = {"; xxd -i < LICENSE.md; echo "};") > license.c
+ (echo "static const unsigned char * const _license_md_end = _license_md_data + sizeof(_license_md_data);" >> license.c)
+ (echo "const unsigned char * const _binary_LICENSE_md_start = _license_md_data;" >> license.c)
+ (echo "const unsigned char * const _binary_LICENSE_md_end = _license_md_end;" >> license.c)
+
lib/cofyc/argparse:
mkdir -p lib/cofyc/argparse
curl -sL https://github.com/cofyc/argparse/archive/refs/heads/master.tar.gz | tar xzv --strip-components=1 -C lib/cofyc/argparse
@@ -91,7 +109,7 @@ lib/tidwall/json.c:
dep: $(LIBS) $(OBJ)
${CC} ${OBJ} ${CFLAGS} ${LDFLAGS} -o dep
- strip --strip-all dep
+ strip -u -r dep 2>/dev/null || true
.PHONY: install
install: dep
diff --git a/src/command/install/main.c b/src/command/install/main.c
@@ -12,6 +12,11 @@
#include <sys/wait.h>
#include <unistd.h>
+#ifdef __APPLE__
+#include <crt_externs.h>
+#define environ (*_NSGetEnviron())
+#endif
+
#include "command/command.h"
#include "common/github-utils.h"
#include "common/net-utils.h"
@@ -294,6 +299,44 @@ static int execute_postinstall_hook(const char *dep_dir) {
}
}
+static int install_dependency(const char *name, const char *spec);
+
+struct template_ctx {
+ const char *lib_path;
+ FILE *fp;
+};
+
+static bool module_dict_getter(void *data, const char *name, size_t len, tinytemplate_value_t *value) {
+ const char *lib_path = data;
+ if (len == 7 && strncmp(name, "dirname", 7) == 0) {
+ tinytemplate_set_string(value, lib_path, strlen(lib_path));
+ return true;
+ }
+ char orig[256];
+ int n = snprintf(orig, sizeof(orig), "{%.*s}", (int)len, name);
+ tinytemplate_set_string(value, orig, n);
+ return true;
+}
+
+static bool template_getter(void *data, const char *name, size_t len, tinytemplate_value_t *value) {
+ struct template_ctx *ctx = data;
+
+ if (len == 6 && strncmp(name, "module", 6) == 0) {
+ tinytemplate_set_dict(value, (void *)ctx->lib_path, module_dict_getter);
+ return true;
+ }
+
+ char orig[256];
+ int n = snprintf(orig, sizeof(orig), "{{%.*s}}", (int)len, name);
+ tinytemplate_set_string(value, orig, n);
+ return true;
+}
+
+static void template_callback(void *data, const char *str, size_t len) {
+ struct template_ctx *ctx = data;
+ fwrite(str, 1, len, ctx->fp);
+}
+
static int install_dependency(const char *name, const char *spec) {
char lib_path[PATH_MAX];
snprintf(lib_path, sizeof(lib_path), "lib/%s", name);
@@ -428,47 +471,7 @@ static int install_dependency(const char *name, const char *spec) {
continue;
}
- struct {
- const char *lib_path;
- FILE *fp;
- } ctx = {lib_path, dst_config};
-
- bool module_dict_getter(void *data, const char *name, size_t len, tinytemplate_value_t *value) {
- if (len == 7 && strncmp(name, "dirname", 7) == 0) {
- const char *lib_path = data;
- tinytemplate_set_string(value, lib_path, strlen(lib_path));
- return true;
- }
- char orig[256];
- int n = snprintf(orig, sizeof(orig), "{%.*s}", (int)len, name);
- tinytemplate_set_string(value, orig, n);
- return true;
- }
-
- bool template_getter(void *data, const char *name, size_t len, tinytemplate_value_t *value) {
- struct {
- const char *lib_path;
- FILE *fp;
- } *ctx = data;
-
- if (len == 6 && strncmp(name, "module", 6) == 0) {
- tinytemplate_set_dict(value, (void *)ctx->lib_path, module_dict_getter);
- return true;
- }
-
- char orig[256];
- int n = snprintf(orig, sizeof(orig), "{{%.*s}}", (int)len, name);
- tinytemplate_set_string(value, orig, n);
- return true;
- }
-
- void template_callback(void *data, const char *str, size_t len) {
- struct {
- const char *lib_path;
- FILE *fp;
- } *ctx = data;
- fwrite(str, 1, len, ctx->fp);
- }
+ struct template_ctx ctx = {lib_path, dst_config};
if (tinytemplate_eval(line, program, &ctx, template_getter, template_callback, errmsg, sizeof(errmsg)) !=
TINYTEMPLATE_STATUS_DONE) {