Skip to content

Commit

Permalink
Makefile: Use SDL2 on Emscripten, use Full LTO on Clang, fixes
Browse files Browse the repository at this point in the history
- SDL2 allows to use proper BGRA surfaces on Emscripten, which are necessary for some guests, it also has less rendering overhead (But still significant compared to native)
- Full LTO on Clang provides faster & smaller executables, at least for RVVM (At the expense of slower compilation)
- Fix SDL2 build on Emscripten
  • Loading branch information
LekKit committed Jan 3, 2025
1 parent 32c309f commit 1c9efea
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions Makefile
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ ifeq ($(OS),windows)
override LDFLAGS += -static
override BIN_EXT := .exe
override LIB_EXT := .dll
USE_WIN32_GUI ?= 1
else

# Emscripten-specific build options
ifeq ($(OS),emscripten)
override CFLAGS += -pthread
override LDFLAGS += -s TOTAL_MEMORY=512MB -s PROXY_TO_PTHREAD
override LDFLAGS += -s TOTAL_MEMORY=512MB
override BIN_EXT := .html
override LIB_EXT := .so
USE_SDL ?= 1
USE_SDL ?= 2
USE_NET ?= 0
else

Expand Down Expand Up @@ -212,6 +213,10 @@ ifneq (,$(findstring linux,$(OS))$(findstring bsd,$(OS))$(findstring sunos,$(OS)
USE_X11 ?= 1
endif

ifeq ($(OS),haiku)
USE_HAIKU_GUI ?= 1
endif

ifneq (,$(findstring darwin,$(OS))$(findstring serenity,$(OS)))
# Enable SDL2 on Darwin, Serenity by default
USE_SDL ?= 2
Expand Down Expand Up @@ -290,9 +295,42 @@ override CFLAGS := -I$(SRCDIR) -DRVVM_VERSION=\"$(VERSION)\" $(CFLAGS)
# Select sources to compile
override SRC := $(wildcard $(SRCDIR)/*.c $(SRCDIR)/devices/*.c)

# Useflag sources
override SRC_USE_WIN32_GUI := $(SRCDIR)/devices/win32window.c
override SRC_CXX_USE_HAIKU_GUI := $(SRCDIR)/devices/haiku_window.cpp
override SRC_USE_X11 := $(SRCDIR)/devices/x11window_xlib.c
override SRC_USE_SDL := $(SRCDIR)/devices/sdl_window.c

override SRC_USE_TAP_LINUX := $(SRCDIR)/devices/tap_linux.c
override SRC_USE_NET := $(SRCDIR)/networking.c $(SRCDIR)/devices/tap_user.c
override SRC_USE_JIT := $(SRCDIR)/rvjit/rvjit.c $(SRCDIR)/rvjit/rvjit_emit.c
override SRC_USE_JNI := $(SRCDIR)/bindings/jni/rvvm_jni.c
override SRC_USE_RV64 := $(wildcard $(SRCDIR)/cpu/riscv64_*.c)
override SRC_USE_RV32 := $(wildcard $(SRCDIR)/cpu/riscv32_*.c)

override SDL_PKGCONF := sdl$(subst 1,,$(USE_SDL))

# Useflag CFLAGS
override CFLAGS_USE_SDL = $(shell pkg-config $(SDL_PKGCONF) --cflags-only-I $(NULL_STDERR))
override CFLAGS_USE_X11 = $(shell pkg-config x11 xext --cflags-only-I $(NULL_STDERR))
override CFLAGS_USE_DEBUG := -DDEBUG -g -fno-omit-frame-pointer
override CFLAGS_USE_DEBUG_FULL := -DDEBUG -Og -ggdb -fno-omit-frame-pointer
override CFLAGS_USE_LIB := -fPIC

# Useflag LDFLAGS
# Needed for floating-point functions like fetestexcept/feraiseexcept
override LDFLAGS_USE_FPU := -lm

# Useflag dependencies
override NEED_USE_X11 := USE_GUI
override NEED_USE_SDL := USE_GUI
override NEED_USE_JNI := USE_LIB

#
# OS-specific useflag CFLAGS/LDFLAGS
#

ifeq ($(OS),windows)
USE_WIN32_GUI ?= 1
ifneq (,$(findstring main, $(subst WinMain,main,$(shell $(CC) $(CFLAGS) $(LDFLAGS) -lgdi32 2>&1))))
# On WinCE it's not expected to link gdi32
override LDFLAGS_USE_WIN32_GUI := -lgdi32
Expand All @@ -306,7 +344,6 @@ endif
endif

ifeq ($(OS),haiku)
USE_HAIKU_GUI ?= 1
override LDFLAGS_USE_HAIKU_GUI := -lbe
override LDFLAGS_USE_NET := -lnetwork
endif
Expand All @@ -319,37 +356,11 @@ ifeq ($(OS),emscripten)
override CFLAGS_USE_SDL := -s USE_SDL=$(USE_SDL)
endif

# Useflag sources
override SRC_USE_WIN32_GUI := $(SRCDIR)/devices/win32window.c
override SRC_CXX_USE_HAIKU_GUI := $(SRCDIR)/devices/haiku_window.cpp
override SRC_USE_X11 := $(SRCDIR)/devices/x11window_xlib.c
override SRC_USE_SDL := $(SRCDIR)/devices/sdl_window.c

override SRC_USE_TAP_LINUX := $(SRCDIR)/devices/tap_linux.c
override SRC_USE_NET := $(SRCDIR)/networking.c $(SRCDIR)/devices/tap_user.c
override SRC_USE_JIT := $(SRCDIR)/rvjit/rvjit.c $(SRCDIR)/rvjit/rvjit_emit.c
override SRC_USE_JNI := $(SRCDIR)/bindings/jni/rvvm_jni.c
override SRC_USE_RV64 := $(wildcard $(SRCDIR)/cpu/riscv64_*.c)
override SRC_USE_RV32 := $(wildcard $(SRCDIR)/cpu/riscv32_*.c)

override SDL_PKGCONF := sdl$(subst 1,,$(USE_SDL))

# Useflag CFLAGS
override CFLAGS_USE_SDL = $(shell pkg-config $(SDL_PKGCONF) --cflags-only-I $(NULL_STDERR))
override CFLAGS_USE_X11 = $(shell pkg-config x11 xext --cflags-only-I $(NULL_STDERR))
override CFLAGS_USE_DEBUG := -DDEBUG -g -fno-omit-frame-pointer
override CFLAGS_USE_DEBUG_FULL := -DDEBUG -Og -ggdb -fno-omit-frame-pointer
override CFLAGS_USE_LIB := -fPIC

# Enable building the lib on lib or install target
ifeq (,$(findstring lib, $(MAKECMDGOALS))$(findstring install, $(MAKECMDGOALS)))
override USE_LIB := 0
endif

# Useflag LDFLAGS
# Needed for floating-point functions like fetestexcept/feraiseexcept
override LDFLAGS_USE_FPU := -lm

# Fix Nix & MacOS brew issues with non-standard library paths
ifneq (,$(findstring linux,$(OS))$(findstring darwin,$(OS)))
ifneq ($(USE_SDL),0)
Expand All @@ -367,11 +378,6 @@ override LDFLAGS_USE_X11 := -Wl,-rpath,$(X11_LIBDIR)
endif
endif

# Useflag dependencies
override NEED_USE_X11 := USE_GUI
override NEED_USE_SDL := USE_GUI
override NEED_USE_JNI := USE_LIB

# Check if RVJIT supports the target architecture
ifeq ($(USE_JIT),1)
ifeq (,$(findstring 86,$(ARCH))$(findstring arm,$(ARCH))$(findstring riscv,$(ARCH)))
Expand All @@ -384,6 +390,10 @@ ifeq ($(USE_TAP_LINUX),1)
$(info $(WARN_PREFIX) Linux TAP is deprecated in favor of USE_NET due to checksum issues)
endif

#
# Useflag automation magic
#

override USEFLAGS := $(sort $(filter USE_%,$(.VARIABLES)))
override SRC_CONDITIONAL := $(filter SRC_USE_%,$(.VARIABLES))

Expand Down Expand Up @@ -492,7 +502,7 @@ override CC_STD := -std=gnu11 -Wstrict-prototypes -Wold-style-definition
override CXX_STD := -std=gnu++11
endif

override CFLAGS := -O2 $(if $(LTO_SUPPORTED),-flto=thin) $(if $(CC_AT_LEAST_4_0),-frounding-math) -fvisibility=hidden -fno-math-errno \
override CFLAGS := -O2 $(if $(LTO_SUPPORTED),-flto) $(if $(CC_AT_LEAST_4_0),-frounding-math) -fvisibility=hidden -fno-math-errno \
$(WARN_OPTS) -Wno-unknown-warning-option -Wno-unsupported-floating-point-opt -Wno-ignored-optimization-argument \
-Wno-missing-braces -Wno-missing-field-initializers -Wno-ignored-pragmas -Wno-atomic-alignment $(CFLAGS)

Expand Down

0 comments on commit 1c9efea

Please sign in to comment.