Newer
Older
barebox / scripts / Makefile.lib
# Backward compatibility - to be removed...
extra-y	+= $(EXTRA_TARGETS)
# Figure out what we need to build from the various variables
# ===========================================================================

# When an object is listed to be built compiled-in and modular,
# only build the compiled-in version

obj-m := $(filter-out $(obj-y),$(obj-m))

# Libraries are always collected in one lib file.
# Filter out objects already built-in

lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))


pbl-y		+= $(pbl-dtb-y)
obj-y		+= $(obj-dtb-y)
extra-y		+= $(patsubst %.dtb.o,%.dtb.S,$(obj-dtb-y))
extra-y		+= $(patsubst %.dtb.o,%.dtb,$(obj-dtb-y))
extra-y		+= $(patsubst %.dtb.o,%.dtb.S,$(pbl-dtb-y))
extra-y		+= $(patsubst %.dtb.o,%.dtb,$(pbl-dtb-y))

# Handle objects in subdirs
# ---------------------------------------------------------------------------
# o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
#   and add the directory to the list of dirs to descend into: $(subdir-y)
# o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
#   and add the directory to the list of dirs to descend into: $(subdir-m)

# lowlevel is present in the PBL if enabled
# otherwise in barebox
ifeq ($(CONFIG_PBL_IMAGE), y)
pbl-y		+= $(lwl-y)
else
obj-y		+= $(lwl-y)
endif

obj-y		+= $(obj-pbl-y)
pbl-y		+= $(obj-pbl-y)

# add pbl- prefix to the target
pbl-y		:= $(shell echo $(pbl-y) | sed -e 's%\(\([^ \t]\+/\)*\)\([^ \t]*\.o\)%\2pbl-\3%g')

# add subdir from $(obj-y) too so we do not need to have the dir define in
# both $(obj-y) and $(pbl-y)
__pbl-y		:= $(filter-out $(pbl-y), $(filter %/, $(obj-y)))
pbl-y		+= $(__pbl-y)

pbl-y		:= $(sort $(patsubst %/, %/built-in-pbl.o, $(pbl-y)))

__subdir-y	:= $(patsubst %/,%,$(filter %/, $(obj-y)))
subdir-y	+= $(__subdir-y)
__subdir-m	:= $(patsubst %/,%,$(filter %/, $(obj-m)))
subdir-m	+= $(__subdir-m)
obj-y		:= $(patsubst %/, %/built-in.o, $(obj-y))
obj-m		:= $(filter-out %/, $(obj-m))

# Subdirectories we need to descend into

subdir-ym	:= $(sort $(subdir-y) $(subdir-m))

# if $(foo-objs) exists, foo.o is a composite object
multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
multi-used   := $(multi-used-y) $(multi-used-m)
single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))

# Build list of the parts of our composite objects, our composite
# objects depend on those (obviously)
multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
multi-objs   := $(multi-objs-y) $(multi-objs-m)

# $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to
# tell kbuild to descend
__subdir-obj-y := $(filter %/built-in-pbl.o, $(pbl-y))
subdir-obj-y := $(filter %/built-in.o, $(obj-y))
subdir-obj-y += $(__subdir-obj-y)

# $(obj-dirs) is a list of directories that contain object files
obj-dirs := $(dir $(multi-objs) $(obj-y) $(pbl-y))

# Replace multi-part objects by their individual parts, look at local dir only
real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))

# Add subdir path

extra-y		:= $(addprefix $(obj)/,$(extra-y))
always		:= $(addprefix $(obj)/,$(always))
targets		:= $(addprefix $(obj)/,$(targets))
obj-y		:= $(addprefix $(obj)/,$(obj-y))
obj-m		:= $(addprefix $(obj)/,$(obj-m))
lib-y		:= $(addprefix $(obj)/,$(lib-y))
pbl-y		:= $(addprefix $(obj)/,$(pbl-y))
subdir-obj-y	:= $(addprefix $(obj)/,$(subdir-obj-y))
real-objs-y	:= $(addprefix $(obj)/,$(real-objs-y))
real-objs-m	:= $(addprefix $(obj)/,$(real-objs-m))
single-used-m	:= $(addprefix $(obj)/,$(single-used-m))
multi-used-y	:= $(addprefix $(obj)/,$(multi-used-y))
multi-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
multi-objs-y	:= $(addprefix $(obj)/,$(multi-objs-y))
multi-objs-m	:= $(addprefix $(obj)/,$(multi-objs-m))
subdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
obj-dirs	:= $(addprefix $(obj)/,$(obj-dirs))
bbenv-y		:= $(addprefix $(obj)/,$(bbenv-y))

# These flags are needed for modversions and compiling, so we define them here
# already
# $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
# end up in (or would, if it gets compiled in)
# Note: It's possible that one object gets potentially linked into more
#       than one module. In that case KBUILD_MODNAME will be set to foo_bar,
#       where foo and bar are the name of the modules.
name-fix = $(subst $(comma),_,$(subst -,_,$1))
basename_flags = -D"KBUILD_BASENAME=KBUILD_STR($(call name-fix,$(basetarget)))"
modname_flags  = $(if $(filter 1,$(words $(modname))),\
                 -D"KBUILD_MODNAME=KBUILD_STR($(call name-fix,$(modname)))")

_c_flags       = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(basetarget).o)
_a_flags       = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(basetarget).o)
_cpp_flags     = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))

# If building barebox in a separate objtree expand all occurrences
# of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').

ifeq ($(KBUILD_SRC),)
__c_flags	= $(_c_flags)
__a_flags	= $(_a_flags)
__cpp_flags     = $(_cpp_flags)
else

# -I$(obj) locates generated .h files
# $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
#   and locates generated .h files
# FIXME: Replace both with specific CFLAGS* statements in the makefiles
__c_flags	= $(call addtree,-I$(obj)) $(call flags,_c_flags)
__a_flags	=                          $(call flags,_a_flags)
__cpp_flags     =                          $(call flags,_cpp_flags)
endif

c_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
		 $(__c_flags) $(modkern_cflags) \
		 -D"KBUILD_STR(s)=\#s" $(basename_flags) $(modname_flags)

a_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
		 $(__a_flags) $(modkern_aflags)

cpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)

ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)

dtc_cpp_flags  = -Wp,-MD,$(depfile).pre -nostdinc                        \
		 -I$(srctree)/arch/$(SRCARCH)/dts/include		 \
		 -I$(srctree)/dts/include                                \
		 -I$(srctree)/include                                    \
		 -I$(srctree)/dts/src/                                   \
		 -undef -D__DTS__

ifdef CONFIG_BOOTM_FITIMAGE_PUBKEY
ifneq ($(CONFIG_BOOTM_FITIMAGE_PUBKEY),"")
dtc_cpp_flags += -DCONFIG_BOOTM_FITIMAGE_PUBKEY=\"$(CONFIG_BOOTM_FITIMAGE_PUBKEY)\"
endif
endif

# Finds the multi-part object the current object will be linked into
modname-multi = $(sort $(foreach m,$(multi-used),\
		$(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))

# Useful for describing the dependency of composite objects
# Usage:
#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
define multi_depend
$(foreach m, $(notdir $1), \
	$(eval $(obj)/$m: \
	$(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
endef

# LEX
# ---------------------------------------------------------------------------
quiet_cmd_flex = LEX     $@
      cmd_flex = $(LEX) -o$@ -L $<

$(obj)/%.lex.c: $(src)/%.l FORCE
	$(call if_changed,flex)

# YACC
# ---------------------------------------------------------------------------
quiet_cmd_bison = YACC    $@
      cmd_bison = $(YACC) -o$@ -t -l $<

$(obj)/%.tab.c: $(src)/%.y FORCE
	$(call if_changed,bison)

quiet_cmd_bison_h = YACC    $@
      cmd_bison_h = $(YACC) -o/dev/null --defines=$@ -t -l $<

$(obj)/%.tab.h: $(src)/%.y FORCE
	$(call if_changed,bison_h)

# Shipped files
# ===========================================================================

quiet_cmd_shipped = SHIPPED $@
cmd_shipped = cat $< > $@

$(obj)/%:: $(src)/%_shipped
	$(call cmd,shipped)

# Commands useful for building a boot image
# ===========================================================================
#
#	Use as following:
#
#	target: source(s) FORCE
#		$(if_changed,ld/objcopy/gzip)
#
#	and add target to EXTRA_TARGETS so that we know we have to
#	read in the saved command line

# Linking
# ---------------------------------------------------------------------------

quiet_cmd_ld = LD      $@
cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
	       $(filter-out FORCE,$^) -o $@

# Objcopy
# ---------------------------------------------------------------------------

quiet_cmd_objcopy = OBJCOPY $@
cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@

# Gzip
# ---------------------------------------------------------------------------

quiet_cmd_gzip = GZIP    $@
cmd_gzip = (cat $(filter-out FORCE,$^) | gzip -n -f -9 > $@) || \
	(rm -f $@ ; false)

%.gz: %
	$(call if_changed,gzip)

# XZ
# ---------------------------------------------------------------------------
# Use xzkern to compress the kernel image and xzmisc to compress other things.
#
# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
# of the kernel decompressor. A BCJ filter is used if it is available for
# the target architecture. xzkern also appends uncompressed size of the data
# using size_append. The .xz format has the size information available at
# the end of the file too, but it's in more complex format and it's good to
# avoid changing the part of the boot code that reads the uncompressed size.
# Note that the bytes added by size_append will make the xz tool think that
# the file is corrupt. This is expected.
#
# xzmisc doesn't use size_append, so it can be used to create normal .xz
# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
# big dictionary would increase the memory usage too much in the multi-call
# decompression mode. A BCJ filter isn't used either.
quiet_cmd_xzkern = XZKERN  $@
cmd_xzkern = (cat $(filter-out FORCE,$^) | \
	sh $(srctree)/scripts/xz_wrap.sh && \
	$(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

quiet_cmd_xzmisc = XZMISC  $@
cmd_xzmisc = (cat $(filter-out FORCE,$^) | \
	xz --check=crc32 --lzma2=dict=1MiB) > $@ || \
	(rm -f $@ ; false)

%.xzkern: %
	$(call if_changed,xzkern)

# DTC
# ---------------------------------------------------------------------------

DTC_FLAGS += -Wno-unit_address_vs_reg \
	 -Wno-unit_address_format \
	 -Wno-avoid_unnecessary_addr_size \
	 -Wno-alias_paths \
	 -Wno-pci_device_reg

# Generate an assembly file to wrap the output of the device tree compiler
quiet_cmd_dt_S_dtb = DTB     $@
cmd_dt_S_dtb = $(srctree)/scripts/gen-dtb-s $(subst -,_,$(*F)) $< $(CONFIG_IMD) > $@
$(obj)/%.dtb.S: $(obj)/%.dtb $(srctree)/scripts/gen-dtb-s FORCE
	$(call if_changed,dt_S_dtb)

quiet_cmd_dtc = DTC     $@
cmd_dtc = $(CPP) $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
	$(objtree)/scripts/dtc/dtc -O dtb -o $@ -b 0 \
		-i $(srctree)/arch/$(SRCARCH)/dts $(DTC_FLAGS) \
		-i $(srctree)/dts/src/$(SRCARCH) \
		-d $(depfile).dtc $(dtc-tmp) ; \
	cat $(depfile).pre $(depfile).dtc > $(depfile)

$(obj)/%.dtb: $(src)/%.dts FORCE
	$(call if_changed_dep,dtc)

dtc-tmp = $(subst $(comma),_,$(dot-target).dts)

obj-y += $(patsubst %,%.bbenv$(DEFAULT_COMPRESSION_SUFFIX).o,$(bbenv-y))
extra-y += $(patsubst %,%.bbenv$(DEFAULT_COMPRESSION_SUFFIX),$(bbenv-y))
extra-y += $(patsubst %,%.bbenv$(DEFAULT_COMPRESSION_SUFFIX).S,$(bbenv-y))
extra-y += $(patsubst %,%.bbenv$(DEFAULT_COMPRESSION_SUFFIX).o,$(bbenv-y))

quiet_cmd_env_S = ENV.S   $@
cmd_env_S =							\
(								\
	echo '\#include <asm-generic/barebox.lds.h>';		\
	echo '.section .bbenv.rodata.$(subst -,_,$(*F)),"a"';	\
	echo '.balign STRUCT_ALIGNMENT';			\
	echo '.global __bbenv_$(subst -,_,$(*F))_start';	\
	echo '__bbenv_$(subst -,_,$(*F))_start:';		\
	echo '.incbin "$<" ';					\
	echo '__bbenv_$(subst -,_,$(*F))_end:';			\
	echo '.global __bbenv_$(subst -,_,$(*F))_end';		\
	echo '.balign STRUCT_ALIGNMENT';			\
) > $@

$(obj)/%.bbenv$(DEFAULT_COMPRESSION_SUFFIX).S: $(src)/%.bbenv$(DEFAULT_COMPRESSION_SUFFIX) FORCE
	$(call if_changed,env_S)

quiet_cmd_env = ENV     $@
cmd_env=$(srctree)/scripts/genenv $(srctree) $(objtree) $@ $<

%.bbenv$(DEFAULT_COMPRESSION_SUFFIX): % FORCE
	$(call if_changed,env)

# Bzip2
# ---------------------------------------------------------------------------

# Bzip2 and LZMA do not include size in file... so we have to fake that;
# append the size as a 32-bit littleendian number as gzip does.
size_append = printf $(shell						\
dec_size=0;								\
for F in $1; do								\
	fsize=$$(stat -c "%s" $$F);					\
	dec_size=$$(expr $$dec_size + $$fsize);				\
done;									\
printf "%08x\n" $$dec_size |						\
	sed 's/\(..\)/\1 /g' | {					\
		read ch0 ch1 ch2 ch3;					\
		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
			printf '%s%03o' '\\' $$((0x$$ch)); 		\
		done;							\
	}								\
)

quiet_cmd_bzip2 = BZIP2   $@
cmd_bzip2 = (cat $(filter-out FORCE,$^) | \
	bzip2 -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

%.bz2: %
	$(call if_changed,bzip2)

# Lzma
# ---------------------------------------------------------------------------

quiet_cmd_lzma = LZMA    $@
cmd_lzma = (cat $(filter-out FORCE,$^) | \
	lzma -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

%.lzma: %
	$(call if_changed,lzma)

quiet_cmd_lzo = LZO     $@
cmd_lzo = (cat $(filter-out FORCE,$^) | \
	lzop -9 && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

%.lzo: %
	$(call if_changed,lzo)

# XZ
# ---------------------------------------------------------------------------
# Use xzkern to compress the kernel image and xzmisc to compress other things.
#
# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
# of the kernel decompressor. A BCJ filter is used if it is available for
# the target architecture. xzkern also appends uncompressed size of the data
# using size_append. The .xz format has the size information available at
# the end of the file too, but it's in more complex format and it's good to
# avoid changing the part of the boot code that reads the uncompressed size.
# Note that the bytes added by size_append will make the xz tool think that
# the file is corrupt. This is expected.
#
# xzmisc doesn't use size_append, so it can be used to create normal .xz
# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
# big dictionary would increase the memory usage too much in the multi-call
# decompression mode. A BCJ filter isn't used either.
quiet_cmd_xzkern = XZKERN  $@
cmd_xzkern = (cat $(filter-out FORCE,$^) | \
	sh $(srctree)/scripts/xz_wrap.sh && \
	$(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

quiet_cmd_xzmisc = XZMISC  $@
cmd_xzmisc = (cat $(filter-out FORCE,$^) | \
	xz --check=crc32 --lzma2=dict=1MiB) > $@ || \
	(rm -f $@ ; false)

# lz4
# ---------------------------------------------------------------------------

quiet_cmd_lz4 = LZ4     $@
cmd_lz4 = (cat $(filter-out FORCE,$^) | \
	lz4c -l -c1 stdin stdout && $(call size_append, $(filter-out FORCE,$^))) > $@ || \
	(rm -f $@ ; false)

%.lz4: %
	$(call if_changed,lz4)

# comp_copy
# ---------------------------------------------------------------------------
# Wrapper which only copies a file, but compatible to the compression
# functions above. Appends the size to the result file
quiet_cmd_comp_copy ?= SHIPPED_S $@
cmd_comp_copy ?= cat $(filter-out FORCE,$^) > $@; \
                 $(call size_append, $(filter-out FORCE,$^)) >> $@

quiet_cmd_disasm = DISASM  $@
cmd_disasm = $(OBJDUMP) -d $< > $@

quiet_cmd_ln = LN      $@
cmd_ln = ln -sf $< $@

# Check size of a file
quiet_cmd_check_file_size = CHKSIZE $2
      cmd_check_file_size = set -e;					\
	size=`stat -c%s $2`;						\
	max_size=`printf "%d" $3`;					\
	if [ $$size -gt $$max_size ] ;					\
	then								\
		echo "$@ size $$size > of the maximum size $$max_size" >&2;	\
		exit 1 ;						\
	fi;

quiet_cmd_imximage__S_dcd= DCD_S   $@
cmd_imximage_S_dcd=						\
(								\
	echo '\#include <asm-generic/barebox.lds.h>';		\
	echo '.balign STRUCT_ALIGNMENT';			\
	echo '.global $(subst -,_,$(*F))_start';		\
	echo '$(subst -,_,$(*F))_start:';			\
	echo '.incbin "$<" ';					\
	echo '$(subst -,_,$(*F))_end:';				\
	echo '.global $(subst -,_,$(*F))_end';			\
	echo '.balign STRUCT_ALIGNMENT';			\
) > $@

overwrite-hab-env = $(shell set -e; \
      test -n "$(CONFIG_HAB_CERTS_ENV)"; \
      test -n "$$$(1)"; \
      echo -D$(1)=\"$(shell echo $$$(1))\")

imxcfg_cpp_flags  = -Wp,-MD,$(depfile) -nostdinc -x assembler-with-cpp \
      -I $(srctree)/include -I $(srctree)/arch/arm/mach-imx/include \
      -include include/generated/autoconf.h \
      $(call overwrite-hab-env,CONFIG_HABV3_SRK_PEM) \
      $(call overwrite-hab-env,CONFIG_HABV3_CSF_CRT_DER) \
      $(call overwrite-hab-env,CONFIG_HABV3_IMG_CRT_DER) \
      $(call overwrite-hab-env,CONFIG_HABV4_TABLE_BIN) \
      $(call overwrite-hab-env,CONFIG_HABV4_CSF_CRT_PEM) \
      $(call overwrite-hab-env,CONFIG_HABV4_IMG_CRT_PEM)

dcd-tmp = $(subst $(comma),_,$(dot-target).dcd.tmp)

quiet_cmd_dcd = DCD     $@
      cmd_dcd = $(CPP) $(imxcfg_cpp_flags) -o $(dcd-tmp) $< ; \
		$(objtree)/scripts/imx/imx-image -d -o $@ -c $(dcd-tmp)

$(obj)/%.dcd: $(obj)/%.imxcfg FORCE
	$(call if_changed,dcd)

$(obj)/%.dcd.S: $(obj)/%.dcd
	$(call cmd,imximage_S_dcd)

imximg-tmp = $(subst $(comma),_,$(dot-target).imxcfg.tmp)

quiet_cmd_imx_image = IMX-IMG $@
      cmd_imx_image = $(CPP) $(imxcfg_cpp_flags) -o $(imximg-tmp) $(2) ; \
		      $(objtree)/scripts/imx/imx-image -o $@ -b -c $(imximg-tmp) $(3) -f $<

quiet_cmd_kwb_image = KWB     $@
      cmd_kwb_image = scripts/kwbimage -p $< $(OPTS_$(@F)) -o $@

quiet_cmd_cboot_bct = BCT     $@
      cmd_cboot_bct = $(objtree)/scripts/tegra/cbootimage -gbct -s $(soc) $< $@

$(obj)/%.bct: $(obj)/%.bct.cfg
	$(call cmd,cboot_bct)

quiet_cmd_b64dec = B64DEC  $@
      cmd_b64dec = base64 -d $< > $@

%: %.base64
	$(call cmd,b64dec)