From d8734cfb71c4bfe04dd819b296fb54196e1259f7 Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Sat, 24 May 2014 15:29:26 +0000
Subject: [PATCH] Improved Makefile path referencing to always use relative
 paths, as using absolute ones fails when the absolute path contains spaces.

---
 Makefile                                      | 22 +++++++++++-------
 miosix/Makefile                               | 10 ++++----
 miosix/_doc/textdoc/Changelog.txt             |  3 +++
 miosix/_examples/sad_trombone/Makefile        | 22 +++++++++++-------
 miosix/_tools/init_project_out_of_git_tree.pl |  2 +-
 miosix/_tools/relpath.pl                      | 23 +++++++++++++++++++
 miosix/config/Makefile.inc                    |  2 +-
 7 files changed, 60 insertions(+), 24 deletions(-)
 create mode 100644 miosix/_tools/relpath.pl

diff --git a/Makefile b/Makefile
index a58ceef5..f53346bb 100644
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,11 @@
 ##
 ## Makefile for Miosix embedded OS
 ##
-MAKEFILE_VERSION := 1.03
-## Path to kernel directory
+MAKEFILE_VERSION := 1.04
+## Path to kernel directory (edited by init_project_out_of_git_repo.pl)
 KPATH := miosix
-## Path to config directory
-CONFPATH := $(realpath $(KPATH))
+## Path to config directory (edited by init_project_out_of_git_repo.pl)
+CONFPATH := $(KPATH)
 include $(CONFPATH)/config/Makefile.inc
 
 ##
@@ -59,10 +59,16 @@ program:
 	$(PROGRAM_CMDLINE)
 
 all-recursive:
-	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i) CONFPATH="$(CONFPATH)" || exit 1;)
+	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i)                               \
+	  KPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(KPATH))       \
+	  CONFPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(CONFPATH)) \
+	  || exit 1;)
 
 clean-recursive:
-	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i) CONFPATH="$(CONFPATH)" clean || exit 1;)
+	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i)                               \
+	  KPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(KPATH))       \
+	  CONFPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(CONFPATH)) \
+	  clean || exit 1;)
 
 clean-topdir:
 	-rm -f $(OBJ) main.elf main.hex main.bin main.map $(OBJ:.o=.d)
@@ -77,10 +83,10 @@ main.elf: $(OBJ) all-recursive
 	$(CXX) $(LFLAGS) -o main.elf $(OBJ) $(KPATH)/$(BOOT_FILE) $(LINK_LIBS)
 
 %.o: %.s
-	$(AS) $(AFLAGS) $< -o $@
+	$(AS)  $(AFLAGS) $< -o $@
 
 %.o : %.c
-	$(CC) $(DFLAGS) $(CFLAGS) $< -o $@
+	$(CC)  $(DFLAGS) $(CFLAGS) $< -o $@
 
 %.o : %.cpp
 	$(CXX) $(DFLAGS) $(CXXFLAGS) $< -o $@
diff --git a/miosix/Makefile b/miosix/Makefile
index 001ad2e1..a1066193 100644
--- a/miosix/Makefile
+++ b/miosix/Makefile
@@ -2,10 +2,8 @@
 ## Makefile for Miosix embedded OS
 ## This makefile builds the whole kernel
 ##
-MAKEFILE_VERSION := 1.03
-## Path to kernel directory
-KPATH := .
-## CONFPATH is forwarded by the parent Makefile
+MAKEFILE_VERSION := 1.04
+## KPATH and CONFPATH are forwarded by the parent Makefile
 include $(CONFPATH)/config/Makefile.inc
 
 ## List of all Miosix OS source files that have no special requirements
@@ -73,13 +71,13 @@ clean:
 	-rm -f $(BOOT_FILE:.o=.d)
 
 %.o: %.s
-	$(AS) $(AFLAGS) $< -o $@
+	$(AS)  $(AFLAGS) $< -o $@
 
 %.o : %.c
 	$(CC)  $(DFLAGS) $(CFLAGS) $< -o $@
 
 %.o : %.cpp
-	$(CXX)  $(DFLAGS) $(CXXFLAGS) $< -o $@
+	$(CXX) $(DFLAGS) $(CXXFLAGS) $< -o $@
 
 #pull in dependecy info for existing .o files
 -include $(OBJ:.o=.d)
diff --git a/miosix/_doc/textdoc/Changelog.txt b/miosix/_doc/textdoc/Changelog.txt
index 6d8ed164..1d57d7bc 100644
--- a/miosix/_doc/textdoc/Changelog.txt
+++ b/miosix/_doc/textdoc/Changelog.txt
@@ -1,5 +1,8 @@
 Changelog for Miosix np embedded OS
 
+- Improved Makefile path referencing to always use relative paths, as using
+  absolute ones fails when the absolute path contains spaces. This proved to
+  be impossible with makefiles alone and required to add a small perl script
 - Added versioning to miosix_settings.h and board_settings.h to prevent
   misconfiguration when those files are updated.
 - Moved board_settings.h in the config directory to allow out of git tree
diff --git a/miosix/_examples/sad_trombone/Makefile b/miosix/_examples/sad_trombone/Makefile
index 3e4544e0..50467d33 100644
--- a/miosix/_examples/sad_trombone/Makefile
+++ b/miosix/_examples/sad_trombone/Makefile
@@ -1,11 +1,11 @@
 ##
 ## Makefile for Miosix embedded OS
 ##
-MAKEFILE_VERSION := 1.03
-## Path to kernel directory
+MAKEFILE_VERSION := 1.04
+## Path to kernel directory (edited by init_project_out_of_git_repo.pl)
 KPATH := miosix
-## Path to config directory
-CONFPATH := $(realpath $(KPATH))
+## Path to config directory (edited by init_project_out_of_git_repo.pl)
+CONFPATH := $(KPATH)
 include $(CONFPATH)/config/Makefile.inc
 
 ##
@@ -59,10 +59,16 @@ program:
 	$(PROGRAM_CMDLINE)
 
 all-recursive:
-	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i) CONFPATH="$(CONFPATH)" || exit 1;)
+	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i)                               \
+	  KPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(KPATH))       \
+	  CONFPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(CONFPATH)) \
+	  || exit 1;)
 
 clean-recursive:
-	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i) CONFPATH="$(CONFPATH)" clean || exit 1;)
+	$(foreach i,$(SUBDIRS),$(MAKE) -C $(i)                               \
+	  KPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(KPATH))       \
+	  CONFPATH=$(shell perl $(KPATH)/_tools/relpath.pl $(i) $(CONFPATH)) \
+	  clean || exit 1;)
 
 clean-topdir:
 	-rm -f $(OBJ) main.elf main.hex main.bin main.map $(OBJ:.o=.d)
@@ -77,10 +83,10 @@ main.elf: $(OBJ) all-recursive
 	$(CXX) $(LFLAGS) -o main.elf $(OBJ) $(KPATH)/$(BOOT_FILE) $(LINK_LIBS)
 
 %.o: %.s
-	$(AS) $(AFLAGS) $< -o $@
+	$(AS)  $(AFLAGS) $< -o $@
 
 %.o : %.c
-	$(CC) $(DFLAGS) $(CFLAGS) $< -o $@
+	$(CC)  $(DFLAGS) $(CFLAGS) $< -o $@
 
 %.o : %.cpp
 	$(CXX) $(DFLAGS) $(CXXFLAGS) $< -o $@
diff --git a/miosix/_tools/init_project_out_of_git_tree.pl b/miosix/_tools/init_project_out_of_git_tree.pl
index c80024fa..ee6eb8ea 100644
--- a/miosix/_tools/init_project_out_of_git_tree.pl
+++ b/miosix/_tools/init_project_out_of_git_tree.pl
@@ -50,7 +50,7 @@ sub copy_and_fixup_makefile
 	while(<$in>)
 	{
 		s/^KPATH := miosix$/KPATH := $relpath\/miosix/;
-		s/^CONFPATH := \$\(realpath \$\(KPATH\)\)$/CONFPATH := \$\(realpath \.\)/;
+		s/^CONFPATH := \$\(KPATH\)$/CONFPATH := \./;
 		print $out "$_";
 	}
 	close($in);
diff --git a/miosix/_tools/relpath.pl b/miosix/_tools/relpath.pl
new file mode 100644
index 00000000..3a915adb
--- /dev/null
+++ b/miosix/_tools/relpath.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+
+# This script is used by the makefiles to convert a relative path from
+# a source directory to a relative path to a target directory when recursively
+# calling make. Unfortunately, GNU make has no such feature built in. It has,
+# however, a way to convert a relative path to an absolute one, that can be
+# passed to recursive makefiles while still referencing the same directory,
+# but dealing with absolute paths and makefiles is totally useless. Why?
+# Because an absolute path may easily end up containing a space character in
+# some of the directories, and GNU make fails *very* badly when dealing with
+# spaces in filenames. Using relative paths instead only requires that the
+# paths between directories containing source files are whitespace free, and
+# noth *all* the directory names up to the root one.
+
+use Cwd qw(abs_path);
+use File::Spec;
+
+die "Use: perl relpath.pl from to\n" unless($#ARGV+1==2);
+my $from=abs_path($ARGV[0]);
+my $to=abs_path($ARGV[1]);
+my $relpath=File::Spec->abs2rel($to,$from);
+print "$relpath";
+#print STDERR "========== from='$from' to='$to' relpath='$relpath'\n";
diff --git a/miosix/config/Makefile.inc b/miosix/config/Makefile.inc
index dab3ec41..f5db34ee 100644
--- a/miosix/config/Makefile.inc
+++ b/miosix/config/Makefile.inc
@@ -265,7 +265,7 @@ endif
 ## a new board or porting Miosix to a new architecture                    ##
 ############################################################################
 
-ifneq ($(MAKEFILE_VERSION),1.03)
+ifneq ($(MAKEFILE_VERSION),1.04)
     $(info You are using an incompatible makefile. Make sure it matches \
       the one distributed with the current version of the kernel)
     $(error Error)
-- 
GitLab