Skip to content

Commit

Permalink
build: lib target now builds a shared library.
Browse files Browse the repository at this point in the history
A new 'LIBRARY' variable also causes the libraries to be installed
along the headers and a newly introduced pkg-config file.

* Makefile (DEFAULT) [LIBRARY]: Add lib to it.
(strip_prefix, simplify_pkgconf_dir): New macros.
(prefix, exec_prefix, includedir, bindir, libdir, datadir)
(PKGCONF_EXEC_PREFIX, PKGCONF_INCLUDEDIR, PKGCONF_LIBDIR): New
variables.
(CFLAGS): Add -fPIC.
(PKGCONF_DIR, LIB_STATIC, LIB_SHARED, PKGCONF_FILE): New variables.
(lib): Express target via LIB_STATIC and LIB_SHARED; add
$(PKGCONF_FILE).
(install_lib, install_headers, install_pkgconf_file): New macros.
(install) [LIBRARY]: Conditionally add lib and pkgconf targets, as
well as associated recipes. Adjust installation paths to use new variabes.
($(LIBDIR)/libsameboy.so, PKGCONF_FILE, pkgconf): New targets.
(.PHONY): Register pkgconf. Add missing space between lib/lib-unsupported
* README.md: Update doc.
* sameboy.pc.in: New file.
  • Loading branch information
Apteryks committed Oct 9, 2024
1 parent 73a6d35 commit 87f0c7b
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 13 deletions.
112 changes: 100 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,34 @@ else
DEFAULT := sdl
endif

ifneq ($(LIBRARY),)
DEFAULT += lib
endif

NULL := /dev/null
ifeq ($(PLATFORM),windows32)
NULL := NUL
endif

# Strip a given prefix from a string.
# arg1: The prefix to strip.
# arg2: The text containing the prefix.
# Return $text with $prefix stripped, else nothing.
define strip_prefix =
$(let stripped,$(subst $(1),,$(2)),$\
$(shell test "$(1)$(stripped)" = "$(2)" && echo $(stripped)))
endef

# Simplify a path for use with pkg-config, by replacing $prefix with '${prefix}'
# arg1: prefix, e.g. an installation prefix, such as /usr/local.
# arg2: pkgdir, e.g. a path such as $datadir, $bindir, etc.
# arg3: pkg-config variable name, e.g. 'prefix' or 'exec_prefix'.
# Defaults to 'prefix'.
define simplify_pkgconf_dir =
$(let stripped,$(call strip_prefix,$(1),$(2)),$\
$(if stripped,$${$(or $(3),prefix)}$(stripped),$(2)))
endef

ifneq ($(shell which xdg-open 2> $(NULL))$(FREEDESKTOP),)
# Running on an FreeDesktop environment, configure for (optional) installation
DESTDIR ?=
Expand All @@ -46,8 +69,26 @@ DATA_DIR ?= $(PREFIX)/share/sameboy/
FREEDESKTOP ?= true
endif

# Autoconf-style conventionally named variables.
prefix ?= $(PREFIX)
exec_prefix ?= $(prefix)
includedir ?= $(prefix)/include
bindir ?= $(exec_prefix)/bin
libdir ?= $(exec_prefix)/lib
datadir ?= $(prefix)/share

# Prettified variants for use in the pkg-config file.
override PKGCONF_EXEC_PREFIX = \
$(call simplify_pkgconf_dir,$(prefix),$(exec_prefix))
override PKGCONF_INCLUDEDIR = \
$(call simplify_pkgconf_dir,$(prefix),$(includedir))
override PKGCONF_LIBDIR = \
$(call simplify_pkgconf_dir,$(exec_prefix),$(libdir),exec_prefix)

default: $(DEFAULT)

CFLAGS += -fPIC # required for the shared library

ifeq ($(MAKECMDGOALS),)
MAKECMDGOALS := $(DEFAULT)
endif
Expand Down Expand Up @@ -111,6 +152,10 @@ BIN := build/bin
OBJ := build/obj
INC := build/include/sameboy
LIBDIR := build/lib
PKGCONF_DIR := $(LIBDIR)/pkgconfig
LIB_STATIC := $(LIBDIR)/libsameboy.a
LIB_SHARED := $(LIBDIR)/libsameboy.so
PKGCONF_FILE := $(PKGCONF_DIR)/sameboy.pc

BOOTROMS_DIR ?= $(BIN)/BootROMs

Expand Down Expand Up @@ -376,10 +421,17 @@ _ios: $(BIN)/SameBoy-iOS.app $(OBJ)/installer
ios-ipa: $(BIN)/SameBoy-iOS.ipa
ios-deb: $(BIN)/SameBoy-iOS.deb
ifeq ($(PLATFORM),windows32)

# Libraries.
lib: lib-unsupported
else
lib: $(LIBDIR)/libsameboy.o $(LIBDIR)/libsameboy.a
else ifeq ($(LIBRARY), shared)
lib: $(LIB_SHARED) $(PKGCONF_FILE)
else ifeq ($(LIBRARY), static)
lib: $(LIBDIR)/libsameboy.o $(LIB_STATIC)
else ifneq ($(LIBRARY),)
lib: $(LIBDIR)/libsameboy.o $(LIB_STATIC) $(LIB_SHARED) $(PKGCONF_FILE)
endif

all: sdl tester libretro lib
ifeq ($(PLATFORM),Darwin)
all: cocoa ios-ipa ios-deb
Expand Down Expand Up @@ -723,12 +775,32 @@ libretro:
CC=$(CC) CFLAGS="$(WARNINGS)" $(MAKE) -C libretro BOOTROMS_DIR=$(abspath $(BOOTROMS_DIR)) BIN=$(abspath $(BIN))

# Install for Linux, and other FreeDesktop platforms.
install_lib = install -Dm 644 -t $(DESTDIR)$(libdir) $(1)
install_headers = install -Dm 644 -t $(DESTDIR)$(includedir)/sameboy $(INC)/*
install_pkgconf_file = install -Dm 644 -t $(DESTDIR)$(libdir)/pkgconfig $(PKGCONF_FILE)

ifneq ($(FREEDESKTOP),)
ifneq ($(LIBRARY),)
install: lib pkgconf
endif
install: $(BIN)/XdgThumbnailer/sameboy-thumbnailer sdl $(shell find FreeDesktop) XdgThumbnailer/sameboy.thumbnailer
(cd $(BIN)/SDL && find . \! -name sameboy -type f -exec install -Dm 644 -T {} "$(DESTDIR)$(DATA_DIR)/{}" \; )
install -Dm 755 -s $(BIN)/SDL/sameboy $(DESTDIR)$(PREFIX)/bin/sameboy
install -Dm 755 -s $(BIN)/XdgThumbnailer/sameboy-thumbnailer $(DESTDIR)$(PREFIX)/bin/sameboy-thumbnailer
install -Dm 644 XdgThumbnailer/sameboy.thumbnailer $(DESTDIR)$(PREFIX)/share/thumbnailers/sameboy.thumbnailer
install -Dm 755 -s $(BIN)/SDL/sameboy $(DESTDIR)$(bindir)/sameboy
install -Dm 755 -s $(BIN)/XdgThumbnailer/sameboy-thumbnailer $(DESTDIR)$(bindir)/sameboy-thumbnailer
install -Dm 644 XdgThumbnailer/sameboy.thumbnailer $(DESTDIR)$(datadir)/thumbnailers/sameboy.thumbnailer
ifneq ($(LIBRARY),)
$(install_headers)
$(install_pkgconf_file)
endif
ifeq ($(LIBRARY), shared)
$(call install_lib,$(LIB_SHARED))
else ifeq ($(LIBRARY), static)
$(call install_lib,$(LIB_STATIC))
else ifneq ($(LIBRARY),)
$(call install_lib,$(LIB_SHARED))
$(call install_lib,$(LIB_STATIC))
endif

ifeq ($(DESTDIR),)
xdg-mime install --novendor FreeDesktop/sameboy.xml
xdg-desktop-menu install --novendor FreeDesktop/sameboy.desktop
Expand All @@ -738,12 +810,12 @@ ifeq ($(DESTDIR),)
xdg-icon-resource install --novendor --theme hicolor --size $$size --context mimetypes FreeDesktop/ColorCartridge/$${size}x$${size}.png x-gameboy-color-rom; \
done
else
install -Dm 644 FreeDesktop/sameboy.xml $(DESTDIR)$(PREFIX)/share/mime/sameboy.xml
install -Dm 644 FreeDesktop/sameboy.desktop $(DESTDIR)$(PREFIX)/share/applications/sameboy.desktop
install -Dm 644 FreeDesktop/sameboy.xml $(DESTDIR)$(datadir)/mime/sameboy.xml
install -Dm 644 FreeDesktop/sameboy.desktop $(DESTDIR)$(datadir)/applications/sameboy.desktop
for size in 16x16 32x32 64x64 128x128 256x256 512x512; do \
install -Dm 644 FreeDesktop/AppIcon/$$size.png $(DESTDIR)$(PREFIX)/share/icons/hicolor/$$size/apps/sameboy.png; \
install -Dm 644 FreeDesktop/Cartridge/$$size.png $(DESTDIR)$(PREFIX)/share/icons/hicolor/$$size/mimetypes/x-gameboy-rom.png; \
install -Dm 644 FreeDesktop/ColorCartridge/$$size.png $(DESTDIR)$(PREFIX)/share/icons/hicolor/$$size/mimetypes/x-gameboy-color-rom.png; \
install -Dm 644 FreeDesktop/AppIcon/$$size.png $(DESTDIR)$(datadir)/icons/hicolor/$$size/apps/sameboy.png; \
install -Dm 644 FreeDesktop/Cartridge/$$size.png $(DESTDIR)$(datadir)/icons/hicolor/$$size/mimetypes/x-gameboy-rom.png; \
install -Dm 644 FreeDesktop/ColorCartridge/$$size.png $(DESTDIR)$(datadir)/icons/hicolor/$$size/mimetypes/x-gameboy-color-rom.png; \
done
endif
endif
Expand Down Expand Up @@ -796,7 +868,23 @@ $(LIBDIR)/libsameboy.a: $(LIBDIR)/libsameboy.o
-@$(MKDIR) -p $(dir $@)
-@rm -f $@
ar -crs $@ $^


$(LIBDIR)/libsameboy.so: $(LIBDIR)/libsameboy.o
-@$(MKDIR) -p $(dir $@)
-@rm -f $@
$(CC) -shared -o $@ $^

$(PKGCONF_FILE): sameboy.pc.in
-@$(MKDIR) -p $(dir $@)
-@rm -f $@
sed -e 's,@prefix@,$(prefix),' \
-e 's/@version@/$(VERSION)/' \
-e 's,@exec_prefix@,$(PKGCONF_EXEC_PREFIX),' \
-e 's,@includedir@,$(PKGCONF_INCLUDEDIR),' \
-e 's,@libdir@,$(PKGCONF_LIBDIR),' $< > $@

pkgconf: $(PKGCONF_FILE)

$(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
Expand All @@ -810,4 +898,4 @@ lib-unsupported:
clean:
rm -rf build

.PHONY: libretro tester cocoa ios _ios ios-ipa ios-deb liblib-unsupported bootroms
.PHONY: libretro tester cocoa ios _ios ios-ipa ios-deb lib lib-unsupported bootroms pkgconf
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,18 @@ On Windows, SameBoy also requires:
To compile, simply run `make`. The targets are:
* `cocoa` (Default for macOS)
* `sdl` (Default for everything else)
* `lib` (Creates libsameboy.o and libsameboy.a for statically linking SameBoy, as well as a headers directory with corresponding headers; currently not supported on Windows due to linker limitations)
* `lib` (Creates libsameboy.o, libsameboy.a and libsameboy.so for linking SameBoy, as well as a headers directory with corresponding headers; currently not supported on Windows due to linker limitations)
* `ios` (Plain iOS .app bundle), `ios-ipa` (iOS IPA archive for side-loading), `ios-deb` (iOS deb package for jailbroken devices)
* `libretro`
* `bootroms`
* `tester`

For convenience, when installing the static and shared libraries is
desired, you can specify the LIBRARY=1 make variable to have the
libraries built as part of the default target, as well as installed,
along the headers. Alternatively, `LIBRARY=shared` will install just
the shared library, while `LIBRARY=static` only the static one.

You may also specify `CONF=debug` (default), `CONF=release`, `CONF=native_release` or `CONF=fat_release` to control optimization, symbols and multi-architectures. `native_release` is faster than `release`, but is optimized to the host's CPU and therefore is not portable. `fat_release` is exclusive to macOS and builds x86-64 and ARM64 fat binaries; this requires using a recent enough `clang` and macOS SDK using `xcode-select`, or setting them explicitly with `CC=` and `SYSROOT=`, respectively. All other configurations will build to your host architecture, except for the iOS targets. You may set `BOOTROMS_DIR=...` to a directory containing precompiled boot ROM files, otherwise the build system will compile and use SameBoy's own boot ROMs.

The SDL port will look for resource files with a path relative to executable and inside the directory specified by the `DATA_DIR` variable. If you are packaging SameBoy, you may wish to override this by setting the `DATA_DIR` variable during compilation to the target path of the directory containing all files (apart from the executable, that's not necessary) from the `build/bin/SDL` directory in the source tree. Make sure the variable ends with a `/` character. On FreeDesktop environments, `DATA_DIR` will default to `/usr/local/share/sameboy/`. `PREFIX` and `DESTDIR` follow their standard usage and default to an empty string an `/usr/local`, respectively
Expand Down
10 changes: 10 additions & 0 deletions sameboy.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
includedir=@includedir@
libdir=@libdir@

Name: sameboy
Description: The SameBoy library
Version: @version@
Cflags: -I${includedir}/sameboy
Libs: -L${libdir} -lsameboy

0 comments on commit 87f0c7b

Please sign in to comment.