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

Support musl #16

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml → .github/workflows/gnu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
luaVersion: "5.4"
- name: Build
run: |
make all-via-docker
make -f Makefile.gcc all-via-docker
- name: Install ckb-debugger
run: |
wget 'https://github.com/nervosnetwork/ckb-standalone-debugger/releases/download/v0.118.0/ckb-debugger-linux-x64.tar.gz'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/llvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
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 }}
make V=1 LLVM_VERSION=${{ matrix.llvm_version }}
- name: Run cases
run: |
cd tests/official && make ci
Expand Down
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
[submodule "deps/compiler-rt-builtins-riscv"]
path = deps/compiler-rt-builtins-riscv
url = https://github.com/nervosnetwork/compiler-rt-builtins-riscv.git
[submodule "deps/ckb-libcxx-builder"]
path = deps/ckb-libcxx-builder
url = https://github.com/xxuejie/ckb-libcxx-builder.git
[submodule "deps/musl"]
path = deps/musl
url = https://github.com/xxuejie/musl.git
98 changes: 60 additions & 38 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,55 +1,73 @@

CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
TARGET := riscv64-unknown-linux-gnu
CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy

# See comments in lualib/Makefile for why __ISO_C_VISIBLE=1999 is needed
CFLAGS := -D__ISO_C_VISIBLE=1999 -fPIC -O3 -fno-builtin -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections -I lualib -I lualib/c-stdlib -I include/ckb-c-stdlib -I include/ckb-c-stdlib/libc -I include/ckb-c-stdlib/molecule -Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -Wno-error=maybe-uninitialized -g
ifeq ($(origin LLVM_VERSION),undefined)
LLVM_VERSION = 18
endif

LDFLAGS := -nostdlib -nostartfiles -fno-builtin -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections
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}

DOCKER_USER := $(shell id -u):$(shell id -g)
DOCKER_EXTRA_FLAGS ?=
# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:aae8a3f79705f67d505d1f1d5ddc694a4fd537ed1c7e9622420a470d59ba2ec3
PORT ?= 9999
CFLAGS := --target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs \
-g -O3 -nostdinc -fdata-sections -ffunction-sections

all: lualib/liblua.a build/lua-loader build/libckblua.so build/dylibtest build/dylibexample build/spawnexample
CFLAGS += -D__ISO_C_VISIBLE=1999 -DCKB_DECLARATION_ONLY -DCKB_MALLOC_DECLARATION_ONLY -DCKB_PRINTF_DECLARATION_ONLY
CFLAGS += -isystem deps/musl/release/include/ -Ideps/ckb -Ic-stdlib
CFLAGS += -I include/ckb-c-stdlib/molecule -I include
CFLAGS += -I lualib -I lualib/c-stdlib

all-via-docker:
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make"
LDFLAGS= -static --gc-sections --nostdlib --sysroot deps/musl/release \
-Ldeps/musl/release/lib -Ldeps/compiler-rt-builtins-riscv/build -lc -lgcc -lcompiler-rt

docker-interactive:
docker run --user ${DOCKER_USER} --rm -it -v "${CURRENT_DIR}:/code" --workdir /code --entrypoint /bin/bash ${DOCKER_EXTRA_FLAGS} ${BUILDER_DOCKER}
LDFLAGS += -wrap=fclose -wrap=fopen -wrap=freopen -wrap=getc -wrap=fread -wrap=fseek -wrap=ftell -wrap=feof -wrap=ferror

lualib/liblua.a:
make -C lualib liblua.a
LICOMPILER_RT_CFLGAS = \
--target=riscv64 -march=rv64imc_zba_zbb_zbc_zbs -mabi=lp64 \
-nostdinc \
-I ../musl/release/include -I ../ckb-libcxx-builder/release/include \
-Os \
-fdata-sections -ffunction-sections -fno-builtin -fvisibility=hidden -fomit-frame-pointer \
-I compiler-rt/lib/builtins \
-DVISIBILITY_HIDDEN -DCOMPILER_RT_HAS_FLOAT16

build/dylibtest: tests/test_cases/dylibtest.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
# docker pull docker.io/cryptape/llvm-n-rust:20240630
DOCKER_IMAGE := docker.io/cryptape/llvm-n-rust@sha256:bafaf76d4f342a69b8691c08e77a330b7740631f3d1d9c9bee4ead521b29ee55

build/dylibexample: examples/dylib.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
all: lualib/liblua.a build/lua-loader build/spawnexample

build/spawnexample: examples/spawn.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
all-via-docker:
docker run --rm -v `pwd`:/code ${DOCKER_IMAGE} bash -c "cd /code && make"

build/lua-loader.o: lua-loader/lua-loader.c
deps/musl/release/:
cd deps/musl/src/stdio
cd deps/musl/ && CLANG=$(CC) DISABLE_STD_FILE=true ./ckb/build.sh

deps/ckb-libcxx-builder/release/:
cd deps/ckb-libcxx-builder && mkdir -p release && LLVM_VERSION= CLANG=$(CC) ./build.sh

deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a: deps/musl/release/ deps/ckb-libcxx-builder/release/
cd deps/compiler-rt-builtins-riscv && \
make CC=$(CC) LD=$(LD) OBJCOPY=$(OBJCOPY) AR=$(AR) RANLIB=$(RANLIB) CFLAGS="$(LICOMPILER_RT_CFLGAS)"

lualib/liblua.a: deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
make -C lualib -f Makefile CC=$(CC) LD=$(LD) OBJCOPY=$(OBJCOPY) AR="$(AR) rc" RANLIB=$(RANLIB) liblua.a

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

# We need the soft floating point number support from libgcc.
# Note when -nostdlib is specified, libgcc is not linked to the program automatically.
# Also note libgcc.a must be appended to the file list, simply -lgcc does not for some reason.
# It seems gcc does not search libgcc in the install path.
build/lua-loader: build/lua-loader.o lualib/liblua.a
$(LD) $(LDFLAGS) -o $@ $^ $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
build/spawnexample: build/spawnexample.o deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
$(LD) $(LDFLAGS) -o $@ $^
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/libckblua.so: build/lua-loader.o lualib/liblua.a
$(LD) $(LDFLAGS) -Wl,--dynamic-list lua-loader/libckblua.syms -fpic -shared -o $@ $^ $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
build/lua-loader.o: lua-loader/lua-loader.c
$(CC) -c $(CFLAGS) -o $@ $<

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

Expand All @@ -65,11 +83,15 @@ clean-local:
rm -f build/dylibexample
rm -f build/spawnexample*

clean-deps: clean
make -C deps/musl/ clean
rm -rf deps/musl/release/
rm -rf deps/musl/config.mak
rm -rf deps/compiler-rt-builtins-riscv/build/libcompiler-rt.a
rm -rf deps/ckb-libcxx-builder/release/*

clean: clean-local
make -C lualib clean

run-docker:
docker run --rm --network host -it -v `pwd`:/code ${BUILDER_DOCKER} bash

run-gdb:
riscv64-unknown-linux-gnu-gdb -ex "target remote 127.0.0.1:${PORT}" build/lua-loader.debug
71 changes: 0 additions & 71 deletions Makefile.clang

This file was deleted.

84 changes: 84 additions & 0 deletions Makefile.gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
TARGET := riscv64-unknown-linux-gnu
CC := $(TARGET)-gcc
LD := $(TARGET)-gcc
OBJCOPY := $(TARGET)-objcopy

# See comments in lualib/Makefile for why __ISO_C_VISIBLE=1999 is needed
CFLAGS := \
-D__ISO_C_VISIBLE=1999 \
-fPIC -O3 -fno-builtin -nostdinc -nostdlib -nostartfiles -fvisibility=hidden -fdata-sections -ffunction-sections \
-I lualib \
-I lualib/c-stdlib \
-I lualib/c-stdlib/my_gnu \
-I include/ckb-c-stdlib \
-I include/ckb-c-stdlib/libc \
-I include/ckb-c-stdlib/molecule \
-Wall -Werror -Wno-nonnull -Wno-nonnull-compare -Wno-unused-function -Wno-error=maybe-uninitialized -g

LDFLAGS := -nostdlib -nostartfiles -fno-builtin -Wl,-static -fdata-sections -ffunction-sections -Wl,--gc-sections

DOCKER_USER := $(shell id -u):$(shell id -g)
DOCKER_EXTRA_FLAGS ?=
# docker pull nervos/ckb-riscv-gnu-toolchain:gnu-bionic-20191012
BUILDER_DOCKER := nervos/ckb-riscv-gnu-toolchain@sha256:aae8a3f79705f67d505d1f1d5ddc694a4fd537ed1c7e9622420a470d59ba2ec3
PORT ?= 9999

all: lualib/liblua.a build/lua-loader build/libckblua.so build/dylibtest build/dylibexample build/spawnexample

all-via-docker:
docker run --rm -v `pwd`:/code ${BUILDER_DOCKER} bash -c "cd /code && make -f Makefile.gcc"

docker-interactive:
docker run --user ${DOCKER_USER} --rm -it -v "${CURRENT_DIR}:/code" --workdir /code --entrypoint /bin/bash ${DOCKER_EXTRA_FLAGS} ${BUILDER_DOCKER}

lualib/liblua.a:
make -C lualib -f Makefile.gcc liblua.a

build/dylibtest: tests/test_cases/dylibtest.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a

build/dylibexample: examples/dylib.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a

build/spawnexample: examples/spawn.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< lualib/liblua.a $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a

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

# We need the soft floating point number support from libgcc.
# Note when -nostdlib is specified, libgcc is not linked to the program automatically.
# Also note libgcc.a must be appended to the file list, simply -lgcc does not for some reason.
# It seems gcc does not search libgcc in the install path.
build/lua-loader: build/lua-loader.o lualib/liblua.a
$(LD) $(LDFLAGS) -o $@ $^ $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
cp $@ $@.debug
$(OBJCOPY) --strip-debug --strip-all $@

build/libckblua.so: build/lua-loader.o lualib/liblua.a
$(LD) $(LDFLAGS) -Wl,--dynamic-list lua-loader/libckblua.syms -fpic -shared -o $@ $^ $(shell $(CC) --print-search-dirs | sed -n '/install:/p' | sed 's/install:\s*//g')libgcc.a
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-docker:
docker run --rm --network host -it -v `pwd`:/code ${BUILDER_DOCKER} bash

run-gdb:
riscv64-unknown-linux-gnu-gdb -ex "target remote 127.0.0.1:${PORT}" build/lua-loader.debug
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@ To test the `ADDITIONAL_ARGUMENTS` locally, we can run `ckb-debugger --bin ./bui
- `-e` is used to evaluate some lua script. For example, running `ckb-debugger --bin ./build/lua-loader.debug -- -e 'print("abcdefg")'` will print out `abcdefg` in to console.
- `-f` is used to enable [file system access](./docs/fs.md). For example, running `ckb-debugger --bin ./build/lua-loader.debug -- -f` would evaluate the `main.lua` file within the file system in the cell data.
- `-r` is used to execute coded loaded from ckb-debugger. For example, running `ckb-debugger ---read-file strings.lua --bin ./build/lua-loader.debug -- -r` will execute the lua file `strings.lua`. Normally, ckb-lua-vm can not read files from local file system, we add this parameter (along with the `--read-file` parameter of `ckb-debugger`) to facilitate testing of running local lua files.


## LLVM & GCC
The project uses LLVM for compilation by default but also supports GCC (via `Makefile.gcc`).
However, please note that LLVM does not support dynamic libraries, so GCC must be used in such cases.
1 change: 1 addition & 0 deletions deps/ckb-libcxx-builder
Submodule ckb-libcxx-builder added at 3d0ca0
65 changes: 65 additions & 0 deletions deps/ckb/ckb_consts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef CKB_C_STDLIB_CKB_CONSTS_H_
#define CKB_C_STDLIB_CKB_CONSTS_H_

#define SYS_exit 93
#define SYS_ckb_vm_version 2041
#define SYS_ckb_current_cycles 2042
#define SYS_ckb_exec 2043
#define SYS_ckb_load_transaction 2051
#define SYS_ckb_load_script 2052
#define SYS_ckb_load_tx_hash 2061
#define SYS_ckb_load_script_hash 2062
#define SYS_ckb_load_cell 2071
#define SYS_ckb_load_header 2072
#define SYS_ckb_load_input 2073
#define SYS_ckb_load_witness 2074
#define SYS_ckb_load_cell_by_field 2081
#define SYS_ckb_load_header_by_field 2082
#define SYS_ckb_load_input_by_field 2083
#define SYS_ckb_load_cell_data_as_code 2091
#define SYS_ckb_load_cell_data 2092
#define SYS_ckb_debug 2177
#define SYS_ckb_load_block_extension 2104
#define SYS_ckb_spawn 2601
#define SYS_ckb_wait 2602
#define SYS_ckb_process_id 2603
#define SYS_ckb_pipe 2604
#define SYS_ckb_write 2605
#define SYS_ckb_read 2606
#define SYS_ckb_inherited_fds 2607
#define SYS_ckb_close 2608

#define CKB_SUCCESS 0
#define CKB_INDEX_OUT_OF_BOUND 1
#define CKB_ITEM_MISSING 2
#define CKB_LENGTH_NOT_ENOUGH 3
#define CKB_INVALID_DATA 4
#define CKB_WAIT_FAILURE 5
#define CKB_INVALID_FD 6
#define CKB_OTHER_END_CLOSED 7
#define CKB_MAX_VMS_SPAWNED 8
#define CKB_MAX_FDS_CREATED 9

#define CKB_SOURCE_INPUT 1
#define CKB_SOURCE_OUTPUT 2
#define CKB_SOURCE_CELL_DEP 3
#define CKB_SOURCE_HEADER_DEP 4
#define CKB_SOURCE_GROUP_INPUT 0x0100000000000001
#define CKB_SOURCE_GROUP_OUTPUT 0x0100000000000002

#define CKB_CELL_FIELD_CAPACITY 0
#define CKB_CELL_FIELD_DATA_HASH 1
#define CKB_CELL_FIELD_LOCK 2
#define CKB_CELL_FIELD_LOCK_HASH 3
#define CKB_CELL_FIELD_TYPE 4
#define CKB_CELL_FIELD_TYPE_HASH 5
#define CKB_CELL_FIELD_OCCUPIED_CAPACITY 6

#define CKB_HEADER_FIELD_EPOCH_NUMBER 0
#define CKB_HEADER_FIELD_EPOCH_START_BLOCK_NUMBER 1
#define CKB_HEADER_FIELD_EPOCH_LENGTH 2

#define CKB_INPUT_FIELD_OUT_POINT 0
#define CKB_INPUT_FIELD_SINCE 1

#endif /* CKB_C_STDLIB_CKB_CONSTS_H_ */
Loading
Loading