joeylib2/make/amiga.mk
2026-10-19 15:46:47 -05:00

179 lines
7.5 KiB
Makefile

# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# Amiga (Bebbo m68k-amigaos-gcc) build rules: JoeyLib's library for the
# Amiga, and programRules, the Amiga's rules for one program, which
# make/programs.mk instantiates for every program (a project reaches both
# through make/project.mk). JoeyLib's own examples are at the end.
PLATFORM := amiga
include $(dir $(lastword $(MAKEFILE_LIST)))/common.mk
$(call requireTool,$(AMIGA_CC),toolchain:amiga)
$(call requireTool,$(AMIGA_AS),toolchain:amiga)
$(call requireTool,$(AMIGA_PTPLAYER)/ptplayer.asm,toolchain:amiga)
# PTPlayer is installed by ./install.sh (AMIGA_PTPLAYER); we
# reference its ptplayer.asm + header from there rather than copying
# them into the source tree, so the installer can refresh / version it
# independently. -I on $(SRC_DIR)/amiga lets ptplayer.h resolve
# <SDI_compiler.h> from the machine-local shim alongside our code.
PTPLAYER_DIR := $(AMIGA_PTPLAYER)
# -fno-common: gcc 6 merges a program's uninitialized global into a library
# one of the same name; gcc 10 on (every other port) refuses the link.
CFLAGS := $(COMMON_CFLAGS) -m68000 -fomit-frame-pointer -fno-common -noixemul -D__OSCOMPAT -I$(SRC_DIR)/amiga -I$(SRC_68K) -I$(PTPLAYER_DIR) -MMD -MP
# OSCOMPAT=1 selects PTPlayer's audio.device-friendly variant (uses
# CIA-B + audio.device interrupts via the OS rather than taking over
# Paula directly), matching the way our HAL cooperates with Intuition.
# vasm is only invoked for ptplayer.asm here -- Frank Wille's source
# is written in Devpac/vasm syntax and there is no maintained GAS-
# syntax port. JoeyLib's own .s files are GAS syntax and assembled by
# the bundled m68k-amigaos-as via the gcc driver.
# -Fhunk matches Bebbo gcc-amigaos's AmigaOS HUNK object format. ELF
# objects from vasm cannot be linked into a HUNK libjoey.a (the ar
# "file format not recognized" failure mode is silent at archive time
# and surfaces as undefined references at every binary's link step).
PTPLAYER_ASFLAGS := -Fhunk -m68000 -quiet -DOSCOMPAT=1
LDFLAGS := -noixemul
PORT_C_SRCS := $(wildcard $(SRC_DIR)/amiga/*.c)
PORT_S_SRCS := $(wildcard $(SRC_DIR)/amiga/*.s)
SHARED_S := $(wildcard $(SRC_68K)/*.s)
# Sprite codegen: the Amiga-planar emitter (spriteEmitPlanar68k.c) and the
# planar sprite interpreter (planarSprite.c) live in src/m68k/ and build into
# obj/68k/; only the CPU-independent dispatch stays in src/codegen.
CODEGEN_DIR := $(REPO_DIR)/src/codegen
LIB_OBJS := \
$(patsubst $(SRC_CORE)/%.c,$(BUILD)/obj/core/%.o,$(CORE_C_SRCS_NO_MIX)) \
$(patsubst $(SRC_DIR)/generic/%.c,$(BUILD)/obj/generic/%.o,$(GENERIC_C_SRCS)) \
$(patsubst $(SRC_DIR)/amiga/%.c,$(BUILD)/obj/port/%.o,$(PORT_C_SRCS)) \
$(patsubst $(SRC_DIR)/amiga/%.s,$(BUILD)/obj/port/%.o,$(PORT_S_SRCS)) \
$(patsubst $(SRC_68K)/%.s,$(BUILD)/obj/68k/%.o,$(SHARED_S)) \
$(BUILD)/obj/port/ptplayer.o \
$(BUILD)/obj/68k/spriteEmitPlanar68k.o \
$(BUILD)/obj/68k/planarHash.o \
$(BUILD)/obj/68k/planarIndexed.o \
$(BUILD)/obj/68k/planarSprite.o \
$(BUILD)/obj/68k/spriteC2p.o \
$(BUILD)/obj/68k/planarTileLayer.o \
$(BUILD)/obj/codegen/spriteCompile.o \
$(BUILD)/obj/codegen/spriteStage.o
LIB := $(LIBDIR)/libjoey.a
$(BUILD)/obj/core/%.o: $(SRC_CORE)/%.c
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -c $< -o $@
$(BUILD)/obj/generic/%.o: $(SRC_DIR)/generic/%.c
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -c $< -o $@
$(BUILD)/obj/port/%.o: $(SRC_DIR)/amiga/%.c
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -c $< -o $@
# Hand-written 68k assembly: GAS syntax, fed through the gcc driver
# so the bundled m68k-amigaos-as (binutils) does the work. It exports every
# .equ and .set as a global absolute symbol (in a hunk object), where every
# other port's assembler keeps them local; they would be in every program's
# link, sfxMix.s's loop counter "sum" among them, so each object's are made
# local again.
AMIGA_NM := $(dir $(AMIGA_CC))m68k-amigaos-nm
AMIGA_OBJCOPY := $(dir $(AMIGA_CC))m68k-amigaos-objcopy
amigaAbsLocal = $(AMIGA_OBJCOPY) $$($(AMIGA_NM) $(1) | awk '$$2 == "A" { print "-L", $$3 }') $(1)
$(BUILD)/obj/port/%.o: $(SRC_DIR)/amiga/%.s
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -c $< -o $@
$(call amigaAbsLocal,$@)
# PTPlayer is third-party Devpac/vasm-syntax assembly with no
# maintained GAS port; vasm is invoked only here.
$(BUILD)/obj/port/ptplayer.o: $(PTPLAYER_DIR)/ptplayer.asm
@mkdir -p $(dir $@)
$(AMIGA_AS) $(PTPLAYER_ASFLAGS) $< -o $@
$(BUILD)/obj/codegen/%.o: $(CODEGEN_DIR)/%.c
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -I$(CODEGEN_DIR) -c $< -o $@
# gcc-amigaos spells a C symbol with a leading underscore; the shared asm
# that names one says it through C_OP (src/m68k/m68kCSym.inc, which the
# assembler finds through -I: the gcc driver hands it no include path).
$(BUILD)/obj/68k/%.o: $(SRC_68K)/%.s
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -Wa,--defsym,JL_ASM_USCORE=1 -Wa,-I$(SRC_68K) -c $< -o $@
$(call amigaAbsLocal,$@)
# 68k sprite emitters (spriteEmitPlanar68k.c) live in src/m68k/;
# -I$(CODEGEN_DIR) lets them resolve spriteEmitter.h.
$(BUILD)/obj/68k/%.o: $(SRC_68K)/%.c
@mkdir -p $(dir $@)
$(AMIGA_CC) $(CFLAGS) -I$(CODEGEN_DIR) -c $< -o $@
# Archived from scratch, so the members -- and every program's layout --
# follow LIB_OBJS rather than the order earlier builds added them in.
$(LIB): $(LIB_OBJS)
@mkdir -p $(dir $@)
rm -f $@
$(AMIGA_AR) rcs $@ $^
# Pull in per-object header-dependency files generated by gcc -MMD/-MP.
# Without this, editing a header (e.g. surfaceInternal.h) doesn't rebuild
# the .c files that include it, leaving a frankenstein binary where
# different TUs see different struct layouts.
-include $(LIB_OBJS:.o=.d)
# ---- programs -----------------------------------------------------------------
# A program is a mixed-case Name with no extension (an AmigaDOS name), in bin/
# or in its own bin/Name/ with its DATA beside it, and Name.info beside it,
# its Workbench icon; joey-run boots the folder as the JOEYLIB hard drive.
PROGRAM_EXT :=
PROGRAM_FOLDERS := yes
programNameRule = $(call capFirst,$(call lc,$(1)))
programTarget = $(call programBin,$(1)) $(call programBin,$(1)).info
# The stack a program started from its icon gets, in bytes: what the gates'
# shells give every program (stack 32768).
AMIGA_ICON_STACK := 32768
# One program, $(1) its id: compiled and linked in one step with the library.
# gcc's -MMD leaves Name.d beside it, for header edits.
define programRules
$(call programBin,$(1)): $(call programVar,$(1),SRCS) $(LIB)
@mkdir -p $$(dir $$@)
$(AMIGA_CC) $(CFLAGS) $(call programVar,$(1),CFLAGS) $(call programVar,$(1),SRCS) $(LIB) -o $$@ $(LDFLAGS) $(call programVar,$(1),LDFLAGS)
-include $(call programBin,$(1)).d
$(call programBin,$(1)).info: $(call programIcon,$(1)) $(ICON_BAKE)
@mkdir -p $$(dir $$@)
$(ICON_BAKE) info $$< $$@ $(AMIGA_ICON_STACK)
endef
# ---- JoeyLib's own build: its examples ------------------------------------------
ifndef JOEY_PROJECT
include $(REPO_DIR)/make/examples.mk
include $(REPO_DIR)/make/programs.mk
.DEFAULT_GOAL := all
.PHONY: all amiga clean-amiga clean
all amiga: $(LIB) programs
clean-amiga:
rm -rf $(BUILD)
# `clean` as an alias for `clean-amiga`. Without it a bare
# `make -f make/amiga.mk clean` FAILS -- and with stderr discarded that
# looks like success while leaving stale objects the next build links.
clean: clean-amiga
endif
# UBER with its probe rows, for the display gate (scripts/verify-amiga-display.sh).
.PHONY: amiga-uberprob
amiga-uberprob: $(call programTarget,UBERPROB)