Skip to content

Commit

Permalink
Makefile.include: add rule to verify thin archives.
Browse files Browse the repository at this point in the history
Thin archives should be created with relative paths so that archives created
in a build container are useful in the host. This check ensures there are no
absolute paths inside thin archives.
that there are
  • Loading branch information
jcarrano committed Oct 23, 2018
1 parent d49d30f commit 6a4246d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ $(BINDIR)/$(MODULE)/:

$(BINDIR)/$(MODULE).a $(OBJ): | $(BINDIR)/$(MODULE)/

relpath = $(shell realpath --relative-to=$(abspath .) $(1))

$(BINDIR)/$(MODULE).a: $(OBJ) | $(DIRS:%=ALL--%)
@# Recreate archive to cleanup deleted/non selected source files objects
$(Q)$(RM) $@
$(Q)$(AR) $(ARFLAGS) $(foreach f,$(abspath $@ $^),$(call relpath,$f))
$(Q)$(AR) $(ARFLAGS) $(foreach f,$(abspath $@ $^),$(call relpath,$(abspath .),$f))

CXXFLAGS = $(filter-out $(CXXUWFLAGS), $(CFLAGS)) $(CXXEXFLAGS)
CCASFLAGS = $(filter-out $(CCASUWFLAGS), $(CFLAGS)) $(CCASEXFLAGS)
Expand All @@ -99,6 +97,9 @@ $(ASMOBJ): $(BINDIR)/$(MODULE)/%.o: %.s
$(ASSMOBJ): $(BINDIR)/$(MODULE)/%.o: %.S $(RIOTBUILD_CONFIG_HEADER_C)
$(Q)$(CCAS) $(CCASFLAGS) $(INCLUDES) -MD -MP -c -o $@ $(abspath $<)

# Include utility library
include $(RIOTMAKE)/utils.inc.mk

# pull in dependency info for *existing* .o files
# deleted header files will be silently ignored
-include $(DEP)
38 changes: 38 additions & 0 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,41 @@ print-size: $(ELFFILE)

endif # BUILD_IN_DOCKER

# Rules to check the correctness of thin archives.

# Each ARCHECK file contains all the absolute paths found inside the archive.
BASELIB_ARCHECKS = $(patsubst %.a,%.a-check,$(filter %.a,$(BASELIBS)))

# For each a file, print the absolute paths found inside it
# If "ar t" is called with an absolute path it will print an abs path regardless
# of how the archive is internally
# In case of a malformed archive, ar prints to stderr (and sets an error code).
# Doing `2>&1` is hacky, the correct thing would be to get the exit code.
# The `| %.a` is necessary to be able to check file produced in docker.
%.a-check: %.a
$(Q)$(AR) t $(call relpath_here,$<) 2>&1 | grep '^/' | '$(LAZYSPONGE)' $(LAZYSPONGE_FLAGS) '$@'

ARCHIVE_CHECK = $(BINDIR)/$(APPLICATION).archive-check

$(ARCHIVE_CHECK): $(BASELIB_ARCHECKS)
$(Q)cat $^ | '$(LAZYSPONGE)' $(LAZYSPONGE_FLAGS) '$@'

# Rule to check if thin archives are correctly produced, that is, with a correct
# relative path.
ifeq ($(BUILD_IN_DOCKER),1)
archive-check: ..in-docker-container
else
archive-check: $(ARCHIVE_CHECK) FORCE
@if [ -s '$<' ] ; then \
$(COLOR_ECHO) '$(COLOR_RED)Found the following absolute paths in archives' ;\
cat '$<';\
elif [ -f '$<' ] ; then \
$(COLOR_ECHO) '$(COLOR_GREEN)Archives correctly formed' ;\
else \
$(COLOR_ECHO) '$(COLOR_RED)Unexpected error (file not found)' ;\
fi
endif # BUILD_IN_DOCKER

# Check given command is available in the path
# check_cmd 'command' 'description'
define check_cmd
Expand Down Expand Up @@ -709,6 +744,9 @@ CFLAGS := $(patsubst -D%,,$(CFLAGS))
CFLAGS := $(patsubst -U%,,$(CFLAGS))
CFLAGS += -include '$(RIOTBUILD_CONFIG_HEADER_C)'

# include utility library
include $(RIOTMAKE)/utils.inc.mk

# include mcuboot support
include $(RIOTMAKE)/mcuboot.mk

Expand Down
1 change: 1 addition & 0 deletions makefiles/docker.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export DOCKER_MAKECMDGOALS_POSSIBLE = \
scan-build \
scan-build-analyze \
tests-% \
archive-check \
#
export DOCKER_MAKECMDGOALS = $(filter $(DOCKER_MAKECMDGOALS_POSSIBLE),$(MAKECMDGOALS))

Expand Down
2 changes: 2 additions & 0 deletions makefiles/utils.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ test-relpath-negtive:
echo $(call relpath,/,/)
echo $(call relpath,/1/2/3,/1/2/3)

# Return a path relative to the current directory
relpath_here = $(call relpath,$(abspath .),$(abspath $(1)))

0 comments on commit 6a4246d

Please sign in to comment.