From 007d3b2555f29e28c58ff9715316065079eb41b3 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sun, 1 Dec 2024 18:57:47 +0200 Subject: [PATCH] Allow building libsameboy as a dynamic lib --- Makefile | 52 ++++++++++++++++++++++++++++++++++++++++------------ README.md | 2 +- version.mk | 2 +- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 870ea76..f6fb007 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,8 @@ ifneq ($(findstring MSYS,$(PLATFORM)),) PLATFORM := windows32 endif +DL_EXT := so + ifeq ($(PLATFORM),windows32) _ := $(shell chcp 65001) EXESUFFIX:=.exe @@ -29,6 +31,7 @@ PB12_COMPRESS := build/pb12$(EXESUFFIX) ifeq ($(PLATFORM),Darwin) DEFAULT := cocoa ENABLE_OPENAL ?= 1 +DL_EXT := dylib else DEFAULT := sdl endif @@ -345,9 +348,6 @@ LDFLAGS += -Wl,-exported_symbols_list,$(NULL) STRIP := strip -x CODESIGN := codesign -fs - endif -ifeq ($(PLATFORM),windows32) -LDFLAGS += -fuse-ld=lld -endif LDFLAGS += -flto CFLAGS += -flto LDFLAGS += -Wno-lto-type-mismatch # For GCC's LTO @@ -356,6 +356,9 @@ else $(error Invalid value for CONF: $(CONF). Use "debug", "release" or "native_release") endif +ifeq ($(PLATFORM),windows32) +LDFLAGS += -fuse-ld=lld +endif # Define our targets @@ -377,9 +380,9 @@ _ios: $(BIN)/SameBoy-iOS.app $(OBJ)/installer ios-ipa: $(BIN)/SameBoy-iOS.ipa ios-deb: $(BIN)/SameBoy-iOS.deb ifeq ($(PLATFORM),windows32) -lib: lib-unsupported +lib: $(LIBDIR)/libsameboy.dll else -lib: $(LIBDIR)/libsameboy.o $(LIBDIR)/libsameboy.a +lib: $(LIBDIR)/libsameboy.o $(LIBDIR)/libsameboy.a $(LIBDIR)/libsameboy.$(DL_EXT) endif all: sdl tester libretro lib ifeq ($(PLATFORM),Darwin) @@ -554,7 +557,7 @@ endif $(BIN)/SameBoy.app/Contents/MacOS/SameBoy: $(BIN)/SameBoy.app/Contents/MacOS/SameBoy.dylib $(CC) $^ -o $@ $(LDFLAGS) $(FAT_FLAGS) -rpath @executable_path/../../ -Wl,-reexport_library,$^ - + $(BIN)/SameBoy.app/Contents/MacOS/SameBoy.dylib: $(COCOA_OBJECTS) $(CORE_OBJECTS) $(QUICKLOOK_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) $(FAT_FLAGS) -shared -install_name @rpath/Contents/MacOS/SameBoy.dylib -framework OpenGL -framework AudioUnit -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework IOKit -framework PreferencePanes -framework Carbon -framework QuartzCore -framework Security -framework WebKit -weak_framework Metal -weak_framework MetalKit -framework Quicklook -framework AppKit -Wl,-exported_symbols_list,QuickLook/exports.sym -Wl,-exported_symbol,_main @@ -614,7 +617,7 @@ ifeq ($(CONF), release) $(CODESIGN) $@ endif -# Windows version builds two, one with a conole and one without it +# Windows version builds two, one with a console and one without it $(BIN)/SDL/sameboy.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) $(GL_LDFLAGS) -Wl,/subsystem:windows @@ -798,14 +801,39 @@ $(LIBDIR)/libsameboy.a: $(LIBDIR)/libsameboy.o -@rm -f $@ ar -crs $@ $^ +$(LIBDIR)/libsameboy.$(DL_EXT): $(CORE_OBJECTS) + -@$(MKDIR) -p $(dir $@) + $(CC) $(LDFLAGS) -shared $(FAT_FLAGS) $(CFLAGS) $^ -o $@ +ifeq ($(CONF), release) + $(STRIP) $@ + $(CODESIGN)$@ +endif + + +# Windows dll + +# To avoid Windows' sort.exe +SORT = $(dir $(shell which grep))\sort.exe + +$(OBJ)/names: $(CORE_OBJECTS) + llvm-nm -gU $(CORE_OBJECTS) -P | grep -Eo "^GB_[^ ]+" | $(SORT) -u > $@ + +$(OBJ)/exports: $(PUBLIC_HEADERS) + grep -Eho "\bGB_[a-zA-Z0-9_]+\b" $^ | $(dir $(shell which grep))\sort.exe -u > $@ + +$(OBJ)/exports.def: $(OBJ)/exports $(OBJ)/names + echo LIBRARY libsameboy > $@ + echo EXPORTS >> $@ + comm -12 $^ >> $@ + +$(LIBDIR)/libsameboy.dll: $(CORE_OBJECTS) | $(OBJ)/exports.def + -@$(MKDIR) -p $(dir $@) + $(CC) $(LDFLAGS) -Wl,-lldmingw -Wl,/def:$(OBJ)/exports.def -shared $(CFLAGS) $^ -o $@ + +# CPPP doesn't like multibyte characters, so we replace the single quote character before processing so it doesn't complain $(INC)/%.h: Core/%.h -@$(MKDIR) -p $(dir $@) - -@# CPPP doesn't like multibyte characters, so we replace the single quote character before processing so it doesn't complain sed "s/'/@SINGLE_QUOTE@/g" $^ | cppp $(CPPP_FLAGS) | sed "s/@SINGLE_QUOTE@/'/g" > $@ - -lib-unsupported: - @echo Due to limitations of lld-link, compiling SameBoy as a library on Windows is not supported. - @false # Clean clean: diff --git a/README.md b/README.md index 9c5f8da..a35cc9a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ SameBoy requires the following tools and libraries to build: * macOS Cocoa frontend: macOS SDK and Xcode (For command line tools and ibtool) * SDL frontend: libsdl2 * [rgbds](https://github.com/gbdev/rgbds/releases/), for boot ROM compilation - * [cppp](https://github.com/BR903/cppp), for cleaning up headers when compiling SameBoy as a library + * [cppp](https://github.com/LIJI32/cppp), for cleaning up headers when compiling SameBoy as a library On Windows, SameBoy also requires: * Visual Studio (For headers, etc.) diff --git a/version.mk b/version.mk index d313dd4..1ba9879 100644 --- a/version.mk +++ b/version.mk @@ -1 +1 @@ -VERSION := 0.16.6 \ No newline at end of file +VERSION := 1.0 \ No newline at end of file