Skip to content

Commit

Permalink
Move build-stamp and generatic files to obj/main/$(TARGET)
Browse files Browse the repository at this point in the history
This allows target level parallel builds, since now there are no
shared files between targets.

Fixes #3261
  • Loading branch information
fiam committed May 25, 2018
1 parent 46f0ca8 commit bf0f05a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 28 deletions.
37 changes: 20 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -285,28 +286,31 @@ 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)

# Use a pattern rule, since they're different than normal rules.
# 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
Expand All @@ -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) $<
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/main/fc/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/fc/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/build_stamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/utils/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions src/utils/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -901,6 +906,8 @@ def usage
end
end

gen = Generator.new(src_root, settings_file, output_dir)

if jsonFile
gen.write_json(jsonFile)
else
Expand Down

0 comments on commit bf0f05a

Please sign in to comment.