forked from cartesi/machine-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
333 lines (272 loc) · 14 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright Cartesi and individual authors (see AUTHORS)
# SPDX-License-Identifier: LGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
#
TARGET_OS?=$(shell uname)
export TARGET_OS
# Install settings
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)_$(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
ifeq ($(TARGET_OS),Darwin)
LIBCARTESI=libcartesi.dylib
LIBCARTESI_GRPC=libcartesi_grpc.dylib
LIBCARTESI_JSONRPC=libcartesi_jsonrpc.dylib
LIBCARTESI_SO=libcartesi-$(MACHINE_EMULATOR_SO_VERSION).dylib
LIBCARTESI_SO_GRPC=libcartesi_grpc-$(MACHINE_EMULATOR_SO_VERSION).dylib
LIBCARTESI_SO_JSONRPC=libcartesi_jsonrpc-$(MACHINE_EMULATOR_SO_VERSION).dylib
else
LIBCARTESI=libcartesi.so
LIBCARTESI_GRPC=libcartesi_grpc.so
LIBCARTESI_JSONRPC=libcartesi_jsonrpc.so
LIBCARTESI_SO=libcartesi-$(MACHINE_EMULATOR_SO_VERSION).so
LIBCARTESI_SO_GRPC=libcartesi_grpc-$(MACHINE_EMULATOR_SO_VERSION).so
LIBCARTESI_SO_JSONRPC=libcartesi_jsonrpc-$(MACHINE_EMULATOR_SO_VERSION).so
endif
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_FILE= install -m0644
INSTALL_EXEC= install -m0755
INSTALL_DIR= cp -RP
SYMLINK= ln -sf
CHMOD_EXEC= chmod 0755
STRIP= strip
STRIP_BINARY= $(STRIP)
STRIP_SHARED= $(STRIP) -S -x
STRIP_STATIC= $(STRIP) -S
EMU_TO_BIN= src/jsonrpc-remote-cartesi-machine src/remote-cartesi-machine src/merkle-tree-hash
EMU_TEST_TO_BIN= src/tests/test-merkle-tree-hash src/tests/test-machine-c-api
EMU_TO_LIB= src/$(LIBCARTESI_SO) src/$(LIBCARTESI_SO_GRPC) src/$(LIBCARTESI_SO_JSONRPC)
EMU_TO_LIB_A= src/libcartesi.a src/libcartesi_grpc.a src/libcartesi_jsonrpc.a
EMU_LUA_TO_BIN= src/cartesi-machine.lua src/cartesi-machine-stored-hash.lua src/rollup-memory-range.lua
EMU_LUA_TEST_TO_BIN= src/cartesi-machine-tests.lua src/uarch-riscv-tests.lua
EMU_TO_LUA_PATH= src/cartesi/util.lua src/cartesi/proof.lua src/cartesi/gdbstub.lua
EMU_TO_LUA_CPATH= src/cartesi.so
EMU_TO_LUA_CARTESI_CPATH= src/cartesi/grpc.so src/cartesi/jsonrpc.so
EMU_TO_INC= $(addprefix src/,jsonrpc-machine-c-api.h grpc-machine-c-api.h machine-c-api.h \
machine-c-defines.h machine-c-version.h pma-defines.h rtc-defines.h htif-defines.h uarch-defines.h)
UARCH_TO_SHARE= uarch-ram.bin
MONGOOSE_VERSION=7.12
# Build settings
DEPDIR = third-party
SRCDIR = $(abspath src)
DOWNLOADDIR = $(DEPDIR)/downloads
DEPDIRS = third-party/mongoose-$(MONGOOSE_VERSION)
SUBCLEAN = $(addsuffix .clean,$(SRCDIR) uarch third-party/riscv-arch-tests)
COREPROTO = lib/grpc-interfaces/core.proto
# Docker image tag
TAG ?= devel
DEBIAN_IMG ?= cartesi/machine-emulator:$(TAG).deb
# Docker image platform
BUILD_PLATFORM ?=
ifneq ($(BUILD_PLATFORM),)
DOCKER_PLATFORM=--platform $(BUILD_PLATFORM)
endif
# Code instrumentation
release?=no
sanitize?=no
coverage?=no
export sanitize
export release
export coverage
# Mac OS X specific settings
ifeq ($(TARGET_OS),Darwin)
export CC = clang
export CXX = clang++
LIBRARY_PATH = "export DYLD_LIBRARY_PATH="
SHA1SUM=shasum
# Linux specific settings
else ifeq ($(TARGET_OS),Linux)
export CC=gcc
export CXX=g++
LIBRARY_PATH = "export LD_LIBRARY_PATH=$(SRCDIR)"
SHA1SUM=sha1sum
# Other system
else
export CC=gcc
export CXX=g++
SHA1SUM=sha1sum
endif
all: source-default
clean: $(SUBCLEAN)
depclean: clean
$(MAKE) -C third-party/riscv-arch-tests depclean
distclean:
rm -rf $(DOWNLOADDIR) $(DEPDIRS)
$(MAKE) -C third-party/riscv-arch-tests depclean
$(MAKE) clean
$(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_PATH) $(LUA_INSTALL_CPATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(INC_INSTALL_PATH) $(IMAGES_INSTALL_PATH) $(UARCH_INSTALL_PATH):
mkdir -m 0755 -p $@
env:
@echo $(LIBRARY_PATH)
@echo "export PATH='$(SRCDIR):${PATH}'"
@echo "export LUA_PATH_5_4='$(SRCDIR)/?.lua;$${LUA_PATH_5_4:-;}'"
@echo "export LUA_CPATH_5_4='$(SRCDIR)/?.so;$${LUA_CPATH_5_4:-;}'"
doc:
cd doc && doxygen Doxyfile
help:
@echo 'Cleaning targets:'
@echo ' clean - clean the src/ artifacts'
@echo ' depclean - clean + dependencies'
@echo ' distclean - depclean + profile information and downloads'
@echo 'Docker targets:'
@echo ' build-debian-image - Build the machine-emulator debian based docker image'
@echo ' build-debian-package - BUild the cartesi-machine.deb package from image'
@echo 'Generic targets:'
@echo '* all - build the src/ code. To build from a clean clone, run: make submodules downloads dep all'
@echo ' doc - build the doxygen documentation (requires doxygen to be installed)'
@echo ' copy - copy generated artifacts out of a docker image'
@echo ' uarch - build microarchitecture'
@echo ' uarch-with-linux-env - build microarchitecture using the linux-env docker image'
@echo ' uarch-tests - build and run microarchitecture rv64i instruction tests'
@echo ' uarch-tests-with-linux-env - build and run microarchitecture rv64i instruction tests using the linux-env docker image'
checksum:
@cd $(DEPDIR) && $(SHA1SUM) -c shasumfile
$(DOWNLOADDIR):
@mkdir -p $(DOWNLOADDIR)
@wget -nc -i $(DEPDIR)/dependencies -P $(DOWNLOADDIR)
$(MAKE) checksum
downloads: $(DOWNLOADDIR)
third-party/downloads/$(MONGOOSE_VERSION).tar.gz: | downloads
third-party/mongoose-$(MONGOOSE_VERSION): third-party/downloads/$(MONGOOSE_VERSION).tar.gz
tar -C third-party -xzf $< mongoose-$(MONGOOSE_VERSION)/mongoose.c mongoose-$(MONGOOSE_VERSION)/mongoose.h
bundle-boost: third-party/downloads/boost
third-party/downloads/boost: | downloads
wget -O third-party/downloads/boost_1_81_0.tar.gz https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.gz
tar -C third-party/downloads -xzf third-party/downloads/boost_1_81_0.tar.gz boost_1_81_0/boost
mv third-party/downloads/boost_1_81_0/boost third-party/downloads/boost
rm -rf third-party/downloads/boost_1_81_0.tar.gz third-party/downloads/boost_1_81_0
dep: $(DEPDIRS)
submodules:
git submodule update --init --recursive
$(COREPROTO):
$(info grpc-interfaces submodule not initialized!)
@exit 1
grpc: | $(COREPROTO)
hash luacartesi grpc test lint coverage-report check-format format check-format-lua check-lua format-lua:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) $@
libcartesi libcartesi_grpc libcartesi_jsonrpc libcartesi.a libcartesi_grpc.a libcartesi_jsonrpc.a libcartesi.so libcartesi_grpc.so libcartesi_jsonrpc.so:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) $@
version:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -sC $(SRCDIR) $@
test-%:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) $@
uarch-tests:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C third-party/riscv-arch-tests
run-uarch-tests:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C third-party/riscv-arch-tests run
source-default:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR)
uarch: $(SRCDIR)/machine-c-version.h
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C uarch
$(SRCDIR)/machine-c-version.h:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) machine-c-version.h
$(SUBCLEAN): %.clean:
$(MAKE) -C $* clean
build-linux-env:
docker build $(DOCKER_PLATFORM) --target linux-env -t cartesi/linux-env:$(TAG) -f Dockerfile .
build-debian-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 .
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 .
copy:
ID=`docker create $(DOCKER_PLATFORM) $(DEBIAN_IMG)` && \
docker cp $$ID:/usr/src/emulator/$(DEB_FILENAME) . && \
docker cp $$ID:/usr/src/emulator/uarch/uarch-ram.bin . && \
docker rm $$ID
check-linux-env:
@if docker images $(DOCKER_PLATFORM) -q cartesi/linux-env:$(TAG)$(image_name) 2>/dev/null | grep -q .; then \
echo "Docker image cartesi/linux-env:$(TAG) exists"; \
else \
echo "Docker image cartesi/linux-env:$(TAG) does not exist. Creating:"; \
$(MAKE) build-linux-env; \
fi
linux-env: check-linux-env
@docker run $(DOCKER_PLATFORM) --hostname linux-env -it --rm \
-e USER=$$(id -u -n) \
-e GROUP=$$(id -g -n) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
-v `pwd`:/opt/cartesi/machine-emulator \
-w /opt/cartesi/machine-emulator \
cartesi/linux-env:$(TAG) /bin/bash
linux-env-exec: check-linux-env
@docker run --hostname linux-env --rm \
-e USER=$$(id -u -n) \
-e GROUP=$$(id -g -n) \
-e UID=$$(id -u) \
-e GID=$$(id -g) \
-v `pwd`:/opt/cartesi/machine-emulator \
-w /opt/cartesi/machine-emulator \
cartesi/linux-env:$(TAG) /bin/bash -c "$(CONTAINER_COMMAND)"
uarch-with-linux-env:
@$(MAKE) linux-env-exec CONTAINER_COMMAND="make uarch"
uarch-tests-with-linux-env:
@$(MAKE) linux-env-exec CONTAINER_COMMAND="make uarch-tests"
install-headers: $(INC_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_TO_INC) $(INC_INSTALL_PATH)
install-emulator: $(BIN_INSTALL_PATH) $(LIB_INSTALL_PATH) $(LUA_INSTALL_CPATH)/cartesi $(LUA_INSTALL_PATH)/cartesi $(IMAGES_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_BIN) $(BIN_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_LIB) $(LIB_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_TO_LIB_A) $(LIB_INSTALL_PATH)
$(INSTALL_FILE) $(EMU_LUA_TO_BIN) $(LUA_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TO_LUA_CPATH) $(LUA_INSTALL_CPATH)
$(INSTALL_EXEC) $(EMU_TO_LUA_CARTESI_CPATH) $(LUA_INSTALL_CPATH)/cartesi
$(INSTALL_FILE) $(EMU_TO_LUA_PATH) $(LUA_INSTALL_PATH)/cartesi
cat tools/template/cartesi-machine.template | sed 's|ARG_LUA_PATH|$(LUA_RUNTIME_PATH)/?.lua|g;s|ARG_LUA_CPATH|$(LUA_RUNTIME_CPATH)/?.so|g;s|ARG_INSTALL_PATH|$(IMAGES_RUNTIME_PATH)|g;s|ARG_LUA_RUNTIME_PATH|$(LUA_RUNTIME_PATH)|g' > $(BIN_INSTALL_PATH)/cartesi-machine
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
$(CHMOD_EXEC) $(BIN_INSTALL_PATH)/cartesi-machine $(BIN_INSTALL_PATH)/cartesi-machine-stored-hash $(BIN_INSTALL_PATH)/rollup-memory-range
$(SYMLINK) $(LIBCARTESI_SO) $(LIB_INSTALL_PATH)/$(LIBCARTESI)
$(SYMLINK) $(LIBCARTESI_SO_GRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_GRPC)
$(SYMLINK) $(LIBCARTESI_SO_JSONRPC) $(LIB_INSTALL_PATH)/$(LIBCARTESI_JSONRPC)
$(INSTALL_DIR) tools/gdb $(SHARE_INSTALL_PATH)/gdb
install-strip: install-emulator
$(STRIP_BINARY) $(subst src/,$(BIN_INSTALL_PATH)/,$(EMU_TO_BIN))
$(STRIP_SHARED) $(subst src/,$(LUA_INSTALL_CPATH)/,$(EMU_TO_LUA_CPATH))
$(STRIP_SHARED) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB))
$(STRIP_STATIC) $(subst src/,$(LIB_INSTALL_PATH)/,$(EMU_TO_LIB_A))
install: install-strip install-headers
install-tests: install
$(INSTALL_FILE) $(EMU_LUA_TEST_TO_BIN) $(LUA_INSTALL_PATH)
$(INSTALL_EXEC) $(EMU_TEST_TO_BIN) $(BIN_INSTALL_PATH)
cat tools/template/cartesi-machine-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)/cartesi-machine-tests
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
$(CHMOD_EXEC) $(BIN_INSTALL_PATH)/cartesi-machine-tests $(BIN_INSTALL_PATH)/uarch-riscv-tests
install-uarch: install $(UARCH_INSTALL_PATH)
$(INSTALL_FILE) uarch/$(UARCH_TO_SHARE) $(UARCH_INSTALL_PATH)
debian-package: install
mkdir -p $(DESTDIR)/DEBIAN $(DOC_INSTALL_PATH)
$(INSTALL_FILE) COPYING $(DOC_INSTALL_PATH)/copyright
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)
.PHONY: help all submodules doc clean distclean downloads checksum src test luacartesi grpc hash uarch \
$(SUBDIRS) $(SUBCLEAN)