Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make external toolchain relocatable

The external toolchain wrapper sets sysroot etc. to an absolute path.
By changing this to a relative path, it is possible to move the host
directory to a different location and still have a working build
system.

This only works for a downloaded external toolchain.  For a pre-installed
external toolchain, it is possible to move the host directory to a
different location, but not the external toolchain directory (it does work
if the external toolchain directory lies within the host directory).  For
an internal or crosstool-ng toolchain, there is no wrapper so updating the
sysroot path should be done in a different way.

See http://lists.busybox.net/pipermail/buildroot/2012-February/050371.html
for information about others things to do to make the host directory
relocatable.

Signed-off-by: Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
Acked-by: Samuel Martin <s.martin49@gmail.com>
Signed-off-by: Peter Korsgaard <jacmet@sunsite.dk>
  • Loading branch information...
commit befb9a3ee0c5994162f527d8ef763d57d68ec48e 1 parent 1b2ab4a
arnout authored July 15, 2012 Peter Korsgaard committed February 05, 2013
3  package/Makefile.in
@@ -65,7 +65,8 @@ ifeq ($(BR2_xtensa),y)
65 65
 TARGET_ABI += -mlongcalls
66 66
 endif
67 67
 
68  
-STAGING_DIR=$(HOST_DIR)/usr/$(GNU_TARGET_NAME)/sysroot
  68
+STAGING_SUBDIR = usr/$(GNU_TARGET_NAME)/sysroot
  69
+STAGING_DIR    = $(HOST_DIR)/$(STAGING_SUBDIR)
69 70
 
70 71
 TARGET_OPTIMIZATION:=$(call qstrip,$(BR2_TARGET_OPTIMIZATION))
71 72
 
16  toolchain/toolchain-external/ext-tool.mk
@@ -121,9 +121,17 @@ endif
121 121
 TOOLCHAIN_EXTERNAL_CROSS=$(TOOLCHAIN_EXTERNAL_BIN)/$(TOOLCHAIN_EXTERNAL_PREFIX)-
122 122
 TOOLCHAIN_EXTERNAL_CC=$(TOOLCHAIN_EXTERNAL_CROSS)gcc
123 123
 TOOLCHAIN_EXTERNAL_CXX=$(TOOLCHAIN_EXTERNAL_CROSS)g++
124  
-TOOLCHAIN_EXTERNAL_WRAPPER_ARGS = \
125  
-	-DBR_CROSS_PATH='"$(TOOLCHAIN_EXTERNAL_BIN)/"' \
126  
-	-DBR_SYSROOT='"$(STAGING_DIR)"'
  124
+TOOLCHAIN_EXTERNAL_WRAPPER_ARGS = -DBR_SYSROOT='"$(STAGING_SUBDIR)"'
  125
+
  126
+ifeq ($(filter $(HOST_DIR)/%,$(TOOLCHAIN_EXTERNAL_BIN)),)
  127
+# TOOLCHAIN_EXTERNAL_BIN points outside HOST_DIR => absolute path
  128
+TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += \
  129
+	-DBR_CROSS_PATH_ABS='"$(TOOLCHAIN_EXTERNAL_BIN)"'
  130
+else
  131
+# TOOLCHAIN_EXTERNAL_BIN points inside HOST_DIR => relative path
  132
+TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += \
  133
+	-DBR_CROSS_PATH_REL='"$(TOOLCHAIN_EXTERNAL_BIN:$(HOST_DIR)/%=%)"'
  134
+endif
127 135
 
128 136
 CC_TARGET_TUNE_:=$(call qstrip,$(BR2_GCC_TARGET_TUNE))
129 137
 CC_TARGET_CPU_:=$(call qstrip,$(BR2_GCC_TARGET_CPU))
@@ -456,7 +464,7 @@ $(HOST_DIR)/usr/bin/ext-toolchain-wrapper: $(STAMP_DIR)/ext-toolchain-installed
456 464
 			ln -sf $(@F) $$base; \
457 465
 			;; \
458 466
 		*) \
459  
-			ln -sf $$i .; \
  467
+			ln -sf $$(echo $$i | sed 's%^$(HOST_DIR)%../..%') .; \
460 468
 			;; \
461 469
 		esac; \
462 470
 	done ;
64  toolchain/toolchain-external/ext-toolchain-wrapper.c
@@ -2,9 +2,12 @@
2 2
  * Buildroot wrapper for external toolchains. This simply executes the real
3 3
  * toolchain with a number of arguments (sysroot/arch/..) hardcoded,
4 4
  * to ensure the external toolchain uses the correct configuration.
  5
+ * The hardcoded path arguments are defined relative to the actual location
  6
+ * of the binary.
5 7
  *
6 8
  * (C) 2011 Peter Korsgaard <jacmet@sunsite.dk>
7 9
  * (C) 2011 Daniel Nyström <daniel.nystrom@timeterminal.se>
  10
+ * (C) 2012 Arnout Vandecappelle (Essensium/Mind) <arnout@mind.be>
8 11
  *
9 12
  * This file is licensed under the terms of the GNU General Public License
10 13
  * version 2.  This program is licensed "as is" without any warranty of any
@@ -17,11 +20,12 @@
17 20
 #include <unistd.h>
18 21
 #include <stdlib.h>
19 22
 
20  
-static char path[PATH_MAX] = BR_CROSS_PATH;
  23
+static char path[PATH_MAX];
  24
+static char sysroot[PATH_MAX];
21 25
 
22 26
 static char *predef_args[] = {
23 27
 	path,
24  
-	"--sysroot", BR_SYSROOT,
  28
+	"--sysroot", sysroot,
25 29
 #ifdef BR_ARCH
26 30
 	"-march=" BR_ARCH,
27 31
 #endif /* BR_ARCH */
@@ -48,22 +52,50 @@ static char *predef_args[] = {
48 52
 #endif
49 53
 };
50 54
 
51  
-static const char *get_basename(const char *name)
52  
-{
53  
-	const char *base;
54  
-
55  
-	base = strrchr(name, '/');
56  
-	if (base)
57  
-		base++;
58  
-	else
59  
-		base = name;
60  
-
61  
-	return base;
62  
-}
63  
-
64 55
 int main(int argc, char **argv)
65 56
 {
66 57
 	char **args, **cur;
  58
+	char *relbasedir, *absbasedir;
  59
+	char *progpath = argv[0];
  60
+	char *basename;
  61
+	int ret;
  62
+
  63
+	/* Calculate the relative paths */
  64
+	basename = strrchr(progpath, '/');
  65
+	if (basename) {
  66
+		*basename = '\0';
  67
+		basename++;
  68
+		relbasedir = malloc(strlen(progpath) + 7);
  69
+		if (relbasedir == NULL) {
  70
+			perror(__FILE__ ": malloc");
  71
+			return 2;
  72
+		}
  73
+		sprintf(relbasedir, "%s/../..", argv[0]);
  74
+		absbasedir = realpath(relbasedir, NULL);
  75
+	} else {
  76
+		basename = progpath;
  77
+		absbasedir = realpath("../..", NULL);
  78
+	}
  79
+	if (absbasedir == NULL) {
  80
+		perror(__FILE__ ": realpath");
  81
+		return 2;
  82
+	}
  83
+
  84
+	/* Fill in the relative paths */
  85
+#ifdef BR_CROSS_PATH_REL
  86
+	ret = snprintf(path, sizeof(path), "%s/" BR_CROSS_PATH_REL "/%s", absbasedir, basename);
  87
+#else /* BR_CROSS_PATH_ABS */
  88
+	ret = snprintf(path, sizeof(path), BR_CROSS_PATH_ABS "/%s", basename);
  89
+#endif
  90
+	if (ret >= sizeof(path)) {
  91
+		perror(__FILE__ ": overflow");
  92
+		return 3;
  93
+	}
  94
+	ret = snprintf(sysroot, sizeof(sysroot), "%s/" BR_SYSROOT, absbasedir);
  95
+	if (ret >= sizeof(sysroot)) {
  96
+		perror(__FILE__ ": overflow");
  97
+		return 3;
  98
+	}
67 99
 
68 100
 	cur = args = malloc(sizeof(predef_args) + (sizeof(char *) * argc));
69 101
 	if (args == NULL) {
@@ -82,8 +114,6 @@ int main(int argc, char **argv)
82 114
 	/* finish with NULL termination */
83 115
 	*cur = NULL;
84 116
 
85  
-	strcat(path, get_basename(argv[0]));
86  
-
87 117
 	if (execv(path, args))
88 118
 		perror(path);
89 119
 

0 notes on commit befb9a3

Please sign in to comment.
Something went wrong with that request. Please try again.