build: prefix include deps into build/

scan_includes now supports --build-prefix so generated INCLUDE/INCBIN dependencies are emitted as /… when missing in the source tree.
This commit is contained in:
vulcandth 2026-01-17 17:14:32 -06:00
parent 38933ff5f7
commit de6fa930b7
2 changed files with 64 additions and 13 deletions

View File

@ -161,8 +161,12 @@ $(info $(shell $(MAKE) -C tools))
# Redirect dependencies that are generated as part of the build into $(BUILDDIR)/.
#
# Heuristic: if a dependency path exists in the source tree, keep it as-is;
# otherwise, assume it is generated into $(BUILDDIR)/.
# For INCLUDE/INCBIN dependencies, this logic now lives in tools/scan_includes
# via its --build-prefix option.
#
# For other rules (e.g. compression), keep the Makefile-side heuristic:
# if a dependency path exists in the source tree, keep it as-is; otherwise,
# assume it is generated into $(BUILDDIR)/.
define map_build_deps
$(strip \
$(foreach dep,$1,\
@ -173,10 +177,12 @@ $(strip \
)
endef
preinclude_deps := includes.asm $(call map_build_deps,$(shell tools/scan_includes includes.asm))
SCAN_INCLUDES := tools/scan_includes --build-prefix $(BUILDDIR)
preinclude_deps := includes.asm $(shell $(SCAN_INCLUDES) includes.asm)
define DEP
$1: $2 $$(call map_build_deps,$$(shell tools/scan_includes $2)) $(preinclude_deps) | $(OBJDIR)/rgbdscheck.o
$1: $2 $$(shell $(SCAN_INCLUDES) $2) $(preinclude_deps) | $(OBJDIR)/rgbdscheck.o
@mkdir -p $$(dir $$@)
$$(RGBASM) $$(RGBASMFLAGS) -o $$@ $$<
endef

View File

@ -1,18 +1,55 @@
#define PROGRAM_NAME "scan_includes"
#define USAGE_OPTS "[-h|--help] [-s|--strict] filename.asm"
#define USAGE_OPTS "[-h|--help] [-s|--strict] [-b|--build-prefix dir] filename.asm"
#include "common.h"
#include <ctype.h>
#include <unistd.h>
void parse_args(int argc, char *argv[], bool *strict) {
static bool has_prefix(const char *s, const char *prefix) {
if (!prefix || !*prefix) {
return false;
}
size_t prefix_len = strlen(prefix);
if (strlen(s) < prefix_len) {
return false;
}
if (strncmp(s, prefix, prefix_len)) {
return false;
}
if (prefix[prefix_len - 1] == '/') {
return true;
}
return s[prefix_len] == '/' || s[prefix_len] == '\0';
}
static char *join_prefix(const char *prefix, const char *path) {
if (!prefix || !*prefix) {
size_t len = strlen(path) + 1;
char *out = xmalloc(len);
memcpy(out, path, len);
return out;
}
size_t prefix_len = strlen(prefix);
bool needs_slash = prefix[prefix_len - 1] != '/';
size_t len = prefix_len + (needs_slash ? 1 : 0) + strlen(path) + 1;
char *out = xmalloc(len);
snprintf(out, len, "%s%s%s", prefix, needs_slash ? "/" : "", path);
return out;
}
void parse_args(int argc, char *argv[], bool *strict, const char **build_prefix) {
struct option long_options[] = {
{"build-prefix", required_argument, 0, 'b'},
{"strict", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{0}
};
for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) {
for (int opt; (opt = getopt_long(argc, argv, "b:sh", long_options)) != -1;) {
switch (opt) {
case 'b':
*build_prefix = optarg;
break;
case 's':
*strict = true;
break;
@ -25,7 +62,7 @@ void parse_args(int argc, char *argv[], bool *strict) {
}
}
void scan_file(const char *filename, bool strict) {
void scan_file(const char *filename, bool strict, const char *build_prefix) {
errno = 0;
FILE *f = fopen(filename, "rb");
if (!f) {
@ -88,10 +125,17 @@ void scan_file(const char *filename, bool strict) {
size_t length = strcspn(ptr, "\"");
ptr += length + 1;
include_path[length] = '\0';
printf("%s ", include_path);
if (is_include) {
scan_file(include_path, strict);
const char *printed_path = include_path;
char *prefixed_path = NULL;
if (build_prefix && !has_prefix(include_path, build_prefix) && access(include_path, F_OK) != 0) {
prefixed_path = join_prefix(build_prefix, include_path);
printed_path = prefixed_path;
}
printf("%s ", printed_path);
if (is_include) {
scan_file(include_path, strict, build_prefix);
}
free(prefixed_path);
} else {
fprintf(stderr, "%s: no file path after INC%s\n", filename, is_include ? "LUDE" : "BIN");
// Continue to process a comment
@ -109,7 +153,8 @@ void scan_file(const char *filename, bool strict) {
int main(int argc, char *argv[]) {
bool strict = false;
parse_args(argc, argv, &strict);
const char *build_prefix = NULL;
parse_args(argc, argv, &strict, &build_prefix);
argc -= optind;
argv += optind;
@ -117,6 +162,6 @@ int main(int argc, char *argv[]) {
usage_exit(1);
}
scan_file(argv[0], strict);
scan_file(argv[0], strict, build_prefix);
return 0;
}