Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build for Apple/OSX #39

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,27 @@ jobs:
# choco install --no-progress -r -y upx
- name: Build native windows exe for x86_64
run: |
cd .\lib\tinycc
bash configure --targetos=WIN32 --config-backtrace=no
make libtcc.a libtcc1.a
cd ..\..
make -f build/win-native.mk
make win-native
- name: Run tests
run: |
.\cjit.exe test\hello.c

osx-native-test:
name: 🍎 OSX native test
needs: [reuse, c-lint]
runs-on: "macos-latest"
steps:
- uses: actions/checkout@v4
# - name: install dependencies
# run: |
# choco install --no-progress -r -y upx
- name: Build native Apple/OSX command executable
run: |
make apple-osx
- name: Run tests
run: |
./cjit.command test/hello.c

semantic-release:
name: 🤖 Semantic release
needs: [musl-test]
Expand Down Expand Up @@ -131,10 +143,10 @@ jobs:
build/release-intro.md
- name: Install build deps
run: |
sudo apt install -qy make musl-tools musl-dev gcc-mingw-w64
sudo apt install -qy make musl-tools musl-dev
- name: Build x86_64 with musl-system
run: |
make musl-linux
make musl-linux RELEASE=1
- name: Upload artifact linux-amd64
uses: actions/upload-artifact@v4
with:
Expand All @@ -150,23 +162,32 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# - name: install dependencies
# run: |
# choco install --no-progress -r -y upx
- name: Build native windows exe for x86_64
run: |
cd .\lib\tinycc
bash configure --targetos=WIN32 --config-backtrace=no
make libtcc.a libtcc1.a
cd ..\..
make -f build/win-native.mk
make win-native RELEASE=1
- name: Upload artifact windows-amd64
uses: actions/upload-artifact@v4
with:
name: release-win-native-x86_64
path: |
cjit.exe

osx-native-release:
name: 🍎 OSX native binary release build
needs: [semantic-release]
runs-on: "macos-latest"
steps:
- uses: actions/checkout@v4
- name: Build native Apple/OSX command executable
run: |
make apple-osx RELEASE=1
- name: Upload artifact apple-osx
uses: actions/upload-artifact@v4
with:
name: release-osx-native
path: |
cjit.command

draft-binary-release:
name: 📦 Pack release
needs: [semantic-release, musl-release, win-native-release]
Expand Down
7 changes: 7 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ linux-x86: ## 🐧 Build a dynamically linked cjit using libs found on Linux x86
win-wsl: ## 🪟 Build cjit.exe for WIN64 on an Ubuntu WSL VM using gcc-mingw-w64
$(MAKE) -f build/win-wsl.mk

win-native: ## 🪟 Build cjit.exe for WIN64 on Windows Server
cd ./lib/tinycc; bash configure --targetos=WIN32 --config-backtrace=no; make libtcc.a libtcc1.a
$(MAKE) -f build/win-native.mk

apple-osx: ## 🍎 Build cjit.command for Apple/OSX using clang static
$(MAKE) -f build/osx.mk

_: ##
------: ## __ Debugging targets

Expand Down
10 changes: 8 additions & 2 deletions build/deps.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

src/embed-musl-libc.c:
bash build/embed-musl-libc.sh
sed -i 's/unsigned char _lib_x86_64_linux_musl_libc_so/const unsigned char musl_libc/' src/embed-musl-libc.c
sed -i 's/unsigned int _lib_x86_64_linux_musl_libc_so_len/const unsigned int musl_libc_len/' src/embed-musl-libc.c

src/embed-libtcc1.c:
$(info Embedding libtcc1: ${embed_libtcc1})
bash build/embed-libtcc1.sh ${embed_libtcc1}
$(info Embedding libtcc1: lib/tinycc/libtcc1.a)
bash build/embed-libtcc1.sh lib/tinycc/libtcc1.a
@sed -i 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
@sed -i 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c

src/embed-headers.c:
$(info Embedding tinycc headers)
bash build/embed-headers.sh
sed -i 's/unsigned char/const char/' src/embed-headers.c
sed -i 's/unsigned int/const unsigned int/' src/embed-headers.c

lib/tinycc/libtcc.a:
cd lib/tinycc && ./configure ${tinycc_config}
Expand Down
7 changes: 4 additions & 3 deletions build/embed-headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ if [ "$1" = "win" ]; then
}
fi

sed -i 's/unsigned char/const char/' $dst
sed -i 's/unsigned int/const unsigned int/' $dst
sed_inplace() { if [[ "$OSTYPE" == "darwin"* ]]; then sed -i'' "$*"; else sed -i "$*"; fi }
sed_inplace -e 's/unsigned char/const char/' $dst
sed_inplace -e 's/unsigned int/const unsigned int/' $dst

([ "$1" = "code" ] || [ "$2" = "code" ]) && {
>&2 echo "Externs to declare:"
Expand All @@ -71,4 +72,4 @@ sed -i 's/unsigned int/const unsigned int/' $dst
rm -f $externs $calls
}

>&2 echo "Done generating src/embed-headers.c"
>&2 echo "Done generating src/embed-headers.c"
5 changes: 3 additions & 2 deletions build/embed-libtcc1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ command -v xxd > /dev/null || {

echo "// $lib" > $dst
xxd -i $lib >> $dst
sed -i 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' $dst
sed -i 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' $dst
# sed_inplace() { if [[ "$OSTYPE" == "darwin"* ]]; then sed -i'' "$*"; else sed -i w$*; fi }
# sed_inplace -e 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' $dst
# sed_inplace -e 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' $dst
5 changes: 3 additions & 2 deletions build/embed-musl-libc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ command -v xxd > /dev/null || {

echo "// $lib" > $dst
xxd -i $lib >> $dst
sed -i 's/unsigned char _lib_x86_64_linux_musl_libc_so/const unsigned char musl_libc/' $dst
sed -i 's/unsigned int _lib_x86_64_linux_musl_libc_so_len/const unsigned int musl_libc_len/' $dst
# sed_inplace() { if [[ "$OSTYPE" == "darwin"* ]]; then sed -i'' "$*"; else sed -i $*; fi }
# sed_inplace -e 's/unsigned char _lib_x86_64_linux_musl_libc_so/const unsigned char musl_libc/' $dst
# sed_inplace -e 's/unsigned int _lib_x86_64_linux_musl_libc_so_len/const unsigned int musl_libc_len/' $dst
11 changes: 5 additions & 6 deletions build/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ endif

CFLAGS ?= -Og -ggdb -DDEBUG=1 -Wall -Wextra

cflags_stack_protect := -fstack-protector-all -D_FORTIFY_SOURCE=2 -fno-strict-overflow

ifdef RELEASE
CFLAGS := -O2 -fomit-frame-pointer
CFLAGS := -O2 -fomit-frame-pointer ${cflags_stack_protect}
endif

cflags := ${CFLAGS} -Isrc -Ilib/tinycc

cflags_stack_protect := -fstack-protector-all -D_FORTIFY_SOURCE=2 -fno-strict-overflow

SOURCES := src/io.o src/file.o src/cflag.o src/cjit.o \
src/embed-libtcc1.o src/embed-headers.o src/kilo.o
src/embed-libtcc1.o src/embed-headers.o

ldadd := lib/tinycc/libtcc.a

embed_libtcc1 := lib/tinycc/libtcc1.a

tinycc_config ?= --cc=${cc} --extra-cflags="${cflags}" --extra-ldflags="${ldflags}"

ifdef DEBUG
tinycc_config += --debug
endif
16 changes: 8 additions & 8 deletions build/linux.mk
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
include build/init.mk

cc := gcc

cflags += -DLIBC_GNU -D_GNU_SOURCE -DREPL_SUPPORTED
ldadd+=lib/tinycc/libtcc.a

SOURCES += src/kilo.o

ifdef ASAN
cflags += -O0 -ggdb -DDEBUG=1 -fno-omit-frame-pointer -fsanitize=address
ldflags += -fsanitize=address -static-libasan
cflags := -Og -ggdb -DDEBUG=1 -fno-omit-frame-pointer -fsanitize=address
ldflags := -fsanitize=address -static-libasan
# tinycc_config += --extra-ldflags="${ldflags}"
endif

all: deps cjit
tinycc_config += --with-libgcc

all: lib/tinycc/libtcc.a cjit

cjit: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldflags} ${ldadd}

deps: lib/tinycc/libtcc.a

tinycc_config += --with-libgcc

include build/deps.mk
22 changes: 10 additions & 12 deletions build/musl.mk
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
CC := musl-gcc

include build/init.mk

cc := musl-gcc
cflags := -Wall -static -Os
cflags += -Isrc -Ilib/tinycc -DLIBC_MUSL -nostdlib -DREPL_SUPPORTED
cflags := -Wall -static -O2 ${cflags_stack_protect}
cflags += -Isrc -Ilib/tinycc -DLIBC_MUSL -nostdlib
cflags += -DREPL_SUPPORTED

ldadd := lib/tinycc/libtcc.a /usr/lib/x86_64-linux-musl/crt1.o /usr/lib/x86_64-linux-musl/libc.a

SOURCES += src/embed-musl-libc.o
SOURCES += src/embed-musl-libc.o src/kilo.o

tinycc_config += --config-musl --enable-static
tynycc_config += --extra-cflags=-static --extra-ldflags=-static

all: deps cjit
all: lib/tinycc/libtcc.a cjit

cjit: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldadd}

deps: lib/tinycc/libtcc.a src/embed-musl-libc.c src/embed-libtcc1.c

tinycc_config += --config-musl --enable-static
tynycc_config += --extra-cflags=-static --extra-ldflags=-static
# check:
# ./cjit test/hello.c

include build/deps.mk
36 changes: 36 additions & 0 deletions build/osx.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
include build/init.mk

cc := clang

all: deps cjit.command

cjit.command: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldflags} ${ldadd}

.c.o:
$(cc) \
$(cflags) \
-c $< -o $@ \
-DVERSION=\"${VERSION}\" \
-DCURRENT_YEAR=\"${CURRENT_YEAR}\"

deps: lib/tinycc/libtcc.a src/embed-libtcc1.c src/embed-headers.c

## Custom deps targets for osx due to different sed

lib/tinycc/libtcc.a:
cd lib/tinycc && ./configure ${tinycc_config}
${MAKE} -C lib/tinycc libtcc.a
${MAKE} -C lib/tinycc libtcc1.a

src/embed-libtcc1.c:
$(info Embedding libtcc1)
sh build/embed-libtcc1.sh lib/tinycc/libtcc1.a$
sed -i'' -e 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
sed -i'' -e 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c

src/embed-headers.c:
$(info Embedding tinycc headers)
bash build/embed-headers.sh win
sed -i'' -e 's/unsigned char/const char/' src/embed-headers.c
sed -i'' -e 's/unsigned int/const unsigned int/' src/embed-headers.c
11 changes: 8 additions & 3 deletions build/win-native.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ ldflags += -static-libgcc

ldadd := lib/tinycc/libtcc.a -lshlwapi

# embed_libtcc1 := .\lib\tinycc\x86_64-win32-libtcc1.a
# The libtcc is built by the calling GNUmakefile
# to: lib/tinycc/libtcc.a

cjit.exe: ${SOURCES}
$(cc) $(cflags) -o $@ $(SOURCES) ${ldflags} ${ldadd}
Expand All @@ -35,9 +36,13 @@ cjit.exe: ${SOURCES}
-DCURRENT_YEAR=\"${CURRENT_YEAR}\"

src/embed-libtcc1.c:
$(info Embedding libtcc1: ${embed_libtcc1})
sh build/embed-libtcc1.sh ${embed_libtcc1}
$(info Embedding libtcc1)
sh build/embed-libtcc1.sh lib/tinycc/libtcc1.a
sed -i 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
sed -i 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c

src/embed-headers.c:
$(info Embedding tinycc headers)
bash build/embed-headers.sh win
sed -i 's/unsigned char/const char/' src/embed-headers.c
sed -i 's/unsigned int/const unsigned int/' src/embed-headers.c
35 changes: 20 additions & 15 deletions build/win-wsl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,31 @@ ldadd += -lrpcrt4 -lshlwapi

ldflags += -static-libgcc

# embed_libtcc1 := lib/tinycc/x86_64-win32-libtcc1.a
tinycc_config += --targetos=WIN32 --config-backtrace=no --enable-cross
tinycc_config += --ar=${ar}

all: deps cjit.exe

cjit.exe: ${SOURCES}
./build/stamp-exe.sh
$(cc) $(cflags) -o $@ $(SOURCES) cjit.res ${ldflags} ${ldadd}

# upx-ucl -q $@
# somehow upx compression helps the binary to function: it
# accidentally fixes some headers that are malformed out of mingw

tinycc_config += --targetos=WIN32 --config-backtrace=no --enable-cross
tinycc_config += --ar=${ar}

deps:
cd lib/tinycc && ./configure ${tinycc_config}
$(MAKE) -C lib/tinycc cross-x86_64-win32
${MAKE} -C lib/tinycc libtcc.a
${MAKE} -C lib/tinycc libtcc1.a
mv lib/tinycc/x86_64-win32-libtcc1.a lib/tinycc/libtcc1.a

include build/deps.mk
@cd lib/tinycc && ./configure ${tinycc_config}
@$(MAKE) -C lib/tinycc cross-x86_64-win32
@${MAKE} -C lib/tinycc libtcc.a
@${MAKE} -C lib/tinycc libtcc1.a
@mv lib/tinycc/x86_64-win32-libtcc1.a lib/tinycc/libtcc1.a
@bash build/embed-libtcc1.sh lib/tinycc/libtcc1.a
@sed -i 's/unsigned char lib_tinycc_libtcc1_a/const unsigned char libtcc1/' src/embed-libtcc1.c
@sed -i 's/unsigned int lib_tinycc_libtcc1_a_len/const unsigned int libtcc1_len/' src/embed-libtcc1.c
@bash build/embed-headers.sh win
@sed -i 's/unsigned char/const char/' src/embed-headers.c
@sed -i 's/unsigned int/const unsigned int/' src/embed-headers.c

.c.o:
$(cc) \
$(cflags) \
-c $< -o $@ \
-DVERSION=\"${VERSION}\" \
-DCURRENT_YEAR=\"${CURRENT_YEAR}\"
6 changes: 3 additions & 3 deletions lib/tinycc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ ifdef CONFIG_WIN32
NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32)
else
CFG = -unx
LIBS+=-lm
# LIBS+=-lm
ifneq ($(CONFIG_ldl),no)
LIBS+=-ldl
# LIBS+=-ldl
endif
ifneq ($(CONFIG_pthread),no)
LIBS+=-lpthread
# LIBS+=-lpthread
endif
# make libtcc as static or dynamic library?
ifeq ($(CONFIG_static),no)
Expand Down
Loading