diff --git a/.gitignore b/.gitignore index cb83c06..b25a0f8 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .build/ /Packages # Example build products +Examples/*/build Examples/*/Source/pdex.dylib *.pdx xcuserdata/ diff --git a/Examples/HelloPlaydate/Makefile b/Examples/HelloPlaydate/Makefile new file mode 100644 index 0000000..8e6db5a --- /dev/null +++ b/Examples/HelloPlaydate/Makefile @@ -0,0 +1,26 @@ +REPO_ROOT := $(shell git rev-parse --show-toplevel) +PRODUCT := HelloPlaydate.pdx +include $(REPO_ROOT)/Examples/swift.mk + +# MARK: - Build PlayDate Wrapper Swift Module +build/Modules/playdate_device.o: $(REPO_ROOT)/Sources/PlayDate/*.swift + @mkdir -p build/Modules + $(SWIFT_EXEC) $(SWIFT_FLAGS) $(SWIFT_FLAGS_DEVICE) -c $^ -emit-module -o $@ + +build/Modules/playdate_simulator.o: $(REPO_ROOT)/Sources/PlayDate/*.swift + @mkdir -p build/Modules + $(SWIFT_EXEC) $(SWIFT_FLAGS) $(SWIFT_FLAGS_SIMULATOR) -c $^ -emit-module -o $@ + +# MARK: - Build Game Swift Object +build/game_device.o: Sources/HelloPlaydate/*.swift | build/Modules/playdate_device.o + $(SWIFT_EXEC) $(SWIFT_FLAGS) $(SWIFT_FLAGS_DEVICE) -c $^ -o $@ +$(OBJDIR)/pdex.elf: build/game_device.o +OBJS += build/game_device.o +# The wrapper object carries symbols that are not emitted into the game +# object, such as the posix_memalign shim the Embedded Swift runtime needs. +OBJS += build/Modules/playdate_device.o + +build/game_simulator.o: Sources/HelloPlaydate/*.swift | build/Modules/playdate_simulator.o + $(SWIFT_EXEC) $(SWIFT_FLAGS) $(SWIFT_FLAGS_SIMULATOR) -c $^ -o $@ +$(OBJDIR)/pdex.${DYLIB_EXT}: build/game_simulator.o +SIMCOMPILER += build/game_simulator.o \ No newline at end of file diff --git a/Examples/HelloPlaydate/Source/pdex.elf b/Examples/HelloPlaydate/Source/pdex.elf new file mode 100755 index 0000000..c001a7c Binary files /dev/null and b/Examples/HelloPlaydate/Source/pdex.elf differ diff --git a/Examples/swift.mk b/Examples/swift.mk new file mode 100644 index 0000000..a050895 --- /dev/null +++ b/Examples/swift.mk @@ -0,0 +1,78 @@ +HEAP_SIZE = 8388208 +STACK_SIZE = 61800 + +# Locate the Playdate SDK +SDK = ${PLAYDATE_SDK_PATH} +ifeq ($(SDK),) +SDK = $(shell egrep '^\s*SDKRoot' ~/.Playdate/config | head -n 1 | cut -c9-) +endif + +ifeq ($(SDK),) +$(error SDK path not found; set ENV value PLAYDATE_SDK_PATH) +endif + +include $(SDK)/C_API/buildsupport/common.mk + +# Determine the Swift toolchain by order of preference: +# +# 1. the presence of a TOOLCHAINS environment value +# 2. a Swift toolchain installed for the current user (e.g. 'Install for me only') +# 3. a Swift toolchain installed for all users (e.g. 'Install for all users on this computer') +RELATIVE_TOOLCHAIN_PATH = Library/Developer/Toolchains/swift-latest.xctoolchain +ifneq ($(TOOLCHAINS),) +else ifneq ($(wildcard $(HOME)/$(RELATIVE_TOOLCHAIN_PATH)),) +TOOLCHAINS = $(shell plutil -extract CFBundleIdentifier raw -o - $(HOME)/$(RELATIVE_TOOLCHAIN_PATH)/Info.plist) +else ifneq ($(wildcard /$(RELATIVE_TOOLCHAIN_PATH)),) +TOOLCHAINS = $(shell plutil -extract CFBundleIdentifier raw -o - /$(RELATIVE_TOOLCHAIN_PATH)/Info.plist) +else +$(error Swift toolchain not found; set ENV value TOOLCHAINS (e.g. TOOLCHAINS=org.swift.59202403121a make)) +endif + +GCC_INCLUDE_PATHS := $(shell $(CC) -E -Wp,-v -xc /dev/null 2>&1 | egrep '^ ' | xargs echo ) +SWIFT_EXEC := "$(shell TOOLCHAINS=$(TOOLCHAINS) xcrun -f swiftc)" +TOOLCHAIN_PATH := $(shell echo $(SWIFT_EXEC)|sed s'/.xctoolchain.*/.xctoolchain/') + +$(info Using Swift toolchain "$(TOOLCHAINS)" (from $(TOOLCHAIN_PATH))) + +C_FLAGS := \ + $(addprefix -I ,$(GCC_INCLUDE_PATHS)) \ + +SWIFT_FLAGS := \ + $(addprefix -Xcc , $(C_FLAGS)) \ + -O \ + -wmo -enable-experimental-feature Embedded \ + -Xfrontend -disable-stack-protector \ + -Xfrontend -function-sections \ + -swift-version 6 \ + -Xcc -DTARGET_EXTENSION \ + -module-cache-path build/module-cache \ + -I $(SDK)/C_API \ + -I build/Modules \ + -I $(REPO_ROOT)/Sources/CPlaydate \ + +C_FLAGS_DEVICE := \ + -mthumb \ + -mcpu=cortex-m7 \ + -mfloat-abi=hard \ + -mfpu=fpv5-sp-d16 \ + -D__FPU_USED=1 \ + -falign-functions=16 \ + -fshort-enums \ + +SWIFT_FLAGS_DEVICE := \ + $(addprefix -Xcc , $(C_FLAGS_DEVICE)) \ + -target armv7em-none-none-eabi \ + -Xfrontend -experimental-platform-c-calling-convention=arm_aapcs_vfp \ + -module-alias PlayDate=playdate_device \ + +C_FLAGS_SIMULATOR := \ + +SWIFT_FLAGS_SIMULATOR := \ + $(addprefix -Xcc , $(C_FLAGS_SIMULATOR)) \ + -module-alias PlayDate=playdate_simulator \ + +SIMCOMPILER += \ + -nostdlib \ + -dead_strip \ + -Wl,-exported_symbol,_eventHandlerShim \ + -Wl,-exported_symbol,_eventHandler \ diff --git a/Scripts/build-embedded.sh b/Scripts/build-embedded.sh index 0a8e2e3..9084622 100755 --- a/Scripts/build-embedded.sh +++ b/Scripts/build-embedded.sh @@ -53,4 +53,5 @@ exec "$swift_bin" build \ -Xcc -I"$include_dir" \ -Xcc -mcpu=cortex-m7 \ -Xcc -mfloat-abi=hard \ - -Xcc -mfpu=fpv5-sp-d16 + -Xcc -mfpu=fpv5-sp-d16 \ + -Xcc -fshort-enums