Skip to content

Commit

Permalink
refactor: update mongoose to 7.12 and refactor how it's bundled
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 30, 2023
1 parent d4d8165 commit 90bd22d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build
third-party/cryptopp-CRYPTOPP_7_0_0
third-party/downloads
third-party/grpc
third-party/mongoose-7.9
third-party/mongoose-*
src/remote-cartesi-machine-proxy
src/remote-cartesi-machine
src/jsonrpc-remote-cartesi-machine
Expand Down
59 changes: 25 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,15 @@ EMU_TO_INC= $(addprefix lib/machine-emulator-defines/,pma-defines.h rtc-defines.
$(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

# Build settings
DEPDIR := third-party
SRCDIR := $(abspath src)
BUILDBASE := $(abspath build)
BUILDDIR = $(BUILDBASE)/$(UNAME)_$(shell uname -m)
DOWNLOADDIR := $(DEPDIR)/downloads
DEPDIRS := third-party/mongoose-$(MONGOOSE_VERSION)
SUBCLEAN := $(addsuffix .clean,$(SRCDIR) uarch third-party/riscv-arch-tests)
DEPDIRS := $(addprefix $(DEPDIR)/,mongoose-7.9)
DEPCLEAN := $(addsuffix .clean,$(DEPDIRS))
COREPROTO := lib/grpc-interfaces/core.proto
LUASOCKET_VERSION ?= 5b18e475f38fcf28429b1cc4b17baee3b9793a62

# Docker image tag
TAG ?= devel
Expand All @@ -104,44 +102,37 @@ LUA_PLAT ?= macosx
export CC = clang
export CXX = clang++
LUACC = "CC=$(CXX)"
LIBRARY_PATH := "export DYLD_LIBRARY_PATH=$(BUILDDIR)/lib"
LUAMYLIBS = "MYLIBS=-L/opt/local/lib/libomp -L/usr/local/opt/llvm/lib -lomp"
LIBRARY_PATH := "export DYLD_LIBRARY_PATH="

# Linux specific settings
else ifeq ($(UNAME),Linux)
LUA_PLAT ?= linux
LIBRARY_PATH := "export LD_LIBRARY_PATH=$(BUILDDIR)/lib:$(SRCDIR)"
LIBRARY_PATH := "export LD_LIBRARY_PATH=$(SRCDIR)"
LUACC = "CC=g++"
LUAMYLIBS = "MYLIBS=\"-lgomp\""
# Unknown platform
else
LUA_PLAT ?= none
INSTALL_PLAT=
endif

# Check if some binary dependencies already exists on build directory to skip
# downloading and building them.
DEPBINS := $(addprefix $(BUILDDIR)/,include/mongoose.h)

all: source-default

clean: $(SUBCLEAN)

depclean: $(DEPCLEAN) clean
rm -rf $(BUILDDIR)
depclean: clean
$(MAKE) -C third-party/riscv-arch-tests depclean

distclean:
rm -rf $(BUILDBASE) $(DOWNLOADDIR) $(DEPDIRS)
rm -rf $(DOWNLOADDIR) $(DEPDIRS)
$(MAKE) -C third-party/riscv-arch-tests depclean
$(MAKE) clean

$(BUILDDIR) $(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):
$(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):$(BUILDDIR)/bin:${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:-;}'"

Expand All @@ -165,15 +156,21 @@ help:
@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) && shasum -c shasumfile

$(DOWNLOADDIR):
@mkdir -p $(DOWNLOADDIR)
@wget -nc -i $(DEPDIR)/dependencies -P $(DOWNLOADDIR)
@cd $(DEPDIR) && shasum -c shasumfile
$(MAKE) checksum

downloads: $(DOWNLOADDIR)

dep: $(DEPBINS)
@rm -f $(BUILDDIR)/lib/*.a
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

dep: $(DEPDIRS)

submodules:
git submodule update --init --recursive
Expand Down Expand Up @@ -205,16 +202,10 @@ source-default:
uarch: $(SRCDIR)/machine-c-version.h
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C uarch

$(BUILDDIR)/include/mongoose.h $(BUILDDIR)/lib/libmongoose.a: | $(BUILDDIR) $(DOWNLOADDIR)
mkdir -p $(BUILDDIR)/include $(BUILDDIR)/lib
if [ ! -d $(DEPDIR)/mongoose-7.9 ]; then tar -xzf $(DOWNLOADDIR)/7.9.tar.gz -C /tmp/; mv /tmp/mongoose-7.9 $(DEPDIR)/; fi
cp $(DEPDIR)/mongoose-7.9/mongoose.c $(BUILDDIR)/lib
cp $(DEPDIR)/mongoose-7.9/mongoose.h $(BUILDDIR)/include

$(SRCDIR)/machine-c-version.h:
@eval $$($(MAKE) -s --no-print-directory env); $(MAKE) -C $(SRCDIR) machine-c-version.h

$(SUBCLEAN) $(DEPCLEAN): %.clean:
$(SUBCLEAN): %.clean:
$(MAKE) -C $* clean

build-linux-env:
Expand Down Expand Up @@ -267,12 +258,12 @@ uarch-tests-with-linux-env:
@$(MAKE) linux-env-exec CONTAINER_COMMAND="make uarch-tests"

install-Darwin:
install_name_tool -delete_rpath $(BUILDDIR)/lib -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi.so
install_name_tool -delete_rpath $(BUILDDIR)/lib -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi/grpc.so
install_name_tool -delete_rpath $(BUILDDIR)/lib -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $(LUA_INSTALL_CPATH)/cartesi/jsonrpc.so
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 $(BUILDDIR)/lib -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $$x ;\
install_name_tool -delete_rpath $(SRCDIR) -add_rpath $(LIB_RUNTIME_PATH) $$x ;\
done

install-Linux:
Expand Down Expand Up @@ -325,5 +316,5 @@ debian-package: install

.SECONDARY: $(DOWNLOADDIR) $(DEPDIRS) $(COREPROTO)

.PHONY: help all submodules doc clean distclean downloads src test luacartesi grpc hash uarch \
$(SUBDIRS) $(SUBCLEAN) $(DEPCLEAN)
.PHONY: help all submodules doc clean distclean downloads checksum src test luacartesi grpc hash uarch \
$(SUBDIRS) $(SUBCLEAN)
29 changes: 14 additions & 15 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ EMULATOR_VERSION_LABEL=

UNAME:=$(shell uname)

BUILDDIR ?= $(abspath ../build/$(UNAME)_$(shell uname -m))

LUA_INC:=$(shell pkg-config --cflags-only-I lua5.4)
LUA_LIB:=-llua5.4
LUA_BIN?=lua5.4
Expand Down Expand Up @@ -60,7 +58,7 @@ ifeq ($(UNAME),Darwin)
ifeq ($(MACOSX_DEPLOYMENT_TARGET),)
export MACOSX_DEPLOYMENT_TARGET := $(shell sw_vers -productVersion | sed -r "s/([[:digit:]]+)\.([[:digit:]]+)\..+/\1.\2.0/")
endif
SOLDFLAGS_Darwin+=-Wl,-rpath,$(BUILDDIR)/lib -Wl,-rpath,$(CURDIR)
SOLDFLAGS_Darwin+=-Wl,-rpath,$(CURDIR)
# Homebrew installation
ifneq (,$(shell which brew))
BREW_PREFIX := $(shell brew --prefix)
Expand Down Expand Up @@ -91,14 +89,14 @@ BOOST_FILESYSTEM_LIB_Darwin:=$(BOOST_LIB_DIR_Darwin) -lboost_system-mt -lboost_f
BOOST_PROCESS_LIB_Darwin:=-lpthread
LIBCARTESI_Darwin=libcartesi-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR).dylib
LIBCARTESI_LDFLAGS_Darwin=-dynamiclib -undefined dynamic_lookup -install_name '@rpath/$(LIBCARTESI_Darwin)'
LIBCARTESI_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(BUILDDIR)/lib -Wl,-rpath,$(CURDIR)
LIBCARTESI_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(CURDIR)
LIBCARTESI_GRPC_Darwin=libcartesi_grpc-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR).dylib
LIBCARTESI_GRPC_LDFLAGS_Darwin=-dynamiclib -undefined dynamic_lookup -install_name '@rpath/$(LIBCARTESI_GRPC_Darwin)'
LIBCARTESI_GRPC_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(BUILDDIR)/lib -Wl,-rpath,$(CURDIR)
LIBCARTESI_GRPC_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(CURDIR)
LIBCARTESI_JSONRPC_Darwin=libcartesi_jsonrpc-$(EMULATOR_VERSION_MAJOR).$(EMULATOR_VERSION_MINOR).dylib
LIBCARTESI_JSONRPC_LDFLAGS_Darwin=-dynamiclib -undefined dynamic_lookup -install_name '@rpath/$(LIBCARTESI_JSONRPC_Darwin)'
LIBCARTESI_JSONRPC_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(BUILDDIR)/lib -Wl,-rpath,$(CURDIR)
CARTESI_EXECUTABLE_LDFLAGS_Darwin=-Wl,-rpath,$(BUILDDIR)/lib -Wl,-rpath,$(CURDIR)
LIBCARTESI_JSONRPC_TESTS_LDFLAGS_Darwin=-Wl,-rpath,$(CURDIR)
CARTESI_EXECUTABLE_LDFLAGS_Darwin=-Wl,-rpath,$(CURDIR)
PROFILE_DATA_Darwin=default.profdata

# Linux specific setup
Expand Down Expand Up @@ -136,7 +134,6 @@ BOOST_FILESYSTEM_LIB=$(BOOST_FILESYSTEM_LIB_$(UNAME))
BOOST_INC=$(BOOST_INC_$(UNAME))
CRYPTOPP_LIB=$(CRYPTOPP_LIB_$(UNAME))
CRYPTOPP_INC=$(CRYPTOPP_INC_$(UNAME))
MONGOOSE_INC=-I$(BUILDDIR)/include
GRPC_INC:=$(GRPC_INC_$(UNAME))
GRPC_LIB:=$(GRPC_LIB_$(UNAME))
PROTOBUF_INC:=$(PROTOBUF_INC_$(UNAME))
Expand Down Expand Up @@ -167,8 +164,12 @@ HASH_LIBS:=$(CRYPTOPP_LIB)
WARNS=-W -Wall -pedantic

# Place our include directories before the system's
INCS=-I../lib/machine-emulator-defines -I../third-party/llvm-flang-uint128 -I../third-party/downloads \
$(LUA_INC) $(CRYPTOPP_INC) $(MONGOOSE_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(UNAME))
INCS= \
-I../lib/machine-emulator-defines \
-I../third-party/llvm-flang-uint128 \
-I../third-party/downloads \
-I../third-party/mongoose-7.12 \
$(LUA_INC) $(CRYPTOPP_INC) $(BOOST_INC) $(PROTOBUF_INC) $(GRPC_INC) $(INCS_$(UNAME))

ifeq ($(dump),yes)
#DEFS+=-DDUMP_ILLEGAL_INSN_EXCEPTIONS
Expand Down Expand Up @@ -275,7 +276,7 @@ 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=c99 -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)

COVERAGE_WORKLOAD=\
Expand Down Expand Up @@ -375,7 +376,7 @@ LUACARTESI_JSONRPC_OBJS:= \
clua-jsonrpc-machine.o \
json-util.o \
base64.o \
$(BUILDDIR)/lib/mongoose.o \
mongoose-impl.o \
jsonrpc-machine-c-api.o \
jsonrpc-virtual-machine.o \
clua.o
Expand Down Expand Up @@ -586,7 +587,7 @@ JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \
slog.o \
jsonrpc-remote-machine.o \
jsonrpc-discover.o \
$(BUILDDIR)/lib/mongoose.o \
mongoose-impl.o \
pma-driver.o \
clint.o \
clint-factory.o \
Expand Down Expand Up @@ -684,8 +685,6 @@ grpc-interfaces: $(PROTO_SOURCES)
remote-cartesi-machine: $(REMOTE_CARTESI_MACHINE_OBJS)
$(CXX) $(LDFLAGS) $(CARTESI_EXECUTABLE_LDFLAGS) -o $@ $(REMOTE_CARTESI_MACHINE_OBJS) $(REMOTE_CARTESI_MACHINE_LIBS)

$(BUILDDIR)/lib/mongoose.o: CFLAGS := $(patsubst %-std=c99,%,$(CFLAGS))

jsonrpc-remote-cartesi-machine: $(JSONRPC_REMOTE_CARTESI_MACHINE_OBJS)
$(CXX) $(LDFLAGS) $(CARTESI_EXECUTABLE_LDFLAGS) -o $@ $(JSONRPC_REMOTE_CARTESI_MACHINE_OBJS) $(JSONRPC_REMOTE_CARTESI_MACHINE_LIBS)

Expand Down
2 changes: 0 additions & 2 deletions src/dtb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,12 @@ void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_le
}

// drives
int drive_index = 0;
for (const auto &f : c.flash_drive) {
fdt.begin_node_num("pmem", f.start);
fdt.prop_string("compatible", "pmem-region");
fdt.prop_u64_list<2>("reg", {f.start, f.length});
fdt.prop_empty("volatile");
fdt.end_node();
drive_index++;
}

// rollup
Expand Down
29 changes: 29 additions & 0 deletions src/mongoose-impl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/>.
//

// Mongoose log is disabled because it generates code using __FILE__ macro,
// which is reported as an error when packaging in some Linux distributions.
#ifdef NDEBUG
#define MG_ENABLE_LOG 0 // NOLINT(cppcoreguidelines-macro-usage)
#endif

// Disable some features we don't need
#define MG_ENABLE_MD5 0 // NOLINT(cppcoreguidelines-macro-usage)

// Allow mongoose to receive up to 12MB messages
#define MG_MAX_RECV_SIZE (12UL * 1024UL * 1024UL)

#include <mongoose.c> // NOLINT(bugprone-suspicious-include)
4 changes: 2 additions & 2 deletions third-party/dependencies
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://github.com/cesanta/mongoose/archive/refs/tags/7.9.tar.gz
https://github.com/nlohmann/json/releases/download/v3.11.2/json.hpp
https://github.com/cesanta/mongoose/archive/refs/tags/7.12.tar.gz
https://github.com/nlohmann/json/releases/download/v3.11.2/json.hpp
2 changes: 1 addition & 1 deletion third-party/shasumfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
6e60602441678ced30ec3bc6e7799b528b8521b9 downloads/7.9.tar.gz
fe939f7f6d7adb76480a0fe593e66b9a6cc9ad55 downloads/7.12.tar.gz
ce36f0cfc3ebba05259186ad6fd2233e14e0e698 downloads/json.hpp

0 comments on commit 90bd22d

Please sign in to comment.