kbuild: change out-of-tree build

This commit changes the working directory
where the build process occurs.

Before this commit, build process occurred under the source
tree for both in-tree and out-of-tree build.

That's why we needed to add $(obj) prefix to all generated
files in makefiles like follows:
  $(obj)u-boot.bin:  $(obj)u-boot

Here, $(obj) is empty for in-tree build, whereas it points
to the output directory for out-of-tree build.

And our old build system changes the current working directory
with "make -C <sub-dir>" syntax when descending into the
sub-directories.

On the other hand, Kbuild uses a different idea
to handle out-of-tree build and directory descending.

The build process of Kbuild always occurs under the output tree.
When "O=dir/to/store/output/files" is given, the build system
changes the current working directory to that directory and
restarts the make.

Kbuild uses "make -f $(srctree)/scripts/Makefile.build obj=<sub-dir>"
syntax for descending into sub-directories.
(We can write it like "make $(obj)=<sub-dir>" with a shorthand.)
This means the current working directory is always the top
of the output directory.

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
Tested-by: Gerhard Sittig <gsi@denx.de>
diff --git a/arch/arm/cpu/arm1136/config.mk b/arch/arm/cpu/arm1136/config.mk
index f74228c..ab1fc4a 100644
--- a/arch/arm/cpu/arm1136/config.mk
+++ b/arch/arm/cpu/arm1136/config.mk
@@ -14,6 +14,6 @@
 ALL-y	+= $(OBJTREE)/SPL
 endif
 else
-ALL-y	+= $(obj)u-boot.imx
+ALL-y	+= u-boot.imx
 endif
 endif
diff --git a/arch/arm/cpu/arm926ejs/config.mk b/arch/arm/cpu/arm926ejs/config.mk
index 4d9895f..f27ca15 100644
--- a/arch/arm/cpu/arm926ejs/config.mk
+++ b/arch/arm/cpu/arm926ejs/config.mk
@@ -13,6 +13,6 @@
 ALL-y	+= $(OBJTREE)/SPL
 endif
 else
-ALL-y	+= $(obj)u-boot.imx
+ALL-y	+= u-boot.imx
 endif
 endif
diff --git a/arch/arm/cpu/arm926ejs/davinci/config.mk b/arch/arm/cpu/arm926ejs/davinci/config.mk
index d5c978b..69e9d5a 100644
--- a/arch/arm/cpu/arm926ejs/davinci/config.mk
+++ b/arch/arm/cpu/arm926ejs/davinci/config.mk
@@ -4,5 +4,5 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 ifndef CONFIG_SPL_BUILD
-ALL-$(CONFIG_SPL_FRAMEWORK)	+= $(obj)u-boot.ais
+ALL-$(CONFIG_SPL_FRAMEWORK)	+= u-boot.ais
 endif
diff --git a/arch/arm/cpu/armv7/am33xx/config.mk b/arch/arm/cpu/armv7/am33xx/config.mk
index 8e3668f..1c06fb4 100644
--- a/arch/arm/cpu/armv7/am33xx/config.mk
+++ b/arch/arm/cpu/armv7/am33xx/config.mk
@@ -7,5 +7,5 @@
 ALL-y	+= $(OBJTREE)/MLO
 ALL-$(CONFIG_SPL_SPI_SUPPORT) += $(OBJTREE)/MLO.byteswap
 else
-ALL-y	+= $(obj)u-boot.img
+ALL-y	+= u-boot.img
 endif
diff --git a/arch/arm/cpu/armv7/config.mk b/arch/arm/cpu/armv7/config.mk
index 38b7c40..d01f3d9 100644
--- a/arch/arm/cpu/armv7/config.mk
+++ b/arch/arm/cpu/armv7/config.mk
@@ -20,6 +20,6 @@
 ALL-y	+= $(OBJTREE)/SPL
 endif
 else
-ALL-y	+= $(obj)u-boot.imx
+ALL-y	+= u-boot.imx
 endif
 endif
diff --git a/arch/arm/cpu/armv7/omap3/config.mk b/arch/arm/cpu/armv7/omap3/config.mk
index 1d6a57c..2a3d1c5 100644
--- a/arch/arm/cpu/armv7/omap3/config.mk
+++ b/arch/arm/cpu/armv7/omap3/config.mk
@@ -11,5 +11,5 @@
 ifdef CONFIG_SPL_BUILD
 ALL-y	+= $(OBJTREE)/MLO
 else
-ALL-y	+= $(obj)u-boot.img
+ALL-y	+= u-boot.img
 endif
diff --git a/arch/arm/cpu/armv7/omap4/config.mk b/arch/arm/cpu/armv7/omap4/config.mk
index 1d6a57c..2a3d1c5 100644
--- a/arch/arm/cpu/armv7/omap4/config.mk
+++ b/arch/arm/cpu/armv7/omap4/config.mk
@@ -11,5 +11,5 @@
 ifdef CONFIG_SPL_BUILD
 ALL-y	+= $(OBJTREE)/MLO
 else
-ALL-y	+= $(obj)u-boot.img
+ALL-y	+= u-boot.img
 endif
diff --git a/arch/arm/cpu/armv7/omap5/config.mk b/arch/arm/cpu/armv7/omap5/config.mk
index 2673af9..261b272 100644
--- a/arch/arm/cpu/armv7/omap5/config.mk
+++ b/arch/arm/cpu/armv7/omap5/config.mk
@@ -9,5 +9,5 @@
 ifdef CONFIG_SPL_BUILD
 ALL-y	+= $(OBJTREE)/MLO
 else
-ALL-y	+= $(obj)u-boot.img
+ALL-y	+= u-boot.img
 endif
diff --git a/arch/arm/cpu/armv7/socfpga/config.mk b/arch/arm/cpu/armv7/socfpga/config.mk
index d33ab7d..3d18491 100644
--- a/arch/arm/cpu/armv7/socfpga/config.mk
+++ b/arch/arm/cpu/armv7/socfpga/config.mk
@@ -4,5 +4,5 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 ifndef CONFIG_SPL_BUILD
-ALL-y	+= $(obj)u-boot.img
+ALL-y	+= u-boot.img
 endif
diff --git a/arch/blackfin/config.mk b/arch/blackfin/config.mk
index 73fa798..c752025 100644
--- a/arch/blackfin/config.mk
+++ b/arch/blackfin/config.mk
@@ -12,7 +12,7 @@
 ifeq ($(CONFIG_BFIN_CPU),)
 CONFIG_BFIN_CPU := \
 	$(shell awk '$$2 == "CONFIG_BFIN_CPU" { print $$3 }' \
-		$(src)include/configs/$(BOARD).h)
+		$(srctree)/include/configs/$(BOARD).h)
 else
 CONFIG_BFIN_CPU := $(strip $(CONFIG_BFIN_CPU:"%"=%))
 endif
@@ -28,10 +28,10 @@
 PLATFORM_RELFLAGS += -mcpu=$(CONFIG_BFIN_CPU)
 
 ifneq ($(CONFIG_BFIN_BOOT_MODE),BFIN_BOOT_BYPASS)
-ALL-y += $(obj)u-boot.ldr
+ALL-y += u-boot.ldr
 endif
 ifeq ($(CONFIG_ENV_IS_EMBEDDED_IN_LDR),y)
-CREATE_LDR_ENV = $(obj)tools/envcrc --binary > $(obj)env-ldr.o
+CREATE_LDR_ENV = tools/envcrc --binary > env-ldr.o
 HOSTCFLAGS_NOPED_ADSP := \
 	$(shell $(CPP) -dD - -mcpu=$(CONFIG_BFIN_CPU) </dev/null \
 		| awk '$$2 ~ /ADSP/ { print "-D" $$2 }')
@@ -47,10 +47,10 @@
 
 LDR_FLAGS += --bmode $(subst BFIN_BOOT_,,$(CONFIG_BFIN_BOOT_MODE))
 LDR_FLAGS += --use-vmas
-LDR_FLAGS += --initcode $(obj)$(CPUDIR)/initcode.o
+LDR_FLAGS += --initcode $(CPUDIR)/initcode.o
 ifneq ($(CONFIG_BFIN_BOOT_MODE),BFIN_BOOT_UART)
 LDR_FLAGS-$(CONFIG_ENV_IS_EMBEDDED_IN_LDR) += \
-	--punchit $$(($(CONFIG_ENV_OFFSET))):$$(($(CONFIG_ENV_SIZE))):$(obj)env-ldr.o
+	--punchit $$(($(CONFIG_ENV_OFFSET))):$$(($(CONFIG_ENV_SIZE))):env-ldr.o
 endif
 ifneq (,$(findstring s,$(MAKEFLAGS)))
 LDR_FLAGS += --quiet
diff --git a/arch/blackfin/cpu/Makefile b/arch/blackfin/cpu/Makefile
index a61594a..369dc74 100644
--- a/arch/blackfin/cpu/Makefile
+++ b/arch/blackfin/cpu/Makefile
@@ -25,9 +25,9 @@
 
 # make sure our initcode (which goes into LDR) does not
 # have relocs or external references
-$(obj)initcode.o: CFLAGS += -fno-function-sections -fno-data-sections
+$(obj)/initcode.o: CFLAGS += -fno-function-sections -fno-data-sections
 READINIT = env LC_ALL=C $(CROSS_COMPILE)readelf -s $<
-$(obj)check_initcode: $(obj)initcode.o
+$(obj)/check_initcode: $(obj)/initcode.o
 ifneq ($(CONFIG_BFIN_BOOT_MODE),BFIN_BOOT_BYPASS)
 	@if $(READINIT) | grep '\<GLOBAL\>.*\<UND\>' ; then \
 		echo "$< contains external references!" 1>&2 ; \
@@ -35,7 +35,7 @@
 	fi
 endif
 
-$(obj)init.lds: init.lds.S
+$(obj)/init.lds: $(src)/init.lds.S
 	$(CPP) $(CPPFLAGS) $(LDPPFLAGS) -ansi -D__ASSEMBLY__ -P $^ -o $@
-$(obj)init.elf: $(obj)init.lds $(obj)init.o $(obj)initcode.o
+$(obj)/init.elf: $(obj)/init.lds $(obj)/init.o $(obj)/initcode.o
 	$(LD) $(LDFLAGS) -T $^ -o $@
diff --git a/arch/mips/cpu/mips32/config.mk b/arch/mips/cpu/mips32/config.mk
index 067f871..7ee7faa 100644
--- a/arch/mips/cpu/mips32/config.mk
+++ b/arch/mips/cpu/mips32/config.mk
@@ -21,4 +21,4 @@
 PLATFORM_LDFLAGS  += -m elf32ltsmip
 endif
 
-CONFIG_STANDALONE_LOAD_ADDR ?= 0x80200000 -T mips.lds
+CONFIG_STANDALONE_LOAD_ADDR ?= 0x80200000 -T $(srctree)/$(src)/mips.lds
diff --git a/arch/mips/cpu/mips64/config.mk b/arch/mips/cpu/mips64/config.mk
index d1a8b2c..02113a1 100644
--- a/arch/mips/cpu/mips64/config.mk
+++ b/arch/mips/cpu/mips64/config.mk
@@ -21,4 +21,4 @@
 PLATFORM_LDFLAGS  += -m elf64ltsmip
 endif
 
-CONFIG_STANDALONE_LOAD_ADDR ?= 0xffffffff80200000 -T mips64.lds
+CONFIG_STANDALONE_LOAD_ADDR ?= 0xffffffff80200000 -T $(srctree)/$(src)/mips64.lds
diff --git a/arch/mips/cpu/xburst/config.mk b/arch/mips/cpu/xburst/config.mk
index d81da21..00b0fd9 100644
--- a/arch/mips/cpu/xburst/config.mk
+++ b/arch/mips/cpu/xburst/config.mk
@@ -12,4 +12,4 @@
 PLATFORM_LDFLAGS  += -m elf32ltsmip
 endif
 
-CONFIG_STANDALONE_LOAD_ADDR ?= 0x80200000 -T mips.lds
+CONFIG_STANDALONE_LOAD_ADDR ?= 0x80200000 -T $(srctree)/$(src)/mips.lds
diff --git a/arch/nds32/config.mk b/arch/nds32/config.mk
index e93e3a8..550f8a4 100644
--- a/arch/nds32/config.mk
+++ b/arch/nds32/config.mk
@@ -10,7 +10,7 @@
 
 CROSS_COMPILE ?= nds32le-linux-
 
-CONFIG_STANDALONE_LOAD_ADDR = 0x300000 -T nds32.lds
+CONFIG_STANDALONE_LOAD_ADDR = 0x300000 -T $(srctree)/$(src)/nds32.lds
 
 PLATFORM_RELFLAGS	+= -fno-strict-aliasing -fno-common -mrelax
 PLATFORM_RELFLAGS	+= -gdwarf-2
diff --git a/arch/powerpc/lib/Makefile b/arch/powerpc/lib/Makefile
index a706d3c..ac780d4 100644
--- a/arch/powerpc/lib/Makefile
+++ b/arch/powerpc/lib/Makefile
@@ -54,11 +54,11 @@
 # Workaround for local bus unaligned access problems
 # on MPC512x and MPC5200
 ifdef CONFIG_MPC512X
-$(obj)ppcstring.o: AFLAGS += -Dmemcpy=__memcpy
+$(obj)/ppcstring.o: AFLAGS += -Dmemcpy=__memcpy
 obj-y += memcpy_mpc5200.o
 endif
 ifdef CONFIG_MPC5200
-$(obj)ppcstring.o: AFLAGS += -Dmemcpy=__memcpy
+$(obj)/ppcstring.o: AFLAGS += -Dmemcpy=__memcpy
 obj-y += memcpy_mpc5200.o
 endif
 endif
diff --git a/arch/sandbox/cpu/Makefile b/arch/sandbox/cpu/Makefile
index b564294..c5f5426 100644
--- a/arch/sandbox/cpu/Makefile
+++ b/arch/sandbox/cpu/Makefile
@@ -10,7 +10,7 @@
 obj-y	:= cpu.o os.o start.o state.o
 
 # os.c is build in the system environment, so needs standard includes
-$(obj)os.o: CFLAGS := $(filter-out -nostdinc,\
+$(obj)/os.o: CFLAGS := $(filter-out -nostdinc,\
 	$(patsubst -I%,-idirafter%,$(CFLAGS)))
-$(obj).depend.os: CPPFLAGS := $(filter-out -nostdinc,\
+$(obj)/.depend.os: CPPFLAGS := $(filter-out -nostdinc,\
 	$(patsubst -I%,-idirafter%,$(CPPFLAGS)))
diff --git a/arch/sparc/config.mk b/arch/sparc/config.mk
index e94e7cb..9bb3724 100644
--- a/arch/sparc/config.mk
+++ b/arch/sparc/config.mk
@@ -7,6 +7,7 @@
 
 CROSS_COMPILE ?= sparc-elf-
 
-CONFIG_STANDALONE_LOAD_ADDR ?= 0x00000000 -L $(gcclibdir) -T sparc.lds
+CONFIG_STANDALONE_LOAD_ADDR ?= 0x00000000 -L $(gcclibdir) \
+			-T $(srctree)/$(src)/sparc.lds
 
 PLATFORM_CPPFLAGS += -DCONFIG_SPARC -D__sparc__
diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 638f790..a35d062 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -23,5 +23,5 @@
 LIBGCC := $(notdir $(NORMAL_LIBGCC))
 extra-y := $(LIBGCC)
 
-$(obj)$(LIBGCC): $(NORMAL_LIBGCC)
+$(obj)/$(LIBGCC): $(NORMAL_LIBGCC)
 	$(OBJCOPY) $< $@ --prefix-symbols=__normal_