From ac04e02a082d66bd121fc4ff4d626ca49e4fc9e8 Mon Sep 17 00:00:00 2001 From: SciresM Date: Tue, 27 Oct 2020 13:55:19 -0700 Subject: [PATCH] Meso build targets (#1196) * meso: commit wip (thanks fincs) rewrite to support build targets * meso: commit mostly-working build system * meso: correct .o dependencies in kernel/kldr * libstratosphere: fix building after PCH related build changes Co-authored-by: fincs --- libraries/config/common.mk | 17 +++-- libraries/config/templates/mesosphere.mk | 2 +- libraries/libmesosphere/Makefile | 81 ++++++++++++++++-------- libraries/libstratosphere/Makefile | 12 ++-- mesosphere/Makefile | 51 +++++++++------ mesosphere/build_mesosphere.py | 10 +-- mesosphere/kernel/Makefile | 75 +++++++++++++++------- mesosphere/kernel_ldr/Makefile | 75 +++++++++++++++------- 8 files changed, 218 insertions(+), 105 deletions(-) diff --git a/libraries/config/common.mk b/libraries/config/common.mk index 164bed034..d5bf46fbb 100644 --- a/libraries/config/common.mk +++ b/libraries/config/common.mk @@ -15,8 +15,10 @@ endif endif +ATMOSPHERE_BUILD_SETTINGS ?= + export ATMOSPHERE_DEFINES := -DATMOSPHERE -export ATMOSPHERE_SETTINGS := -fPIE -g +export ATMOSPHERE_SETTINGS := -fPIE -g $(ATMOSPHERE_BUILD_SETTINGS) export ATMOSPHERE_CFLAGS := -Wall -ffunction-sections -fdata-sections -fno-strict-aliasing -fwrapv \ -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-stack-protector \ -Wno-format-truncation -Wno-format-zero-length -Wno-stringop-truncation @@ -132,10 +134,15 @@ FIND_SOURCE_FILES=$(foreach dir,$1,$(filter-out $(notdir $(wildcard $(dir)/*.arc $(foreach dir,$1,$(call FIND_SPECIFIC_SOURCE_FILES,$(dir),os,$(ATMOSPHERE_OS_NAME),$2)) \ $(foreach dir,$1,$(call FIND_SPECIFIC_SOURCE_FILES_EX,$(dir),cpu,$(ATMOSPHERE_CPU_NAME) $(ATMOSPHERE_CPU_EXTENSIONS),$2)) +ATMOSPHERE_GCH_IDENTIFIER ?= ams_placeholder_gch_identifier + #--------------------------------------------------------------------------------- # Rules for compiling pre-compiled headers #--------------------------------------------------------------------------------- -%.gch: %.hpp - @echo $< - $(CXX) -w -x c++-header -MMD -MP -MF $(DEPSDIR)/$*.d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) - @cp $@ $(<).gch +%.hpp.gch/$(ATMOSPHERE_GCH_IDENTIFIER): %.hpp | %.hpp.gch + $(SILENTMSG) Precompiling $(notdir $<) for $(ATMOSPHERE_GCH_IDENTIFIER) + $(SILENTCMD)$(CXX) -w -x c++-header -MMD -MP -MQ$@ -MF $(DEPSDIR)/$(notdir $*).d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) + +%.hpp.gch: %.hpp + $(SILENTMSG) Precompiling $(notdir $<) + $(SILENTCMD)$(CXX) -w -x c++-header -MMD -MP -MQ$@ -MF $(DEPSDIR)/$(notdir $*).d $(CXXFLAGS) -c $< -o $@ $(ERROR_FILTER) diff --git a/libraries/config/templates/mesosphere.mk b/libraries/config/templates/mesosphere.mk index 6d66c65c0..19a8dd2a0 100644 --- a/libraries/config/templates/mesosphere.mk +++ b/libraries/config/templates/mesosphere.mk @@ -29,7 +29,7 @@ export CXXWRAPS := -Wl,--wrap,__cxa_pure_virtual \ -Wl,--wrap,_ZSt20__throw_length_errorPKc \ -Wl,--wrap,_ZNSt11logic_errorC2EPKc -export LIBS := -lmesosphere +export LIBS := -l$(LIBMESOSPHERE_NAME) #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/libraries/libmesosphere/Makefile b/libraries/libmesosphere/Makefile index 5722de3f5..14e7bc50d 100644 --- a/libraries/libmesosphere/Makefile +++ b/libraries/libmesosphere/Makefile @@ -1,12 +1,14 @@ #--------------------------------------------------------------------------------- # pull in common atmosphere configuration #--------------------------------------------------------------------------------- -include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../config/common.mk +THIS_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST))) +CURRENT_DIRECTORY := $(abspath $(dir $(THIS_MAKEFILE))) +include $(CURRENT_DIRECTORY)/../config/common.mk #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -PRECOMPILED_HEADERS := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/include/mesosphere.hpp +PRECOMPILED_HEADERS := include/mesosphere.hpp DEFINES := $(ATMOSPHERE_DEFINES) -DATMOSPHERE_IS_MESOSPHERE SETTINGS := $(ATMOSPHERE_SETTINGS) -O2 -mgeneral-regs-only -ffixed-x18 -Wextra -Werror -fno-non-call-exceptions @@ -28,7 +30,7 @@ LIBDIRS := $(ATMOSPHERE_LIBRARIES_DIR)/libvapours # no real need to edit anything past this point unless you need to add additional # rules for different file extensions #--------------------------------------------------------------------------------- -ifneq ($(BUILD),$(notdir $(CURDIR))) +ifneq ($(__RECURSIVE__),1) #--------------------------------------------------------------------------------- export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) $(CURDIR)/include \ @@ -54,7 +56,7 @@ endif export OFILES_BIN := $(addsuffix .o,$(BINFILES)) export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) -export GCH_FILES := $(foreach hdr,$(PRECOMPILED_HEADERS:.hpp=.gch),$(notdir $(hdr))) +export GCH_DIRS := $(PRECOMPILED_HEADERS:.hpp=.hpp.gch) export OFILES := $(OFILES_BIN) $(OFILES_SRC) export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) @@ -62,41 +64,70 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ -I. -.PHONY: clean all +#--------------------------------------------------------------------------------- + +ATMOSPHERE_BUILD_CONFIGS := +all: release + +define ATMOSPHERE_ADD_TARGET + +ATMOSPHERE_BUILD_CONFIGS += $(strip $1) + +$(strip $1): $$(ATMOSPHERE_LIBRARY_DIR)/$(strip $2) + +$$(ATMOSPHERE_LIBRARY_DIR)/$(strip $2) : $$(ATMOSPHERE_LIBRARY_DIR) $$(ATMOSPHERE_BUILD_DIR)/$(strip $1) $$(SOURCES) $$(INCLUDES) $$(GCH_DIRS) + @$$(MAKE) __RECURSIVE__=1 OUTPUT=$$(CURDIR)/$$@ $(3) \ + ATMOSPHERE_GCH_IDENTIFIER="$$(ATMOSPHERE_BOARD_NAME)_$$(ATMOSPHERE_ARCH_NAME)_$(strip $1)" \ + DEPSDIR=$$(CURDIR)/$$(ATMOSPHERE_BUILD_DIR)/$(strip $1) \ + --no-print-directory -C $$(ATMOSPHERE_BUILD_DIR)/$(strip $1) \ + -f $$(THIS_MAKEFILE) + +clean-$(strip $1): + @echo clean $(strip $1) ... + @rm -fr $$(ATMOSPHERE_BUILD_DIR)/$(strip $1) $$(ATMOSPHERE_LIBRARY_DIR)/$(strip $2) + @rm -fr $$(foreach hdr,$$(GCH_DIRS),$$(hdr)/$$(ATMOSPHERE_BOARD_NAME)_$$(ATMOSPHERE_ARCH_NAME)_$(strip $1)) + @for i in $$(GCH_DIRS) $$(ATMOSPHERE_BUILD_DIR) $$(ATMOSPHERE_LIBRARY_DIR); do [ -d $$$$i ] && rmdir --ignore-fail-on-non-empty $$$$i || true; done + +endef + +$(eval $(call ATMOSPHERE_ADD_TARGET, release, $(TARGET).a, \ + ATMOSPHERE_BUILD_SETTINGS="" \ +)) + +$(eval $(call ATMOSPHERE_ADD_TARGET, debug, $(TARGET)_debug.a, \ + ATMOSPHERE_BUILD_SETTINGS="-DMESOSPHERE_BUILD_FOR_DEBUGGING" \ +)) + +$(eval $(call ATMOSPHERE_ADD_TARGET, audit, $(TARGET)_audit.a, \ + ATMOSPHERE_BUILD_SETTINGS="-DMESOSPHERE_BUILD_FOR_AUDITING" \ +)) #--------------------------------------------------------------------------------- -all: lib/$(TARGET).a -lib: +-include $(ATMOSPHERE_BOARD_NAME)_$(ATMOSPHERE_ARCH_NAME).mk + +ALL_GCH_IDENTIFIERS := $(foreach config,$(ATMOSPHERE_BUILD_CONFIGS),$(ATMOSPHERE_BOARD_NAME)_$(ATMOSPHERE_ARCH_NAME)_$(config)) +ALL_GCH_FILES := $(foreach hdr,$(PRECOMPILED_HEADERS:.hpp=.hpp.gch),$(foreach id,$(ALL_GCH_IDENTIFIERS),$(hdr)/$(id))) + +.PHONY: clean all $(foreach config,$(ATMOSPHERE_BUILD_CONFIGS),$(config) clean-$(config)) + +$(ATMOSPHERE_LIBRARY_DIR) $(GCH_DIRS): @[ -d $@ ] || mkdir -p $@ -release: +$(ATMOSPHERE_BUILD_DIR)/%: @[ -d $@ ] || mkdir -p $@ -lib/$(TARGET).a : lib release $(SOURCES) $(INCLUDES) - @$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \ - BUILD_CFLAGS="-DNDEBUG=1 -O2" \ - DEPSDIR=$(CURDIR)/release \ - --no-print-directory -C release \ - -f $(CURDIR)/Makefile - -dist-bin: all - @tar --exclude=*~ -cjf $(TARGET).tar.bz2 include lib - -dist-src: - @tar --exclude=*~ -cjf $(TARGET)-src.tar.bz2 include source Makefile - -dist: dist-src dist-bin - #--------------------------------------------------------------------------------- clean: @echo clean ... - @rm -fr release lib *.bz2 + @rm -fr $(ATMOSPHERE_BUILD_DIR) $(ATMOSPHERE_LIBRARY_DIR) *.bz2 $(ALL_GCH_FILES) + @for i in $(GCH_DIRS); do [ -d $$i ] && rmdir --ignore-fail-on-non-empty $$i || true; done #--------------------------------------------------------------------------------- else -DEPENDS := $(OFILES:.o=.d) $(GCH_FILES:.gch=.d) +GCH_FILES := $(foreach hdr,$(PRECOMPILED_HEADERS:.hpp=.hpp.gch),$(CURRENT_DIRECTORY)/$(hdr)/$(ATMOSPHERE_GCH_IDENTIFIER)) +DEPENDS := $(OFILES:.o=.d) $(foreach hdr,$(GCH_FILES),$(notdir $(patsubst %.hpp.gch/,%.d,$(dir $(hdr))))) #--------------------------------------------------------------------------------- # main targets diff --git a/libraries/libstratosphere/Makefile b/libraries/libstratosphere/Makefile index 4174c842c..542a86d10 100644 --- a/libraries/libstratosphere/Makefile +++ b/libraries/libstratosphere/Makefile @@ -1,7 +1,9 @@ #--------------------------------------------------------------------------------- # pull in common atmosphere configuration #--------------------------------------------------------------------------------- -include $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/../config/common.mk +THIS_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST))) +CURRENT_DIRECTORY := $(abspath $(dir $(THIS_MAKEFILE))) +include $(CURRENT_DIRECTORY)/../config/common.mk #--------------------------------------------------------------------------------- # pull in switch rules @@ -15,7 +17,7 @@ include $(DEVKITPRO)/libnx/switch_rules #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -PRECOMPILED_HEADERS := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))/include/stratosphere.hpp +PRECOMPILED_HEADERS := $(CURRENT_DIRECTORY)/include/stratosphere.hpp DEFINES := $(ATMOSPHERE_DEFINES) -DATMOSPHERE_IS_STRATOSPHERE -D_GNU_SOURCE SETTINGS := $(ATMOSPHERE_SETTINGS) -O2 @@ -66,7 +68,7 @@ endif export OFILES_BIN := $(addsuffix .o,$(BINFILES)) export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) -export GCH_FILES := $(foreach hdr,$(PRECOMPILED_HEADERS:.hpp=.gch),$(notdir $(hdr))) +export GCH_FILES := $(PRECOMPILED_HEADERS:.hpp=.hpp.gch) export OFILES := $(OFILES_BIN) $(OFILES_SRC) export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) @@ -103,12 +105,12 @@ dist: dist-src dist-bin #--------------------------------------------------------------------------------- clean: @echo clean ... - @rm -fr release lib *.bz2 include/stratosphere.hpp.gch + @rm -fr release lib *.bz2 $(GCH_FILES) #--------------------------------------------------------------------------------- else -DEPENDS := $(OFILES:.o=.d) $(GCH_FILES:.gch=.d) +DEPENDS := $(OFILES:.o=.d) $(foreach hdr,$(GCH_FILES:.gch=.d),$(notdir $(hdr))) #--------------------------------------------------------------------------------- # main targets diff --git a/mesosphere/Makefile b/mesosphere/Makefile index 5a02471ac..d168d278e 100644 --- a/mesosphere/Makefile +++ b/mesosphere/Makefile @@ -1,26 +1,41 @@ -TARGETS := kernel.bin kernel_ldr.bin -CLEAN_TARGETS := $(foreach target,$(TARGETS),$(target:.bin=)-clean) +ATMOSPHERE_BUILD_CONFIGS := +all: release -SUBFOLDERS := $(MODULES) +define ATMOSPHERE_ADD_TARGET -all: mesosphere.bin +ATMOSPHERE_BUILD_CONFIGS += $(strip $1) -clean: $(CLEAN_TARGETS) - @rm -f mesosphere.bin +$(strip $1): mesosphere$(strip $2).bin -mesosphere.bin: $(TARGETS) - @python build_mesosphere.py - @echo "Built mesosphere.bin..." +mesosphere$(strip $2).bin: kernel/kernel$(strip $2).bin kernel_ldr/kernel_ldr$(strip $2).bin + @python build_mesosphere.py kernel_ldr/kernel_ldr$(strip $2).bin kernel/kernel$(strip $2).bin mesosphere$(strip $2).bin + @echo "Built mesosphere$(strip $2).bin..." -$(TARGETS): check_libmeso - $(MAKE) -C $(@:.bin=) - @cp $(@:.bin=)/$(@) $(@) +kernel/kernel$(strip $2).bin: check_libmeso$(strip $1) + @$$(MAKE) -C kernel $(strip $1) -check_libmeso: - @$(MAKE) --no-print-directory -C ../libraries/libmesosphere +kernel_ldr/kernel_ldr$(strip $2).bin: check_libmeso$(strip $1) + @$$(MAKE) -C kernel_ldr $(strip $1) -$(CLEAN_TARGETS): - $(MAKE) -C $(@:-clean=) clean - @rm -f $(@:-clean=).bin +check_libmeso$(strip $1): + @$$(MAKE) -C ../libraries/libmesosphere $(strip $1) -.PHONY: all clean $(CLEAN_TARGETS) +clean-$(strip $1): + @$$(MAKE) -C ../libraries/libmesosphere clean-$(strip $1) + @$$(MAKE) -C kernel clean-$(strip $1) + @$$(MAKE) -C kernel_ldr clean-$(strip $1) + @rm -f mesosphere$(strip $2).bin + +endef + +$(eval $(call ATMOSPHERE_ADD_TARGET, release, ,)) +$(eval $(call ATMOSPHERE_ADD_TARGET, debug, _debug,)) +$(eval $(call ATMOSPHERE_ADD_TARGET, audit, _audit,)) + +clean: + @$(MAKE) -C ../libraries/libmesosphere clean + @$(MAKE) -C kernel clean + @$(MAKE) -C kernel_ldr clean + @rm -f mesosphere*.bin + +.PHONY: all clean $(foreach config,$(ATMOSPHERE_BUILD_CONFIGS),$(config) clean-$(config)) diff --git a/mesosphere/build_mesosphere.py b/mesosphere/build_mesosphere.py index bbd06b62c..f5fb44fc8 100644 --- a/mesosphere/build_mesosphere.py +++ b/mesosphere/build_mesosphere.py @@ -11,12 +11,12 @@ def align_up(val, algn): def main(argc, argv): - if argc != 1: - print('Usage: %s' % argv[0]) + if argc != 4: + print('Usage: %s kernel_ldr.bin kernel.bin output.bin' % argv[0]) return 1 - with open('kernel_ldr/kernel_ldr.bin', 'rb') as f: + with open(argv[1], 'rb') as f: kernel_ldr = f.read() - with open('kernel/kernel.bin', 'rb') as f: + with open(argv[2], 'rb') as f: kernel = f.read() kernel_metadata_offset = 4 assert (kernel_metadata_offset <= len(kernel) - 0x40) @@ -37,7 +37,7 @@ def main(argc, argv): kernel_ldr_end = kernel_ldr_offset + len(kernel_ldr) mesosphere_end = align_up(kernel_ldr_end, 0x1000) - with open('mesosphere.bin', 'wb') as f: + with open(argv[3], 'wb') as f: f.write(kernel[:kernel_metadata_offset + 4]) f.write(pk('