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

192 lines
8.7 KiB
Makefile

# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# The hosted ports' shared build rules (src/hosted): JoeyLib's library for
# the hosted core and one backend, and programRules, the rules for one
# program, which make/programs.mk instantiates for every program.
# Included by make/sdl3.mk (a native program), make/web.mk (the same
# SDL3 backend compiled to WebAssembly), make/psp.mk (the same again, for
# the PSP), make/macos.mk (and again, for macOS), make/win32.mk,
# make/win16.mk and make/be.mk (Haiku), after each has included common.mk
# and set:
# HOSTED_CC the compiler
# HOSTED_AR the archiver
# HOSTED_BACKEND the backend folder under src/hosted
# HOSTED_DEFINES the platform and backend macros
# HOSTED_SDK_CFLAGS the backend's library headers (the backend only)
# HOSTED_LINK what a program links after the library
# PROGRAM_EXT the program file's extension
# and, when the backend is C++ (src/hosted/<backend>/*.cpp, the Be API's),
# HOSTED_CXX its C++ compiler
# and may set HOSTED_ARFLAGS, what the archiver is given before the
# library's name (rcs; LLVM's libtool, which archives macOS's universal
# objects, takes -static -o), and HOSTED_PROGRAM_DEPS, files every program
# is rebuilt after (the web page's shell), and, when a program needs its
# DATA at link time (the web port packs it into the program),
# HOSTED_LINK_DATA: the link flags for one program's DATA folder, $(1) the
# program's id. When the program file is packed from what is linked (the
# PSP's EBOOT.PBP, from an ELF; a Mac app, from a program), the port sets
# HOSTED_LINK_OUT, the file one program is linked to, and HOSTED_PACK, the
# command that packs it into the program file ($(1) the id in both).
# A port whose programs show an icon (make/common.mk's programIcon, baked by
# tools/iconBake) sets HOSTED_ICON, the file one program's icon is baked
# to, and HOSTED_ICON_BAKE, the command that bakes it there from the
# program's picture; the program is rebuilt after it, and HOSTED_ICON_LINK
# is what of it the link takes (a resource object, a resource script), or
# HOSTED_PACK packs it in ($(1) the id in all three). Where the system keeps
# the icon in the program's resources (Windows), HOSTED_ICON_ID is its
# number there; the backend is compiled knowing it.
#
# The stage is the chunky generic one, as on the BLANK port: the core, every
# generic (the sprite interpreter stub among them -- no machine code is
# generated), the hosted core and the backend.
HOSTED_DIR := $(SRC_DIR)/hosted
BACKEND_DIR := $(HOSTED_DIR)/$(HOSTED_BACKEND)
CFLAGS := $(COMMON_CFLAGS) $(HOSTED_DEFINES) -I$(HOSTED_DIR) -MMD -MP
HOSTED_ARFLAGS ?= rcs
# HOSTED_X86 (make/win16.mk): sprites are compiled to x86 code as on DOS --
# src/x86's emitter and src/codegen's dispatch in place of the stub.
ifeq ($(HOSTED_X86),1)
CFLAGS += -I$(SRC_X86)
HOSTED_SPRITE_OBJS := $(BUILD)/obj/x86/spriteEmitX86.o $(BUILD)/obj/codegen/spriteCompile.o $(BUILD)/obj/codegen/spriteStage.o
else
HOSTED_SPRITE_OBJS := $(BUILD)/obj/generic/spriteEmitStub.o
endif
# HOSTED_BACKEND_EXTRA (make/psp.mk): a folder of the backend's own for one
# system it runs on, built with the backend's flags; the backend includes
# its headers.
HOSTED_C_SRCS := $(wildcard $(HOSTED_DIR)/*.c)
# -DJOEYLIB_HOSTED_POSIX_SERIAL in HOSTED_DEFINES (make/sdl3.mk, make/macos.mk,
# make/be.mk):
# the serial add-on through termios, src/hosted/posix.
POSIX_C_SRCS := $(if $(filter -DJOEYLIB_HOSTED_POSIX_SERIAL,$(HOSTED_DEFINES)),$(wildcard $(HOSTED_DIR)/posix/*.c))
BACKEND_C_SRCS := $(wildcard $(BACKEND_DIR)/*.c)
BACKEND_CXX_SRCS := $(wildcard $(BACKEND_DIR)/*.cpp)
# Assembly of the backend's own, assembled by HOSTED_AS (make/win16.mk).
BACKEND_ASM_SRCS := $(wildcard $(BACKEND_DIR)/*.asm)
EXTRA_C_SRCS := $(if $(HOSTED_BACKEND_EXTRA),$(wildcard $(HOSTED_BACKEND_EXTRA)/*.c))
HOSTED_ICON_ID := 1
BACKEND_CFLAGS := $(CFLAGS) $(HOSTED_SDK_CFLAGS) $(if $(HOSTED_BACKEND_EXTRA),-I$(HOSTED_BACKEND_EXTRA)) -DHOSTED_ICON_ID=$(HOSTED_ICON_ID)
LIB_OBJS := \
$(patsubst $(SRC_CORE)/%.c,$(BUILD)/obj/core/%.o,$(CORE_C_SRCS)) \
$(patsubst $(SRC_DIR)/generic/%.c,$(BUILD)/obj/generic/%.o,$(GENERIC_C_SRCS)) \
$(HOSTED_SPRITE_OBJS) \
$(patsubst $(HOSTED_DIR)/%.c,$(BUILD)/obj/hosted/%.o,$(HOSTED_C_SRCS)) \
$(patsubst $(HOSTED_DIR)/posix/%.c,$(BUILD)/obj/posix/%.o,$(POSIX_C_SRCS)) \
$(patsubst $(BACKEND_DIR)/%.c,$(BUILD)/obj/backend/%.o,$(BACKEND_C_SRCS)) \
$(patsubst $(BACKEND_DIR)/%.cpp,$(BUILD)/obj/backend/%.o,$(BACKEND_CXX_SRCS)) \
$(patsubst $(BACKEND_DIR)/%.asm,$(BUILD)/obj/backend/%.o,$(BACKEND_ASM_SRCS)) \
$(patsubst $(HOSTED_BACKEND_EXTRA)/%.c,$(BUILD)/obj/backendExtra/%.o,$(EXTRA_C_SRCS))
LIB := $(LIBDIR)/libjoey.a
$(BUILD)/obj/core/%.o: $(SRC_CORE)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -c $< -o $@
$(BUILD)/obj/generic/%.o: $(SRC_DIR)/generic/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -c $< -o $@
$(BUILD)/obj/x86/%.o: $(SRC_X86)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -I$(SRC_CG) -c $< -o $@
$(BUILD)/obj/codegen/%.o: $(SRC_CG)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -I$(SRC_CG) -c $< -o $@
$(BUILD)/obj/hosted/%.o: $(HOSTED_DIR)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -c $< -o $@
$(BUILD)/obj/posix/%.o: $(HOSTED_DIR)/posix/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(CFLAGS) -c $< -o $@
# Only the backend sees its library's headers.
$(BUILD)/obj/backend/%.o: $(BACKEND_DIR)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(BACKEND_CFLAGS) -c $< -o $@
$(BUILD)/obj/backend/%.o: $(BACKEND_DIR)/%.cpp
@mkdir -p $(dir $@)
$(HOSTED_CXX) $(BACKEND_CFLAGS) -c $< -o $@
$(BUILD)/obj/backend/%.o: $(BACKEND_DIR)/%.asm
@mkdir -p $(dir $@)
$(HOSTED_AS) $< $@
$(BUILD)/obj/backendExtra/%.o: $(HOSTED_BACKEND_EXTRA)/%.c
@mkdir -p $(dir $@)
$(HOSTED_CC) $(BACKEND_CFLAGS) -c $< -o $@
# Archived from scratch, so the members follow LIB_OBJS rather than the order
# earlier builds added them in.
$(LIB): $(LIB_OBJS)
@mkdir -p $(dir $@)
rm -f $@
$(HOSTED_AR) $(HOSTED_ARFLAGS) $@ $^
# Pull in per-object header-dependency files generated by -MMD/-MP.
-include $(LIB_OBJS:.o=.d)
# ---- programs -----------------------------------------------------------------
PROGRAM_FOLDERS := yes
# The file one program, $(1) its id, is linked to: the program file itself
# unless the port packs it afterward.
hostedLinkOut = $(or $(call HOSTED_LINK_OUT,$(1)),$(call programBin,$(1)))
# One program, $(1) its id: compiled and linked in one step with the library
# and the backend's libraries (and the math library the mixer's tables use), its DATA too where the port packs it in, and its icon where
# the link takes it, then packed where the port packs it. -MMD leaves the
# dependency file beside what is linked, for header edits; it is named after
# the program only while the link compiles one source, so an icon reaches
# the link compiled.
define programRules
$(call programBin,$(1)): $(call programVar,$(1),SRCS) $(LIB) $(HOSTED_PROGRAM_DEPS) $(if $(HOSTED_LINK_DATA),$(call programVar,$(1),DATA)) $(call HOSTED_ICON,$(1))
@mkdir -p $$(dir $$@) $(dir $(call hostedLinkOut,$(1)))
$(HOSTED_CC) $(CFLAGS) $(call programVar,$(1),CFLAGS) $(call programVar,$(1),SRCS) $(call HOSTED_ICON_LINK,$(1)) $(LIB) $(HOSTED_LINK) $(if $(HOSTED_LINK_DATA),$(call HOSTED_LINK_DATA,$(1))) -lm -o $(call hostedLinkOut,$(1)) $(call programVar,$(1),LDFLAGS)
$(call HOSTED_PACK,$(1))
-include $(basename $(call hostedLinkOut,$(1))).d
$(if $(call HOSTED_ICON,$(1)),$(call hostedIconRules,$(1)))
endef
# $(call hostedIconScript,ID,FORMAT): a Windows port's bake: the program's
# icon in iconBake's FORMAT (ico, ico16) as icon.ico in its icon folder,
# and icon.rc, the resource script that gives it the number HOSTED_ICON_ID
# (by its whole path, so the resource compiler may run anywhere).
hostedIconScript = $(ICON_BAKE) $(2) $(call programIcon,$(1)) $(call programIconDir,$(1))/icon.ico && printf '%s ICON "%s"\n' $(HOSTED_ICON_ID) $(call programIconDir,$(1))/icon.ico > $(call programIconDir,$(1))/icon.rc
# One program's icon, $(1) its id, baked by the port's command.
define hostedIconRules
$(call HOSTED_ICON,$(1)): $(call programIcon,$(1)) $(ICON_BAKE)
@mkdir -p $$(dir $$@)
$(call HOSTED_ICON_BAKE,$(1))
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 $(PLATFORM) clean-$(PLATFORM) clean
all $(PLATFORM): $(LIB) programs
clean-$(PLATFORM):
rm -rf $(BUILD)
# `clean` as an alias for `clean-<port>`, so a bare
# `make -f make/<port>.mk clean` cleans instead of failing.
clean: clean-$(PLATFORM)
endif