Skip to content

Commit

Permalink
refactor: allow cross compilation with TARGET_OS make flag
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Nov 6, 2023
1 parent 2a32fcd commit 9df439f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ARG MACHINE_EMULATOR_VERSION=0.0.0
ARG TARGETARCH

RUN make install-tests
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt install -y \
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y \
./cartesi-machine-v${MACHINE_EMULATOR_VERSION}_${TARGETARCH}.deb \
&& rm -rf /var/lib/apt/lists/*

Expand All @@ -62,7 +62,7 @@ ARG TARGETARCH
COPY --from=installer \
/usr/src/emulator/cartesi-machine-v${MACHINE_EMULATOR_VERSION}_${TARGETARCH}.deb \
cartesi-machine.deb
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt install -y \
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y \
./cartesi-machine.deb \
&& rm -rf /var/lib/apt/lists/* \
&& rm cartesi-machine.deb
Expand Down
31 changes: 15 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@
# with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
#

UNAME:=$(shell uname)
TARGET_OS?=$(shell uname)
export TARGET_OS

# Install settings
ARCH:= $(shell dpkg --print-architecture 2>/dev/null || echo amd64)
DEB_ARCH:= $(shell dpkg --print-architecture 2>/dev/null || echo amd64)
PREFIX= /usr
MACHINE_EMULATOR_VERSION:= $(shell make -sC src version)
MACHINE_EMULATOR_SO_VERSION:= $(shell make -sC src so-version)
DEB_FILENAME= cartesi-machine-v$(MACHINE_EMULATOR_VERSION)_$(ARCH).deb
DEB_FILENAME= cartesi-machine-v$(MACHINE_EMULATOR_VERSION)_$(DEB_ARCH).deb
BIN_RUNTIME_PATH= $(PREFIX)/bin
LIB_RUNTIME_PATH= $(PREFIX)/lib
DOC_RUNTIME_PATH= $(PREFIX)/doc/cartesi-machine
SHARE_RUNTIME_PATH= $(PREFIX)/share/cartesi-machine
IMAGES_RUNTIME_PATH= $(SHARE_RUNTIME_PATH)/images
LUA_RUNTIME_CPATH= $(PREFIX)/lib/lua/5.4
LUA_RUNTIME_PATH= $(PREFIX)/share/lua/5.4
INSTALL_PLAT = install-$(UNAME)
INSTALL_PLAT = install-$(TARGET_OS)

LIBCARTESI_Darwin=libcartesi.dylib
LIBCARTESI_Linux=libcartesi.so
Expand All @@ -54,10 +55,11 @@ INC_INSTALL_PATH:= $(DESTDIR)$(PREFIX)/include/cartesi-machine
INSTALL= cp -RP
CHMOD_EXEC= chmod 0755
CHMOD_DATA= chmod 0644
STRIP_EXEC= strip -x
STRIP= strip
STRIP_EXEC= $(STRIP) -x

EMU_TO_BIN= jsonrpc-remote-cartesi-machine remote-cartesi-machine merkle-tree-hash
EMU_TO_LIB= $(LIBCARTESI_SO_$(UNAME)) $(LIBCARTESI_SO_GRPC_$(UNAME))
EMU_TO_LIB= $(LIBCARTESI_SO_$(TARGET_OS)) $(LIBCARTESI_SO_GRPC_$(TARGET_OS))
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
Expand Down Expand Up @@ -97,21 +99,18 @@ export release
export coverage

# Mac OS X specific settings
ifeq ($(UNAME),Darwin)
LUA_PLAT ?= macosx
ifeq ($(TARGET_OS),Darwin)
export CC = clang
export CXX = clang++
LUACC = "CC=$(CXX)"
LIBRARY_PATH := "export DYLD_LIBRARY_PATH="

# Linux specific settings
else ifeq ($(UNAME),Linux)
LUA_PLAT ?= linux
else ifeq ($(TARGET_OS),Linux)
export CC=gcc
export CXX=g++
LIBRARY_PATH := "export LD_LIBRARY_PATH=$(SRCDIR)"
LUACC = "CC=g++"
# Unknown platform
else
LUA_PLAT ?= none
INSTALL_PLAT=
endif

Expand Down Expand Up @@ -292,8 +291,8 @@ install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/c
cat tools/template/cartesi-machine-stored-hash.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)/cartesi-machine-stored-hash
cat tools/template/rollup-memory-range.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)/rollup-memory-range
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_$(UNAME)) $(LIBCARTESI_$(UNAME))
cd $(LIB_INSTALL_PATH) && ln -sf $(LIBCARTESI_SO_GRPC_$(UNAME)) $(LIBCARTESI_GRPC_$(UNAME))
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 $(LUA_INSTALL_PATH) && $(CHMOD_DATA) $(EMU_LUA_TO_BIN)
$(INSTALL) $(EMU_TO_INC) $(INC_INSTALL_PATH)
$(INSTALL) tools/gdb $(SHARE_INSTALL_PATH)/gdb
Expand All @@ -311,7 +310,7 @@ install: install-emulator install-strip $(INSTALL_PLAT)
debian-package: install
mkdir -p $(DESTDIR)/DEBIAN $(DOC_INSTALL_PATH)
$(INSTALL) COPYING $(DOC_INSTALL_PATH)/copyright
cat tools/template/control.template | sed 's|ARG_VERSION|$(MACHINE_EMULATOR_VERSION)|g;s|ARG_ARCH|$(ARCH)|g' > $(DESTDIR)/DEBIAN/control
cat tools/template/control.template | sed 's|ARG_VERSION|$(MACHINE_EMULATOR_VERSION)|g;s|ARG_ARCH|$(DEB_ARCH)|g' > $(DESTDIR)/DEBIAN/control
dpkg-deb -Zxz --root-owner-group --build $(DESTDIR) $(DEB_FILENAME)

.SECONDARY: $(DOWNLOADDIR) $(DEPDIRS) $(COREPROTO)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Obs: Please note that Apple Clang Version number does not follow upstream LLVM/C
#### Debian Bookworm

```
apt-get install build-essential wget git clang-tidy-15 clang-format-15 \
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 \
libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
Expand Down Expand Up @@ -87,7 +87,7 @@ $ make clean
### Install

```bash
$ make install
$ sudo make install PREFIX=/usr/local
```

## Running Tests
Expand Down
60 changes: 33 additions & 27 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EMULATOR_VERSION_MINOR=15
EMULATOR_VERSION_PATCH=2
EMULATOR_VERSION_LABEL=

UNAME:=$(shell uname)
TARGET_OS?=$(shell uname)

LUA_INC:=$(shell pkg-config --cflags-only-I lua5.4)
LUA_LIB:=-llua5.4
Expand Down Expand Up @@ -54,7 +54,7 @@ CC_Darwin=clang
CXX_Darwin=clang++
INCS_Darwin=

ifeq ($(UNAME),Darwin)
ifeq ($(TARGET_OS),Darwin)
ifeq ($(MACOSX_DEPLOYMENT_TARGET),)
export MACOSX_DEPLOYMENT_TARGET := $(shell sw_vers -productVersion | sed -r "s/([[:digit:]]+)\.([[:digit:]]+)\..+/\1.\2.0/")
endif
Expand Down Expand Up @@ -117,24 +117,24 @@ LIBCARTESI_JSONRPC_TESTS_LDFLAGS_Linux=-Wl,-rpath,'$$ORIGIN/..'
CARTESI_EXECUTABLE_LDFLAGS_Linux=-Wl,-rpath,'$$ORIGIN/'
PROFILE_DATA_Linux=

CC=$(CC_$(UNAME))
CXX=$(CXX_$(UNAME))
SOLDFLAGS:=$(SOLDFLAGS_$(UNAME)) $(GCLDFLAGS)
PTHREAD_LIB:=$(PTHREAD_LIB_$(UNAME))
BOOST_INC:=$(BOOST_INC_$(UNAME))
GRPC_INC:=$(GRPC_INC_$(UNAME))
GRPC_LIB:=$(GRPC_LIB_$(UNAME))
PROTOBUF_INC:=$(PROTOBUF_INC_$(UNAME))
PROTOBUF_LIB:=$(PROTOBUF_LIB_$(UNAME))
LIBCARTESI=$(LIBCARTESI_$(UNAME))
LIBCARTESI_LDFLAGS=$(LIBCARTESI_LDFLAGS_$(UNAME))
LIBCARTESI_TESTS_LDFLAGS=$(LIBCARTESI_TESTS_LDFLAGS_$(UNAME))
CC=$(CC_$(TARGET_OS))
CXX=$(CXX_$(TARGET_OS))
SOLDFLAGS:=$(SOLDFLAGS_$(TARGET_OS)) $(GCLDFLAGS)
PTHREAD_LIB:=$(PTHREAD_LIB_$(TARGET_OS))
BOOST_INC:=$(BOOST_INC_$(TARGET_OS))
GRPC_INC:=$(GRPC_INC_$(TARGET_OS))
GRPC_LIB:=$(GRPC_LIB_$(TARGET_OS))
PROTOBUF_INC:=$(PROTOBUF_INC_$(TARGET_OS))
PROTOBUF_LIB:=$(PROTOBUF_LIB_$(TARGET_OS))
LIBCARTESI=$(LIBCARTESI_$(TARGET_OS))
LIBCARTESI_LDFLAGS=$(LIBCARTESI_LDFLAGS_$(TARGET_OS))
LIBCARTESI_TESTS_LDFLAGS=$(LIBCARTESI_TESTS_LDFLAGS_$(TARGET_OS))
LIBCARTESI_LIB=-L. -lcartesi-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR)
CARTESI_EXECUTABLE_LDFLAGS=$(CARTESI_EXECUTABLE_LDFLAGS_$(UNAME))
CARTESI_EXECUTABLE_LDFLAGS=$(CARTESI_EXECUTABLE_LDFLAGS_$(TARGET_OS))

LIBCARTESI_GRPC=$(LIBCARTESI_GRPC_$(UNAME))
LIBCARTESI_GRPC_LDFLAGS=$(LIBCARTESI_GRPC_LDFLAGS_$(UNAME))
LIBCARTESI_GRPC_TESTS_LDFLAGS=$(LIBCARTESI_GRPC_TESTS_LDFLAGS_$(UNAME))
LIBCARTESI_GRPC=$(LIBCARTESI_GRPC_$(TARGET_OS))
LIBCARTESI_GRPC_LDFLAGS=$(LIBCARTESI_GRPC_LDFLAGS_$(TARGET_OS))
LIBCARTESI_GRPC_TESTS_LDFLAGS=$(LIBCARTESI_GRPC_TESTS_LDFLAGS_$(TARGET_OS))
LIBCARTESI_GRPC_LIB=-L. -lcartesi_grpc-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR)

LIBCARTESI_LIBS:=
Expand All @@ -158,7 +158,7 @@ INCS= \
-I../third-party/tiny_sha3 \
-I../third-party/downloads \
-I../third-party/mongoose-7.12 \
$(LUA_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(UNAME))
$(LUA_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(TARGET_OS))

ifeq ($(dump),yes)
#DEFS+=-DDUMP_ILLEGAL_INSN_EXCEPTIONS
Expand Down Expand Up @@ -199,16 +199,18 @@ DEFS+=-DNDEBUG
## Mongoose log is disabled because it generates code using __FILE__ macro,
## which is reported as an error when packaging in some Linux distributions.
MONGOOSE_DEFS+=-DMG_ENABLE_LOG=0
# Disable jump tables, because it degrades the instruction decoding performance in the interpret loop,
# since it generates a memory indirection that has a high cost in opcode switches.
OPTFLAGS+=-fno-jump-tables
ifneq (,$(filter gcc,$(CC)))
# The following flag helps GCC to eliminate more redundant computations in the interpret loop,
# saving some host instructions and improving performance.
# This flag is usually enabled by default at -O3,
# but we don't use -O3 because it enables some other flags that are not worth for the interpreter.
OPTFLAGS+=-fgcse-after-reload -fpredictive-commoning -fsplit-paths -ftree-partial-pre
else ifneq (,$(filter emcc,$(CC)))
OPTFLAGS+=-O3
endif
# Disable jump tables, because it degrades the instruction decoding performance in the interpret loop,
# since it generates a memory indirection that has a high cost in opcode switches.
OPTFLAGS+=-fno-jump-tables
endif

# Link time optimizations
Expand Down Expand Up @@ -268,9 +270,9 @@ EMPTY:=
SPACE:=$(EMPTY) $(EMPTY)
CLANG_TIDY_HEADER_FILTER=$(PWD)/($(subst $(SPACE),|,$(LINTER_HEADERS)))

CXXFLAGS+=$(OPTFLAGS) -std=c++17 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS) $(MYCFLAGS)
CFLAGS+=$(OPTFLAGS) -std=gnu99 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS) $(MYCFLAGS)
LDFLAGS+=$(UBFLAGS) $(MYLDFLAGS)
CXXFLAGS+=$(OPTFLAGS) -std=c++17 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS)
CFLAGS+=$(OPTFLAGS) -std=gnu99 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS)
LDFLAGS+=$(UBFLAGS)

COVERAGE_WORKLOAD=\
dhrystone 100; \
Expand All @@ -295,6 +297,10 @@ $(error invalid value for COVERAGE_TOOLCHAIN: $(COVERAGE_TOOLCHAIN))
endif
endif

CXXFLAGS+=$(MYCXXFLAGS)
CFLAGS+=$(MYCFLAGS)
LDFLAGS+=$(MYLDFLAGS)

all: luacartesi grpc hash c-api jsonrpc-remote-cartesi-machine

.PHONY: all generate use clean test lint format format-lua check-format check-format-lua luacartesi grpc hash c-api compile_flags.txt
Expand Down Expand Up @@ -501,7 +507,7 @@ $(PROFILE_DATA_Darwin):

use: CXXFLAGS += -fprofile-use -Wno-missing-profile
use: LDFLAGS += -fprofile-use
use: $(PROFILE_DATA_$(UNAME)) luacartesi
use: $(PROFILE_DATA_$(TARGET_OS)) luacartesi

compile_flags.txt:
@echo "$(CXXFLAGS)" "-xc++" | sed -e $$'s/ \{1,\}/\\\n/g' | grep -v "MMD" > $@
Expand Down Expand Up @@ -757,7 +763,7 @@ clean-objs:
@rm -f *.o *.d

clean-libcartesi: clean-objs
@rm -f $(LIBCARTESI) $(LIBCARTESI_GRPC) cartesi.so cartesi/grpc.so cartesi/jsonrpc.so
@rm -f *.so *.a cartesi/*.so cartesi/*.a

clean-executables:
@rm -f jsonrpc-remote-cartesi-machine remote-cartesi-machine merkle-tree-hash
Expand Down
1 change: 1 addition & 0 deletions src/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5504,6 +5504,7 @@ static FORCE_INLINE fetch_status fetch_insn(STATE_ACCESS &a, uint64_t &pc, uint3
/// \brief Checks that false brk is consistent with rest of state
template <typename STATE_ACCESS>
static void assert_no_brk(STATE_ACCESS &a) {
(void) a;
assert(get_pending_irq_mask(a) == 0); // LCOV_EXCL_LINE
assert(a.read_iflags_X() == 0); // LCOV_EXCL_LINE
assert(a.read_iflags_Y() == 0); // LCOV_EXCL_LINE
Expand Down

0 comments on commit 9df439f

Please sign in to comment.