Makefile rebuilds entire project when JS bootstrap/TizenTube files change #2

Open opened by ternarykitten.bsky.social

When I change the JS embeds, the entire project rebuilds all .cpp files. It should only rerun the command that compiles the minified JS into an object, and the final link command.

(I had to repeatedly edit embeds while debugging #1.)

Investigation #

If I run remake --trace, I get output:

       Prerequisite 'nxtube_ota_bootstrap_js.h' is newer than target 'assert.o'.
       Prerequisite 'tizentube_js.h' is newer than target 'assert.o'.
      Must remake target 'assert.o'.
/opt/devkitpro/devkitA64/base_rules:14: update target 'assert.o' due to: nxtube_ota_bootstrap_js.h tizentube_js.h
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
echo assert.cpp
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
assert.cpp
##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
true add aarch64-none-elf-g++ " -g -O3 -ffunction-sections -Wno-format-zero-length -fdata-sections -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIC -fvisibility=hidden  -I/home/user/code/nxtube/include  -I/home/user/code/nxtube/build -I/home/user/code/nxtube/source -D__SWITCH__ -D__RTLD_6XX__  -DEXL_LOAD_KIND=Module -DEXL_LOAD_KIND_ENUM=2 -DEXL_PROGRAM_ID=0x01003A400C3DA000 -I"/opt/devkitpro/libnx/include" -I/home/user/code/nxtube/source -I/home/user/code/nxtube/source/lib -I/home/user/code/nxtube/source/nn -I/home/user/code/nxtube/source/program -I/home/user/code/nxtube/source/rtld  -DNXTUBE_DIAGNOSTIC_OVERLAY=1 -fno-rtti -fno-exceptions -fno-asynchronous-unwind-tables -fno-unwind-tables -std=gnu++23 -c /home/user/code/nxtube/source/lib/diag/assert.cpp -o assert.o" /home/user/code/nxtube/source/lib/diag/assert.cpp
##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

It appears all .cpp files have a dependency ("prerequisite") on the .h files generated from JS. I'm not sure where this prerequisite comes from. I suspect it's related to $(BUILD): $(TIZENTUBE_BUNDLE) $(NXTUBE_BOOTSTRAP_BUNDLE) or %.js.o %_js.h : %.js. Perhaps the former makes all sub-targets depend on the JS files? (But the bundle variables are JS files but they depend on generated headers.)

If I comment out $(BUILD)'s dependency on $(NXTUBE_BOOTSTRAP_BUNDLE) and run make clean; make --print-data-base -j8 > rules3-nobootstrap.txt 2>&1, then assert.o: still has a dependency on nxtube_ota_bootstrap_js.h.

Investigating the root cause #

I think the issue is GENERATED_BINFILES -> BINFILES (and/or the assets directory) -> HFILES_BIN -> $(OFILES_SRC) : $(HFILES_BIN)... sigh this line of code was inherited all the way from ExLaunch, and the homebrew Makefile template (GitHub search).

Was the issue present the whole time, or introduced by GENERATED_BINFILES? Clearing GENERATED_BINFILES seems to result in inconsistent build errors where sometimes tizentube_js.h isn't ready by the time YouTube.cpp needs it. I don't know how GENERATED_BINFILES fixes it though; perhaps without it, we first evaluate BINFILES before assets/* is populated, and only a second build succeeds? (CMake would probably have the main build depend on the .h targets.)

Now that I think about it, why not replace the $(OFILES_SRC) : $(HFILES_BIN) rule with making the main build wait on headers (not rebuilding every source file), then relying on the regular .d mechanism to trigger fine-grained rebuilds?

I attempted to edit common.mk with:

$(OUTPUT).elf	:	$(OFILES) $(HFILES_BIN)

But when I slow down the %.js.o %_js.h : %.js rule with sleep 5, then YouTube.cpp builds before the header is regenerated, despite depending on the header. When I run make clean; rm -r assets/*; make --print-data-base -j8, I get an actual missing header error. I guess only the elf linking happens-after generation, which is insufficient.

A solution? #

I ended up settling on:

$(OUTPUT).elf	:	$(OFILES) $(HFILES_BIN)

$(OFILES_SRC)	: | $(HFILES_BIN)

The intent is that build happens-after generating headers, uses the headers that exist, and source files trigger header regeneration when needed. There is a hole where you could generate headers, then edit the .js sources, before building YouTube.cpp, and it wouldn't regenerate the header that's stale but exists. $(BUILD): $(TIZENTUBE_BUNDLE) $(NXTUBE_BOOTSTRAP_BUNDLE) regenerates assets/* but not headers. It seems that making the .elf depend on headers forces them to be rebuilt, and the second rule ensures it happens-before YouTube.cpp, and the .d file triggers YouTube.cpp to be rebuilt. (If I removed the first HFILES_BIN and run touch assets/* && make -j8, YouTube.cpp never gets rebuilt.)

Well the code works, but i don't know why Make doesn't see that YouTube.cpp depends on %_js.h, which depends on %.js, which is dirty. Does the | dependency override the normal one? same problem commented out. Is "this file must be rebuilt if its prerequisite is newer" not the case for transitive prerequisites?

  • Maybe. If I edit YouTube.d and add /home/user/code/nxtube/assets/nxtube_ota_bootstrap.js \ (a prerequisite of build/nxtube_ota_bootstrap_js.h already present), then YouTube.cpp builds when I touch assets/*.
    • Hmm, it does build YouTube.cpp when I remove the transitive dependency from .d and elf dependency from common.mk, but not when I sleep 5.
  • Man, fuck Makefiles.
Sign up or Sign in to add to the discussion.