Skip to content

Commit

Permalink
chore: compile in release mode with debug info by default (for packag…
Browse files Browse the repository at this point in the history
…ing)
  • Loading branch information
edubart committed Aug 6, 2024
1 parent e6bf24e commit 9e05d27
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 41 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -63,7 +63,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -85,7 +85,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -166,7 +166,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -182,7 +182,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
- name: Simple boot inside the docker image
Expand Down Expand Up @@ -255,7 +255,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
- name: Test microarchitecture interpreter
Expand Down Expand Up @@ -319,7 +319,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -337,7 +337,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -353,7 +353,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
- name: Simple boot inside the docker image
Expand Down Expand Up @@ -413,7 +413,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
- name: Test microarchitecture interpreter
Expand Down Expand Up @@ -472,7 +472,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand Down Expand Up @@ -536,7 +536,7 @@ jobs:
cache-to: type=gha,mode=max,scope=debian-coverage
build-args: |
GIT_COMMIT=${GITHUB_SHA}
RELEASE=no
DEBUG=yes
COVERAGE=yes
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand All @@ -553,7 +553,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=no
DEBUG=yes
COVERAGE=yes
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
Expand Down Expand Up @@ -617,7 +617,7 @@ jobs:
cache-from: type=gha,scope=debian-sanitize
cache-to: type=gha,mode=max,scope=debian-sanitize
build-args: |
RELEASE=no
DEBUG=yes
GIT_COMMIT=${GITHUB_SHA}
SANITIZE=yes
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
Expand All @@ -635,7 +635,7 @@ jobs:
push: false
load: true
build-args: |
RELEASE=no
DEBUG=yes
SANITIZE=yes
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
Expand Down Expand Up @@ -697,7 +697,7 @@ jobs:
cache-from: type=gha,scope=debian
cache-to: type=gha,mode=max,scope=debian
build-args: |
RELEASE=${{ (startsWith(github.ref, 'refs/tags/v') && 'yes' || 'no') }}
DEBUG=${{ (startsWith(github.ref, 'refs/tags/v') && 'no' || 'yes') }}
GIT_COMMIT=${GITHUB_SHA}
MACHINE_EMULATOR_VERSION=${{ env.MACHINE_EMULATOR_VERSION }}
project: ${{ vars.DEPOT_PROJECT }}
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM --platform=$TARGETPLATFORM cartesi/toolchain:0.17.0-rv64ima-lp64 as linux-env
ARG GIT_COMMIT=""
ARG RELEASE=no
ARG DEBUG=no
ARG COVERAGE=no
ARG SANITIZE=no

Expand Down Expand Up @@ -36,7 +36,7 @@ COPY third-party third-party
FROM --platform=$TARGETPLATFORM dep-builder as builder

COPY . .
RUN make -j$(nproc) git_commit=$GIT_COMMIT release=$RELEASE coverage=$COVERAGE sanitize=$SANITIZE
RUN make -j$(nproc) git_commit=$GIT_COMMIT debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE

FROM --platform=$TARGETPLATFORM builder as debian-packager
ARG MACHINE_EMULATOR_VERSION=0.0.0
Expand Down
21 changes: 16 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,21 @@ DOCKER_PLATFORM=--platform $(BUILD_PLATFORM)
endif

# Code instrumentation
debug?=no
relwithdebinfo?=no
release?=no
sanitize?=no
coverage?=no

# If not build type is chosen, set the default to release with debug information,
# so the emulator is packaged correctly by default.
ifeq (,$(filter yes,$(relwithdebinfo) $(release) $(debug) $(sanitize)))
relwithdebinfo=yes
endif

export sanitize
export debug
export relwithdebinfo
export release
export coverage

Expand Down Expand Up @@ -239,22 +250,22 @@ $(SRCDIR)/machine-c-version.h:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) machine-c-version.h

build-emulator-builder-image:
docker build $(DOCKER_PLATFORM) --build-arg RELEASE=$(release) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --target builder -t cartesi/machine-emulator:builder -f Dockerfile .
docker build $(DOCKER_PLATFORM) --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --target builder -t cartesi/machine-emulator:builder -f Dockerfile .

build-emulator-linux-env-image build-linux-env:
docker build $(DOCKER_PLATFORM) --target linux-env -t cartesi/machine-emulator:linux-env -f Dockerfile .

build-emulator-image:
docker build $(DOCKER_PLATFORM) --build-arg RELEASE=$(release) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) -t cartesi/machine-emulator:$(TAG) -f Dockerfile .
docker build $(DOCKER_PLATFORM) --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) -t cartesi/machine-emulator:$(TAG) -f Dockerfile .

build-emulator-tests-image: build-emulator-builder-image build-emulator-image
docker build $(DOCKER_PLATFORM) --build-arg RELEASE=$(release) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) --build-arg TAG=$(TAG) -t cartesi/machine-emulator:tests -f tests/Dockerfile .
docker build $(DOCKER_PLATFORM) --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) --build-arg TAG=$(TAG) -t cartesi/machine-emulator:tests -f tests/Dockerfile .

build-emulator-tests-builder-image: build-emulator-builder-image
docker build $(DOCKER_PLATFORM) --target tests-builder --build-arg RELEASE=$(release) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) --build-arg TAG=$(TAG) -t cartesi/machine-emulator:tests-builder -f tests/Dockerfile .
docker build $(DOCKER_PLATFORM) --target tests-builder --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) --build-arg TAG=$(TAG) -t cartesi/machine-emulator:tests-builder -f tests/Dockerfile .

build-debian-package:
docker build $(DOCKER_PLATFORM) --target debian-packager --build-arg RELEASE=$(release) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) -t $(DEBIAN_IMG) -f Dockerfile .
docker build $(DOCKER_PLATFORM) --target debian-packager --build-arg DEBUG=$(debug) --build-arg COVERAGE=$(coverage) --build-arg SANITIZE=$(sanitize) --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) -t $(DEBIAN_IMG) -f Dockerfile .

build-tests-debian-packages: build-emulator-builder-image
docker build $(DOCKER_PLATFORM) --target tests-debian-packager --build-arg MACHINE_EMULATOR_VERSION=$(MACHINE_EMULATOR_VERSION) --build-arg TAG=$(TAG) -t cartesi/machine-emulator:tests-debian-packager -f tests/Dockerfile .
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Both `libcartesi` and `libcartes_jsonrpc` C libraries can be compiled in standal

```bash
make bundle-boost
make -C src release=yes libcartesi.a libcartesi_jsonrpc.a libcartesi.so libcartesi_jsonrpc.so
make -C src libcartesi.a libcartesi_jsonrpc.a libcartesi.so libcartesi_jsonrpc.so
```

The `.a` and `.so` files will be available in `src` directory, you can use any of them to link your application.
Expand All @@ -155,17 +155,17 @@ You can even use other toolchains to cross compile targeting other platforms:

```bash
# Target WASM with Emscripten toolchain
make -C src release=yes \
make -C src \
CC=emcc CXX=em++ AR="emar rcs" \
libcartesi.a

# Target WASM with WASI SDK toolchain
make -C src release=yes \
make -C src \
CC=/opt/wasi-sdk/bin/clang CXX=/opt/wasi-sdk/bin/clang++ AR="/opt/wasi-sdk/bin/llvm-ar rcs" \
libcartesi.a

# Target Windows with mingw-w64 toolchain
make -C src release=yes \
make -C src \
CC=x86_64-w64-mingw32-gcc \
CXX=x86_64-w64-mingw32-g++ \
AR="x86_64-w64-mingw32-ar rcs" \
Expand Down
27 changes: 19 additions & 8 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,36 +175,44 @@ DEFS+=-DDUMP_HIST
#DEFS+=-DDUMP_COUNTERS
endif

# By default we compile in release with debug information,
# so the emulator is packaged correctly by default.
ifeq (,$(filter yes,$(relwithdebinfo) $(release) $(debug) $(sanitize)))
relwithdebinfo=yes
endif

ifeq ($(relwithdebinfo),yes)
OPTFLAGS+=-O2 -g
INTERPRET_CXXFLAGS+=-DNDEBUG # disable asserts only for interpret.cpp
else ifeq ($(release),yes)
OPTFLAGS+=-O2
DEFS+=-DNDEBUG # disable all asserts
else ifeq ($(debug),yes)
OPTFLAGS+=-Og -g
else ifeq ($(sanitize),yes)
OPTFLAGS+=-Og -g
else
OPTFLAGS+=-O2 -g
OPTFLAGS+=-O1 -g
endif

# Git commit hash (for releases)
ifneq ($(git_commit),)
DEFS+=-DGIT_COMMIT='"$(git_commit)"'
endif

# Optimization flags
# The SHA3 is third party library we always want to compile with O3
SHA3_CFLAGS=-O3

# Optimization flags for the interpreter
ifneq (,$(filter yes,$(relwithdebinfo) $(release)))
DEFS+=-DNDEBUG
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
INTERPRET_CXXFLAGS+=-fgcse-after-reload -fpredictive-commoning -fsplit-paths -ftree-partial-pre
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
INTERPRET_CXXFLAGS+=-fno-jump-tables
endif

# Link time optimizations
Expand Down Expand Up @@ -526,14 +534,17 @@ jsonrpc-discover.cpp: jsonrpc-discover.json
@touch $@

sha3.o: ../third-party/tiny_sha3/sha3.c
$(CC) $(CFLAGS) -c -o $@ $<
$(CC) $(CFLAGS) $(SHA3_CFLAGS) -c -o $@ $<

uarch-pristine-ram.o: $(UARCH_PRISTINE_RAM_C)
$(CC) $(CFLAGS) -c -o $@ $<

uarch-pristine-hash.o: $(UARCH_PRISTINE_HASH_C)
$(CC) $(CFLAGS) -c -o $@ $<

interpret.o: interpret.cpp machine-c-version.h
$(CXX) $(CXXFLAGS) $(INTERPRET_CXXFLAGS) -c -o $@ $<

%.o: %.cpp machine-c-version.h
$(CXX) $(CXXFLAGS) -c -o $@ $<

Expand Down
8 changes: 4 additions & 4 deletions tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
ARG TAG=devel
FROM --platform=$TARGETPLATFORM cartesi/toolchain:0.17.0 as machine-tests-builder
ARG RELEASE=no
ARG DEBUG=no
ARG COVERAGE=no
ARG SANITIZE=no

COPY . /usr/src/emulator

WORKDIR /usr/src/emulator

RUN make -j$(nproc) build-tests-machine release=$RELEASE coverage=$COVERAGE sanitize=$SANITIZE
RUN make -j$(nproc) build-tests-machine debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE

FROM --platform=$TARGETPLATFORM cartesi/machine-emulator:builder as tests-builder
ARG RELEASE=no
ARG DEBUG=no
ARG COVERAGE=no
ARG SANITIZE=no

COPY --from=machine-tests-builder /usr/src/emulator/tests/build/machine /usr/src/emulator/tests/build/machine

RUN make -j$(nproc) build-tests-misc build-tests-uarch build-tests-images release=$RELEASE coverage=$COVERAGE sanitize=$SANITIZE
RUN make -j$(nproc) build-tests-misc build-tests-uarch build-tests-images debug=$DEBUG coverage=$COVERAGE sanitize=$SANITIZE

FROM tests-builder as tests-debian-packager
ARG MACHINE_EMULATOR_VERSION=0.0.0
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ test-uarch:
$(LUA) ./lua/cartesi-machine-tests.lua --jobs=$(NUM_JOBS) run_uarch

test-uarch-compare:
$(LUA) ./lua/cartesi-machine-tests.lua --test="^rv64ui.*$$" --jobs=$(NUM_JOBS) run_host_and_uarch
$(LUA) ./lua/cartesi-machine-tests.lua --test="^rv64ui.*$$" --concurrency=update_merkle_tree:1 --jobs=$(NUM_JOBS) run_host_and_uarch

test-uarch-rv64ui:
$(LUA) ./lua/uarch-riscv-tests.lua run
Expand Down

0 comments on commit 9e05d27

Please sign in to comment.