diff --git a/Makefile b/Makefile index 3cf298f1ab6..6ccd9e5bca2 100644 --- a/Makefile +++ b/Makefile @@ -274,9 +274,10 @@ TARGET_BIN = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)_$(BUILD_SUFFIX).bin TARGET_HEX = $(BIN_DIR)/$(FORKNAME)_$(FC_VER)_$(TARGET)_$(BUILD_SUFFIX).hex endif +TARGET_OBJ_DIR = $(OBJECT_DIR)/$(TARGET) TARGET_ELF = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).elf -TARGET_OBJS = $(addsuffix .o,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(TARGET_SRC)))) -TARGET_DEPS = $(addsuffix .d,$(addprefix $(OBJECT_DIR)/$(TARGET)/,$(basename $(TARGET_SRC)))) +TARGET_OBJS = $(addsuffix .o,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(TARGET_SRC)))) +TARGET_DEPS = $(addsuffix .d,$(addprefix $(TARGET_OBJ_DIR)/,$(basename $(TARGET_SRC)))) TARGET_MAP = $(OBJECT_DIR)/$(FORKNAME)_$(TARGET).map @@ -285,20 +286,23 @@ CLEAN_ARTIFACTS += $(TARGET_HEX) CLEAN_ARTIFACTS += $(TARGET_ELF) $(TARGET_OBJS) $(TARGET_MAP) # Make sure build date and revision is updated on every incremental build -$(OBJECT_DIR)/$(TARGET)/build/version.o : $(TARGET_SRC) +$(TARGET_OBJ_DIR)/build/version.o : $(TARGET_SRC) # Settings generator .PHONY: .FORCE settings clean-settings -UTILS_DIR = $(ROOT)/src/utils -SETTINGS_GENERATOR = $(UTILS_DIR)/settings.rb -BUILD_STAMP = $(UTILS_DIR)/build_stamp.rb -STAMP = $(BIN_DIR)/build.stamp - -GENERATED_SETTINGS = $(SRC_DIR)/fc/settings_generated.h $(SRC_DIR)/fc/settings_generated.c -SETTINGS_FILE = $(SRC_DIR)/fc/settings.yaml -GENERATED_FILES = $(GENERATED_SETTINGS) +UTILS_DIR = $(ROOT)/src/utils +SETTINGS_GENERATOR = $(UTILS_DIR)/settings.rb +BUILD_STAMP = $(UTILS_DIR)/build_stamp.rb +STAMP = $(TARGET_OBJ_DIR)/build.stamp + +GENERATED_SETTINGS = $(TARGET_OBJ_DIR)/settings_generated.h $(TARGET_OBJ_DIR)/settings_generated.c +SETTINGS_FILE = $(SRC_DIR)/fc/settings.yaml +GENERATED_FILES = $(GENERATED_SETTINGS) $(GENERATED_SETTINGS): $(SETTINGS_GENERATOR) $(SETTINGS_FILE) $(STAMP) +# Make sure the generated files are in the include path +CFLAGS += -I$(TARGET_OBJ_DIR) + $(STAMP): .FORCE $(V1) CFLAGS="$(CFLAGS)" TARGET=$(TARGET) ruby $(BUILD_STAMP) $(SETTINGS_FILE) $(STAMP) @@ -306,7 +310,7 @@ $(STAMP): .FORCE # See https://www.gnu.org/software/make/manual/make.html#Pattern-Examples %generated.h %generated.c: $(V1) echo "settings.yaml -> settings_generated.h, settings_generated.c" "$(STDOUT)" - $(V1) CFLAGS="$(CFLAGS)" TARGET=$(TARGET) ruby $(SETTINGS_GENERATOR) . $(SETTINGS_FILE) + $(V1) CFLAGS="$(CFLAGS)" TARGET=$(TARGET) ruby $(SETTINGS_GENERATOR) . $(SETTINGS_FILE) -o $(TARGET_OBJ_DIR) settings-json: $(V0) CFLAGS="$(CFLAGS)" TARGET=$(TARGET) ruby $(SETTINGS_GENERATOR) . $(SETTINGS_FILE) --json settings.json @@ -329,18 +333,18 @@ $(TARGET_ELF): $(TARGET_OBJS) $(V0) $(SIZE) $(TARGET_ELF) # Compile -$(OBJECT_DIR)/$(TARGET)/%.o: %.c +$(TARGET_OBJ_DIR)/%.o: %.c $(V1) mkdir -p $(dir $@) $(V1) echo %% $(notdir $<) "$(STDOUT)" $(V1) $(CROSS_CC) -c -o $@ $(CFLAGS) $< # Assemble -$(OBJECT_DIR)/$(TARGET)/%.o: %.s +$(TARGET_OBJ_DIR)/%.o: %.s $(V1) mkdir -p $(dir $@) $(V1) echo %% $(notdir $<) "$(STDOUT)" $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $< -$(OBJECT_DIR)/$(TARGET)/%.o: %.S +$(TARGET_OBJ_DIR)/%.o: %.S $(V1) mkdir -p $(dir $@) $(V1) echo %% $(notdir $<) "$(STDOUT)" $(V1) $(CROSS_CC) -c -o $@ $(ASFLAGS) $< @@ -377,8 +381,7 @@ $(VALID_TARGETS): clean: $(V0) echo "Cleaning $(TARGET)" $(V0) rm -f $(CLEAN_ARTIFACTS) - $(V0) rm -rf $(OBJECT_DIR)/$(TARGET) - $(V0) rm -f $(GENERATED_SETTINGS) + $(V0) rm -rf $(TARGET_OBJ_DIR) $(V0) echo "Cleaning $(TARGET) succeeded." ## clean_test : clean up all temporary / machine-generated files (tests) diff --git a/src/main/fc/settings.c b/src/main/fc/settings.c index 427635a85ad..a816fc1e85b 100644 --- a/src/main/fc/settings.c +++ b/src/main/fc/settings.c @@ -4,10 +4,11 @@ #include "common/string_light.h" #include "common/utils.h" -#include "fc/settings_generated.h" +#include "settings_generated.h" + #include "fc/settings.h" -#include "fc/settings_generated.c" +#include "settings_generated.c" void setting_get_name(const setting_t *val, char *buf) { diff --git a/src/main/fc/settings.h b/src/main/fc/settings.h index 10aeeb6a8d5..3c05d123c53 100644 --- a/src/main/fc/settings.h +++ b/src/main/fc/settings.h @@ -6,7 +6,7 @@ #include "config/parameter_group.h" -#include "fc/settings_generated.h" +#include "settings_generated.h" typedef struct lookupTableEntry_s { const char * const *values; diff --git a/src/utils/build_stamp.rb b/src/utils/build_stamp.rb index 5c75c2c451b..a067c4653ca 100644 --- a/src/utils/build_stamp.rb +++ b/src/utils/build_stamp.rb @@ -65,7 +65,9 @@ def hash_config output = File.join(@stamp_dir, "stamp") stdout, stderr = @compiler.run(input, output, ["-dM", "-E"]) File.delete(input) - File.delete(output) + if File.file?(output) + File.delete(output) + end return Digest::SHA1.hexdigest(stdout) end end diff --git a/src/utils/compiler.rb b/src/utils/compiler.rb index da62d1cb131..54979c193a7 100644 --- a/src/utils/compiler.rb +++ b/src/utils/compiler.rb @@ -80,7 +80,10 @@ def run(input, output, args = nil, options = { noerror: false }) if args all_args.push(*args) end - all_args << "-o" << output << input + if output + all_args << "-o" << output + end + all_args << input stdout, stderr, compile_status = Open3.capture3(join_args(all_args)) raise "Compiler error:\n#{all_args.join(' ')}\n#{stderr}" if not options[:noerror] and not compile_status.success? return stdout, stderr diff --git a/src/utils/settings.rb b/src/utils/settings.rb index 718484e7e6b..792744f5845 100644 --- a/src/utils/settings.rb +++ b/src/utils/settings.rb @@ -260,10 +260,10 @@ def encode_value(buf, val) OFF_ON_TABLE = Hash["name" => "off_on", "values" => ["OFF", "ON"]] class Generator - def initialize(src_root, settings_file) + def initialize(src_root, settings_file, output_dir) @src_root = src_root @settings_file = settings_file - @output_dir = File.dirname(settings_file) + @output_dir = output_dir || File.dirname(settings_file) @compiler = Compiler.new @@ -416,7 +416,7 @@ def write_impl_file(file) } add_header.call("platform.h") add_header.call("config/parameter_group_ids.h") - add_header.call("settings.h") + add_header.call("fc/settings.h") foreach_enabled_group do |group| (group["headers"] || []).each do |h| @@ -623,10 +623,12 @@ def mktmpdir # Use a temporary dir reachable by relative path # since g++ in cygwin fails to open files # with absolute paths - tmp = File.join("obj", "tmp") + tmp = File.join(@output_dir, "tmp") FileUtils.mkdir_p(tmp) unless File.directory?(tmp) value = yield(tmp) - FileUtils.remove_dir(tmp) + if File.directory?(tmp) + FileUtils.remove_dir(tmp) + end value end @@ -882,17 +884,20 @@ def usage exit(1) end - gen = Generator.new(src_root, settings_file) opts = GetoptLong.new( + [ "--output-dir", "-o", GetoptLong::REQUIRED_ARGUMENT ], [ "--help", "-h", GetoptLong::NO_ARGUMENT ], [ "--json", "-j", GetoptLong::REQUIRED_ARGUMENT ], ) jsonFile = nil + output_dir = nil opts.each do |opt, arg| case opt + when "--output-dir" + output_dir = arg when "--help" usage() exit(0) @@ -901,6 +906,8 @@ def usage end end + gen = Generator.new(src_root, settings_file, output_dir) + if jsonFile gen.write_json(jsonFile) else