Merge pull request #1460 from luckytyphlosion/remove-temps

Buildsystem no longer builds temporary files for C compilation by default, and other makefile optimizations.
This commit is contained in:
GriffinR 2021-06-13 17:51:17 -04:00 committed by GitHub
commit b0dfaa92be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 169 additions and 66 deletions

142
Makefile
View file

@ -1,6 +1,10 @@
TOOLCHAIN := $(DEVKITARM) TOOLCHAIN := $(DEVKITARM)
COMPARE ?= 0 COMPARE ?= 0
ifeq (compare,$(MAKECMDGOALS))
COMPARE := 1
endif
# don't use dkP's base_tools anymore # don't use dkP's base_tools anymore
# because the redefinition of $(CC) conflicts # because the redefinition of $(CC) conflicts
# with when we want to use $(CC) to preprocess files # with when we want to use $(CC) to preprocess files
@ -36,6 +40,10 @@ MAKER_CODE := 01
REVISION := 0 REVISION := 0
MODERN ?= 0 MODERN ?= 0
ifeq (modern,$(MAKECMDGOALS))
MODERN := 1
endif
# use arm-none-eabi-cpp for macOS # use arm-none-eabi-cpp for macOS
# as macOS's default compiler is clang # as macOS's default compiler is clang
# and clang's preprocessor will warn on \u # and clang's preprocessor will warn on \u
@ -143,12 +151,28 @@ infoshell = $(foreach line, $(shell $1 | sed "s/ /__SPACE__/g"), $(info $(subst
# Build tools when building the rom # Build tools when building the rom
# Disable dependency scanning for clean/tidy/tools # Disable dependency scanning for clean/tidy/tools
# Use a separate minimal makefile for speed
# Since we don't need to reload most of this makefile
ifeq (,$(filter-out all rom compare modern berry_fix libagbsyscall,$(MAKECMDGOALS))) ifeq (,$(filter-out all rom compare modern berry_fix libagbsyscall,$(MAKECMDGOALS)))
$(call infoshell, $(MAKE) tools) $(call infoshell, $(MAKE) -f make_tools.mk)
else else
NODEP := 1 NODEP ?= 1
endif endif
# check if we need to scan dependencies based on the rule
ifeq (,$(MAKECMDGOALS))
SCAN_DEPS ?= 1
else
# clean, tidy, tools, mostlyclean, clean-tools, $(TOOLDIRS), tidymodern, tidynonmodern don't even build the ROM
# berry_fix and libagbsyscall do their own thing
ifeq (,$(filter-out clean tidy tools mostlyclean clean-tools $(TOOLDIRS) tidymodern tidynonmodern berry_fix libagbsyscall,$(MAKECMDGOALS)))
SCAN_DEPS ?= 0
else
SCAN_DEPS ?= 1
endif
endif
ifeq ($(SCAN_DEPS),1)
C_SRCS_IN := $(wildcard $(C_SUBDIR)/*.c $(C_SUBDIR)/*/*.c $(C_SUBDIR)/*/*/*.c) C_SRCS_IN := $(wildcard $(C_SUBDIR)/*.c $(C_SUBDIR)/*/*.c $(C_SUBDIR)/*/*/*.c)
C_SRCS := $(foreach src,$(C_SRCS_IN),$(if $(findstring .inc.c,$(src)),,$(src))) C_SRCS := $(foreach src,$(C_SRCS_IN),$(if $(findstring .inc.c,$(src)),,$(src)))
C_OBJS := $(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(C_SRCS)) C_OBJS := $(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(C_SRCS))
@ -178,11 +202,11 @@ OBJS := $(C_OBJS) $(GFLIB_OBJS) $(C_ASM_OBJS) $(ASM_OBJS) $(DATA_ASM_OBJS) $
OBJS_REL := $(patsubst $(OBJ_DIR)/%,%,$(OBJS)) OBJS_REL := $(patsubst $(OBJ_DIR)/%,%,$(OBJS))
SUBDIRS := $(sort $(dir $(OBJS))) SUBDIRS := $(sort $(dir $(OBJS)))
$(shell mkdir -p $(SUBDIRS))
endif
AUTO_GEN_TARGETS := AUTO_GEN_TARGETS :=
$(shell mkdir -p $(SUBDIRS))
all: rom all: rom
tools: $(TOOLDIRS) tools: $(TOOLDIRS)
@ -196,7 +220,7 @@ ifeq ($(COMPARE),1)
endif endif
# For contributors to make sure a change didn't affect the contents of the ROM. # For contributors to make sure a change didn't affect the contents of the ROM.
compare: ; @$(MAKE) COMPARE=1 compare: all
clean: mostlyclean clean-tools clean: mostlyclean clean-tools
@ -270,47 +294,79 @@ else
$(C_BUILDDIR)/librfu_intr.o: CFLAGS := -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast $(C_BUILDDIR)/librfu_intr.o: CFLAGS := -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast
endif endif
ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: c_dep :=
else
$(C_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(C_SUBDIR)/$*.c ]] && $(SCANINC) -I include -I tools/agbcc/include -I gflib $(C_SUBDIR)/$*.c)
endif
ifeq ($(DINFO),1) ifeq ($(DINFO),1)
override CFLAGS += -g override CFLAGS += -g
endif endif
$(C_BUILDDIR)/%.o : $(C_SUBDIR)/%.c $$(c_dep)
@$(CPP) $(CPPFLAGS) $< -o $(C_BUILDDIR)/$*.i
@$(PREPROC) $(C_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s
@echo -e ".text\n\t.align\t2, 0\n" >> $(C_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s
ifeq ($(NODEP),1)
$(GFLIB_BUILDDIR)/%.o: c_dep :=
else
$(GFLIB_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(GFLIB_SUBDIR)/$*.c ]] && $(SCANINC) -I include -I tools/agbcc/include -I gflib $(GFLIB_SUBDIR)/$*.c)
endif
$(GFLIB_BUILDDIR)/%.o : $(GFLIB_SUBDIR)/%.c $$(c_dep)
@$(CPP) $(CPPFLAGS) $< -o $(GFLIB_BUILDDIR)/$*.i
@$(PREPROC) $(GFLIB_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(GFLIB_BUILDDIR)/$*.s
@echo -e ".text\n\t.align\t2, 0\n" >> $(GFLIB_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(GFLIB_BUILDDIR)/$*.s
ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: c_asm_dep :=
else
$(C_BUILDDIR)/%.o: c_asm_dep = $(shell [[ -f $(C_SUBDIR)/$*.s ]] && $(SCANINC) -I "" $(C_SUBDIR)/$*.s)
endif
$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.s $$(c_asm_dep)
$(AS) $(ASFLAGS) -o $@ $<
# The dep rules have to be explicit or else missing files won't be reported. # The dep rules have to be explicit or else missing files won't be reported.
# As a side effect, they're evaluated immediately instead of when the rule is invoked. # As a side effect, they're evaluated immediately instead of when the rule is invoked.
# It doesn't look like $(shell) can be deferred so there might not be a better way. # It doesn't look like $(shell) can be deferred so there might not be a better way.
ifeq ($(SCAN_DEPS),1)
ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.c
ifeq (,$(KEEP_TEMPS))
@echo "$(CC1) <flags> -o $@ $<"
@$(CPP) $(CPPFLAGS) $< | $(PREPROC) $< charmap.txt -i | $(CC1) $(CFLAGS) -o - - | cat - <(echo -e ".text\n\t.align\t2, 0") | $(AS) $(ASFLAGS) -o $@ -
else
@$(CPP) $(CPPFLAGS) $< -o $(C_BUILDDIR)/$*.i
@$(PREPROC) $(C_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(C_BUILDDIR)/$*.s
@echo -e ".text\n\t.align\t2, 0\n" >> $(C_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s
endif
else
define C_DEP
$1: $2 $$(shell $(SCANINC) -I include -I tools/agbcc/include -I gflib $2)
ifeq (,$$(KEEP_TEMPS))
@echo "$$(CC1) <flags> -o $$@ $$<"
@$$(CPP) $$(CPPFLAGS) $$< | $$(PREPROC) $$< charmap.txt -i | $$(CC1) $$(CFLAGS) -o - - | cat - <(echo -e ".text\n\t.align\t2, 0") | $$(AS) $$(ASFLAGS) -o $$@ -
else
@$$(CPP) $$(CPPFLAGS) $$< -o $$(C_BUILDDIR)/$3.i
@$$(PREPROC) $$(C_BUILDDIR)/$3.i charmap.txt | $$(CC1) $$(CFLAGS) -o $$(C_BUILDDIR)/$3.s
@echo -e ".text\n\t.align\t2, 0\n" >> $$(C_BUILDDIR)/$3.s
$$(AS) $$(ASFLAGS) -o $$@ $$(C_BUILDDIR)/$3.s
endif
endef
$(foreach src, $(C_SRCS), $(eval $(call C_DEP,$(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(src)),$(src),$(patsubst $(C_SUBDIR)/%.c,%,$(src)))))
endif
ifeq ($(NODEP),1)
$(GFLIB_BUILDDIR)/%.o: $(GFLIB_SUBDIR)/%.c $$(c_dep)
ifeq (,$(KEEP_TEMPS))
@echo "$(CC1) <flags> -o $@ $<"
@$(CPP) $(CPPFLAGS) $< | $(PREPROC) $< charmap.txt -i | $(CC1) $(CFLAGS) -o - - | cat - <(echo -e ".text\n\t.align\t2, 0") | $(AS) $(ASFLAGS) -o $@ -
else
@$(CPP) $(CPPFLAGS) $< -o $(GFLIB_BUILDDIR)/$*.i
@$(PREPROC) $(GFLIB_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(GFLIB_BUILDDIR)/$*.s
@echo -e ".text\n\t.align\t2, 0\n" >> $(GFLIB_BUILDDIR)/$*.s
$(AS) $(ASFLAGS) -o $@ $(GFLIB_BUILDDIR)/$*.s
endif
else
define GFLIB_DEP
$1: $2 $$(shell $(SCANINC) -I include -I tools/agbcc/include -I gflib $2)
ifeq (,$$(KEEP_TEMPS))
@echo "$$(CC1) <flags> -o $$@ $$<"
@$$(CPP) $$(CPPFLAGS) $$< | $$(PREPROC) $$< charmap.txt -i | $$(CC1) $$(CFLAGS) -o - - | cat - <(echo -e ".text\n\t.align\t2, 0") | $$(AS) $$(ASFLAGS) -o $$@ -
else
@$$(CPP) $$(CPPFLAGS) $$< -o $$(GFLIB_BUILDDIR)/$3.i
@$$(PREPROC) $$(GFLIB_BUILDDIR)/$3.i charmap.txt | $$(CC1) $$(CFLAGS) -o $$(GFLIB_BUILDDIR)/$3.s
@echo -e ".text\n\t.align\t2, 0\n" >> $$(GFLIB_BUILDDIR)/$3.s
$$(AS) $$(ASFLAGS) -o $$@ $$(GFLIB_BUILDDIR)/$3.s
endif
endef
$(foreach src, $(GFLIB_SRCS), $(eval $(call GFLIB_DEP,$(patsubst $(GFLIB_SUBDIR)/%.c,$(GFLIB_BUILDDIR)/%.o, $(src)),$(src),$(patsubst $(GFLIB_SUBDIR)/%.c,%, $(src)))))
endif
ifeq ($(NODEP),1)
$(C_BUILDDIR)/%.o: $(C_SUBDIR)/%.s
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(AS) $(ASFLAGS) -o $@
else
define SRC_ASM_DATA_DEP
$1: $2 $$(shell $(SCANINC) -I include -I "" $2)
$$(PREPROC) $$< charmap.txt | $$(CPP) -I include - | $$(AS) $$(ASFLAGS) -o $$@
endef
$(foreach src, $(C_ASM_SRCS), $(eval $(call SRC_ASM_DATA_DEP,$(patsubst $(C_SUBDIR)/%.s,$(C_BUILDDIR)/%.o, $(src)),$(src))))
endif
ifeq ($(NODEP),1) ifeq ($(NODEP),1)
$(ASM_BUILDDIR)/%.o: $(ASM_SUBDIR)/%.s $(ASM_BUILDDIR)/%.o: $(ASM_SUBDIR)/%.s
@ -327,12 +383,8 @@ ifeq ($(NODEP),1)
$(DATA_ASM_BUILDDIR)/%.o: $(DATA_ASM_SUBDIR)/%.s $(DATA_ASM_BUILDDIR)/%.o: $(DATA_ASM_SUBDIR)/%.s
$(PREPROC) $< charmap.txt | $(CPP) -I include - | $(AS) $(ASFLAGS) -o $@ $(PREPROC) $< charmap.txt | $(CPP) -I include - | $(AS) $(ASFLAGS) -o $@
else else
define DATA_ASM_DEP $(foreach src, $(REGULAR_DATA_ASM_SRCS), $(eval $(call SRC_ASM_DATA_DEP,$(patsubst $(DATA_ASM_SUBDIR)/%.s,$(DATA_ASM_BUILDDIR)/%.o, $(src)),$(src))))
$1: $2 $$(shell $(SCANINC) -I include -I "" $2) endif
$$(PREPROC) $$< charmap.txt | $$(CPP) -I include - | $$(AS) $$(ASFLAGS) -o $$@
endef
$(foreach src, $(REGULAR_DATA_ASM_SRCS), $(eval $(call DATA_ASM_DEP,$(patsubst $(DATA_ASM_SUBDIR)/%.s,$(DATA_ASM_BUILDDIR)/%.o, $(src)),$(src))))
$(foreach src, $(C_ASM_SRCS), $(eval $(call DATA_ASM_DEP,$(patsubst $(C_SUBDIR)/%.s,$(C_BUILDDIR)/%.o, $(src)),$(src))))
endif endif
$(SONG_BUILDDIR)/%.o: $(SONG_SUBDIR)/%.s $(SONG_BUILDDIR)/%.o: $(SONG_SUBDIR)/%.s
@ -367,7 +419,7 @@ $(ROM): $(ELF)
$(OBJCOPY) -O binary $< $@ $(OBJCOPY) -O binary $< $@
$(FIX) $@ -p --silent $(FIX) $@ -p --silent
modern: ; @$(MAKE) MODERN=1 modern: all
berry_fix/berry_fix.gba: berry_fix berry_fix/berry_fix.gba: berry_fix

11
make_tools.mk Normal file
View file

@ -0,0 +1,11 @@
MAKEFLAGS += --no-print-directory
TOOLDIRS := $(filter-out tools/agbcc tools/binutils,$(wildcard tools/*))
.PHONY: all $(TOOLDIRS)
all: $(TOOLDIRS)
$(TOOLDIRS):
@$(MAKE) -C $@

View file

@ -23,32 +23,59 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <memory> #include <memory>
#include <cstring>
#include <cerrno>
#include "preproc.h" #include "preproc.h"
#include "c_file.h" #include "c_file.h"
#include "char_util.h" #include "char_util.h"
#include "utf8.h" #include "utf8.h"
#include "string_parser.h" #include "string_parser.h"
CFile::CFile(std::string filename) : m_filename(filename) CFile::CFile(const char * filenameCStr, bool isStdin)
{ {
FILE *fp = std::fopen(filename.c_str(), "rb"); FILE *fp;
if (isStdin) {
fp = stdin;
m_filename = std::string{"<stdin>/"}.append(filenameCStr);
} else {
fp = std::fopen(filenameCStr, "rb");
m_filename = std::string(filenameCStr);
}
std::string& filename = m_filename;
if (fp == NULL) if (fp == NULL)
FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str()); FATAL_ERROR("Failed to open \"%s\" for reading.\n", filename.c_str());
std::fseek(fp, 0, SEEK_END); m_size = 0;
m_buffer = (char *)malloc(CHUNK_SIZE + 1);
if (m_buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str());
}
m_size = std::ftell(fp); std::size_t numAllocatedBytes = CHUNK_SIZE + 1;
std::size_t bufferOffset = 0;
std::size_t count;
if (m_size < 0) while ((count = std::fread(m_buffer + bufferOffset, 1, CHUNK_SIZE, fp)) != 0) {
FATAL_ERROR("File size of \"%s\" is less than zero.\n", filename.c_str()); if (!std::ferror(fp)) {
m_size += count;
m_buffer = new char[m_size + 1]; if (std::feof(fp)) {
break;
}
std::rewind(fp); numAllocatedBytes += CHUNK_SIZE;
bufferOffset += CHUNK_SIZE;
if (std::fread(m_buffer, m_size, 1, fp) != 1) m_buffer = (char *)realloc(m_buffer, numAllocatedBytes);
FATAL_ERROR("Failed to read \"%s\".\n", filename.c_str()); if (m_buffer == NULL) {
FATAL_ERROR("Failed to allocate memory to process file \"%s\"!", filename.c_str());
}
} else {
FATAL_ERROR("Failed to read \"%s\". (error: %s)", filename.c_str(), std::strerror(errno));
}
}
m_buffer[m_size] = 0; m_buffer[m_size] = 0;
@ -56,6 +83,7 @@ CFile::CFile(std::string filename) : m_filename(filename)
m_pos = 0; m_pos = 0;
m_lineNum = 1; m_lineNum = 1;
m_isStdin = isStdin;
} }
CFile::CFile(CFile&& other) : m_filename(std::move(other.m_filename)) CFile::CFile(CFile&& other) : m_filename(std::move(other.m_filename))
@ -64,13 +92,14 @@ CFile::CFile(CFile&& other) : m_filename(std::move(other.m_filename))
m_pos = other.m_pos; m_pos = other.m_pos;
m_size = other.m_size; m_size = other.m_size;
m_lineNum = other.m_lineNum; m_lineNum = other.m_lineNum;
m_isStdin = other.m_isStdin;
other.m_buffer = nullptr; other.m_buffer = NULL;
} }
CFile::~CFile() CFile::~CFile()
{ {
delete[] m_buffer; free(m_buffer);
} }
void CFile::Preproc() void CFile::Preproc()

View file

@ -30,7 +30,7 @@
class CFile class CFile
{ {
public: public:
CFile(std::string filename); CFile(const char * filenameCStr, bool isStdin);
CFile(CFile&& other); CFile(CFile&& other);
CFile(const CFile&) = delete; CFile(const CFile&) = delete;
~CFile(); ~CFile();
@ -42,6 +42,7 @@ private:
long m_size; long m_size;
long m_lineNum; long m_lineNum;
std::string m_filename; std::string m_filename;
bool m_isStdin;
bool ConsumeHorizontalWhitespace(); bool ConsumeHorizontalWhitespace();
bool ConsumeNewline(); bool ConsumeNewline();
@ -55,4 +56,6 @@ private:
void RaiseWarning(const char* format, ...); void RaiseWarning(const char* format, ...);
}; };
#define CHUNK_SIZE 4096
#endif // C_FILE_H #endif // C_FILE_H

View file

@ -103,9 +103,9 @@ void PreprocAsmFile(std::string filename)
} }
} }
void PreprocCFile(std::string filename) void PreprocCFile(const char * filename, bool isStdin)
{ {
CFile cFile(filename); CFile cFile(filename, isStdin);
cFile.Preproc(); cFile.Preproc();
} }
@ -132,9 +132,9 @@ char* GetFileExtension(char* filename)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc != 3) if (argc < 3 || argc > 4)
{ {
std::fprintf(stderr, "Usage: %s SRC_FILE CHARMAP_FILE", argv[0]); std::fprintf(stderr, "Usage: %s SRC_FILE CHARMAP_FILE [-i]\nwhere -i denotes if input is from stdin\n", argv[0]);
return 1; return 1;
} }
@ -147,9 +147,17 @@ int main(int argc, char **argv)
if ((extension[0] == 's') && extension[1] == 0) if ((extension[0] == 's') && extension[1] == 0)
PreprocAsmFile(argv[1]); PreprocAsmFile(argv[1]);
else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) else if ((extension[0] == 'c' || extension[0] == 'i') && extension[1] == 0) {
PreprocCFile(argv[1]); if (argc == 4) {
else if (argv[3][0] == '-' && argv[3][1] == 'i' && argv[3][2] == '\0') {
PreprocCFile(argv[1], true);
} else {
FATAL_ERROR("unknown argument flag \"%s\".\n", argv[3]);
}
} else {
PreprocCFile(argv[1], false);
}
} else
FATAL_ERROR("\"%s\" has an unknown file extension of \"%s\".\n", argv[1], extension); FATAL_ERROR("\"%s\" has an unknown file extension of \"%s\".\n", argv[1], extension);
return 0; return 0;