diff --git a/Makefile b/Makefile index bb0e3e706459..6c7d052a2ac8 100644 --- a/Makefile +++ b/Makefile @@ -162,8 +162,6 @@ MAKEFLAGS += --no-print-directory .SECONDARY: # Delete files that weren't built properly .DELETE_ON_ERROR: -# Secondary expansion is required for dependency variables in object rules. -.SECONDEXPANSION: RULES_NO_SCAN += libagbsyscall clean clean-assets tidy tidymodern tidynonmodern generated clean-generated .PHONY: all rom modern compare @@ -330,7 +328,10 @@ endef # Calls SCANINC to find dependencies define C_SCANINC ifneq ($(NODEP),1) -$1.o: $2 $$(shell $(SCANINC) $(INCLUDE_SCANINC_ARGS) -I tools/agbcc/include -I gflib $2) +$1.o: $1.d +$1.d: $2 + $(SCANINC) -M $1.d $(INCLUDE_SCANINC_ARGS) -I tools/agbcc/include -I gflib $2 +include $1.d endif endef @@ -359,7 +360,10 @@ endef define ASM_SCANINC ifneq ($(NODEP),1) -$1.o: $2 $$(shell $(SCANINC) $(INCLUDE_SCANINC_ARGS) -I "" $2) +$1.o: $1.d +$1.d: $2 + $(SCANINC) -M $1.d $(INCLUDE_SCANINC_ARGS) -I "" $2 +include $1.d endif endef diff --git a/tools/scaninc/scaninc.cpp b/tools/scaninc/scaninc.cpp index dcb16c0e7944..f5ee7ad6deeb 100644 --- a/tools/scaninc/scaninc.cpp +++ b/tools/scaninc/scaninc.cpp @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include "scaninc.h" #include "source_file.h" @@ -38,15 +41,19 @@ bool CanOpenFile(std::string path) return true; } -const char *const USAGE = "Usage: scaninc [-I INCLUDE_PATH] FILE_PATH\n"; +const char *const USAGE = "Usage: scaninc [-I INCLUDE_PATH] [-M DEPENDENCY_OUT_PATH] FILE_PATH\n"; int main(int argc, char **argv) { std::queue filesToProcess; std::set dependencies; + std::set dependencies_includes; std::vector includeDirs; + bool makeformat = false; + std::string make_outfile; + argc--; argv++; @@ -68,6 +75,13 @@ int main(int argc, char **argv) } includeDirs.push_back(includeDir); } + else if(arg.substr(0, 2) == "-M") + { + makeformat = true; + argc--; + argv++; + make_outfile = std::string(argv[0]); + } else { FATAL_ERROR(USAGE); @@ -112,6 +126,7 @@ int main(int argc, char **argv) { path = include; } + dependencies_includes.insert(path); bool inserted = dependencies.insert(path).second; if (inserted && exists) { @@ -121,8 +136,36 @@ int main(int argc, char **argv) includeDirs.pop_back(); } - for (const std::string &path : dependencies) + if(!makeformat) + { + for (const std::string &path : dependencies) + { + std::printf("%s\n", path.c_str()); + } + std::cout << std::endl; + } + else { - std::printf("%s\n", path.c_str()); + // Write out make rules to a file + std::ofstream output(make_outfile); + + // Print a make rule for the object file + size_t ext_pos = make_outfile.find_last_of("."); + auto object_file = make_outfile.substr(0, ext_pos + 1) + "o"; + output << object_file.c_str() << ": "; + for (const std::string &path : dependencies) + { + output << path << " "; + } + + // Dependency list rule. + // Although these rules are identical, they need to be separate, else make will trigger the rule again after the file is created for the first time. + output << "\n" << make_outfile.c_str() << ": "; + for (const std::string &path : dependencies_includes) + { + output << path << " "; + } + output.flush(); + output.close(); } }