klibc: fix sparc build

Fix build of klibc on sparc. Following patch is only tested on a kernel
build but should work on a native klibc build too.

The patch fixes a bug in klibc/Kbuild so assignments to the variable
targets in arch/$(KLIBCARCH)/Makefile.inc is not lost.
Without this 'make clean' did not work as expected.

Based on input from maximilian attems <maks@sternwelten.at> and
Fabio Massimo Di Nitto <fabbione@ubuntu.com>.

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
diff --git a/klibc/Kbuild b/klibc/Kbuild
index 6533860..fb50a1c 100644
--- a/klibc/Kbuild
+++ b/klibc/Kbuild
@@ -81,7 +81,7 @@
 
 SOLIBHASH = $(shell cat $(SOLIB).hash)
 
-targets  := arch/$(KLIBCARCH)/crt0.o
+targets  += arch/$(KLIBCARCH)/crt0.o
 targets  += $(libc-y) $(KLIBCARCHOBJS)
 
 # Generate syscall stubs
diff --git a/klibc/arch/sparc/Makefile.inc b/klibc/arch/sparc/Makefile.inc
index 41c23c5..3389a16 100644
--- a/klibc/arch/sparc/Makefile.inc
+++ b/klibc/arch/sparc/Makefile.inc
@@ -7,11 +7,10 @@
 # accordingly.
 #
 
-KLIBCARCHOBJS = \
-	arch/$(KLIBCARCH)/sdiv.o \
-	arch/$(KLIBCARCH)/udiv.o \
-	arch/$(KLIBCARCH)/srem.o \
-	arch/$(KLIBCARCH)/urem.o \
+m4-targets := arch/$(KLIBCARCH)/sdiv.o arch/$(KLIBCARCH)/srem.o \
+              arch/$(KLIBCARCH)/udiv.o arch/$(KLIBCARCH)/urem.o
+
+KLIBCARCHOBJS = $(m4-targets) \
 	arch/$(KLIBCARCH)/smul.o \
 	arch/$(KLIBCARCH)/umul.o \
 	arch/$(KLIBCARCH)/setjmp.o \
@@ -23,29 +22,20 @@
 	libgcc/__umoddi3.o \
 	libgcc/__udivmoddi4.o
 
-arch/$(KLIBCARCH)/sdiv.S: arch/$(KLIBCARCH)/divrem.m4
-	@echo 'building $@ from $^'
-	@(echo "define(NAME,\`.div')define(OP,\`div')define(S,\`true')"; \
-	 cat $^) | m4 > $@
-	@chmod 444 $@
+adir := $(obj)/arch/$(KLIBCARCH)
 
-arch/$(KLIBCARCH)/udiv.S: arch/$(KLIBCARCH)/divrem.m4
-	@echo 'building $@ from $^'
-	@(echo "define(NAME,\`.udiv')define(OP,\`div')define(S,\`false')"; \
-	 cat $^) | m4 > $@
-	@chmod 444 $@
+$(adir)/sdiv.S: m4 := define(NAME,\`.div')define(OP,\`div')define(S,\`true')
+$(adir)/srem.S: m4 := define(NAME,\`.rem')define(OP,\`rem')define(S,\`true')
+$(adir)/udiv.S: m4 := define(NAME,\`.udiv')define(OP,\`div')define(S,\`false')
+$(adir)/urem.S: m4 := define(NAME,\`.urem')define(OP,\`rem')define(S,\`false')
 
-arch/$(KLIBCARCH)/srem.S: arch/$(KLIBCARCH)/divrem.m4
-	@echo 'building $@ from $^'
-	@(echo "define(NAME,\`.rem')define(OP,\`rem')define(S,\`true')"; \
-	 cat $^) | m4 > $@
-	@chmod 444 $@
+targets += $(m4-targets) $(m4-targets:.o=.S)
 
-arch/$(KLIBCARCH)/urem.S: arch/$(KLIBCARCH)/divrem.m4
-	@echo 'building $@ from $^'
-	@(echo "define(NAME,\`.urem')define(OP,\`rem')define(S,\`false')"; \
-	 cat $^) | m4 > $@
-	@chmod 444 $@
+quiet_cmd_m4 = M4      $@
+      cmd_m4 = (echo "$(m4)"; cat $^) | m4 > $@
 
-archclean:
-	rm -f arch/$(KLIBCARCH)/?div.S arch/$(KLIBCARCH)/?rem.S
+# build .o from .S
+$(addprefix $(obj)/,$(m4-targets)): $(adir)/%.o : $(adir)/%.S
+# build .S from .m4
+$(addprefix $(obj)/,$(m4-targets:.o=.S)): $(src)/arch/$(KLIBCARCH)/divrem.m4
+	$(call if_changed,m4)