Skip to content

Commit

Permalink
feat: support compiling libcartesi as a static library
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 6, 2023
1 parent 9df439f commit 7a31801
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RUN apt-get update && \
DEBIAN_FRONTEND="noninteractive" apt-get install --no-install-recommends -y \
build-essential vim wget git clang-tidy-15 clang-format-15 lcov \
libboost1.81-dev libssl-dev \
ca-certificates automake libtool patchelf pkg-config lua5.4 liblua5.4-dev \
ca-certificates automake libtool pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks && \
update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-15 120 && \
Expand Down
44 changes: 20 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,25 @@ LIBCARTESI_Darwin=libcartesi.dylib
LIBCARTESI_Linux=libcartesi.so
LIBCARTESI_GRPC_Darwin=libcartesi_grpc.dylib
LIBCARTESI_GRPC_Linux=libcartesi_grpc.so
LIBCARTESI_JSONRPC_Darwin=libcartesi_jsonrpc.dylib
LIBCARTESI_JSONRPC_Linux=libcartesi_jsonrpc.so

LIBCARTESI_SO_Darwin:=libcartesi-$(MACHINE_EMULATOR_SO_VERSION).dylib
LIBCARTESI_SO_Linux:=libcartesi-$(MACHINE_EMULATOR_SO_VERSION).so
LIBCARTESI_SO_GRPC_Darwin:=libcartesi_grpc-$(MACHINE_EMULATOR_SO_VERSION).dylib
LIBCARTESI_SO_GRPC_Linux:=libcartesi_grpc-$(MACHINE_EMULATOR_SO_VERSION).so

BIN_INSTALL_PATH:= $(DESTDIR)$(BIN_RUNTIME_PATH)
LIB_INSTALL_PATH:= $(DESTDIR)$(LIB_RUNTIME_PATH)
DOC_INSTALL_PATH:= $(DESTDIR)$(DOC_RUNTIME_PATH)
SHARE_INSTALL_PATH:= $(DESTDIR)$(SHARE_RUNTIME_PATH)
IMAGES_INSTALL_PATH:= $(DESTDIR)$(IMAGES_RUNTIME_PATH)
UARCH_INSTALL_PATH:= $(SHARE_INSTALL_PATH)/uarch
LUA_INSTALL_CPATH:= $(DESTDIR)$(LUA_RUNTIME_CPATH)
LUA_INSTALL_PATH:= $(DESTDIR)$(LUA_RUNTIME_PATH)
INC_INSTALL_PATH:= $(DESTDIR)$(PREFIX)/include/cartesi-machine
LIBCARTESI_SO_JSONRPC_Darwin:=libcartesi_jsonrpc-$(MACHINE_EMULATOR_SO_VERSION).dylib
LIBCARTESI_SO_JSONRPC_Linux:=libcartesi_jsonrpc-$(MACHINE_EMULATOR_SO_VERSION).so

BIN_INSTALL_PATH:= $(abspath $(DESTDIR)$(BIN_RUNTIME_PATH))
LIB_INSTALL_PATH:= $(abspath $(DESTDIR)$(LIB_RUNTIME_PATH))
DOC_INSTALL_PATH:= $(abspath $(DESTDIR)$(DOC_RUNTIME_PATH))
SHARE_INSTALL_PATH:= $(abspath $(DESTDIR)$(SHARE_RUNTIME_PATH))
IMAGES_INSTALL_PATH:= $(abspath $(DESTDIR)$(IMAGES_RUNTIME_PATH))
UARCH_INSTALL_PATH:= $(abspath $(SHARE_INSTALL_PATH)/uarch)
LUA_INSTALL_CPATH:= $(abspath $(DESTDIR)$(LUA_RUNTIME_CPATH))
LUA_INSTALL_PATH:= $(abspath $(DESTDIR)$(LUA_RUNTIME_PATH))
INC_INSTALL_PATH:= $(abspath $(DESTDIR)$(PREFIX)/include/cartesi-machine)

INSTALL= cp -RP
CHMOD_EXEC= chmod 0755
Expand All @@ -59,14 +63,15 @@ STRIP= strip
STRIP_EXEC= $(STRIP) -x

EMU_TO_BIN= jsonrpc-remote-cartesi-machine remote-cartesi-machine merkle-tree-hash
EMU_TO_LIB= $(LIBCARTESI_SO_$(TARGET_OS)) $(LIBCARTESI_SO_GRPC_$(TARGET_OS))
EMU_TO_LIB= $(LIBCARTESI_SO_$(TARGET_OS)) $(LIBCARTESI_SO_GRPC_$(TARGET_OS)) $(LIBCARTESI_SO_JSONRPC_$(TARGET_OS)) libcartesi.a
EMU_LUA_TO_BIN= cartesi-machine.lua cartesi-machine-stored-hash.lua rollup-memory-range.lua
EMU_LUA_TEST_TO_BIN= cartesi-machine-tests.lua uarch-riscv-tests.lua
EMU_TO_LUA_PATH= cartesi/util.lua cartesi/proof.lua cartesi/gdbstub.lua
EMU_TO_LUA_CPATH= cartesi.so
EMU_TO_LUA_CARTESI_CPATH= cartesi/grpc.so cartesi/jsonrpc.so
EMU_TO_INC= $(addprefix lib/machine-emulator-defines/,pma-defines.h rtc-defines.h) \
$(addprefix src/,jsonrpc-machine-c-api.h grpc-machine-c-api.h machine-c-api.h machine-c-defines.h machine-c-version.h)

UARCH_TO_SHARE= uarch-ram.bin

MONGOOSE_VERSION=7.12
Expand Down Expand Up @@ -257,18 +262,8 @@ uarch-tests-with-linux-env:
@$(MAKE) linux-env-exec CONTAINER_COMMAND="make uarch-tests"

install-Darwin:
install_name_tool -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi.so
install_name_tool -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi/grpc.so
install_name_tool -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi/jsonrpc.so
cd $(BIN_INSTALL_PATH) && \
for x in $(EMU_TO_BIN); do \
install_name_tool -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $$x ;\
done

install-Linux:
cd $(BIN_INSTALL_PATH) && for x in $(EMU_TO_BIN); do patchelf --set-rpath $(LIB_RUNTIME_PATH) $$x ; done
cd $(LIB_INSTALL_PATH) && for x in $(EMU_TO_LIB); do patchelf --set-rpath $(LIB_RUNTIME_PATH) $$x; done
cd $(LUA_INSTALL_CPATH) && for x in $(EMU_TO_LUA_CPATH) $(EMU_TO_LUA_CARTESI_CPATH); do patchelf --set-rpath $(LIB_RUNTIME_PATH) $$x ; done

install-tests: install
cd src && $(INSTALL) $(EMU_LUA_TEST_TO_BIN) $(LUA_INSTALL_PATH)
Expand All @@ -277,8 +272,6 @@ install-tests: install
cat tools/template/uarch-riscv-tests.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/uarch-riscv-tests
cd $(LUA_INSTALL_PATH) && $(CHMOD_DATA) $(EMU_LUA_TEST_TO_BIN)
cd $(BIN_INSTALL_PATH) && $(CHMOD_EXEC) cartesi-machine-tests uarch-riscv-tests
patchelf --set-rpath $(LIB_RUNTIME_PATH) src/tests/test-merkle-tree-hash
patchelf --set-rpath $(LIB_RUNTIME_PATH) src/tests/test-machine-c-api

install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(INC_INSTALL_PATH) $(IMAGES_INSTALL_PATH)
cd src && $(INSTALL) $(EMU_TO_BIN) $(BIN_INSTALL_PATH)
Expand All @@ -293,10 +286,12 @@ install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/c
cd $(BIN_INSTALL_PATH) && $(CHMOD_EXEC) $(EMU_TO_BIN) cartesi-machine cartesi-machine-stored-hash rollup-memory-range
cd $(LIB_INSTALL_PATH) && ln -sf $(LIBCARTESI_SO_$(TARGET_OS)) $(LIBCARTESI_$(TARGET_OS))
cd $(LIB_INSTALL_PATH) && ln -sf $(LIBCARTESI_SO_GRPC_$(TARGET_OS)) $(LIBCARTESI_GRPC_$(TARGET_OS))
cd $(LIB_INSTALL_PATH) && ln -sf $(LIBCARTESI_SO_JSONRPC_$(TARGET_OS)) $(LIBCARTESI_JSONRPC_$(TARGET_OS))
cd $(LUA_INSTALL_PATH) && $(CHMOD_DATA) $(EMU_LUA_TO_BIN)
$(INSTALL) $(EMU_TO_INC) $(INC_INSTALL_PATH)
$(INSTALL) tools/gdb $(SHARE_INSTALL_PATH)/gdb
cd $(LUA_INSTALL_CPATH) && $(CHMOD_EXEC) $(EMU_TO_LUA_CPATH)
$(MAKE) $(INSTALL_PLAT)

install-uarch: install $(UARCH_INSTALL_PATH)
$(INSTALL) uarch/$(UARCH_TO_SHARE) $(UARCH_INSTALL_PATH)
Expand All @@ -306,7 +301,8 @@ install-strip: install-emulator
cd $(LIB_INSTALL_PATH) && $(STRIP_EXEC) $(EMU_TO_LIB)
cd $(LUA_INSTALL_CPATH) && $(STRIP_EXEC) $(EMU_TO_LUA_CPATH)

install: install-emulator install-strip $(INSTALL_PLAT)
.NOTPARALLEL: install
install: install-strip
debian-package: install
mkdir -p $(DESTDIR)/DEBIAN $(DOC_INSTALL_PATH)
$(INSTALL) COPYING $(DOC_INSTALL_PATH)/copyright
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Obs: Please note that Apple Clang Version number does not follow upstream LLVM/C
```
sudo apt-get install build-essential wget git clang-tidy-15 clang-format-15 \
libboost1.81-dev libssl-dev \
ca-certificates automake libtool patchelf pkg-config lua5.4 liblua5.4-dev \
ca-certificates automake libtool pkg-config lua5.4 liblua5.4-dev \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
luarocks
Expand Down
Loading

0 comments on commit 7a31801

Please sign in to comment.