diff mbox

[OpenWrt-Devel,01/13] build metadata: Allow to build a subset of profiles in a single build

Message ID 1453171813-36623-2-git-send-email-openwrt@daniel.thecshore.com
State Superseded
Headers show

Commit Message

Daniel Dickinson Jan. 19, 2016, 2:50 a.m. UTC
From: Daniel Dickinson <openwrt@daniel.thecshore.com>

Certain platforms have large numbers of possible images, and it can be
desirable to build neither all images nor only a single image,
therefore this patch makes selecting target profiles a menu instead of a
single choice, which allows the user to build a specific subset of all
possible images for a target.

This patch contains only the build machinery changes to support
selecting multiple images and the .config generator, but does
not include profile changes that are needed to make selecting
multiple profiles possible in menuconfig.

This is done to isolate the changes as much as possible, although
obviously this patch touches a greaty many files due to changing
the image generation logic (which is necessary in order to
eliminate the single-valued PROFILE variable, which gets in the
way of allowing multiple images to be built at once.

Signed-off-by: Daniel Dickinson <openwrt@daniel.thecshore.com>
---
 include/image.mk                     | 60 +++++++++++++++-----------
 include/target.mk                    |  7 ++-
 scripts/metadata.pl                  | 83 ++++++++++++++++++++++++++++++++++--
 scripts/metadata.pm                  |  5 +++
 target/imagebuilder/files/Makefile   |  5 ++-
 target/linux/adm5120/image/Makefile  |  4 +-
 target/linux/ar71xx/image/Makefile   | 13 +++---
 target/linux/at91/image/Makefile     |  2 +-
 target/linux/au1000/image/Makefile   |  2 +-
 target/linux/brcm47xx/image/Makefile |  2 +-
 target/linux/cns3xxx/image/Makefile  |  2 +-
 target/linux/gemini/image/Makefile   |  4 +-
 target/linux/ipq806x/image/Makefile  |  2 +-
 target/linux/ixp4xx/image/Makefile   |  2 +-
 target/linux/kirkwood/image/Makefile | 62 +++++++++++++--------------
 target/linux/lantiq/image/Makefile   | 43 +++++++++----------
 target/linux/malta/image/Makefile    |  2 +-
 target/linux/mcs814x/image/Makefile  |  4 +-
 target/linux/mediatek/image/Makefile |  2 +-
 target/linux/mpc85xx/image/Makefile  |  6 +--
 target/linux/mvebu/image/Makefile    | 10 ++---
 target/linux/mxs/image/Makefile      |  4 +-
 target/linux/netlogic/image/Makefile |  2 +-
 target/linux/octeon/image/Makefile   |  2 +-
 target/linux/omap/image/Makefile     |  2 +-
 target/linux/omap24xx/image/Makefile |  2 +-
 target/linux/oxnas/image/Makefile    | 22 +++++-----
 target/linux/ppc40x/image/Makefile   |  4 +-
 target/linux/ppc44x/image/Makefile   |  2 +-
 target/linux/ramips/image/Makefile   |  8 ++--
 target/linux/rb532/image/Makefile    |  2 +-
 target/linux/realview/image/Makefile |  6 +--
 target/linux/sunxi/image/Makefile    | 51 +++++++++++-----------
 target/linux/x86/image/Makefile      | 14 +++---
 34 files changed, 271 insertions(+), 172 deletions(-)

Comments

Felix Fietkau Jan. 19, 2016, 10:04 a.m. UTC | #1
On 2016-01-19 03:50, openwrt@daniel.thecshore.com wrote:
> From: Daniel Dickinson <openwrt@daniel.thecshore.com>
> 
> Certain platforms have large numbers of possible images, and it can be
> desirable to build neither all images nor only a single image,
> therefore this patch makes selecting target profiles a menu instead of a
> single choice, which allows the user to build a specific subset of all
> possible images for a target.
> 
> This patch contains only the build machinery changes to support
> selecting multiple images and the .config generator, but does
> not include profile changes that are needed to make selecting
> multiple profiles possible in menuconfig.
> 
> This is done to isolate the changes as much as possible, although
> obviously this patch touches a greaty many files due to changing
> the image generation logic (which is necessary in order to
> eliminate the single-valued PROFILE variable, which gets in the
> way of allowing multiple images to be built at once.
> 
> Signed-off-by: Daniel Dickinson <openwrt@daniel.thecshore.com>
> ---
> diff --git a/include/image.mk b/include/image.mk
> index 4eee4ad..bb46bb9 100644
> --- a/include/image.mk
> +++ b/include/image.mk
> @@ -192,7 +191,7 @@ ifneq ($(CONFIG_NAND_SUPPORT),)
>  # $(1) board name
>  # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
>  # $(3) rootfstype (e.g. squashfs or ubifs)
> -# $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
> +# $(4) options to pass-through to ubinize (i.e. $(<profile>_UBI_OPTS)))
>     define Image/Build/UbinizeImage
>  	sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
>  		"$(KDIR)/root.$(3)" \
> @@ -203,12 +202,13 @@ ifneq ($(CONFIG_NAND_SUPPORT),)
>  endif
>  
>  ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
> +# $(1) profile
>      define Image/mkfs/ubifs/generate
>  	$(CP) ./ubinize$(1).cfg $(KDIR)
>  	( cd $(KDIR); \
>  		$(STAGING_DIR_HOST)/bin/ubinize \
> -		$(if $($(PROFILE)_UBI_OPTS), \
> -			$(shell echo $($(PROFILE)_UBI_OPTS)), \
> +		$(if $($(1)_UBI_OPTS), \
> +			$(shell echo $($(1)_UBI_OPTS)), \
>  			$(shell echo $(UBI_OPTS)) \
>  		) \
>  		-o $(KDIR)/root$(1).ubi \
> @@ -216,12 +216,13 @@ ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
>  	)
>      endef
>  
> +# $(1) profile
>      define Image/mkfs/ubifs
>  
> -        ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
> +        ifneq ($($(1)_UBIFS_OPTS)$(UBIFS_OPTS),)
>  		$(STAGING_DIR_HOST)/bin/mkfs.ubifs \
> -			$(if $($(PROFILE)_UBIFS_OPTS), \
> -				$(shell echo $($(PROFILE)_UBIFS_OPTS)), \
> +			$(if $($(1)_UBIFS_OPTS), \
> +				$(shell echo $($(1)_UBIFS_OPTS)), \
>  				$(shell echo $(UBIFS_OPTS)) \
>  			) \
>  			$(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
> @@ -233,13 +234,13 @@ ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
>  			-o $(KDIR)/root.ubifs \
>  			-d $(TARGET_DIR)
>          endif
> -	$(call Image/Build,ubifs)
> +	$(call Image/Build,ubifs,$(1))
>  
> -        ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
> +        ifneq ($($(1)_UBI_OPTS)$(UBI_OPTS),)
>  		$(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
>  		$(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
>          endif
> -	$(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
> +	$(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi,$(1)))
>      endef
>  endif
>
Please don't add extra abstractions to this code. The
$(PROFILE)_UBI_OPTS stuff is legacy crap and any target still using it
should just stay that way and not get multi-profile selection until it
is converted to the new code.

> @@ -248,7 +249,8 @@ define Image/mkfs/cpiogz
>  endef
>  
>  define Image/mkfs/targz
> -	$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 --sort=name -C $(TARGET_DIR)/ .
> +# Move to Image/Build to eliminate profile from mkfs
> +	touch $(KDIR)/root.targz
>  endef
>  
>  E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
> @@ -293,6 +295,11 @@ define BuildImage/mkfs
>  
>  endef
>  
> +# $(1) profile
> +define Image/Build/targz
> +	$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(call sanitize,$(1)),-$(call sanitize,$(1)))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 --sort=name -C $(TARGET_DIR)/ .
> +endef
> +
>  # Build commands that can be called from Device/* templates
>  define Build/uImage
>  	mkimage -A $(LINUX_KARCH) \
> @@ -547,6 +554,12 @@ define Device
>  
>  endef
>  
> +# $(1) macro
> +# $(2) macro parameter
> +define BuildForProfiles
> +  $(foreach profile,$(PROFILES_BUILD),$(call $(1),$(2),$(profile)))
> +endef
Please don't call all the legacy Image/* code once per profile.
This will generate unnecessary duplicate files and extra build time.
If you rework targets to just stop using $(PROFILE) and check the
profile enabled/disabled variables directly, you don't need this at all.

> @@ -565,29 +578,28 @@ define BuildImage
>  
>      image_prepare: compile
>  		mkdir -p $(KDIR)/tmp
> -		$(call Image/Prepare)
> +		$(call BuildForProfiles,Image/Prepare)
>    else
>      image_prepare:
>  		mkdir -p $(KDIR)/tmp
>    endif
>  
>    mkfs_prepare: image_prepare
> -	$(call Image/mkfs/prepare)
> +	$(call BuildForProfiles,Image/mkfs/prepare)
>  
>    kernel_prepare: mkfs_prepare
> -	$(call Image/BuildKernel)
> -	$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
> -	$(call Image/InstallKernel)
> +	$(call BuildForProfiles,Image/BuildKernel)
> +	$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call BuildForProfiles,Image/BuildKernel/Initramfs)))
> +	$(call BuildForProfiles,Image/InstallKernel)
>  
>    $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
>    $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
>  
>    install: kernel_prepare
>  	$(foreach fs,$(TARGET_FILESYSTEMS),
> -		$(call Image/Build,$(fs))
> +		$(call BuildForProfiles,Image/Build,$(fs))
>  	)
> -	$(call Image/mkfs/ubifs)
> +	$(foreach profile,$(PROFILES_BUILD),$(call Image/mkfs/ubifs,$(profile)))
>  	$(call Image/Checksum,md5sum --binary,md5sums)
>  	$(call Image/Checksum,openssl dgst -sha256,sha256sums)
> -
>  endef
All of the above can be removed, as explained earlier.

> diff --git a/include/target.mk b/include/target.mk
> index f129298..92e52db 100644
> --- a/include/target.mk
> +++ b/include/target.mk
> @@ -70,6 +70,11 @@ define Profile
>    DUMPINFO += \
>  	echo "Target-Profile: $(1)"; \
>  	echo "Target-Profile-Name: $(NAME)"; \
> +	echo "Target-Profile-Type: $(if $(filter All,$(1)),all,$(if $(filter Minimal,$(1)),minimal,normal))"; \
> +	echo "Target-Profile-Skip: $(if $(filter Default,$(1)),$(if $(PROFILE_SKIP_DEFAULT),1,0),$(if $(PROFILE_SKIP),1,0))"; \
> +	echo "Target-Profile-Skip-Single: $(if $(filter All,$(1)),$(if $(PROFILE_SKIP_SINGLE),1,0),0)"; \
> +	echo "Target-Profile-Default: $(if $(filter All,$(1)),1,$(if $(filter Default,$(1)),1,0))"; \
> +	echo "Target-Profile-Build-If-All: $(if $(filter All,$(1)),0,$(if $(filter Minimal,$(1)),0,1))"; \
Please get rid of the magic flag setting based on the profile name. Make
these explicit by setting variables in the appropriate profiles.
Also, the types and flags are rather confusing here. From the metadata
processing point of view, there should be nothing special about profiles
named 'Default' and 'Minimal' aside from a few simple distinctions:
There should be one profile that is enable by default
There should be a distinction between device profiles and profiles that
build all (or most) devices.
If you write the code to clearly handle the above, this should make the
rest of your patch less confusing.

Now that I read your patches again, I get the sense that I disagree with
part of your changes on a conceptual level. I think giving the
impression that selecting multiple entries in the profile list will
'Build multiple profiles' seems wrong to me, making the description
'Build all profiles' even more wrong. The code is not doing that,
because that would imply having a different package selection for every
profile build, and the build system can't do that.

I think a cleaner description would be building for all devices or a
subset of the devices on the list, and just not feed the impression that
it has that much to do with 'profiles' at all.

Essentially, this is nothing but a simple way to make an image that is
compatible with a specific set of devices, and we should present it to
the user that way.

- Felix
Daniel Dickinson Jan. 19, 2016, 11:21 a.m. UTC | #2
On 19/01/16 05:04 AM, Felix Fietkau wrote:

[snip]

[legacy UBI_OPTS targets]

>>
> Please don't add extra abstractions to this code. The
> $(PROFILE)_UBI_OPTS stuff is legacy crap and any target still using it
> should just stay that way and not get multi-profile selection until it
> is converted to the new code.

Okay.

>> +# $(1) macro
>> +# $(2) macro parameter
>> +define BuildForProfiles
>> +  $(foreach profile,$(PROFILES_BUILD),$(call $(1),$(2),$(profile)))
>> +endef
> Please don't call all the legacy Image/* code once per profile.
> This will generate unnecessary duplicate files and extra build time.
> If you rework targets to just stop using $(PROFILE) and check the
> profile enabled/disabled variables directly, you don't need this at all.

I think doing that, particularly on targets such as ar71xx, turns this 
into a much broader reworking of profile/image system than I had planned 
on undertaking, or thought belonged in a single patch series.  See below.

>> diff --git a/include/target.mk b/include/target.mk
>> index f129298..92e52db 100644
>> --- a/include/target.mk
>> +++ b/include/target.mk
>> @@ -70,6 +70,11 @@ define Profile
>>     DUMPINFO += \
>>   	echo "Target-Profile: $(1)"; \
>>   	echo "Target-Profile-Name: $(NAME)"; \
>> +	echo "Target-Profile-Type: $(if $(filter All,$(1)),all,$(if $(filter Minimal,$(1)),minimal,normal))"; \
>> +	echo "Target-Profile-Skip: $(if $(filter Default,$(1)),$(if $(PROFILE_SKIP_DEFAULT),1,0),$(if $(PROFILE_SKIP),1,0))"; \
>> +	echo "Target-Profile-Skip-Single: $(if $(filter All,$(1)),$(if $(PROFILE_SKIP_SINGLE),1,0),0)"; \
>> +	echo "Target-Profile-Default: $(if $(filter All,$(1)),1,$(if $(filter Default,$(1)),1,0))"; \
>> +	echo "Target-Profile-Build-If-All: $(if $(filter All,$(1)),0,$(if $(filter Minimal,$(1)),0,1))"; \
> Please get rid of the magic flag setting based on the profile name. Make
> these explicit by setting variables in the appropriate profiles.

Ok.

> Also, the types and flags are rather confusing here. From the metadata
> processing point of view, there should be nothing special about profiles
> named 'Default' and 'Minimal' aside from a few simple distinctions:
> There should be one profile that is enable by default
> There should be a distinction between device profiles and profiles that
> build all (or most) devices.
> If you write the code to clearly handle the above, this should make the
> rest of your patch less confusing.
>
> Now that I read your patches again, I get the sense that I disagree with
> part of your changes on a conceptual level. I think giving the
> impression that selecting multiple entries in the profile list will
> 'Build multiple profiles' seems wrong to me, making the description
> 'Build all profiles' even more wrong. The code is not doing that,
> because that would imply having a different package selection for every
> profile build, and the build system can't do that.
>
> I think a cleaner description would be building for all devices or a
> subset of the devices on the list, and just not feed the impression that
> it has that much to do with 'profiles' at all.
>
> Essentially, this is nothing but a simple way to make an image that is
> compatible with a specific set of devices, and we should present it to
> the user that way.

I the confusion stems from that fact that in the past selecting a 
profile was how you picked what images to build, and so I thought of a 
profile as an 'image-selector' rather as in terms of the package sets 
the profiles select.

In fact as I pointed out in another email the profiles on the targets 
that have 'Default' or 'Generic' targets that build images for all 
profiles for the arch_subtarget end up all using the same rootfs, but do 
still generate distinct images, or in the case of brcm47xx one has a few 
sets of generic profiles that build multiple images each.

There are actually other moving parts to the 'profile/image' equation 
(which is my on ar71xx most profiles correspond to a single device's 
images), which include the per-image kernel and bootloader 'magic' (and 
I think if the correct terminology is used, the kernel and 'magic' to 
get images accepted by the bootloader is a per-image (or image family) 
piece rather than a per-profile piece.

There perhaps should really be an additional tier in the build selection 
so that instead of only talking about selecting profiles, one selects 
from generic profiles and then makes a sub-selection of images to build 
for a particular profile, and to get rid of the number of board-specific 
'profiles' that are each the same as the other except they invoke the 
magic for a specific board.

Some care needs to be take with platforms such as sunxi because of e.g. 
board-specific u-boot.  There is no reason the uboots can't all be 
built, but one probably doesn't want to build uboot for boards one is 
not using.

Regards,

Daniel
Felix Fietkau Jan. 19, 2016, 11:45 a.m. UTC | #3
On 2016-01-19 12:21, Daniel Dickinson wrote:
>>> +# $(1) macro
>>> +# $(2) macro parameter
>>> +define BuildForProfiles
>>> +  $(foreach profile,$(PROFILES_BUILD),$(call $(1),$(2),$(profile)))
>>> +endef
>> Please don't call all the legacy Image/* code once per profile.
>> This will generate unnecessary duplicate files and extra build time.
>> If you rework targets to just stop using $(PROFILE) and check the
>> profile enabled/disabled variables directly, you don't need this at all.
> 
> I think doing that, particularly on targets such as ar71xx, turns this 
> into a much broader reworking of profile/image system than I had planned 
> on undertaking, or thought belonged in a single patch series.  See below.
I don't think it takes that much reworking, especially since targets
like ar71xx push so many build aspects through a small number of templates.
Since selecting "Default" already builds all of those images in one run,
I don't see any reason for adding multiple Image/Build/* calls per
profile. It's just a matter of reworking the target specific templates
in the right way.

>> Also, the types and flags are rather confusing here. From the metadata
>> processing point of view, there should be nothing special about profiles
>> named 'Default' and 'Minimal' aside from a few simple distinctions:
>> There should be one profile that is enable by default
>> There should be a distinction between device profiles and profiles that
>> build all (or most) devices.
>> If you write the code to clearly handle the above, this should make the
>> rest of your patch less confusing.
>>
>> Now that I read your patches again, I get the sense that I disagree with
>> part of your changes on a conceptual level. I think giving the
>> impression that selecting multiple entries in the profile list will
>> 'Build multiple profiles' seems wrong to me, making the description
>> 'Build all profiles' even more wrong. The code is not doing that,
>> because that would imply having a different package selection for every
>> profile build, and the build system can't do that.
>>
>> I think a cleaner description would be building for all devices or a
>> subset of the devices on the list, and just not feed the impression that
>> it has that much to do with 'profiles' at all.
>>
>> Essentially, this is nothing but a simple way to make an image that is
>> compatible with a specific set of devices, and we should present it to
>> the user that way.
> 
> I the confusion stems from that fact that in the past selecting a 
> profile was how you picked what images to build, and so I thought of a 
> profile as an 'image-selector' rather as in terms of the package sets 
> the profiles select.
> 
> In fact as I pointed out in another email the profiles on the targets 
> that have 'Default' or 'Generic' targets that build images for all 
> profiles for the arch_subtarget end up all using the same rootfs, but do 
> still generate distinct images, or in the case of brcm47xx one has a few 
> sets of generic profiles that build multiple images each.
> 
> There are actually other moving parts to the 'profile/image' equation 
> (which is my on ar71xx most profiles correspond to a single device's 
> images), which include the per-image kernel and bootloader 'magic' (and 
> I think if the correct terminology is used, the kernel and 'magic' to 
> get images accepted by the bootloader is a per-image (or image family) 
> piece rather than a per-profile piece.
> 
> There perhaps should really be an additional tier in the build selection 
> so that instead of only talking about selecting profiles, one selects 
> from generic profiles and then makes a sub-selection of images to build 
> for a particular profile, and to get rid of the number of board-specific 
> 'profiles' that are each the same as the other except they invoke the 
> magic for a specific board.
I think that makes sense, but that kind of rework doesn't have to be
done as part of the patch series we're discussing here. Let's just defer
that piece of the discussion, since it would lead to much more intrusive
changes.

> Some care needs to be take with platforms such as sunxi because of e.g. 
> board-specific u-boot.  There is no reason the uboots can't all be 
> built, but one probably doesn't want to build uboot for boards one is 
> not using.
Then let's just leave out multi-profile selection for sunxi for now.
We should start simple and mostly stick to the platforms that matter
most, e.g. ar71xx, ramips, etc.
I think multi-profile selection should simply be opt-in per target.

- Felix
diff mbox

Patch

diff --git a/include/image.mk b/include/image.mk
index 4eee4ad..bb46bb9 100644
--- a/include/image.mk
+++ b/include/image.mk
@@ -90,8 +90,6 @@  define add_jffs2_mark
 	echo -ne '\xde\xad\xc0\xde' >> $(1)
 endef
 
-PROFILE_SANITIZED := $(call sanitize,$(PROFILE))
-
 define split_args
 $(foreach data, \
 	$(subst |,$(space),\
@@ -114,8 +112,9 @@  define prepare_generic_squashfs
 	$(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
 endef
 
+# $(1) profile
 define Image/BuildKernel/Initramfs
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(1))
 endef
 
 define Image/BuildKernel/MkuImage
@@ -192,7 +191,7 @@  ifneq ($(CONFIG_NAND_SUPPORT),)
 # $(1) board name
 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
 # $(3) rootfstype (e.g. squashfs or ubifs)
-# $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
+# $(4) options to pass-through to ubinize (i.e. $(<profile>_UBI_OPTS)))
    define Image/Build/UbinizeImage
 	sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
 		"$(KDIR)/root.$(3)" \
@@ -203,12 +202,13 @@  ifneq ($(CONFIG_NAND_SUPPORT),)
 endif
 
 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
+# $(1) profile
     define Image/mkfs/ubifs/generate
 	$(CP) ./ubinize$(1).cfg $(KDIR)
 	( cd $(KDIR); \
 		$(STAGING_DIR_HOST)/bin/ubinize \
-		$(if $($(PROFILE)_UBI_OPTS), \
-			$(shell echo $($(PROFILE)_UBI_OPTS)), \
+		$(if $($(1)_UBI_OPTS), \
+			$(shell echo $($(1)_UBI_OPTS)), \
 			$(shell echo $(UBI_OPTS)) \
 		) \
 		-o $(KDIR)/root$(1).ubi \
@@ -216,12 +216,13 @@  ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
 	)
     endef
 
+# $(1) profile
     define Image/mkfs/ubifs
 
-        ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
+        ifneq ($($(1)_UBIFS_OPTS)$(UBIFS_OPTS),)
 		$(STAGING_DIR_HOST)/bin/mkfs.ubifs \
-			$(if $($(PROFILE)_UBIFS_OPTS), \
-				$(shell echo $($(PROFILE)_UBIFS_OPTS)), \
+			$(if $($(1)_UBIFS_OPTS), \
+				$(shell echo $($(1)_UBIFS_OPTS)), \
 				$(shell echo $(UBIFS_OPTS)) \
 			) \
 			$(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
@@ -233,13 +234,13 @@  ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
 			-o $(KDIR)/root.ubifs \
 			-d $(TARGET_DIR)
         endif
-	$(call Image/Build,ubifs)
+	$(call Image/Build,ubifs,$(1))
 
-        ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
+        ifneq ($($(1)_UBI_OPTS)$(UBI_OPTS),)
 		$(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
 		$(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
         endif
-	$(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
+	$(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi,$(1)))
     endef
 endif
 
@@ -248,7 +249,8 @@  define Image/mkfs/cpiogz
 endef
 
 define Image/mkfs/targz
-	$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE_SANITIZED),-$(PROFILE_SANITIZED))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 --sort=name -C $(TARGET_DIR)/ .
+# Move to Image/Build to eliminate profile from mkfs
+	touch $(KDIR)/root.targz
 endef
 
 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024)))
@@ -293,6 +295,11 @@  define BuildImage/mkfs
 
 endef
 
+# $(1) profile
+define Image/Build/targz
+	$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(call sanitize,$(1)),-$(call sanitize,$(1)))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 --sort=name -C $(TARGET_DIR)/ .
+endef
+
 # Build commands that can be called from Device/* templates
 define Build/uImage
 	mkimage -A $(LINUX_KARCH) \
@@ -427,7 +434,7 @@  define Build/sysupgrade-nand
 endef
 
 define Device/Init
-  PROFILES := $(PROFILE)
+  PROFILES := $(PROFILES_BUILD)
   DEVICE_NAME := $(1)
   KERNEL:=
   KERNEL_INITRAMFS = $$(KERNEL)
@@ -460,8 +467,8 @@  define Device/Export
 endef
 
 define Device/Check
-  _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
-  _COMPILE_TARGET = $$(if $(if $(IB),,$(CONFIG_IB)$$(filter $(PROFILE),$$(PROFILES))),compile,compile-disabled)
+  _TARGET = $(if $(filter $(1),$(PROFILES_BUILD)),install,install-disabled)
+  _COMPILE_TARGET = $(if $(if $(IB),,$(CONFIG_IB)$(filter $(1),$(PROFILES_BUILD))),compile,compile-disabled)
 endef
 
 ifndef IB
@@ -547,6 +554,12 @@  define Device
 
 endef
 
+# $(1) macro
+# $(2) macro parameter
+define BuildForProfiles
+  $(foreach profile,$(PROFILES_BUILD),$(call $(1),$(2),$(profile)))
+endef
+
 define BuildImage
 
   download:
@@ -565,29 +578,28 @@  define BuildImage
 
     image_prepare: compile
 		mkdir -p $(KDIR)/tmp
-		$(call Image/Prepare)
+		$(call BuildForProfiles,Image/Prepare)
   else
     image_prepare:
 		mkdir -p $(KDIR)/tmp
   endif
 
   mkfs_prepare: image_prepare
-	$(call Image/mkfs/prepare)
+	$(call BuildForProfiles,Image/mkfs/prepare)
 
   kernel_prepare: mkfs_prepare
-	$(call Image/BuildKernel)
-	$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
-	$(call Image/InstallKernel)
+	$(call BuildForProfiles,Image/BuildKernel)
+	$(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call BuildForProfiles,Image/BuildKernel/Initramfs)))
+	$(call BuildForProfiles,Image/InstallKernel)
 
   $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
   $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
 
   install: kernel_prepare
 	$(foreach fs,$(TARGET_FILESYSTEMS),
-		$(call Image/Build,$(fs))
+		$(call BuildForProfiles,Image/Build,$(fs))
 	)
-	$(call Image/mkfs/ubifs)
+	$(foreach profile,$(PROFILES_BUILD),$(call Image/mkfs/ubifs,$(profile)))
 	$(call Image/Checksum,md5sum --binary,md5sums)
 	$(call Image/Checksum,openssl dgst -sha256,sha256sums)
-
 endef
diff --git a/include/target.mk b/include/target.mk
index f129298..92e52db 100644
--- a/include/target.mk
+++ b/include/target.mk
@@ -70,6 +70,11 @@  define Profile
   DUMPINFO += \
 	echo "Target-Profile: $(1)"; \
 	echo "Target-Profile-Name: $(NAME)"; \
+	echo "Target-Profile-Type: $(if $(filter All,$(1)),all,$(if $(filter Minimal,$(1)),minimal,normal))"; \
+	echo "Target-Profile-Skip: $(if $(filter Default,$(1)),$(if $(PROFILE_SKIP_DEFAULT),1,0),$(if $(PROFILE_SKIP),1,0))"; \
+	echo "Target-Profile-Skip-Single: $(if $(filter All,$(1)),$(if $(PROFILE_SKIP_SINGLE),1,0),0)"; \
+	echo "Target-Profile-Default: $(if $(filter All,$(1)),1,$(if $(filter Default,$(1)),1,0))"; \
+	echo "Target-Profile-Build-If-All: $(if $(filter All,$(1)),0,$(if $(filter Minimal,$(1)),0,1))"; \
 	echo "Target-Profile-Packages: $(PACKAGES) $(call extra_packages,$(DEFAULT_PACKAGES) $(PACKAGES))"; \
 	if [ -f ./config/profile-$(1) ]; then \
 		echo "Target-Profile-Kconfig: yes"; \
@@ -82,7 +87,7 @@  define Profile
 	echo "@@"; \
 	echo;
   ifeq ($(CONFIG_TARGET_$(call target_conf,$(BOARD)_$(if $(SUBTARGET),$(SUBTARGET)_))$(1)),y)
-    PROFILE=$(1)
+    PROFILES_BUILD += $(1)
   endif
 endef
 endif
diff --git a/scripts/metadata.pl b/scripts/metadata.pl
index 48b1b7a..d9f23a1 100755
--- a/scripts/metadata.pl
+++ b/scripts/metadata.pl
@@ -275,8 +275,16 @@  EOF
 print <<EOF;
 endchoice
 
-choice
-	prompt "Target Profile"
+menu "Target Profile"
+
+EOF
+	print <<EOF;
+config PROFILE_SINGLE
+	bool
+	prompt "Select single profile"
+
+	choice
+		prompt "Single Target Profile" if PROFILE_SINGLE
 
 EOF
 
@@ -284,12 +292,80 @@  EOF
 		my $profiles = $target->{profiles};
 
 		foreach my $profile (@$profiles) {
+			next if ($profile->{skip});
+			next if ($profile->{skip_single});
+			print <<EOF;
+		config PROFILE_SINGLE_TARGET_$target->{conf}_$profile->{id}
+		bool "$profile->{name}"
+		depends on TARGET_$target->{conf}
+		select TARGET_$target->{conf}_$profile->{id}
+	$profile->{config}
+EOF
+		}
+	}
+
+		print <<EOF;
+        endchoice
+EOF
+
+	my %profile_all;
+	my %profile_minimal;
+
+	foreach my $target (@target) {
+		my $profiles = $target->{profiles};
+
+		foreach my $profile (@$profiles) {
+			if ($profile->{type} eq 'all') {
+				$profile_all{$target->{conf}} = "TARGET_$target->{conf}_$profile->{id}";
+			} elsif ($profile->{type} eq 'minimal') {
+				$profile_minimal{$target->{conf}} = "TARGET_$target->{conf}_$profile->{id}";
+			}
+		}
+	}
+
+	foreach my $target (@target) {
+		my $profiles = $target->{profiles};
+
+		foreach my $profile (@$profiles) {
+			next if ($profile->{skip});
 			print <<EOF;
 config TARGET_$target->{conf}_$profile->{id}
 	bool "$profile->{name}"
 	depends on TARGET_$target->{conf}
+	depends on ( !PROFILE_SINGLE || PROFILE_SINGLE_TARGET_$target->{conf}_$profile->{id} )
+EOF
+			if ((not ($profile->{type} eq 'minimal')) and ($profile_minimal{$target->{conf}})) {
+				print <<EOF;
+	depends on !$profile_minimal{$target->{conf}}
+EOF
+			}
+
+			if ($profile->{build_if_all}) {
+				if ($profile_all{$target->{conf}}) {
+					print <<EOF;
+	default y if $profile_all{$target->{conf}}
+EOF
+
+				}
+			}
+
+			if ($profile->{default}) {
+				print <<EOF;
+	default y
+EOF
+			}
+
+			if ($profile->{type} eq 'minimal') {
+				print <<EOF;
+	default n
+EOF
+
+			}
+
+			print <<EOF;
 $profile->{config}
 EOF
+
 			$profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
 			my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
 			foreach my $pkg (@pkglist) {
@@ -306,9 +382,8 @@  EOF
 			print "$help\n";
 		}
 	}
-
 	print <<EOF;
-endchoice
+endmenu
 
 config HAS_SUBTARGETS
 	bool
diff --git a/scripts/metadata.pm b/scripts/metadata.pm
index 0ed3524..cdc2eb8 100644
--- a/scripts/metadata.pm
+++ b/scripts/metadata.pm
@@ -94,6 +94,11 @@  sub parse_target_metadata($) {
 		/^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
 		/^Target-Profile-Config:/ and $profile->{config} = get_multiline(*FILE, "\t");
 		/^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
+		/^Target-Profile-Type:\s*(.+)\s*$/ and $profile->{type} = $1;
+		/^Target-Profile-Default:\s*(.+)\s*$/ and $profile->{default} = $1;
+		/^Target-Profile-Skip:\s*(.+)\s*$/ and $profile->{skip} = $1;
+		/^Target-Profile-Skip-Single:\s*(.+)\s*$/ and $profile->{skip_single} = $1;
+		/^Target-Profile-Build-If-All:\s*(.+)\s*$/ and $profile->{build_if_all} = $1;
 	}
 	close FILE;
 	foreach my $target (@target) {
diff --git a/target/imagebuilder/files/Makefile b/target/imagebuilder/files/Makefile
index 1056a42..a5a95d2 100644
--- a/target/imagebuilder/files/Makefile
+++ b/target/imagebuilder/files/Makefile
@@ -39,7 +39,8 @@  Building images:
 	target profile and package set. You can use the following parameters
 	to change that:
 
-	make image PROFILE="<profilename>" # override the default target profile
+	make image PROFILE="<profilename>" # override the default targets to specified single profile
+	make image PROFILES_BUILD="<profilename>[ <profilename> ... ]" # override list of profiles to build
 	make image PACKAGES="<pkg1> [<pkg2> [<pkg3> ...]]" # include extra packages
 	make image FILES="<path>" # include extra files from <path>
 	make image BIN_DIR="<path>" # alternative output directory for the images
@@ -175,7 +176,7 @@  build_image: FORCE
 	@echo
 	@echo Building images...
 	$(NO_TRACE_MAKE) -C target/linux/$(BOARD)/image install TARGET_BUILD=1 IB=1 EXTRA_IMAGE_NAME="$(EXTRA_IMAGE_NAME)" \
-		$(if $(USER_PROFILE),PROFILE="$(USER_PROFILE)")
+		$(if $(USER_PROFILE),PROFILES_BUILD="$(if $(PROFILES_BUILD),$(PROFILES_BUILD),$(USER_PROFILE))")
 
 clean:
 	rm -rf $(TMP_DIR) $(DL_DIR) $(TARGET_DIR) $(BIN_DIR)
diff --git a/target/linux/adm5120/image/Makefile b/target/linux/adm5120/image/Makefile
index 4ca4d48..c0864b7 100644
--- a/target/linux/adm5120/image/Makefile
+++ b/target/linux/adm5120/image/Makefile
@@ -93,11 +93,11 @@  endef
 include $(SUBTARGET).mk
 
 define Image/Build
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/Profile/$(2),$(1))
 endef
 
 define Image/Build/Initramfs
-	$(call Image/Build/Profile/$(PROFILE),Initramfs)
+	$(call Image/Build/Profile/$(2),Initramfs)
 endef
 
 define Image/BuildKernel
diff --git a/target/linux/ar71xx/image/Makefile b/target/linux/ar71xx/image/Makefile
index 31eb345..22fe1fe 100644
--- a/target/linux/ar71xx/image/Makefile
+++ b/target/linux/ar71xx/image/Makefile
@@ -1590,7 +1590,7 @@  qihoo_c301_mtdlayout=mtdparts=spi0.0:256k(u-boot)ro,64k(u-boot-env),64k(devdata)
 yun_mtdlayout_8M=mtdparts=spi0.0:256k(u-boot)ro,64k(u-boot-env)ro,6464k(rootfs),1280k(kernel),64k(nvram),64k(art),7744k@0x50000(firmware)
 yun_mtdlayout_16M=mtdparts=spi0.0:256k(u-boot)ro,64k(u-boot-env)ro,14656k(rootfs),1280k(kernel),64k(nvram),64k(art),15936k@0x50000(firmware)
 
-
+# $(1) profile
 define Image/BuildKernel
 	cp $(KDIR)/vmlinux.elf $(VMLINUX).elf
 	cp $(KDIR)/vmlinux $(VMLINUX).bin
@@ -1600,7 +1600,7 @@  define Image/BuildKernel
 	$(call MkuImage,lzma,,$(KDIR)/vmlinux.bin.lzma,$(UIMAGE)-lzma.bin)
 	cp $(KDIR)/loader-generic.elf $(VMLINUX)-lzma.elf
 	-mkdir -p $(KDIR_TMP)
-	$(call Image/Build/Profile/$(IMAGE_PROFILE),buildkernel)
+	$(call Image/Build/Profile/$(call IMAGE_PROFILE,$(1)),buildkernel)
 endef
 
 define Image/BuildKernel/Initramfs
@@ -2544,10 +2544,12 @@  define Image/Build/jffs2
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
+# $(1) profile
 define Image/Build/Initramfs
-	$(call Image/Build/Profile/$(IMAGE_PROFILE),initramfs)
+	$(call Image/Build/Profile/$(call IMAGE_PROFILE,$(1)),initramfs)
 endef
 
+# $(1) profile
 define Image/Prepare
 	gzip -9n -c $(KDIR)/vmlinux > $(KDIR)/vmlinux.bin.gz
 	$(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
@@ -2557,13 +2559,14 @@  ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
 	$(call Image/BuildLoader,generic,elf,,,-initramfs)
 endif
 	$(call Image/BuildLoader,generic,elf)
-	$(call Image/Build/Profile/$(if $(CONFIG_IB),Default,$(IMAGE_PROFILE)),loader)
+	$(call Image/Build/Profile/$(call IMAGE_PROFILE,$(1)),loader)
 endef
 
 # $(1): filesystem type.
+# $(2): profile
 define Image/Build
 	$(call Image/Build/$(call rootfs_type,$(1)),$(1))
-	$(call Image/Build/Profile/$(IMAGE_PROFILE),$(1))
+	$(call Image/Build/Profile/$(call IMAGE_PROFILE,$(2)),$(1))
 endef
 
 $(eval $(call BuildImage))
diff --git a/target/linux/at91/image/Makefile b/target/linux/at91/image/Makefile
index 4e472f2..648b7f5 100644
--- a/target/linux/at91/image/Makefile
+++ b/target/linux/at91/image/Makefile
@@ -94,7 +94,7 @@  define Image/BuildKernel
 	if [ $(CONFIG_FLEXIBITY_ROOT) ]; then \
 		$(INSTALL_BIN) $(BIN_DIR)/$(IMG_PREFIX)-uImage $(TARGET_DIR)/uImage ; \
 	fi
-	$(call Image/Build/Kernel/$(PROFILE))
+	$(call Image/Build/Kernel/$(2))
 endef
 
 define Image/Build
diff --git a/target/linux/au1000/image/Makefile b/target/linux/au1000/image/Makefile
index 744e5c6..0f6ceb1 100644
--- a/target/linux/au1000/image/Makefile
+++ b/target/linux/au1000/image/Makefile
@@ -67,7 +67,7 @@  define Image/Build
 	tar -C $(BIN_DIR) -cvzf $(BIN_DIR)/$(IMG_PREFIX)-$(1)-sysupgrade.bin \
 		$(IMG_PREFIX)-vmlinux.bin -C $(TMP_DIR) $(IMG_PREFIX)-root.fs
 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y)
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(2))
 endif
 endef
 
diff --git a/target/linux/brcm47xx/image/Makefile b/target/linux/brcm47xx/image/Makefile
index c290f9a..782beef 100644
--- a/target/linux/brcm47xx/image/Makefile
+++ b/target/linux/brcm47xx/image/Makefile
@@ -377,7 +377,7 @@  define Image/Build
 #	$(call Image/Build/Chk,$(1),wnr3500U,U12H136T00_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1)))
 #	$(call Image/Build/Chk,$(1),wnr3500v2_VC,U12H127T70_NETGEAR,2,$(patsubst jffs2-%,jffs2,$(1)))
 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(2))
 endif
 endef
 
diff --git a/target/linux/cns3xxx/image/Makefile b/target/linux/cns3xxx/image/Makefile
index 13e5efd..ce6530d 100644
--- a/target/linux/cns3xxx/image/Makefile
+++ b/target/linux/cns3xxx/image/Makefile
@@ -26,7 +26,7 @@  define Image/BuildKernel/Initramfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	$(call BuildFirmware/Generic,$(1))
 	cp $(KDIR)/uImage $(BIN_DIR)/$(IMG_PREFIX)-uImage
 endef
diff --git a/target/linux/gemini/image/Makefile b/target/linux/gemini/image/Makefile
index 1c51b62..47460fd 100644
--- a/target/linux/gemini/image/Makefile
+++ b/target/linux/gemini/image/Makefile
@@ -51,7 +51,7 @@  endef
 
 ifeq ($(SUBTARGET),wiligear)
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	-$(STAGING_DIR_HOST)/bin/mkfwimage2 \
 		-m GEOS -f 0x30000000 -z \
 		-v WILI-S.WILIBOARD.v5.00.SL3512.OpenWrt.00000.000000.000000 \
@@ -70,7 +70,7 @@  endif
 
 ifeq ($(SUBTARGET),raidsonic)
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	dd if=$(BIN_DIR)/$(IMG_PREFIX)-$(1).img of=$(BIN_DIR)/rd.gz bs=6144k count=1
 #	dd if=/dev/zero of=$(BIN_DIR)/hddapp.tgz bs=6144k count=1
 	dd if=$(BIN_DIR)/$(IMG_PREFIX)-$(1).img of=$(BIN_DIR)/hddapp.tgz bs=6144k count=1 seek=1
diff --git a/target/linux/ipq806x/image/Makefile b/target/linux/ipq806x/image/Makefile
index 58c4e1e..0e1347f 100644
--- a/target/linux/ipq806x/image/Makefile
+++ b/target/linux/ipq806x/image/Makefile
@@ -28,7 +28,7 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-$(1)-root.img bs=2k conv=sync
 endef
 
diff --git a/target/linux/ixp4xx/image/Makefile b/target/linux/ixp4xx/image/Makefile
index bbd62f0..e805db5 100644
--- a/target/linux/ixp4xx/image/Makefile
+++ b/target/linux/ixp4xx/image/Makefile
@@ -47,7 +47,7 @@  define BuildFirmware/Generic
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	$(call BuildFirmware/Generic,$(1))
 endef
 
diff --git a/target/linux/kirkwood/image/Makefile b/target/linux/kirkwood/image/Makefile
index 24cb317..7a762f4 100644
--- a/target/linux/kirkwood/image/Makefile
+++ b/target/linux/kirkwood/image/Makefile
@@ -73,21 +73,21 @@  define Image/BuildKernel/Template
  ifneq ($(1),)
 	$(CP) $(DTS_DIR)/kirkwood-$(1).dtb $(BIN_DIR)/$(IMG_PREFIX)-$(1).dtb
 
-	$(CP) $(KDIR)/zImage $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage
-	cat $(BIN_DIR)/$(IMG_PREFIX)-$(1).dtb >> $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage
+	$(CP) $(KDIR)/zImage $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage
+	cat $(BIN_DIR)/$(IMG_PREFIX)-$(1).dtb >> $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage
 	$(call Image/BuildKernel/MkuImage, \
 		none, 0x8000, 0x8000, \
-		$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage, \
-		$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-uImage \
+		$(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage, \
+		$(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-uImage \
 	)
 
   ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
-	$(CP) $(KDIR)/zImage-initramfs $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage-initramfs
-	cat $(BIN_DIR)/$(IMG_PREFIX)-$(1).dtb >> $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage-initramfs
+	$(CP) $(KDIR)/zImage-initramfs $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage-initramfs
+	cat $(BIN_DIR)/$(IMG_PREFIX)-$(1).dtb >> $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage-initramfs
 	$(call Image/BuildKernel/MkuImage, \
 		none, 0x8000, 0x8000, \
-		$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage-initramfs, \
-		$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-uImage-initramfs \
+		$(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-zImage-initramfs, \
+		$(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-uImage-initramfs \
 	)
   endif
  endif
@@ -117,7 +117,7 @@  define Image/InstallKernel/Template
 endef
 
 define Image/Build/jffs2-nand-2048-128k
-	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-$(1).img \
+	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-$(1).img \
 		bs=2048 conv=sync
 endef
 
@@ -126,74 +126,74 @@  define Image/Build/squashfs
 	( \
 		dd if=$(KDIR)/zImage bs=4096k conv=sync; \
 		dd if=$(KDIR)/root.$(1) bs=128k conv=sync; \
-	) > $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-$(1).img
+	) > $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-$(1).img
 endef
 
 define Image/Build/ubifs
 
- ifneq ($($(PROFILE)_UBIFS_OPTS),)
-	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-rootfs.ubifs
+ ifneq ($($(2)_UBIFS_OPTS),)
+	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-rootfs.ubifs
  endif
 endef
 
 define Image/Build/ubi
 
- ifneq ($($(PROFILE)_UBI_OPTS),)
-	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-rootfs.ubi
+ ifneq ($($(2)_UBI_OPTS),)
+	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,($2))-rootfs.ubi
  endif
 endef
 
-Image/BuildKernel/Template/Generic=$(call Image/BuildKernel/Template)
+Image/BuildKernel/Template/Generic=$(call Image/BuildKernel/Template,,$(1))
 Image/InstallKernel/Template/Generic=$(call Image/InstallKernel/Template)
 
-Image/BuildKernel/Template/DOCKSTAR=$(call Image/BuildKernel/Template,dockstar)
+Image/BuildKernel/Template/DOCKSTAR=$(call Image/BuildKernel/Template,dockstar,$(1))
 Image/InstallKernel/Template/DOCKSTAR=$(call Image/InstallKernel/Template,dockstar)
 
-Image/BuildKernel/Template/GOFLEXHOME=$(call Image/BuildKernel/Template,goflexhome)
+Image/BuildKernel/Template/GOFLEXHOME=$(call Image/BuildKernel/Template,goflexhome,$(1))
 Image/InstallKernel/Template/GOFLEXHOME=$(call Image/InstallKernel/Template,goflexhome)
 
-Image/BuildKernel/Template/GOFLEXNET=$(call Image/BuildKernel/Template,goflexnet)
+Image/BuildKernel/Template/GOFLEXNET=$(call Image/BuildKernel/Template,goflexnet,$(1))
 Image/InstallKernel/Template/GOFLEXNET=$(call Image/InstallKernel/Template,goflexnet)
 
-Image/BuildKernel/Template/IB62X0=$(call Image/BuildKernel/Template,ib62x0)
+Image/BuildKernel/Template/IB62X0=$(call Image/BuildKernel/Template,ib62x0,$(1))
 Image/InstallKernel/Template/IB62X0=$(call Image/InstallKernel/Template,ib62x0)
 
-Image/BuildKernel/Template/ICONNECT=$(call Image/BuildKernel/Template,iconnect)
+Image/BuildKernel/Template/ICONNECT=$(call Image/BuildKernel/Template,iconnect,$(1))
 Image/InstallKernel/Template/ICONNECT=$(call Image/InstallKernel/Template,iconnect)
 
-Image/BuildKernel/Template/IOMEGA_IX2_200=$(call Image/BuildKernel/Template,iomega_ix2_200)
+Image/BuildKernel/Template/IOMEGA_IX2_200=$(call Image/BuildKernel/Template,iomega_ix2_200,$(1))
 Image/InstallKernel/Template/IOMEGA_IX2_200=$(call Image/InstallKernel/Template,iomega_ix2_200)
 
-Image/BuildKernel/Template/NSA310S=$(call Image/BuildKernel/Template,nsa310s)
+Image/BuildKernel/Template/NSA310S=$(call Image/BuildKernel/Template,nsa310s,$(1))
 Image/InstallKernel/Template/NSA310S=$(call Image/InstallKernel/Template,nsa310s)
 
-Image/BuildKernel/Template/POGOE02=$(call Image/BuildKernel/Template,pogo_e02)
+Image/BuildKernel/Template/POGOE02=$(call Image/BuildKernel/Template,pogo_e02,$(1))
 Image/InstallKernel/Template/POGOE02=$(call Image/InstallKernel/Template,pogo_e02)
 
-Image/BuildKernel/Template/SHEEVAPLUG=$(call Image/BuildKernel/Template,sheevaplug)
+Image/BuildKernel/Template/SHEEVAPLUG=$(call Image/BuildKernel/Template,sheevaplug,$(1))
 Image/InstallKernel/Template/SHEEVAPLUG=$(call Image/InstallKernel/Template,sheevaplug)
 
-Image/BuildKernel/Template/SHEEVAPLUGSATA=$(call Image/BuildKernel/Template,sheevaplug-esata)
+Image/BuildKernel/Template/SHEEVAPLUGSATA=$(call Image/BuildKernel/Template,sheevaplug-esata,$(1))
 Image/InstallKernel/Template/SHEEVAPLUGSATA=$(call Image/InstallKernel/Template,sheevaplug-esata)
 
-Image/BuildKernel/Template/GuruplugServerPlus=$(call Image/BuildKernel/Template,guruplug-server-plus)
+Image/BuildKernel/Template/GuruplugServerPlus=$(call Image/BuildKernel/Template,guruplug-server-plus,$(1))
 Image/InstallKernel/Template/GuruplugServerPlus=$(call Image/InstallKernel/Template,guruplug-server-plus)
 
-Image/BuildKernel/Template/Topkick1281P2=$(call Image/BuildKernel/Template,topkick)
+Image/BuildKernel/Template/Topkick1281P2=$(call Image/BuildKernel/Template,topkick,$(1))
 Image/InstallKernel/Template/Topkick1281P2=$(call Image/InstallKernel/Template,topkick)
 
 define Image/BuildKernel
-	$(call Image/BuildKernel/Template/$(PROFILE))
+	$(call Image/BuildKernel/Template/$(2),$(2))
 endef
 
 define Image/InstallKernel
-	$(call Image/InstallKernel/Template/$(PROFILE))
+	$(call Image/InstallKernel/Template/$(2))
 endef
 
 define Image/Build
 	$(if $(Image/Build/$(1)), \
-		$(call Image/Build/$(1),$(1)), \
-		$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-$(1).img \
+		$(call Image/Build/$(1),$(1),$(2)), \
+		$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-$(1).img \
 	)
 endef
 
diff --git a/target/linux/lantiq/image/Makefile b/target/linux/lantiq/image/Makefile
index 553bdc1..dc4eafc 100644
--- a/target/linux/lantiq/image/Makefile
+++ b/target/linux/lantiq/image/Makefile
@@ -68,10 +68,10 @@  endef
 
 define Image/BuildNAND/squashfs
 
-ifneq ($($(PROFILE)_UBI_OPTS),)
+ifneq ($($(2)_UBI_OPTS),)
 	$(call prepare_generic_squashfs,$(KDIR)/root.$(1))
-	$(call Image/Build/UbinizeImage,$(PROFILE),,squashfs,$($(PROFILE)_UBI_OPTS))
-	$(call Image/Build/SysupgradeNAND,$(PROFILE),$(1),$(KDIR)/uImage-$(PROFILE))
+	$(call Image/Build/UbinizeImage,$(2),,squashfs,$($(2)_UBI_OPTS))
+	$(call Image/Build/SysupgradeNAND,$(2),$(1),$(KDIR)/uImage-$(2))
 endif
 endef
 
@@ -125,16 +125,16 @@  endef
 
 define Image/BuildEVA/ubifs
 
-ifneq ($($(PROFILE)_UBIFS_OPTS),)
-	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs.ubifs
+ifneq ($($(2)_UBIFS_OPTS),)
+	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs.ubifs
 endif
 endef
 
 define Image/BuildEVA/ubi
 
-ifneq ($($(PROFILE)_UBI_OPTS),)
-	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs.ubi
-	$(CP) $(KDIR)/root-overlay.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs-overlay.ubi
+ifneq ($($(2)_UBI_OPTS),)
+	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs.ubi
+	$(CP) $(KDIR)/root-overlay.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs-overlay.ubi
 endif
 endef
 
@@ -165,26 +165,26 @@  endef
 
 define Image/Build/ubifs
 
-ifneq ($($(PROFILE)_UBIFS_OPTS),)
-	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs.ubifs
+ifneq ($($(2)_UBIFS_OPTS),)
+	$(CP) $(KDIR)/root.ubifs $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs.ubifs
 endif
 endef
 
 define Image/Build/ubi
 
-ifneq ($($(PROFILE)_UBI_OPTS),)
-	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs.ubi
-	$(CP) $(KDIR)/root-overlay.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-rootfs-overlay.ubi
+ifneq ($($(2)_UBI_OPTS),)
+	$(CP) $(KDIR)/root.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs.ubi
+	$(CP) $(KDIR)/root-overlay.ubi $(BIN_DIR)/$(IMG_PREFIX)-$(2)-rootfs-overlay.ubi
 endif
 endef
 
 
 define Image/BuildNAND/ubifs
 
-ifneq ($($(PROFILE)_UBIFS_OPTS),)
-ifneq ($($(PROFILE)_UBI_OPTS),)
-	$(call Image/Build/UbinizeImage,$(PROFILE),,ubifs,$($(PROFILE)_UBI_OPTS))
-	$(call Image/Build/SysupgradeNAND,$(PROFILE),$(1),$(KDIR)/uImage-$(PROFILE))
+ifneq ($($(2)_UBIFS_OPTS),)
+ifneq ($($(2)_UBI_OPTS),)
+	$(call Image/Build/UbinizeImage,$(2),,ubifs,$($(2)_UBI_OPTS))
+	$(call Image/Build/SysupgradeNAND,$(2),$(1),$(KDIR)/uImage-$(2))
 endif
 endif
 endef
@@ -426,22 +426,21 @@  Image/BuildKernel/Profile/VGV7519BRN=$(call Image/BuildKernel/Template,VGV7519BR
 Image/Build/Profile/VGV7519BRN=$(call Image/Build/$(1),$(1),VGV7519BRN,5D00008000,0x12345678,0x2083b8ed,$(1))
 
 define Image/Prepare
-	$(call Image/Prepare/Profile,$(PROFILE))
+	$(call Image/Prepare/Profile,$(2),,$(2))
 endef
 
 endif
 
-
 define Image/BuildKernel
-	$(call Image/BuildKernel/Profile/$(PROFILE))
+	$(call Image/BuildKernel/Profile/$(2),,$(2))
 endef
 
 define Image/InstallKernel
-	$(call Image/InstallKernel/Template/$(PROFILE))
+	$(call Image/InstallKernel/Template/$(2))
 endef
 
 define Image/Build
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/Profile/$(2),$(1),$(2))
 endef
 
 $(eval $(call BuildImage))
diff --git a/target/linux/malta/image/Makefile b/target/linux/malta/image/Makefile
index 180e045..a247e58 100644
--- a/target/linux/malta/image/Makefile
+++ b/target/linux/malta/image/Makefile
@@ -48,7 +48,7 @@  define Image/Build/Initramfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
diff --git a/target/linux/mcs814x/image/Makefile b/target/linux/mcs814x/image/Makefile
index 3afd660..c25cdf4 100644
--- a/target/linux/mcs814x/image/Makefile
+++ b/target/linux/mcs814x/image/Makefile
@@ -49,9 +49,9 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/Profile/$(2),$(1),$(2))
 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
 	$(call Image/Build/Initramfs)
 endif
diff --git a/target/linux/mediatek/image/Makefile b/target/linux/mediatek/image/Makefile
index 82525c9..5ef9d7f 100644
--- a/target/linux/mediatek/image/Makefile
+++ b/target/linux/mediatek/image/Makefile
@@ -22,7 +22,7 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 endef
 
 $(eval $(call BuildImage))
diff --git a/target/linux/mpc85xx/image/Makefile b/target/linux/mpc85xx/image/Makefile
index 0ec067e..ac4541d 100644
--- a/target/linux/mpc85xx/image/Makefile
+++ b/target/linux/mpc85xx/image/Makefile
@@ -65,11 +65,9 @@  define Image/Build/squashfs
 	cp $(KDIR)/root.squashfs $(BIN_DIR)/$(IMG_PREFIX)-root.squashfs
 endef
 
-PROFILE ?= Default
-
 define Image/Build
-	$(call Image/Build/$(1),$(1))
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
+	$(call Image/Build/Profile/$(2),$(1),$(2))
 endef
 
 $(eval $(call BuildImage))
diff --git a/target/linux/mvebu/image/Makefile b/target/linux/mvebu/image/Makefile
index 123be9e..fd51052 100644
--- a/target/linux/mvebu/image/Makefile
+++ b/target/linux/mvebu/image/Makefile
@@ -204,21 +204,21 @@  endef
 $(eval $(call MultiProfile,Default,$(PROFILES_LIST)))
 
 define Image/BuildKernel
-	$(call Image/BuildKernel/Profile/$(PROFILE))
+	$(call Image/BuildKernel/Profile/$(2),$(1),$(2))
 endef
 
 define Image/Build/squashfs
 	# Align the squashfs image size before calling the profiles,
 	# otherwise the size would keep growing
 	$(call prepare_generic_squashfs,$(KDIR)/root.squashfs)
-	$(call Image/Build/Profile/$(PROFILE)/squashfs)
+	$(call Image/Build/Profile/$(2)/squashfs)
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
-	$(call Image/Build/Profile/$(PROFILE)/BuildSysupgrade,$(1))
+	$(call Image/Build/$(1),$(1),$(2))
+	$(call Image/Build/Profile/$(2)/BuildSysupgrade,$(1),$(2))
 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
-	$(call Image/Build/Profile/$(PROFILE)/Initramfs)
+	$(call Image/Build/Profile/$(2)/Initramfs,$(1),$(2))
 endif
 endef
 
diff --git a/target/linux/mxs/image/Makefile b/target/linux/mxs/image/Makefile
index 94fed82..502c51e 100644
--- a/target/linux/mxs/image/Makefile
+++ b/target/linux/mxs/image/Makefile
@@ -81,8 +81,8 @@  define Image/Build/Profile/duckbill
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
-	$(call Image/Build/Profile/$(PROFILE),$(1),$(IMG_PREFIX)-$(PROFILE)-sdcard.img)
+	$(call Image/Build/$(1),$(1),$(2))
+	$(call Image/Build/Profile/$(2),$(1),$(IMG_PREFIX)-$(2)-sdcard.img)
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
diff --git a/target/linux/netlogic/image/Makefile b/target/linux/netlogic/image/Makefile
index 9541f6a..24ea44e 100644
--- a/target/linux/netlogic/image/Makefile
+++ b/target/linux/netlogic/image/Makefile
@@ -19,7 +19,7 @@  define Image/Build/Initramfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
diff --git a/target/linux/octeon/image/Makefile b/target/linux/octeon/image/Makefile
index e74b06d..26af496 100644
--- a/target/linux/octeon/image/Makefile
+++ b/target/linux/octeon/image/Makefile
@@ -58,7 +58,7 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
diff --git a/target/linux/omap/image/Makefile b/target/linux/omap/image/Makefile
index 3fa2848..d3dca86 100644
--- a/target/linux/omap/image/Makefile
+++ b/target/linux/omap/image/Makefile
@@ -33,7 +33,7 @@  define Image/BuildKernel
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 endef
 
 define Image/Build/jffs2-64k
diff --git a/target/linux/omap24xx/image/Makefile b/target/linux/omap24xx/image/Makefile
index 316cce0..5e01682 100644
--- a/target/linux/omap24xx/image/Makefile
+++ b/target/linux/omap24xx/image/Makefile
@@ -20,7 +20,7 @@  endef
 
 define Image/Build
 	$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-root.$(1)
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 endef
 
 
diff --git a/target/linux/oxnas/image/Makefile b/target/linux/oxnas/image/Makefile
index 5a96bad..31ed4d2 100644
--- a/target/linux/oxnas/image/Makefile
+++ b/target/linux/oxnas/image/Makefile
@@ -82,17 +82,17 @@  define Image/InstallKernel/Template
 endef
 
 define Image/Build/squashfs
-	$(call Image/Build/SysupgradeNAND,$(PROFILE_SANITIZED),$(1),$(KDIR)/fit-$(PROFILE_SANITIZED).itb)
-	$(call Image/Build/UbinizeImage,$(PROFILE_SANITIZED),--uboot-env --kernel $(KDIR)/fit-$(PROFILE_SANITIZED).itb,$(1),$($(PROFILE)_UBI_OPTS))
-	cp $(KDIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-squashfs-ubinized.bin $(BIN_DIR)
+	$(call Image/Build/SysupgradeNAND,$(call sanitize,$(2)),$(1),$(KDIR)/fit-$(call sanitize,$(2)).itb)
+	$(call Image/Build/UbinizeImage,$(call sanitize,$(2)),--uboot-env --kernel $(KDIR)/fit-$(call sanitize,$(2)).itb,$(1),$($(2)_UBI_OPTS))
+	cp $(KDIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-squashfs-ubinized.bin $(BIN_DIR)
 endef
 
 define Image/Build/ubifs
 
- ifneq ($($(PROFILE)_UBIFS_OPTS),)
-	$(call Image/Build/SysupgradeNAND,$(PROFILE_SANITIZED),ubifs,$(KDIR)/fit-$(PROFILE_SANITIZED).itb)
-	$(call Image/Build/UbinizeImage,$(PROFILE_SANITIZED),--uboot-env --kernel $(KDIR)/fit-$(PROFILE_SANITIZED).itb,ubifs,$($(PROFILE)_UBI_OPTS))
-	cp $(KDIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-ubifs-ubinized.bin $(BIN_DIR)
+ ifneq ($($(call)_UBIFS_OPTS),)
+	$(call Image/Build/SysupgradeNAND,$(call sanitize,$(2)),ubifs,$(KDIR)/fit-$(call sanitize,$(2)).itb)
+	$(call Image/Build/UbinizeImage,$(call sanitize,$(2)),--uboot-env --kernel $(KDIR)/fit-$(call sanitize,$(2)).itb,ubifs,$($(2)_UBI_OPTS))
+	cp $(KDIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-ubifs-ubinized.bin $(BIN_DIR)
  endif
 
 endef
@@ -110,17 +110,17 @@  Image/BuildKernel/Template/KD20=$(call Image/BuildKernel/Template,kd20)
 Image/InstallKernel/Template/KD20=$(call Image/InstallKernel/Template,kd20)
 
 define Image/BuildKernel
-	$(call Image/BuildKernel/Template/$(PROFILE))
+	$(call Image/BuildKernel/Template/$(2))
 endef
 
 define Image/InstallKernel
-	$(call Image/InstallKernel/Template/$(PROFILE))
+	$(call Image/InstallKernel/Template/$(2))
 endef
 
 define Image/Build
 	$(if $(Image/Build/$(1)), \
-		$(call Image/Build/$(1),$(1)), \
-		$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-$(1).img \
+		$(call Image/Build/$(1),$(1),$(2)), \
+		$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-$(call sanitize,$(2))-$(1).img \
 	)
 endef
 
diff --git a/target/linux/ppc40x/image/Makefile b/target/linux/ppc40x/image/Makefile
index 4070774..9d1c7ae 100644
--- a/target/linux/ppc40x/image/Makefile
+++ b/target/linux/ppc40x/image/Makefile
@@ -15,11 +15,11 @@  endef
 
 define Image/BuildKernel
 	cp $(KDIR)/uImage $(BIN_DIR)/$(IMG_PREFIX)-uImage
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(2))
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 endef
 
 define Image/Build/ext2
diff --git a/target/linux/ppc44x/image/Makefile b/target/linux/ppc44x/image/Makefile
index e2303f6..8c7ca8d 100644
--- a/target/linux/ppc44x/image/Makefile
+++ b/target/linux/ppc44x/image/Makefile
@@ -20,7 +20,7 @@  define Image/BuildKernel
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 endef
 
 define Image/Build/jffs2-256k
diff --git a/target/linux/ramips/image/Makefile b/target/linux/ramips/image/Makefile
index 2427ac6..b58ca7e 100644
--- a/target/linux/ramips/image/Makefile
+++ b/target/linux/ramips/image/Makefile
@@ -19,7 +19,7 @@  VMLINUX:=$(IMG_PREFIX)-vmlinux
 UIMAGE:=$(IMG_PREFIX)-uImage
 
 define Image/Build/Initramfs
-	$(call Image/Build/Profile/$(PROFILE),initramfs)
+	$(call Image/Build/Profile/$(2),initramfs)
 endef
 
 DEVICE_VARS += DTS
@@ -1131,13 +1131,13 @@  define Image/BuildKernel/Initramfs
 	$(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
 	$(call MkImage,lzma,$(KDIR)/vmlinux-initramfs.bin.lzma,$(KDIR)/uImage-initramfs.lzma)
 	cp $(KDIR)/uImage-initramfs.lzma $(BIN_DIR)/$(UIMAGE)-initramfs.bin
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(2))
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/Profile/$(2),$(1))
 endef
 endif
 
diff --git a/target/linux/rb532/image/Makefile b/target/linux/rb532/image/Makefile
index 1a1f343..94a8927 100644
--- a/target/linux/rb532/image/Makefile
+++ b/target/linux/rb532/image/Makefile
@@ -68,7 +68,7 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
 	$(CP) $(KDIR)/vmlinux.elf $(BIN_DIR)/$(IMG_PREFIX)-$(1).kernel
 	$(STAGING_DIR_HOST)/bin/patch-cmdline $(BIN_DIR)/$(IMG_PREFIX)-$(1).kernel '$(strip $(call Image/cmdline/$(1))) '
 	./gen_image.sh $(BIN_DIR)/$(IMG_PREFIX)-combined-$(1).bin \
diff --git a/target/linux/realview/image/Makefile b/target/linux/realview/image/Makefile
index ad7aa69..175339f 100644
--- a/target/linux/realview/image/Makefile
+++ b/target/linux/realview/image/Makefile
@@ -20,14 +20,14 @@  define Image/Build/squashfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
-	$(call Build/sdcard-img)
+	$(call Image/Build/$(1),$(1),$(2))
+	$(call Build/sdcard-img,$(2))
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
 
 define Build/sdcard-img
 	./gen_realview_sdcard_img.sh \
-	$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)sdcard.img \
+	$(BIN_DIR)/$(IMG_PREFIX)-$(2)sdcard.img \
 	$(KDIR)/root.ext4 \
 	$(CONFIG_TARGET_ROOTFS_PARTSIZE)
 endef
diff --git a/target/linux/sunxi/image/Makefile b/target/linux/sunxi/image/Makefile
index 5a5c98a..6758ba1 100644
--- a/target/linux/sunxi/image/Makefile
+++ b/target/linux/sunxi/image/Makefile
@@ -31,85 +31,86 @@  define Image/BuildKernel
 endef
 
 define Image/Build/SDCard
-	rm -f $(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img
-	mkdosfs $(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img -C $(FAT32_BLOCKS)
+	rm -f $(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img
+	mkdosfs $(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img -C $(FAT32_BLOCKS)
 
-	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img $(KDIR)/uboot-sunxi-$(PROFILE)-boot.scr ::boot.scr
-	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img $(DTS_DIR)/$(2).dtb ::dtb
-	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img $(BIN_DIR)/$(IMG_PREFIX)-uImage ::uImage
+	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img $(KDIR)/uboot-sunxi-$(3)-boot.scr ::boot.scr
+	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img $(DTS_DIR)/$(2).dtb ::dtb
+	mcopy -i $(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img $(BIN_DIR)/$(IMG_PREFIX)-uImage ::uImage
 
 	./gen_sunxi_sdcard_img.sh \
-		$(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-sdcard-vfat-$(1).img \
-		$(KDIR_TMP)/$(IMG_PREFIX)-$(PROFILE)-boot.img \
+		$(BIN_DIR)/$(IMG_PREFIX)-$(3)-sdcard-vfat-$(1).img \
+		$(KDIR_TMP)/$(IMG_PREFIX)-$(3)-boot.img \
 		$(KDIR)/root.$(1) \
 		$(CONFIG_SUNXI_SD_BOOT_PARTSIZE) \
 		$(CONFIG_TARGET_ROOTFS_PARTSIZE) \
-		$(KDIR)/uboot-sunxi-$(PROFILE)-u-boot-with-spl.bin
+		$(KDIR)/uboot-sunxi-$(3)-u-boot-with-spl.bin
 
   ifneq ($(CONFIG_TARGET_IMAGES_GZIP),)
-	gzip -f9n $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE)-sdcard-vfat-$(1).img
+	gzip -f9n $(BIN_DIR)/$(IMG_PREFIX)-$(3)-sdcard-vfat-$(1).img
   endif
 endef
 
 define Image/Build/Profile/A10-OLinuXino-Lime
-	$(call Image/Build/SDCard,$(1),sun4i-a10-olinuxino-lime)
+	$(call Image/Build/SDCard,$(1),sun4i-a10-olinuxino-lime,$(2))
 endef
 
 define Image/Build/Profile/A13-OLinuXino
-	$(call Image/Build/SDCard,$(1),sun5i-a13-olinuxino)
+	$(call Image/Build/SDCard,$(1),sun5i-a13-olinuxino,$(2))
 endef
 
 define Image/Build/Profile/A20-OLinuXino-Lime
-	$(call Image/Build/SDCard,$(1),sun7i-a20-olinuxino-lime)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-olinuxino-lime,$(2))
 endef
 
 define Image/Build/Profile/A20-OLinuXino_MICRO
-	$(call Image/Build/SDCard,$(1),sun7i-a20-olinuxino-micro)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-olinuxino-micro,$(2))
 endef
 
 define Image/Build/Profile/Bananapi
-	$(call Image/Build/SDCard,$(1),sun7i-a20-bananapi)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-bananapi,$(2))
 endef
 
 define Image/Build/Profile/Bananapro
-	$(call Image/Build/SDCard,$(1),sun7i-a20-bananapro)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-bananapro,$(2))
 endef
 
 define Image/Build/Profile/Lamobo_R1
-	$(call Image/Build/SDCard,$(1),sun7i-a20-lamobo-r1)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-lamobo-r1,$(2))
 endef
 
 define Image/Build/Profile/Cubieboard
-	$(call Image/Build/SDCard,$(1),sun4i-a10-cubieboard)
+	$(call Image/Build/SDCard,$(1),sun4i-a10-cubieboard,$(2))
 endef
 
 define Image/Build/Profile/Cubieboard2
-	$(call Image/Build/SDCard,$(1),sun7i-a20-cubieboard2)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-cubieboard2,$(2))
 endef
 
 define Image/Build/Profile/Cubietruck
-	$(call Image/Build/SDCard,$(1),sun7i-a20-cubietruck)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-cubietruck,$(2))
 endef
 
 define Image/Build/Profile/OLIMEX_A13_SOM
-	$(call Image/Build/SDCard,$(1),sun5i-a13-olinuxino)
+	$(call Image/Build/SDCard,$(1),sun5i-a13-olinuxino,$(2))
 endef
 
 define Image/Build/Profile/Mele_M9
-	$(call Image/Build/SDCard,$(1),sun6i-a31-hummingbird)
+	$(call Image/Build/SDCard,$(1),sun6i-a31-hummingbird,$(2))
 endef
 
 define Image/Build/Profile/Linksprite_pcDuino
-	$(call Image/Build/SDCard,$(1),sun4i-a10-pcduino)
+	$(call Image/Build/SDCard,$(1),sun4i-a10-pcduino,$(2))
 endef
 
 define Image/Build/Profile/Linksprite_pcDuino3
-	$(call Image/Build/SDCard,$(1),sun7i-a20-pcduino3)
+	$(call Image/Build/SDCard,$(1),sun7i-a20-pcduino3,$(2))
+endef
 endef
 
 define Image/Build
-	$(call Image/Build/$(1),$(1))
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/$(1),$(1),$(2))
+	$(call Image/Build/Profile/$(2),$(1),$(2))
 
 	dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
 endef
diff --git a/target/linux/x86/image/Makefile b/target/linux/x86/image/Makefile
index 8e56372..9f4f3e1 100644
--- a/target/linux/x86/image/Makefile
+++ b/target/linux/x86/image/Makefile
@@ -214,20 +214,20 @@  define Image/Build/Initramfs
 endef
 
 define Image/Build
-	$(call Image/Build/$(1))
+	$(call Image/Build/$(1),($1),$(2))
   ifneq ($(1),iso)
-	$(call Image/Build/grub2,$(1))
-	$(call Image/Build/vdi,$(1))
-	$(call Image/Build/vmdk,$(1))
+	$(call Image/Build/grub2,$(1),$(2))
+	$(call Image/Build/vdi,$(1),$(2))
+	$(call Image/Build/vmdk,$(1),$(2))
 	$(CP) $(KDIR)/root.$(1) $(BIN_DIR)/$(IMG_PREFIX)-rootfs-$(1).img
   else
 	$(CP) $(KDIR)/root.iso $(BIN_DIR)/$(IMG_PREFIX).iso
   endif
 	$(CP) $(KDIR)/bzImage $(BIN_DIR)/$(IMG_PREFIX)-vmlinuz
-	$(call Image/Build/gzip/$(1))
-	$(call Image/Build/Profile/$(PROFILE),$(1))
+	$(call Image/Build/gzip/$(1),$(2))
+	$(call Image/Build/Profile/$(2),$(1),$(2))
 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y)
-	$(call Image/Build/Initramfs)
+	$(call Image/Build/Initramfs,$(2))
 endif
 endef