Skip to content

Commit

Permalink
Support LLVM Toolchain (nervosnetwork#14)
Browse files Browse the repository at this point in the history
* add support for llvm toolchain
  • Loading branch information
contrun authored Oct 26, 2023
1 parent 5d32198 commit ab2045a
Show file tree
Hide file tree
Showing 46 changed files with 637 additions and 195 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/llvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI for LLVM

on: [push, pull_request]

jobs:
build:
strategy:
matrix:
llvm_version: [16]

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: leafo/gh-actions-lua@v9
with:
luaVersion: "5.4"
- name: Install ckb-debugger
run: |
wget 'https://github.com/nervosnetwork/ckb-standalone-debugger/releases/download/v0.107.0/ckb-debugger-linux-x64.tar.gz'
tar zxvf ckb-debugger-linux-x64.tar.gz
chmod +x ckb-debugger
cp ckb-debugger ~/.cargo/bin
ckb-debugger --version
- name: Install LLVM
run: |
curl -sf -L https://apt.llvm.org/llvm.sh | sudo bash -s -- ${{ matrix.llvm_version }}
- name: Build
run: |
make -f Makefile.clang V=1 LLVM_VERSION=${{ matrix.llvm_version }}
- name: Run cases
run: |
cd tests/official && make ci
cd ../test_cases && make ci-no-dylib
cd ../ckb-c-stdlib-tests && make all-via-docker && make ci
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "include/ckb-c-stdlib"]
path = include/ckb-c-stdlib
url = https://github.com/nervosnetwork/ckb-c-stdlib.git
[submodule "deps/compiler-rt-builtins-riscv"]
path = deps/compiler-rt-builtins-riscv
url = https://github.com/nervosnetwork/compiler-rt-builtins-riscv.git
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ clean-local:
rm -f build/libckblua*
rm -f build/dylibtest
rm -f build/dylibexample
rm -f build/spawnexample
rm -f build/spawnexample*

clean: clean-local
make -C lualib clean
Expand Down
71 changes: 71 additions & 0 deletions Makefile.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

ifeq ($(origin LLVM_VERSION),undefined)
LLVM_VERSION = 16
endif

LLVM_SUFFIX = $(if $(LLVM_VERSION),-$(LLVM_VERSION),)
CC := clang${LLVM_SUFFIX}
LD := ld.lld${LLVM_SUFFIX}
OBJCOPY := llvm-objcopy${LLVM_SUFFIX}
AR := llvm-ar${LLVM_SUFFIX}
RANLIB := llvm-ranlib${LLVM_SUFFIX}

CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs
CFLAGS += -D__ISO_C_VISIBLE=1999 -DCKB_DECLARATION_ONLY -DCKB_MALLOC_DECLARATION_ONLY -DCKB_PRINTF_DECLARATION_ONLY -fPIC -g -O3
CFLAGS += -fno-builtin-printf -fno-builtin-memcmp \
-nostdinc -nostdlib\
-fdata-sections -ffunction-sections

CFLAGS += -I lualib -I lualib/c-stdlib -I include/ckb-c-stdlib -I include/ckb-c-stdlib/libc -I include/ckb-c-stdlib/molecule
CFLAGS += -I deps/compiler-rt-builtins-riscv/compiler-rt/lib/builtins
CFLAGS += -Wall -Werror -Wno-error=unused-command-line-argument -Wno-error=incompatible-library-redeclaration -Wno-error=invalid-noreturn -Wno-error=unused-function

LDFLAGS := -static --gc-sections
LDFLAGS += -Ldeps/compiler-rt-builtins-riscv/build -lcompiler-rt

all: lualib/liblua.a build/lua-loader build/spawnexample

deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a:
cd deps/compiler-rt-builtins-riscv && $(MAKE) CC=$(CC) LD=$(LD) OBJCOPY=$(OBJCOPY) AR=$(AR) RANLIB=$(RANLIB)

build/stdlib.o: include/ckb-c-stdlib/libc/src/impl.c
@echo $(CC) $(filter-out -DCKB_DECLARATION_ONLY, $(CFLAGS)) -c -o $@ $<
@$(CC) $(filter-out -DCKB_DECLARATION_ONLY, $(CFLAGS)) -c -o $@ $<

lualib/liblua.a:
make -C lualib -f Makefile.clang CC=$(CC) liblua.a

build/spawnexample.o: examples/spawn.c
$(CC) -c $(CFLAGS) -o $@ $<

build/spawnexample: build/spawnexample.o build/stdlib.o deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
$(LD) $(LDFLAGS) -o $@ $^
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/lua-loader.o: lua-loader/lua-loader.c
$(CC) -c $(CFLAGS) -o $@ $<

build/lua-loader: build/lua-loader.o build/stdlib.o lualib/liblua.a deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
$(LD) $(LDFLAGS) -o $@ $^
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

fmt:
clang-format -style="{BasedOnStyle: google, IndentWidth: 4, SortIncludes: false}" -i lualib/*.c lualib/*.h lua-loader/*.h lua-loader/*.c include/*.c include/*.h tests/test_cases/*.c

clean-local:
rm -f build/*.o
rm -f build/lua-loader
rm -f build/lua-loader*
rm -f build/libckblua*
rm -f build/dylibtest
rm -f build/dylibexample
rm -f build/spawnexample*

clean: clean-local
make -C lualib clean

run-gdb:
riscv64-unknown-linux-gnu-gdb -ex "target remote 127.0.0.1:${PORT}" build/lua-loader.debug
1 change: 1 addition & 0 deletions deps/compiler-rt-builtins-riscv
2 changes: 1 addition & 1 deletion lua-loader/lua-cell-fs-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

int dochunk(lua_State *L, int status);

int exit(int c);
void exit(int c);

FSFile *ckb_must_get_file(char *filename) {
FSFile *file = 0;
Expand Down
3 changes: 1 addition & 2 deletions lua-loader/lua-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@

extern void malloc_config(uintptr_t min, uintptr_t max);

int exit(int c) {
void exit(int c) {
ckb_exit(c);
return 0;
}
void enable_local_access(int b);
void enable_fs_access(int b);
Expand Down
231 changes: 231 additions & 0 deletions lualib/Makefile.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
# Makefile for building Lua
# See ../doc/readme.html for installation and customization instructions.

# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================

# Your platform. See PLATS for possible values.
PLAT= guess

CC ?= clang-16
# -ULUA_USE_LINUX -ULUA_USE_POSIX to make lua dependencies minimal,
# otherwise, we may unable to find various platform dependent symbols, e.g.
# setsignal.
# __ISO_C_VISIBLE=1999 is needed because lua requires LLONG_MAX to be defined.
# And if __ISO_C_VISIBLE=1999 then LLONG_MAX is defined in ckb-c-stdlib
# See https://github.com/nervosnetwork/ckb-c-stdlib/blob/efe1fe6b3cdda12e248e25664f5c04cbf7876265/libc/limits.h#L94
# and https://github.com/nervosnetwork/ckb-lua/blob/b71d44288e8fb085074d3af056b359b8c6c22afa/lualib/luaconf.h#L502
CFLAGS= --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs -Ic-stdlib -I../include/ckb-c-stdlib -I../include/ckb-c-stdlib/libc -O3 -Wall -Wextra -D__ISO_C_VISIBLE=1999 -DCKB_DECLARATION_ONLY -DCKB_MALLOC_DECLARATION_ONLY -DLUA_COMPAT_5_3 -ULUA_USE_LINUX -ULUA_USE_POSIX -fno-builtin-printf -fno-builtin-memcmp -nostdinc -nostdlib $(SYSCFLAGS) $(MYCFLAGS)
LDFLAGS= $(SYSLDFLAGS) $(MYLDFLAGS)
LIBS= -lm $(SYSLIBS) $(MYLIBS)

AR= ar rc
RANLIB= ranlib
RM= rm -f
UNAME= uname

SYSCFLAGS=
SYSLDFLAGS=
SYSLIBS=

MYCFLAGS=-fPIC -fno-builtin -fdata-sections -ffunction-sections -g -Wno-unused-parameter -Wno-int-to-pointer-cast
MYLDFLAGS=
MYLIBS=
MYOBJS=

# Special flags for compiler modules; -Os reduces code size.
CMCFLAGS=

# == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE =======

PLATS= guess aix bsd c89 freebsd generic linux linux-readline macosx mingw posix solaris

LUA_A= liblua.a
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o
LIB_O= lauxlib.o lbaselib.o lcorolib.o ldblib.o liolib.o lmathlib.o loadlib.o lstrlib.o ltablib.o lutf8lib.o linit.o stdlib.o
BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

LUA_T= lua
LUA_O= lua.o

LUAC_T= luac
LUAC_O= luac.o

ALL_O= $(BASE_O) $(LUA_O) $(LUAC_O)
ALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)
ALL_A= $(LUA_A)

# Targets start here.
default: $(PLAT)

all: $(ALL_T)

o: $(ALL_O)

a: $(ALL_A)

$(LUA_A): $(BASE_O)
$(AR) $@ $(BASE_O)
$(RANLIB) $@

$(LUA_T): $(LUA_O) $(LUA_A)
$(CC) -o $@ $(LDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)

$(LUAC_T): $(LUAC_O) $(LUA_A)
$(CC) -o $@ $(LDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)

test:
./$(LUA_T) -v

clean:
$(RM) $(ALL_T) $(ALL_O)

depend:
@$(CC) $(CFLAGS) -MM l*.c

echo:
@echo "PLAT= $(PLAT)"
@echo "CC= $(CC)"
@echo "CFLAGS= $(CFLAGS)"
@echo "LDFLAGS= $(LDFLAGS)"
@echo "LIBS= $(LIBS)"
@echo "AR= $(AR)"
@echo "RANLIB= $(RANLIB)"
@echo "RM= $(RM)"
@echo "UNAME= $(UNAME)"

# Convenience targets for popular platforms.
ALL= all

help:
@echo "Do 'make PLATFORM' where PLATFORM is one of these:"
@echo " $(PLATS)"
@echo "See doc/readme.html for complete instructions."

guess:
@echo Guessing `$(UNAME)`
@$(MAKE) `$(UNAME)`

AIX aix:
$(MAKE) $(ALL) CC="xlc" CFLAGS="-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-ldl" SYSLDFLAGS="-brtl -bexpall"

bsd:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN" SYSLIBS="-Wl,-E"

c89:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_C89" CC="gcc -std=c89"
@echo ''
@echo '*** C89 does not guarantee 64-bit integers for Lua.'
@echo '*** Make sure to compile all external Lua libraries'
@echo '*** with LUA_USE_C89 to ensure consistency'
@echo ''

FreeBSD NetBSD OpenBSD freebsd:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE -I/usr/include/edit" SYSLIBS="-Wl,-E -ledit" CC="cc"

generic: $(ALL)

Linux linux: linux-noreadline

linux-noreadline:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl"

linux-readline:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX -DLUA_USE_READLINE" SYSLIBS="-Wl,-E -ldl -lreadline"

Darwin macos macosx:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_MACOSX -DLUA_USE_READLINE" SYSLIBS="-lreadline"

mingw:
$(MAKE) "LUA_A=lua54.dll" "LUA_T=lua.exe" \
"AR=$(CC) -shared -o" "RANLIB=strip --strip-unneeded" \
"SYSCFLAGS=-DLUA_BUILD_AS_DLL" "SYSLIBS=" "SYSLDFLAGS=-s" lua.exe
$(MAKE) "LUAC_T=luac.exe" luac.exe

posix:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX"

SunOS solaris:
$(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_REENTRANT" SYSLIBS="-ldl"

# Targets that do not create files (not all makes understand .PHONY).
.PHONY: all $(PLATS) help test clean default o a depend echo

# Compiler modules may use special flags.
llex.o:
$(CC) $(CFLAGS) $(CMCFLAGS) -c llex.c

lparser.o:
$(CC) $(CFLAGS) $(CMCFLAGS) -c lparser.c

lcode.o:
$(CC) $(CFLAGS) $(CMCFLAGS) -c lcode.c

stdlib.o:
$(CC) -I../include/ckb-c-stdlib -DCKB_DECLARATION_ONLY=1 $(CFLAGS) $(CMCFLAGS) -o $@ -c c-stdlib/src/impl.c

# DO NOT DELETE

lapi.o: lapi.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lstring.h \
ltable.h lundump.h lvm.h
lauxlib.o: lauxlib.c lprefix.h lua.h luaconf.h lauxlib.h
lbaselib.o: lbaselib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
lcode.o: lcode.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
ldo.h lgc.h lstring.h ltable.h lvm.h
lcorolib.o: lcorolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
lctype.o: lctype.c lprefix.h lctype.h lua.h luaconf.h llimits.h
ldblib.o: ldblib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
ldebug.o: ldebug.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
lobject.h ltm.h lzio.h lmem.h lcode.h llex.h lopcodes.h lparser.h \
ldebug.h ldo.h lfunc.h lstring.h lgc.h ltable.h lvm.h
ldo.o: ldo.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h lopcodes.h \
lparser.h lstring.h ltable.h lundump.h lvm.h
ldump.o: ldump.c lprefix.h lua.h luaconf.h lobject.h llimits.h lstate.h \
ltm.h lzio.h lmem.h lundump.h
lfunc.o: lfunc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h
lgc.o: lgc.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h
linit.o: linit.c lprefix.h lua.h luaconf.h lualib.h lauxlib.h
liolib.o: liolib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
llex.o: llex.c lprefix.h lua.h luaconf.h lctype.h llimits.h ldebug.h \
lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lgc.h llex.h lparser.h \
lstring.h ltable.h
lmathlib.o: lmathlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
lmem.o: lmem.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h
loadlib.o: loadlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
lobject.o: lobject.c lprefix.h lua.h luaconf.h lctype.h llimits.h \
ldebug.h lstate.h lobject.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h \
lvm.h
lopcodes.o: lopcodes.c lprefix.h lopcodes.h llimits.h lua.h luaconf.h
lparser.o: lparser.c lprefix.h lua.h luaconf.h lcode.h llex.h lobject.h \
llimits.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h \
ldo.h lfunc.h lstring.h lgc.h ltable.h
lstate.o: lstate.c lprefix.h lua.h luaconf.h lapi.h llimits.h lstate.h \
lobject.h ltm.h lzio.h lmem.h ldebug.h ldo.h lfunc.h lgc.h llex.h \
lstring.h ltable.h
lstring.o: lstring.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lstring.h lgc.h
lstrlib.o: lstrlib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
ltable.o: ltable.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
ltablib.o: ltablib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
ltm.o: ltm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
lua.o: lua.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
luac.o: luac.c lprefix.h lua.h luaconf.h lauxlib.h ldebug.h lstate.h \
lobject.h llimits.h ltm.h lzio.h lmem.h lopcodes.h lopnames.h lundump.h
lundump.o: lundump.c lprefix.h lua.h luaconf.h ldebug.h lstate.h \
lobject.h llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h \
lundump.h
lutf8lib.o: lutf8lib.c lprefix.h lua.h luaconf.h lauxlib.h lualib.h
lvm.o: lvm.c lprefix.h lua.h luaconf.h ldebug.h lstate.h lobject.h \
llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h \
ltable.h lvm.h ljumptab.h
lzio.o: lzio.c lprefix.h lua.h luaconf.h llimits.h lmem.h lstate.h \
lobject.h ltm.h lzio.h

# (end of Makefile)
Loading

0 comments on commit ab2045a

Please sign in to comment.