
Date: Mon, 16 Apr 2012 19:10:19 +0900
Subject: [PATCH 3/5] make: add host_cross option

When make crash with cross compiler, must set host_cross option.

A member host_table.is_cross is implying whether host compiler is
for cross build or not according by new option.
Example in ARM case, target is x86 binary and may emulating arm's crash,
not wants cross compiler.
Although this is depending on compiler itself,
have to take care of possible differences in both CFLAGS.
Thus add explicit member.

Signed-off-by: Toshikazu Nakayama <nakayama.ts@ncos.nec.co.jp>
---
 Makefile    |   20 ++++++++++++++------
 configure.c |   21 +++++++++++++++++----
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index e7e468e..6bb1a5f 100644
--- a/Makefile
+++ b/Makefile
@@ -246,11 +246,19 @@ ifneq ($(target),)
 CONF_TARGET_FLAG="-t$(target)"
 endif
 
+# host_cross could be set on command line when invoking cross compiler.
+# Like: make target=ARM host_cross=X86
+# otherwise host will be the same as the build
+ifneq ($(host_cross),)
+CONF_HOST_FLAG="-h$(host_cross)"
+endif
+
 # To build the extensions library by default, uncomment the third command
 # line below.  Otherwise they can be built by entering "make extensions".
 
 all: make_configure
-	@./configure ${CONF_TARGET_FLAG} -p "RPMPKG=${RPMPKG}" -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} \
+		-p "RPMPKG=${RPMPKG}" -b
 	@make --no-print-directory gdb_merge
 #	@make --no-print-directory extensions
 
@@ -294,7 +302,7 @@ make_configure: force
 	@cc ${CONF_FLAGS} -o configure configure.c ${WARNING_ERROR} ${WARNING_OPTIONS}
 
 clean: make_configure
-	@./configure ${CONF_TARGET_FLAG} -q -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} -q -b
 	@make --no-print-directory do_clean
 
 do_clean:
@@ -313,15 +321,15 @@ unconfig: make_configure
 	@./configure -u
 
 warn: make_configure
-	@./configure ${CONF_TARGET_FLAG} -w -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} -w -b
 	@make --no-print-directory gdb_merge
 
 Warn: make_configure
-	@./configure ${CONF_TARGET_FLAG} -W -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} -W -b
 	@make --no-print-directory gdb_merge
 
 nowarn: make_configure
-	@./configure ${CONF_TARGET_FLAG} -n -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} -n -b
 	@make --no-print-directory gdb_merge
 
 main.o: ${GENERIC_HFILES} main.c
@@ -611,7 +619,7 @@ dis:
 	objdump --disassemble --line-numbers ${PROGRAM} > ${PROGRAM}.dis
 
 extensions: make_configure
-	@./configure ${CONF_TARGET_FLAG} -q -b
+	@./configure ${CONF_TARGET_FLAG} ${CONF_HOST_FLAG} -q -b
 	@make --no-print-directory do_extensions
 
 do_extensions:
diff --git a/configure.c b/configure.c
index 63d3244..18b1fb3 100755
--- a/configure.c
+++ b/configure.c
@@ -116,6 +116,7 @@ static struct host_table {
 	int host;
 	const char *cflags;		/* TARGET_CFLAGS */
 	const char *gdb_conf_flags;	/* GDB_CONF_FLAGS */
+	int is_cross;
 } arm[] = {
 	{
 		.host = X86,
@@ -279,6 +280,7 @@ struct target_data {
 	char release[MAXSTRLEN];
 	struct stat statbuf;
 	const char *target_as_param;
+	const char *host_as_param;
 } target_data = { 0 }; 
 
 int
@@ -289,7 +291,7 @@ main(int argc, char **argv)
 
 	sp = setup_gdb_defaults();
 
-	while ((c = getopt(argc, argv, "gsqnWwubdr:p:P:t:")) > 0) {
+	while ((c = getopt(argc, argv, "gsqnWwubdr:p:P:t:h:")) > 0) {
 		switch (c) {
 		case 'q':
 			target_data.flags |= QUIET;
@@ -325,6 +327,9 @@ main(int argc, char **argv)
 		case 't':
 			target_data.target_as_param = optarg;
 			break;
+		case 'h':
+			target_data.host_as_param = optarg;
+			break;
 		}
 	}
 
@@ -362,7 +367,7 @@ get_current_configuration(struct supported_gdb_version *sp)
 	FILE *fp;
 	static char buf[512];
 	char *p;
-	int i, host, target;
+	int i, host, target, is_cross;
 
 #ifdef __alpha__
         target_data.target = ALPHA;
@@ -402,12 +407,20 @@ get_current_configuration(struct supported_gdb_version *sp)
 	/*
 	 * Check the requirements for host and target.
 	 */
-	host = target_data.host;
+	if (target_data.host_as_param) { /* cross compiler */
+		host = name_to_target((char *)target_data.host_as_param);
+		is_cross = TRUE;
+	} else {
+		host = target_data.host;
+		is_cross = FALSE;
+	}
 	if (target_data.target_as_param) {
 		target = name_to_target((char *)target_data.target_as_param);
 		if (target != host) {
 			for (i = 0; i < target_arch[target].num_hosts; i++)
-				if (target_arch[target].hosts[i].host == host)
+				if (target_arch[target].hosts[i].host == host &&
+				    target_arch[target].hosts[i].is_cross ==
+					is_cross)
 					break;
 			if (i == target_arch[target].num_hosts) {
 				fprintf(stderr,
-- 
1.7.0.4


