lib_srcs.mk (11813B)
1 # mk/lib_srcs.mk 2 # =========================================================================== 3 # libkit source selection -> LIB_OBJS / LIB_DEPS. 4 # 5 # Consumes the KIT_*_ENABLED flags (mk/config.mk) and BUILD_DIR; mirrors the 6 # `#if` gates in the corresponding sources so the build drops exactly what the 7 # compile drops. The matching frontend `#if`s live in src/api/lang_registry.c. 8 9 # Flattened, archive-unique object path. libkit.a is built with `ar`, which 10 # stores each member under its basename only — so two sources sharing a 11 # basename (the eight link.c, or src/{abi,arch,obj,os}/registry.c, …) would 12 # collide as members even though their build/ paths differ. We keep the 13 # mirrored build/ subdirs but fold the full sub-path into the basename, making 14 # every member name unique. An immediate-parent prefix is not enough on its 15 # own: src/obj/wasm/emit.c and src/arch/wasm/emit.c would both be wasm_emit.o. 16 # $(1)=sources $(2)=tree root to strip $(3)=dest subdir under BUILD_DIR 17 # e.g. src/arch/aa64/native.c -> $(BUILD_DIR)/lib/arch/aa64/arch_aa64_native.o 18 flatobjs = $(foreach s,$(1),$(BUILD_DIR)/$(3)/$(dir $(patsubst $(2)/%,%,$(s)))$(subst /,_,$(basename $(patsubst $(2)/%,%,$(s)))).o) 19 20 # When a native feature is disabled, drop its per-arch TU from all three 21 # native backends and, where one exists, compile a stub in its place. 22 # $(1) = TU basename (e.g. disasm.c) $(2) = stub source, or empty 23 define arch-feature-off 24 LIB_SRCS_ARCH_AA64 := $$(filter-out %/$(1),$$(LIB_SRCS_ARCH_AA64)) 25 LIB_SRCS_ARCH_X64 := $$(filter-out %/$(1),$$(LIB_SRCS_ARCH_X64)) 26 LIB_SRCS_ARCH_RISCV := $$(filter-out %/$(1),$$(LIB_SRCS_ARCH_RISCV)) 27 LIB_SRCS_NONARCH += $(2) 28 endef 29 30 # Core lib sources. Optional subsystems, backend directories, object format 31 # directories, and ABI implementations are added below from their own groups. 32 LIB_SRCS_ABI_CORE = src/abi/abi.c src/abi/registry.c 33 LIB_SRCS_API_CORE = $(filter-out src/api/archive.c src/api/disasm.c \ 34 src/api/link.c src/api/cas.c src/api/package.c \ 35 src/api/compress.c src/api/stubs.c,$(wildcard src/api/*.c)) 36 LIB_SRCS_ARCH_CORE = $(filter-out src/arch/%_stubs.c,$(wildcard src/arch/*.c)) 37 LIB_SRCS_ASM_CORE = $(wildcard src/asm/*.c) 38 LIB_SRCS_CG_CORE = $(wildcard src/cg/*.c) 39 LIB_SRCS_CORE = $(wildcard src/core/*.c) 40 LIB_SRCS_OBJ_CORE = $(filter-out src/obj/%_stubs.c,$(wildcard src/obj/*.c)) 41 LIB_SRCS_NONARCH = $(LIB_SRCS_ABI_CORE) \ 42 $(LIB_SRCS_API_CORE) \ 43 $(LIB_SRCS_ARCH_CORE) \ 44 $(LIB_SRCS_ASM_CORE) \ 45 $(LIB_SRCS_CG_CORE) \ 46 $(LIB_SRCS_CORE) \ 47 $(LIB_SRCS_OBJ_CORE) 48 49 # These source lists use `:=` (not `=`) so each `find` runs exactly once at 50 # parse time. With recursive `=`, every reference below (LIB_SRCS is rebuilt 51 # and re-expanded many times) would re-spawn the `find`, turning ~20 finds into 52 # hundreds on every `make` invocation. 53 LIB_SRCS_ARCH_AA64 := $(shell find src/arch/aa64 -name '*.c' 2>/dev/null) 54 LIB_SRCS_ARCH_X64 := $(shell find src/arch/x64 -name '*.c' 2>/dev/null) 55 LIB_SRCS_ARCH_RISCV := $(shell find src/arch/riscv -name '*.c' 2>/dev/null) 56 LIB_SRCS_ARCH_WASM := $(shell find src/arch/wasm -name '*.c' 2>/dev/null) 57 LIB_SRCS_ARCH_C_TARGET := $(shell find src/arch/c_target -name '*.c' 2>/dev/null) 58 ifneq ($(KIT_OPT_ENABLED),1) 59 $(eval $(call arch-feature-off,opt_coord.c,)) 60 endif 61 ifneq ($(KIT_DISASM_ENABLED),1) 62 $(eval $(call arch-feature-off,disasm.c,src/arch/disasm_stubs.c)) 63 endif 64 ifneq ($(KIT_LINK_ENABLED),1) 65 $(eval $(call arch-feature-off,link.c,src/arch/link_stubs.c)) 66 endif 67 ifneq ($(KIT_DBG_ENABLED),1) 68 $(eval $(call arch-feature-off,dbg.c,src/arch/dbg_stubs.c)) 69 endif 70 ifneq ($(KIT_EMU_ENABLED),1) 71 LIB_SRCS_ARCH_RISCV := $(filter-out %/emu.c,$(LIB_SRCS_ARCH_RISCV)) 72 LIB_SRCS_NONARCH += src/arch/emu_stubs.c 73 endif 74 ifneq ($(KIT_INTERP_ENABLED),1) 75 LIB_SRCS_NONARCH += src/interp/interp_stubs.c 76 endif 77 78 LIB_SRCS_OBJ_ELF := $(shell find src/obj/elf -name '*.c' 2>/dev/null) 79 LIB_SRCS_OBJ_MACHO := $(shell find src/obj/macho -name '*.c' 2>/dev/null) 80 LIB_SRCS_OBJ_COFF := $(shell find src/obj/coff -name '*.c' 2>/dev/null) 81 LIB_SRCS_OBJ_WASM := $(shell find src/obj/wasm -name '*.c' 2>/dev/null) 82 ifneq ($(KIT_LINK_ENABLED),1) 83 LIB_SRCS_OBJ_ELF := $(filter-out %/link.c %/link_dyn.c,$(LIB_SRCS_OBJ_ELF)) 84 LIB_SRCS_OBJ_MACHO := $(filter-out %/link.c,$(LIB_SRCS_OBJ_MACHO)) 85 LIB_SRCS_OBJ_COFF := $(filter-out %/link.c,$(LIB_SRCS_OBJ_COFF)) 86 LIB_SRCS_NONARCH += src/obj/link_stubs.c 87 endif 88 ifneq ($(KIT_EMU_ENABLED),1) 89 LIB_SRCS_OBJ_ELF := $(filter-out %/emu_load.c,$(LIB_SRCS_OBJ_ELF)) 90 LIB_SRCS_NONARCH += src/obj/emu_stubs.c 91 endif 92 ifneq ($(KIT_AR_ENABLED),1) 93 LIB_SRCS_OBJ_COFF := $(filter-out %/archive.c,$(LIB_SRCS_OBJ_COFF)) 94 LIB_SRCS_NONARCH += src/obj/archive_stubs.c 95 endif 96 97 LIB_SRCS_OPT := $(filter-out src/opt/pass_o2.c,$(shell find src/opt -name '*.c' 2>/dev/null)) 98 LIB_SRCS_INTERP := $(filter-out %/interp_stubs.c,$(shell find src/interp -name '*.c' 2>/dev/null)) 99 LIB_SRCS_WASM_CORE := $(shell find src/wasm -name '*.c' 2>/dev/null) 100 LIB_SRCS_API_AR = src/api/archive.c 101 LIB_SRCS_API_DISASM = src/api/disasm.c 102 LIB_SRCS_API_LINK = src/api/link.c 103 # Distribution subsystem (content store + signed packages). The cas layer 104 # needs blake2b (-> monocypher); the pkg layer adds the crypto/container shims 105 # and the second monocypher TU. The compression codecs (deflate + lz4 block) 106 # are shared by pkg and the standalone compress API, so they live in their own 107 # CODEC group pulled in once if either is enabled. The lz4 frame layer 108 # (LIB_SRCS_DIST_COMPRESS) is needed only by the compress API. All vendored .c 109 # (lz4.c, lz4frame.c's xxhash/lz4hc/lz4frame) are #included by their src/dist 110 # shim (amalgamation), so they are NOT compiled standalone. 111 LIB_SRCS_API_CAS = src/api/cas.c 112 LIB_SRCS_API_PKG = src/api/package.c 113 LIB_SRCS_API_COMPRESS = src/api/compress.c 114 LIB_SRCS_DIST_CAS = src/dist/dist.c src/dist/blake2b.c src/dist/blob.c \ 115 src/dist/tree.c src/dist/cas.c 116 LIB_SRCS_VENDOR_CAS = vendor/monocypher/monocypher.c 117 LIB_SRCS_DIST_CODEC = src/dist/deflate.c src/dist/lz4.c 118 LIB_SRCS_DIST_COMPRESS = src/dist/lz4frame.c 119 LIB_SRCS_DIST_PKG = src/dist/b64.c src/dist/ed25519.c src/dist/minisig.c \ 120 src/dist/tar.c src/dist/kpkg.c src/dist/manifest.c \ 121 src/dist/trust.c 122 LIB_SRCS_VENDOR_PKG = vendor/monocypher/monocypher-ed25519.c 123 LIB_SRCS_DEBUG := $(shell find src/debug -name '*.c' 2>/dev/null) 124 LIB_SRCS_DBG := $(shell find src/dbg -name '*.c' 2>/dev/null) 125 LIB_SRCS_EMU := $(shell find src/emu -name '*.c' 2>/dev/null) \ 126 $(shell find src/os -name '*.c' 2>/dev/null) 127 LIB_SRCS_LINK := $(shell find src/link -name '*.c' 2>/dev/null) 128 ifneq ($(KIT_JIT_ENABLED),1) 129 LIB_SRCS_LINK := $(filter-out %/link_jit.c,$(LIB_SRCS_LINK)) 130 endif 131 132 LIB_SRC_ABI_AAPCS64 = src/abi/abi_aapcs64.c 133 LIB_SRC_ABI_APPLE_ARM64 = src/abi/abi_apple_arm64.c 134 LIB_SRC_ABI_AAPCS64_WINDOWS = src/abi/abi_aapcs64_windows.c 135 LIB_SRC_ABI_SYSV_X64 = src/abi/abi_sysv_x64.c 136 LIB_SRC_ABI_APPLE_X64 = src/abi/abi_apple_x64.c 137 LIB_SRC_ABI_WIN64_X64 = src/abi/abi_win64_x64.c 138 LIB_SRC_ABI_RV64 = src/abi/abi_rv64.c 139 140 LIB_SRCS = $(LIB_SRCS_NONARCH) 141 ifeq ($(KIT_OPT_ENABLED),1) 142 LIB_SRCS += $(LIB_SRCS_OPT) 143 endif 144 ifeq ($(KIT_INTERP_ENABLED),1) 145 LIB_SRCS += $(LIB_SRCS_INTERP) 146 endif 147 ifeq ($(KIT_AR_ENABLED),1) 148 LIB_SRCS += $(LIB_SRCS_API_AR) 149 endif 150 ifeq ($(KIT_DISASM_ENABLED),1) 151 LIB_SRCS += $(LIB_SRCS_API_DISASM) 152 endif 153 ifeq ($(KIT_DWARF_ENABLED),1) 154 LIB_SRCS += $(LIB_SRCS_DEBUG) 155 endif 156 ifeq ($(KIT_LINK_ENABLED),1) 157 LIB_SRCS += $(LIB_SRCS_API_LINK) $(LIB_SRCS_LINK) 158 endif 159 ifeq ($(KIT_DBG_ENABLED),1) 160 LIB_SRCS += $(LIB_SRCS_DBG) 161 endif 162 ifeq ($(KIT_EMU_ENABLED),1) 163 LIB_SRCS += $(LIB_SRCS_EMU) 164 endif 165 ifeq ($(KIT_CAS_ENABLED),1) 166 LIB_SRCS += $(LIB_SRCS_API_CAS) $(LIB_SRCS_DIST_CAS) $(LIB_SRCS_VENDOR_CAS) 167 endif 168 # Shared compression codecs (deflate + lz4 block): pulled in once if either the 169 # compress API or the pkg layer needs them. 170 KIT_NEED_CODEC := 171 ifeq ($(KIT_COMPRESS_ENABLED),1) 172 LIB_SRCS += $(LIB_SRCS_API_COMPRESS) $(LIB_SRCS_DIST_COMPRESS) 173 KIT_NEED_CODEC := 1 174 endif 175 ifeq ($(KIT_PKG_ENABLED),1) 176 LIB_SRCS += $(LIB_SRCS_API_PKG) $(LIB_SRCS_DIST_PKG) $(LIB_SRCS_VENDOR_PKG) 177 KIT_NEED_CODEC := 1 178 endif 179 ifeq ($(KIT_NEED_CODEC),1) 180 LIB_SRCS += $(LIB_SRCS_DIST_CODEC) 181 endif 182 ifeq ($(KIT_ARCH_AA64_ENABLED),1) 183 LIB_SRCS += $(LIB_SRCS_ARCH_AA64) 184 endif 185 ifeq ($(KIT_ARCH_X64_ENABLED),1) 186 LIB_SRCS += $(LIB_SRCS_ARCH_X64) 187 endif 188 ifneq ($(filter 1,$(KIT_ARCH_RV32_ENABLED) $(KIT_ARCH_RV64_ENABLED)),) 189 LIB_SRCS += $(LIB_SRCS_ARCH_RISCV) 190 endif 191 ifeq ($(KIT_ARCH_WASM_ENABLED),1) 192 LIB_SRCS += $(LIB_SRCS_ARCH_WASM) 193 endif 194 ifeq ($(KIT_ARCH_C_TARGET_ENABLED),1) 195 LIB_SRCS += $(LIB_SRCS_ARCH_C_TARGET) 196 endif 197 ifneq ($(filter 1,$(KIT_ARCH_WASM_ENABLED) $(KIT_OBJ_WASM_ENABLED) $(KIT_LANG_WASM_ENABLED)),) 198 LIB_SRCS += $(LIB_SRCS_WASM_CORE) 199 LIB_SRCS += $(LIB_SRCS_OBJ_WASM) 200 endif 201 ifeq ($(KIT_OBJ_ELF_ENABLED),1) 202 LIB_SRCS += $(LIB_SRCS_OBJ_ELF) 203 endif 204 ifeq ($(KIT_OBJ_MACHO_ENABLED),1) 205 LIB_SRCS += $(LIB_SRCS_OBJ_MACHO) 206 endif 207 ifeq ($(KIT_OBJ_COFF_ENABLED),1) 208 LIB_SRCS += $(LIB_SRCS_OBJ_COFF) 209 endif 210 ifneq ($(filter 1,$(KIT_ARCH_AA64_ENABLED) $(KIT_ARCH_C_TARGET_ENABLED)),) 211 ifneq ($(filter 1,$(KIT_OBJ_ELF_ENABLED) $(KIT_OBJ_MACHO_ENABLED) $(KIT_OBJ_COFF_ENABLED)),) 212 LIB_SRCS += $(LIB_SRC_ABI_AAPCS64) 213 endif 214 ifeq ($(KIT_OBJ_MACHO_ENABLED),1) 215 LIB_SRCS += $(LIB_SRC_ABI_APPLE_ARM64) 216 endif 217 ifeq ($(KIT_OBJ_COFF_ENABLED),1) 218 LIB_SRCS += $(LIB_SRC_ABI_AAPCS64_WINDOWS) 219 endif 220 endif 221 ifneq ($(filter 1,$(KIT_ARCH_X64_ENABLED) $(KIT_ARCH_C_TARGET_ENABLED)),) 222 ifneq ($(filter 1,$(KIT_OBJ_ELF_ENABLED) $(KIT_OBJ_MACHO_ENABLED)),) 223 LIB_SRCS += $(LIB_SRC_ABI_SYSV_X64) 224 endif 225 ifeq ($(KIT_OBJ_MACHO_ENABLED),1) 226 LIB_SRCS += $(LIB_SRC_ABI_APPLE_X64) 227 endif 228 ifeq ($(KIT_OBJ_COFF_ENABLED),1) 229 LIB_SRCS += $(LIB_SRC_ABI_WIN64_X64) 230 endif 231 endif 232 ifneq ($(filter 1,$(KIT_ARCH_RV32_ENABLED) $(KIT_ARCH_RV64_ENABLED) $(KIT_ARCH_C_TARGET_ENABLED)),) 233 ifeq ($(KIT_OBJ_ELF_ENABLED),1) 234 LIB_SRCS += $(LIB_SRC_ABI_RV64) 235 endif 236 endif 237 238 # Per-frontend source sets. Each is gated by its KIT_LANG_*_ENABLED flag 239 # from mk/config.mk so the matching `#if` in src/api/lang_registry.c and 240 # the build agree on which frontends are compiled in. 241 LANG_CPP_SRCS := $(shell find lang/cpp -name '*.c' 2>/dev/null) 242 LANG_C_SRCS := $(shell find lang/c -name '*.c' 2>/dev/null) 243 LANG_WASM_SRCS := $(shell find lang/wasm -name '*.c' 2>/dev/null) 244 LANG_TOY_SRCS = $(wildcard lang/toy/*.c) 245 246 LANG_OBJS = 247 ifeq ($(KIT_LANG_CPP_ENABLED),1) 248 LANG_OBJS += $(call flatobjs,$(LANG_CPP_SRCS),lang,lang) 249 endif 250 ifeq ($(KIT_LANG_C_ENABLED),1) 251 LANG_OBJS += $(call flatobjs,$(LANG_C_SRCS),lang,lang) 252 endif 253 ifeq ($(KIT_LANG_WASM_ENABLED),1) 254 LANG_OBJS += $(call flatobjs,$(LANG_WASM_SRCS),lang,lang) 255 endif 256 ifeq ($(KIT_LANG_TOY_ENABLED),1) 257 LANG_OBJS += $(call flatobjs,$(LANG_TOY_SRCS),lang,lang) 258 endif 259 260 # Library assembly sources (currently none). The compile rules in the Makefile 261 # consume LIB_ASMS, so keep it defined even when empty. 262 LIB_ASMS = 263 264 # Compile-rule source groups (each shares one compile-flag profile; the rules 265 # that consume these live in the Makefile). lang_registry.c is split out: it is 266 # a libkit TU but compiles with frontend includes (-Ilang), not the library set. 267 LIB_SRCS_LANGREG := $(filter src/api/lang_registry.c,$(LIB_SRCS)) 268 LIB_SRCS_C_GENERAL := $(filter-out $(LIB_SRCS_LANGREG),$(filter src/%.c,$(LIB_SRCS))) 269 LIB_SRCS_VENDOR := $(filter vendor/%.c,$(LIB_SRCS)) 270 271 LIB_OBJS = $(call flatobjs,$(LIB_SRCS_C_GENERAL),src,lib) \ 272 $(call flatobjs,$(LIB_SRCS_LANGREG),src,lib) \ 273 $(call flatobjs,$(LIB_SRCS_VENDOR),vendor,vendor) \ 274 $(LANG_OBJS) \ 275 $(call flatobjs,$(LIB_ASMS),src,lib) 276 LIB_DEPS = $(LIB_OBJS:.o=.d)