
Date: Mon, 16 Apr 2012 17:42:05 +0900
Subject: [PATCH 1/4] configure: retake the way of host compiler detection

When make crash with target=[TARGET] option at different host environs,
configure will detect TARGET from the new target and host tables which
declare according by current build cases.

This way can provide easier way for new relationship supports between
host and target just by adding corresponding table entry.

Signed-off-by: Toshikazu Nakayama <nakayama.ts@ncos.nec.co.jp>
---
 configure.c |  406 ++++++++++++++++++++++++-----------------------------------
 1 files changed, 163 insertions(+), 243 deletions(-)

diff --git a/configure.c b/configure.c
index 41ecbcb..63d3244 100755
--- a/configure.c
+++ b/configure.c
@@ -112,35 +112,84 @@ char *get_extra_flags(char *);
 #define X86_64  8
 #define ARM	9
 
-#define TARGET_X86    "TARGET=X86"
-#define TARGET_ALPHA  "TARGET=ALPHA"
-#define TARGET_PPC    "TARGET=PPC"
-#define TARGET_IA64   "TARGET=IA64"
-#define TARGET_S390   "TARGET=S390"
-#define TARGET_S390X  "TARGET=S390X"
-#define TARGET_PPC64  "TARGET=PPC64"
-#define TARGET_X86_64 "TARGET=X86_64"
-#define TARGET_ARM    "TARGET=ARM"
-
-#define TARGET_CFLAGS_X86    "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_ALPHA  "TARGET_CFLAGS="
-#define TARGET_CFLAGS_PPC    "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_IA64   "TARGET_CFLAGS="
-#define TARGET_CFLAGS_S390   "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_S390X  "TARGET_CFLAGS="
-#define TARGET_CFLAGS_PPC64  "TARGET_CFLAGS=-m64"
-#define TARGET_CFLAGS_X86_64 "TARGET_CFLAGS="
-#define TARGET_CFLAGS_ARM            "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_ARM_ON_X86     "TARGET_CFLAGS=-D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_ARM_ON_X86_64  "TARGET_CFLAGS=-m32 -D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_X86_ON_X86_64  "TARGET_CFLAGS=-m32 -D_FILE_OFFSET_BITS=64"
-#define TARGET_CFLAGS_PPC_ON_PPC64   "TARGET_CFLAGS=-m32 -D_FILE_OFFSET_BITS=64 -fPIC"
-
-#define GDB_TARGET_DEFAULT        "GDB_CONF_FLAGS="
-#define GDB_TARGET_ARM_ON_X86     "GDB_CONF_FLAGS=--target=arm-elf-linux"
-#define GDB_TARGET_ARM_ON_X86_64  "GDB_CONF_FLAGS=--target=arm-elf-linux CFLAGS=-m32"
-#define GDB_TARGET_X86_ON_X86_64  "GDB_CONF_FLAGS=--target=i686-pc-linux-gnu CFLAGS=-m32"
-#define GDB_TARGET_PPC_ON_PPC64   "GDB_CONF_FLAGS=--target=ppc-elf-linux CFLAGS=-m32"
+static struct host_table {
+	int host;
+	const char *cflags;		/* TARGET_CFLAGS */
+	const char *gdb_conf_flags;	/* GDB_CONF_FLAGS */
+} arm[] = {
+	{
+		.host = X86,
+		.cflags = "-D_FILE_OFFSET_BITS=64",
+		.gdb_conf_flags = "--target=arm-elf-linux",
+	},
+	{
+		.host = X86_64,
+		.cflags = "-m32 -D_FILE_OFFSET_BITS=64",
+		.gdb_conf_flags = "--target=arm-elf-linux CFLAGS=-m32",
+	},
+}, x86[] = {
+	{
+		.host = X86_64,
+		.cflags = "-m32 -D_FILE_OFFSET_BITS=64",
+		.gdb_conf_flags = "--target=i686-pc-linux-gnu CFLAGS=-m32",
+	},
+}, ppc[] = {
+	{
+		.host = PPC64,
+		.cflags = "-m32 -D_FILE_OFFSET_BITS=64 -fPIC",
+		.gdb_conf_flags = "--target=ppc-elf-linux CFLAGS=-m32",
+	}
+};
+
+static struct target_table {
+	char *target;			/* TARGET alias */
+	const char *cflags;		/* TARGET_CFLAGS default */
+	const char *gdb_conf_flags;	/* GDB_CONF_FLAGS default */
+	struct host_table *hosts, *host;
+	int num_hosts;
+} target_arch[] = {
+	{
+		.target = "UNKNOWN",
+	},
+	{
+		.target = "X86",
+		.cflags = "-D_FILE_OFFSET_BITS=64",
+		.hosts = x86,
+		.num_hosts = (sizeof(x86)/sizeof(struct host_table)),
+	},
+	{
+		.target = "ALPHA",
+	},
+	{
+		.target = "PPC",
+		.cflags = "-D_FILE_OFFSET_BITS=64",
+		.hosts = ppc,
+		.num_hosts = (sizeof(ppc)/sizeof(struct host_table)),
+	},
+	{
+		.target = "IA64",
+	},
+	{
+		.target = "S390",
+		.cflags = "-D_FILE_OFFSET_BITS=64",
+	},
+	{
+		.target = "S39X",
+	},
+	{
+		.target = "PPC64",
+		.cflags = "-m64",
+	},
+	{
+		.target = "X86_64",
+	},
+	{
+		.target = "ARM",
+		.cflags = "-D_FILE_OFFSET_BITS=64",
+		.hosts = arm,
+		.num_hosts = (sizeof(arm)/sizeof(struct host_table)),
+	},
+};
 
 /*
  *  The original plan was to allow the use of a particular version
@@ -313,6 +362,7 @@ get_current_configuration(struct supported_gdb_version *sp)
 	FILE *fp;
 	static char buf[512];
 	char *p;
+	int i, host, target;
 
 #ifdef __alpha__
         target_data.target = ALPHA;
@@ -349,39 +399,40 @@ get_current_configuration(struct supported_gdb_version *sp)
 	 */
 	target_data.host = target_data.target;
 
+	/*
+	 * Check the requirements for host and target.
+	 */
+	host = target_data.host;
 	if (target_data.target_as_param) {
-		if ((target_data.target == X86 || target_data.target == X86_64) &&
-		    (name_to_target((char *)target_data.target_as_param) == ARM)) {
-			/* 
-			 *  Debugging of ARM core files supported on X86, and on
-			 *  X86_64 when built as a 32-bit executable.
-			 */
-			target_data.target = ARM;
-		} else if ((target_data.target == X86_64) &&
-			(name_to_target((char *)target_data.target_as_param) == X86)) {
+		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)
+					break;
+			if (i == target_arch[target].num_hosts) {
+				fprintf(stderr,
+					"\ntarget=%s is not supported on the "
+					"%s host architecture\n\n",
+					target_data.target_as_param,
+					target_to_name(host));
+				exit(1);
+			}
 			/*
-			 *  Build an X86 crash binary on an X86_64 host.
+			 * target can build at this host.
 			 */
-			target_data.target = X86;
-		} else if ((target_data.target == PPC64) &&
-			(name_to_target((char *)target_data.target_as_param) == PPC)) {
+			target_data.target = target;
+			target_arch[target].host =
+						&target_arch[target].hosts[i];
+		} else { /* target == host */
+			if (target_data.initial_gdb_target != UNKNOWN &&
+			    host != target_data.initial_gdb_target)
+				arch_mismatch(sp);
 			/*
-			 *  Build an PPC crash binary on an PPC64 host.
+			 * target can build.
 			 */
-			target_data.target = PPC;
-		} else if (name_to_target((char *)target_data.target_as_param) ==
-			target_data.host) {
-			if ((target_data.initial_gdb_target != UNKNOWN) &&
-			    (target_data.host != target_data.initial_gdb_target))
-				arch_mismatch(sp);
-		} else {
-			fprintf(stderr,
-			    "\ntarget=%s is not supported on the %s host architecture\n\n",
-				target_data.target_as_param,
-				target_to_name(target_data.host));
-			exit(1);
+			target_data.target = target;
 		}
-        }
+	}
 
 	/*
 	 *  Impose implied (sticky) target if an initial build has been
@@ -389,39 +440,21 @@ get_current_configuration(struct supported_gdb_version *sp)
 	 */
 	if (target_data.initial_gdb_target && 
 	    (target_data.target != target_data.initial_gdb_target)) {
-		if ((target_data.initial_gdb_target == ARM) &&
-		    (target_data.target != ARM)) {
-			if ((target_data.target == X86) || 
-			    (target_data.target == X86_64))
-				target_data.target = ARM;
-			else
-				arch_mismatch(sp);
-		}
-		if ((target_data.target == ARM) &&
-		    (target_data.initial_gdb_target != ARM))
-			arch_mismatch(sp);
-
-		if ((target_data.initial_gdb_target == X86) &&
-		    (target_data.target != X86)) {
-			if (target_data.target == X86_64) 
-				target_data.target = X86;
-			else
-				arch_mismatch(sp);
-		}
-		if ((target_data.target == X86) &&
-		    (target_data.initial_gdb_target != X86))
-			arch_mismatch(sp);
-
-		if ((target_data.initial_gdb_target == PPC) &&
-		    (target_data.target != PPC)) {
-			if (target_data.target == PPC64) 
-				target_data.target = PPC;
-			else
-				arch_mismatch(sp);
-		}
-		if ((target_data.target == PPC) &&
-		    (target_data.initial_gdb_target != PPC))
+		/*
+		 * If make split target= option, check previous setup and
+		 * rebuild with previous target.
+		 */
+		target = target_data.initial_gdb_target;
+		for (i = 0; i < target_arch[target].num_hosts; i++)
+			if (target_arch[target].hosts[i].host == host)
+				break;
+		if (i == target_arch[target].num_hosts)
 			arch_mismatch(sp);
+		/*
+		 * initial target can build at this host.
+		 */
+		target_data.target = target;
+		target_arch[target].host = &target_arch[target].hosts[i];
 	}
 
         if ((fp = fopen("Makefile", "r")) == NULL) {
@@ -497,36 +530,9 @@ show_configuration(void)
 	if (target_data.flags & QUIET)
 		return;
 
-	switch (target_data.target)
-	{
-	case X86:    
-		printf("TARGET: X86\n");
-		break;
-	case ALPHA: 
-		printf("TARGET: ALPHA\n");
-		break;
-	case PPC:    
-		printf("TARGET: PPC\n");
-		break;
-	case IA64:   
-		printf("TARGET: IA64\n");
-		break;
-	case S390:
-		printf("TARGET: S390\n");
-		break;
-	case S390X:
-		printf("TARGET: S390X\n");
-		break;
-	case PPC64:
-		printf("TARGET: PPC64\n");
-		break;
-	case X86_64:
-		printf("TARGET: X86_64\n");
-		break;
-	case ARM:
-		printf("TARGET: ARM\n");
-		break;
-	}
+	if (target_data.target <
+	    sizeof(target_arch) / sizeof(struct target_table))
+		printf("TARGET: %s\n", target_arch[target_data.target].target);
 
 	if (strlen(target_data.program)) {
 		for (i = 0; i < (strlen("TARGET")-strlen(target_data.program)); 
@@ -548,70 +554,31 @@ build_configure(struct supported_gdb_version *sp)
 {
 	FILE *fp1, *fp2;
 	char buf[512];
-	char *target;
-	char *target_CFLAGS;
-	char *gdb_conf_flags;
+	char target[128];
+	char target_CFLAGS[128];
+	char gdb_conf_flags[128];
 	char *ldflags;
 	char *cflags;
 
 	get_current_configuration(sp);
 
-	target = target_CFLAGS = NULL;
-
-	gdb_conf_flags = GDB_TARGET_DEFAULT;
-	switch (target_data.target)
-	{
-	case X86:
-		target = TARGET_X86;
-		if (target_data.host == X86_64) {
-                        target_CFLAGS = TARGET_CFLAGS_X86_ON_X86_64;
-			gdb_conf_flags = GDB_TARGET_X86_ON_X86_64;
-		} else
-			target_CFLAGS = TARGET_CFLAGS_X86;
-		break;
-	case ALPHA:
-		target = TARGET_ALPHA;
-		target_CFLAGS = TARGET_CFLAGS_ALPHA;
-		break;
-	case PPC:
-		target = TARGET_PPC;
-		if (target_data.host == PPC64) {
-                        target_CFLAGS = TARGET_CFLAGS_PPC_ON_PPC64;
-			gdb_conf_flags = GDB_TARGET_PPC_ON_PPC64;
-		} else
-			target_CFLAGS = TARGET_CFLAGS_PPC;
-		break;
-	case IA64:
-		target = TARGET_IA64;
-                target_CFLAGS = TARGET_CFLAGS_IA64;
-		break;
-	case S390:
-		target = TARGET_S390;
-		target_CFLAGS = TARGET_CFLAGS_S390;
-		break;
-	case S390X:
-		target = TARGET_S390X;
-		target_CFLAGS = TARGET_CFLAGS_S390X;
-		break;
-	case PPC64:
-                target = TARGET_PPC64;
-                target_CFLAGS = TARGET_CFLAGS_PPC64;
-                break;
-	case X86_64:
-                target = TARGET_X86_64;
-                target_CFLAGS = TARGET_CFLAGS_X86_64;
-                break;
-	case ARM:
-                target = TARGET_ARM;
-                if (target_data.host == X86) {
-                        target_CFLAGS = TARGET_CFLAGS_ARM_ON_X86;
-			gdb_conf_flags = GDB_TARGET_ARM_ON_X86;
-                } else if (target_data.host == X86_64) {
-                        target_CFLAGS = TARGET_CFLAGS_ARM_ON_X86_64;
-			gdb_conf_flags = GDB_TARGET_ARM_ON_X86_64;
-		} else
-                        target_CFLAGS = TARGET_CFLAGS_ARM;
-                break;
+	sprintf(target, "TARGET=%s", target_arch[target_data.target].target);
+	if (target_arch[target_data.target].host != NULL) {
+		sprintf(target_CFLAGS, "TARGET_CFLAGS=%s",
+			(target_arch[target_data.target].host->cflags) ?
+			target_arch[target_data.target].host->cflags :
+			"");
+		sprintf(gdb_conf_flags, "GDB_CONF_FLAGS=%s",
+			(target_arch[target_data.target].host->gdb_conf_flags)
+			? target_arch[target_data.target].host->gdb_conf_flags
+			: "");
+	} else {
+		sprintf(target_CFLAGS, "TARGET_CFLAGS=%s",
+			(target_arch[target_data.target].cflags) ?
+			target_arch[target_data.target].cflags : "");
+		sprintf(gdb_conf_flags, "GDB_CONF_FLAGS=%s",
+			(target_arch[target_data.target].gdb_conf_flags) ?
+			target_arch[target_data.target].gdb_conf_flags : "");
 	}
 
 	ldflags = get_extra_flags("LDFLAGS.extra");
@@ -1377,6 +1344,7 @@ set_initial_target(struct supported_gdb_version *sp)
 	FILE *fp;
 	char crash_target[512];
 	char buf[512];
+	int i;
 
 	target_data.initial_gdb_target = UNKNOWN;
 
@@ -1408,41 +1376,19 @@ set_initial_target(struct supported_gdb_version *sp)
 
 	fclose(fp);
 
-	if (strncmp(buf, "X86_64", strlen("X86_64")) == 0) 
-		target_data.initial_gdb_target = X86_64;
-	else if (strncmp(buf, "X86", strlen("X86")) == 0) 
-		target_data.initial_gdb_target = X86;
-	else if (strncmp(buf, "ALPHA", strlen("ALPHA")) == 0)
-		target_data.initial_gdb_target = ALPHA;
-	else if (strncmp(buf, "PPC64", strlen("PPC64")) == 0)
-		target_data.initial_gdb_target = PPC64;
-	else if (strncmp(buf, "PPC", strlen("PPC")) == 0)
-		target_data.initial_gdb_target = PPC;
-	else if (strncmp(buf, "IA64", strlen("IA64")) == 0)
-		target_data.initial_gdb_target = IA64;
-	else if (strncmp(buf, "S390X", strlen("S390X")) == 0)
-		target_data.initial_gdb_target = S390X;
-	else if (strncmp(buf, "S390", strlen("S390")) == 0)
-		target_data.initial_gdb_target = S390;
-	else if (strncmp(buf, "ARM", strlen("ARM")) == 0)
-		target_data.initial_gdb_target = ARM;
+	for (i = 0; i < sizeof(target_arch) / sizeof(struct target_table); i++)
+		if (strncmp(buf, target_arch[i].target,
+			    strlen(target_arch[i].target)) == 0) {
+			target_data.initial_gdb_target = i;
+			break;
+		}
 }
 
 char *
 target_to_name(int target)
 {
-	switch (target)
-	{
-	case X86:    return("X86");
-	case ALPHA:  return("ALPHA");
-	case PPC:    return("PPC");
-	case IA64:   return("IA64");
-	case S390:   return("S390");
-	case S390X:  return("S390X");
-	case PPC64:  return("PPC64");
-	case X86_64: return("X86_64");
-	case ARM:    return("ARM"); 
-	}
+	if (target < sizeof(target_arch) / sizeof(struct target_table))
+		return target_arch[target].target;
 
 	return "UNKNOWN";
 }
@@ -1450,42 +1396,16 @@ target_to_name(int target)
 int
 name_to_target(char *name)
 {
-	if (strncmp(name, "X86_64", strlen("X86_64")) == 0)
-                return X86_64;
-	else if (strncmp(name, "x86_64", strlen("x86_64")) == 0)
-                return X86_64;
-        else if (strncmp(name, "X86", strlen("X86")) == 0)
-                return X86;
-        else if (strncmp(name, "x86", strlen("x86")) == 0)
-                return X86;
-        else if (strncmp(name, "ALPHA", strlen("ALPHA")) == 0)
-                return ALPHA;
-        else if (strncmp(name, "alpha", strlen("alpha")) == 0)
-                return ALPHA;
-        else if (strncmp(name, "PPC64", strlen("PPC64")) == 0)
-                return PPC64;
-        else if (strncmp(name, "ppc64", strlen("ppc64")) == 0)
-                return PPC64;
-        else if (strncmp(name, "PPC", strlen("PPC")) == 0)
-                return PPC;
-        else if (strncmp(name, "ppc", strlen("ppc")) == 0)
-                return PPC;
-        else if (strncmp(name, "IA64", strlen("IA64")) == 0)
-                return IA64;
-        else if (strncmp(name, "ia64", strlen("ia64")) == 0)
-                return IA64;
-        else if (strncmp(name, "S390X", strlen("S390X")) == 0)
-                return S390X;
-        else if (strncmp(name, "s390x", strlen("s390x")) == 0)
-                return S390X;
-        else if (strncmp(name, "S390", strlen("S390")) == 0)
-                return S390;
-        else if (strncmp(name, "s390", strlen("s390")) == 0)
-                return S390;
-        else if (strncmp(name, "ARM", strlen("ARM")) == 0)
-                return ARM;
-        else if (strncmp(name, "arm", strlen("arm")) == 0)
-                return ARM;
+	int i;
+	char buf[128];
+
+	for (i = 0; i < sizeof(target_arch) / sizeof(struct target_table); i++)
+		if (target_arch[i].target &&
+		    (strncmp(name, target_arch[i].target,
+			strlen(target_arch[i].target)) == 0) ||
+		     strncmp(name, lower_case(target_arch[i].target, buf),
+			strlen(lower_case(target_arch[i].target, buf)) == 0))
+			return i;
 
 	return UNKNOWN;
 }
-- 
1.7.0.4


