Refactor and unify objcopy calls in the build system. Also now properly handles DEBUG builds on hosted targets to keep debug symbols if necessary.

Change-Id: I884031b79c6d49479e4d95752f35ced68872dd5d
diff --git a/android/android.make b/android/android.make
index dfbc64e..b1f8588 100644
--- a/android/android.make
+++ b/android/android.make
@@ -116,7 +116,7 @@
 
 $(BUILDDIR)/$(BINARY): $$(OBJ) $(FIRMLIB) $(VOICESPEEXLIB) $(CORE_LIBS) $(CPUFEAT_BUILD)/cpu-features.o
 	$(call PRINTS,LD $(BINARY))$(CC) -o $@ $^ $(LDOPTS) $(GLOBAL_LDOPTS) -Wl,-Map,$(BUILDDIR)/rockbox.map
-	$(call PRINTS,OC $(@F))$(OC) -S -x $@
+	$(call PRINTS,OC $(@F))$(call objcopy,$@,$@)
 
 $(BINLIB_DIR)/$(BINARY): $(BUILDDIR)/$(BINARY)
 	$(call PRINTS,CP $(BINARY))cp $^ $@
diff --git a/apps/codecs/codecs.make b/apps/codecs/codecs.make
index f2d2f13..3e4ec3a 100644
--- a/apps/codecs/codecs.make
+++ b/apps/codecs/codecs.make
@@ -202,10 +202,4 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(CODECLDFLAGS)
-ifndef APP_TYPE
-	$(SILENT)$(OC) -O binary $(CODECDIR)/$*.elf $@ # objcopy native
-else ifeq (,$(findstring sdl-sim,$(APP_TYPE)))
-	$(SILENT)$(OC) -S -x $(CODECDIR)/$*.elf $@	   # objcopy hosted
-else
-	$(SILENT)cp $(CODECDIR)/$*.elf $@			   # no objcopy, keep debug symbols
-endif
+	$(SILENT)$(call objcopy,$(CODECDIR)/$*.elf,$@)
diff --git a/apps/plugins/chessbox/chessbox.make b/apps/plugins/chessbox/chessbox.make
index 357130c..ee4940d 100644
--- a/apps/plugins/chessbox/chessbox.make
+++ b/apps/plugins/chessbox/chessbox.make
@@ -44,7 +44,7 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(CHESSBOX_OVLFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
 
 # special pattern rule for compiling chessbox with extra flags
 $(CHESSBOX_OBJDIR)/%.o: $(CHESSBOX_SRCDIR)/%.c $(CHESSBOX_SRCDIR)/chessbox.make
diff --git a/apps/plugins/goban/goban.make b/apps/plugins/goban/goban.make
index 2bc85ad..a8b4128 100644
--- a/apps/plugins/goban/goban.make
+++ b/apps/plugins/goban/goban.make
@@ -43,4 +43,4 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(GOBAN_OVLFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
diff --git a/apps/plugins/imageviewer/imageviewer.make b/apps/plugins/imageviewer/imageviewer.make
index 7f7d943..97af1e9 100644
--- a/apps/plugins/imageviewer/imageviewer.make
+++ b/apps/plugins/imageviewer/imageviewer.make
@@ -40,11 +40,7 @@
 		$(filter-out $(PLUGIN_CRT0),$(filter %.o, $^)) \
 		$(filter %.a, $+) \
 		-lgcc $(IMGDEC_OVLFLAGS)
-ifdef APP_TYPE
-	$(SILENT)cp $(IMGVBUILDDIR)/$*.elf $@
-else
-	$(SILENT)$(OC) -O binary $(IMGVBUILDDIR)/$*.elf $@
-endif
+	$(SILENT)$(call objcopy,$(IMGVBUILDDIR)/$*.elf,$@)
 
 # rule to create reference map for image decoder
 $(IMGVBUILDDIR)/%.refmap: $(APPSDIR)/plugin.h $(IMGVSRCDIR)/imageviewer.h $(PLUGINLINK_LDS) $(PLUGIN_LIBS)
diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make
index 9e2db62..a70f028 100644
--- a/apps/plugins/lua/lua.make
+++ b/apps/plugins/lua/lua.make
@@ -56,4 +56,4 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(LUA_OVLFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
diff --git a/apps/plugins/pdbox/pdbox.make b/apps/plugins/pdbox/pdbox.make
index 239e1c0..3edc97e 100644
--- a/apps/plugins/pdbox/pdbox.make
+++ b/apps/plugins/pdbox/pdbox.make
@@ -36,10 +36,6 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(PDBOXLDFLAGS)
-ifdef APP_TYPE
-	$(SILENT)cp $*.elf $@
-else
-	$(SILENT)$(OC) -O binary $*.elf $@
-endif
+	$(SILENT)$(call objcopy,$*.elf,$@)
 
 
diff --git a/apps/plugins/pictureflow/pictureflow.make b/apps/plugins/pictureflow/pictureflow.make
index e3d4544..9c29f7a 100644
--- a/apps/plugins/pictureflow/pictureflow.make
+++ b/apps/plugins/pictureflow/pictureflow.make
@@ -50,7 +50,7 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(PICTUREFLOW_OVLFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
 
 # special pattern rule for compiling pictureflow with extra flags
 $(PICTUREFLOW_OBJDIR)/%.o: $(PICTUREFLOW_SRCDIR)/%.c $(PICTUREFLOW_SRCDIR)/pictureflow.make
diff --git a/apps/plugins/plugins.make b/apps/plugins/plugins.make
index b80523d..27bc69f 100644
--- a/apps/plugins/plugins.make
+++ b/apps/plugins/plugins.make
@@ -120,13 +120,7 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(PLUGINLDFLAGS)
-ifndef APP_TYPE
-	$(SILENT)$(OC) -O binary $(BUILDDIR)/$*.elf $@ # objcopy native
-else ifeq (,$(findstring sdl-sim,$(APP_TYPE)))
-	$(SILENT)$(OC) -S -x $(BUILDDIR)/$*.elf $@	   # objcopy hosted
-else
-	$(SILENT)cp $(BUILDDIR)/$*.elf $@			   # no objcopy, keep debug symbols
-endif
+	$(SILENT)$(call objcopy,$(BUILDDIR)/$*.elf,$@)
 
 $(BUILDDIR)/apps/plugins/%.lua: $(ROOTDIR)/apps/plugins/%.lua
 	$(call PRINTS,CP $(subst $(ROOTDIR)/,,$<))cp $< $(BUILDDIR)/apps/plugins/
diff --git a/apps/plugins/rockboy/rockboy.make b/apps/plugins/rockboy/rockboy.make
index 4869ced..625c40e 100644
--- a/apps/plugins/rockboy/rockboy.make
+++ b/apps/plugins/rockboy/rockboy.make
@@ -37,4 +37,4 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(ROCKBOY_OVLFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
diff --git a/apps/plugins/zxbox/zxbox.make b/apps/plugins/zxbox/zxbox.make
index a04801a..4638d5f 100644
--- a/apps/plugins/zxbox/zxbox.make
+++ b/apps/plugins/zxbox/zxbox.make
@@ -45,7 +45,7 @@
 		$(filter %.o, $^) \
 		$(filter %.a, $+) \
 		-lgcc $(ZXBOX_LDFLAGS)
-	$(call PRINTS,LD $(@F))$(OC) -O binary $(basename $@).elf $@
+	$(call PRINTS,LD $(@F))$(call objcopy,$(basename $@).elf,$@)
 
 # special pattern rule for compiling zxbox with extra flags
 $(ZXBOX_OBJDIR)/%.o: $(ZXBOX_SRCDIR)/%.c $(ZXBOX_SRCDIR)/zxbox.make
diff --git a/bootloader/bootloader.make b/bootloader/bootloader.make
index 88305f3..d1f580a 100644
--- a/bootloader/bootloader.make
+++ b/bootloader/bootloader.make
@@ -30,7 +30,7 @@
 		-Wl,--gc-sections -Wl,-Map,$(BUILDDIR)/bootloader.map
 
 $(BUILDDIR)/bootloader.bin : $(BUILDDIR)/bootloader.elf
-	$(call PRINTS,OBJCOPY $(@F))$(OC) $(if $(filter yes, $(USE_ELF)), -S -x, -O binary) $< $@
+	$(call PRINTS,OC $(@F))$(call objcopy,$<,$@)
 
 $(BUILDDIR)/bootloader.asm: $(BUILDDIR)/bootloader.bin
 	$(TOOLSDIR)/sh2d -sh1 $< > $@
diff --git a/firmware/target/hosted/ypr0/ypr0.make b/firmware/target/hosted/ypr0/ypr0.make
index 68f5390..d0f4cb7 100644
--- a/firmware/target/hosted/ypr0/ypr0.make
+++ b/firmware/target/hosted/ypr0/ypr0.make
@@ -21,4 +21,4 @@
 		$(LDOPTS) $(GLOBAL_LDOPTS) -Wl,-Map,$(BUILDDIR)/rockbox.map
 
 $(BUILDDIR)/rockbox : $(BUILDDIR)/rockbox.elf
-	$(call PRINTS,OC $(@F))$(OC) -S -x $< $@
+	$(call PRINTS,OC $(@F))$(call objcopy,$^,$@)
diff --git a/tools/functions.make b/tools/functions.make
index db7bbe9..ead62f7 100644
--- a/tools/functions.make
+++ b/tools/functions.make
@@ -28,6 +28,20 @@
 
 a2lnk = $(patsubst lib%.a,-l%,$(notdir $(1)))
 
+# objcopy wrapper that keeps debug symbols in DEBUG builds
+# handles the $(1) == $(2) case too
+ifndef APP_TYPE
+objcopy = $(OC) $(if $(filter yes, $(USE_ELF)), -S -x, -O binary) $(1) $(2)	# objcopy native
+else ifneq (,$(findstring sdl-sim,$(APP_TYPE)))
+objcopy = cp $(1) $(1).tmp;mv -f $(1).tmp $(2)		# objcopy simulator
+else
+  ifdef DEBUG
+    objcopy = cp $(1) $(1).tmp;mv -f $(1).tmp $(2)	# objcopy hosted (DEBUG)
+  else
+    objcopy = $(OC) -S -x $(1) $(2)					# objcopy hosted (!DEBUG)
+   endif
+endif
+
 # calculate dependencies for a list of source files $(2) and output them to $(1)
 mkdepfile = $(SILENT)perl $(TOOLSDIR)/multigcc.pl $(CC) $(PPCFLAGS) $(OTHER_INC) -MG -MM -include config.h -- $(2) | \
 	sed -e "s: lang.h: lang/lang.h:" \
diff --git a/tools/root.make b/tools/root.make
index 30e3b3f..0fc6c55 100644
--- a/tools/root.make
+++ b/tools/root.make
@@ -220,10 +220,10 @@
 		-T$(LINKROM) -Wl,-Map,$(BUILDDIR)/rombox.map
 
 $(BUILDDIR)/rockbox.bin : $(BUILDDIR)/rockbox.elf
-	$(call PRINTS,OC $(@F))$(OC) $(if $(filter yes, $(USE_ELF)), -S -x, -O binary) $< $@
+	$(call PRINTS,OC $(@F))$(call objcopy,$<,$@)
 
 $(BUILDDIR)/rombox.bin : $(BUILDDIR)/rombox.elf
-	$(call PRINTS,OC $(@F))$(OC) -O binary $< $@
+	$(call PRINTS,OC $(@F))$(call objcopy,$<,$@)
 
 #
 # If there's a flashfile defined for this target (rockbox.ucl for Archos
diff --git a/uisimulator/uisimulator.make b/uisimulator/uisimulator.make
index 1ab3fda..8756ac6 100644
--- a/uisimulator/uisimulator.make
+++ b/uisimulator/uisimulator.make
@@ -33,6 +33,7 @@
 $(BUILDDIR)/$(BINARY): $$(OBJ) $(FIRMLIB) $(VOICESPEEXLIB) $(CORE_LIBS) $(SIMLIB)
 	$(call PRINTS,LD $(BINARY))$(CC) -o $@ $^ $(SIMLIB) $(LDOPTS) $(GLOBAL_LDOPTS) \
 	-Wl,-Map,$(BUILDDIR)/rockbox.map
+	$(SILENT)$(call objcopy,$@,$@)
 
 $(BUILDDIR)/uisimulator/%.o: $(ROOTDIR)/uisimulator/%.c
 	$(SILENT)mkdir -p $(dir $@)