Making it easy to add C extensions for Playdate projects

Since I started work on a Lua extension, I can see a future where projects may use a bunch of those and I think that dropping them into your projects should be as easy as possible,

Not sure what the ratio is of makefile users vs other means of building a project but here is an idea for those of you using makefiles. I'll take my extension modplayer as an example.

For me the source code for the extension is in a subfolder named modplayer in the root of the project. I have a demo app which needs those files in order to work correctly. I could list all the source files in the project's makefile but that would more or less hard code the path to those files.

If someone wants to add modplayer to their project, I imagine they would either unzip the modplayer repo somewhere in their project or use a git submodule. Let's say that they do this at the root of their project in a folder also named modplayer.

Now, as far as their project's makefile is concerned, the source code to the modplayer extension is now in modplayer/modplayer, which means they also need to somewhat hard code this in their project. Any changes made to the extension code, like adding a file, and everything needs updating.

As long as makefile users include common.mk from the sdk, I propose to add a simple makefile modplayer.mk in the extension's source folder:

# -- Find out more about where this file is relative to the Makefile including it
RELATIVE_FILE_PATH := $(lastword $(MAKEFILE_LIST))
RELATIVE_DIR := $(subst /$(notdir $(RELATIVE_FILE_PATH)),,$(RELATIVE_FILE_PATH))
RELATIVE_PARENT_DIR := $(subst /$(notdir $(RELATIVE_DIR)),,$(RELATIVE_DIR))

# -- Add us as an include search folder only if it's not already there
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
UINCDIR := $(call uniq, $(UINCDIR) $(RELATIVE_PARENT_DIR))

# -- Add our source files
SRC := $(SRC) \
	   $(RELATIVE_DIR)/modplayer.c \
	   $(RELATIVE_DIR)/platform.c \
	   $(RELATIVE_DIR)/lmp/littlemodplayer.c

This allows for a simple include line in the project using the extension (extension demo or otherwise) making the extension code fully relocatable in the directory structure and easy to use:

include modplayer/modplayer.mk

It also makes sure that all include statements are fully qualified (preventing name collisions).

You still need to add a call to registering the extension but even that could be streamlined with some naming conventions maybe.

Sorry for the long post. I have way too much fun with Makefiles :face_with_hand_over_mouth:

If anyone is interested, there's an example of this in action in the current develop branch of modplayer:

2 Likes

I modified @dustin 's playbox2d to try this out with multiple librairies (modplayer and playbox2d) and it seems to work great on macOS.

We would need more testing on Linux and Windows though.

I also submitted a PR to playbox2d.

1 Like