Skip to content

Commit

Permalink
makefiles/blob.inc.mk: introduce blob -> hdr utility makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspar030 committed Nov 18, 2019
1 parent 5a70576 commit a100c1c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ ASSMOBJ := $(ASSMSRC:%.S=$(BINDIR)/$(MODULE)/%.o)
OBJ := $(OBJC) $(OBJCXX) $(ASMOBJ) $(ASSMOBJ) $(GENOBJC)
DEP := $(OBJC:.o=.d) $(OBJCXX:.o=.d) $(ASSMOBJ:.o=.d)

include $(RIOTMAKE)/blob.inc.mk

$(BINDIR)/$(MODULE)/:
$(Q)mkdir -p $@

Expand Down
65 changes: 65 additions & 0 deletions makefiles/blob.inc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# makes any file in BLOBS available via '#include "blob/<filename>.h"
#
# # Usage:
#
# Add this to an application or module Makefile:
#
# BLOBS += foo.ext
#
# Then include in C file or header:
#
# #include "blob/foo.ext.h"
#
# The blob can then be accessed using the symbols "foo_ext" and "foo_ext_len".
#
# # Subfolders
#
# It is possible to add files from subfolders to BLOBS:
#
# BLOBS += subfolder/my_file.foo
#
# The subfolder will be part of the generated header's path, but *not* of the
# generated symbols. E.g., above blob would be made available by including
# "blobs/subfolder/my_file.ext.h", which would define the symbols "my_file_ext"
# and "my_file_ext_len". Beware possible symbol name clashes.
#

# use "blobs/blob" so the headers can be included as "blob/foo.h", but
# we don't have to add $(BINDIR)/$(MODULE) to the include path.
BLOB_HDR_DIR ?= $(BINDIR)/$(MODULE)/blobs/blob
BLOB_H := $(BLOBS:%=$(BLOB_HDR_DIR)/%.h)

ifneq (, $(BLOB_H))
# add $(BINDIR)/$(MODULE)/blobs to include path
CFLAGS += -I$(dir $(BLOB_HDR_DIR))
endif

# In order to allow automatic folder creation in subtrees,
# this Makefile makes use SECONDEXPANSION, PRECIOUS and
# order only prerequisites.
# Folder targets are "tagged" by appending "/."
# The final recipe uses "| $$(@D)/." to create necessary trees.
#
# Inspiration from:
# http://ismail.badawi.io/blog/2017/03/28/automatic-directory-creation-in-make/
#
.PRECIOUS: $(BLOB_HDR_DIR)/. $(BLOB_HDR_DIR)%/.

$(BLOB_HDR_DIR)/.:
@mkdir -p $@

$(BLOB_HDR_DIR)%/.:
@mkdir -p $@

XXD ?= xxd

$(BLOB_H): $(BLOB_HDR_DIR)/%.h: % $(BLOBS) | $$(@D)/.
@command -v $(XXD) > /dev/null 2>&1 || ( \
echo "error: xxd binary '$(XXD)' not found! (Maybe install vim package?)"; exit 1)

${Q}cd $(dir $<); xxd -i $(notdir $<) | sed 's/^unsigned/const unsigned/g'> $@

# make C and C++ objects of this module depend on generated headers, so they
# get re-build on changes to the blob files.
$(OBJC) $(OBJCXX): $(BLOB_H)

0 comments on commit a100c1c

Please sign in to comment.