/* IBM PowerPC systems running AIX */

#include <lcc-conf.h>

#include <string.h>

static char rcsid[] = "$Id: aix.c,v 1.1.1.1 2002/04/21 13:25:10 peter Exp $";

#ifndef LOCALDIR
#define LOCALDIR "/usr/local/"
#endif

#ifndef GCCDIR
#define GCCDIR LOCALDIR "lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/"
#endif

#ifndef LCCDIR
#define LCCDIR LOCALDIR "lib/lcc/"
#endif

char *suffixes[] = { ".c", ".i", ".s", ".o", ".out", 0 };

char inputs[256] = "";

/***********************************************************************
  The IBM native compilers, when given a -v (or -#) option, produce an
  abbreviated output form that makes it impossible to tell how to invoke
  their components.  We therefore must assume that gcc has been
  installed, sigh...

  For example:
	% xlc -v sc.c
	exec: /usr/lpp/xlC/exe/xlcentry(xlcentry, -D_AIX, -D_AIX32,
		-D_AIX41, -D_IBMR2, -D_POWER, -qtbtable=full,
		-qansialias, -osc.o, sc.c, /tmp/xlc25qKMa,
		/tmp/xlc25qKMb, /dev/null, sc.lst, sc, /tmp/xlc25qKMc,
		NULL)
	exec: /usr/lpp/xlC/exe/xlCcode(xlCcode, -qtbtable=full,
		-qansialias, /tmp/xlc25qKMa, /tmp/xlc25qKMb, sc.o,
		sc.lst, /tmp/xlc25qKMc, NULL)
	exec: /bin/ld(ld, /lib/crt0.o, -bpT:0x10000000,
		-bpD:0x20000000,  sc.o,  -lc,  NULL)

  Compare this with gcc output:
	/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/cpp -lang-c
		-v -D__GNUC__=2 -D__GNUC_MINOR__=95 -D_IBMR2 -D_POWER
		-D_AIX -D_AIX32 -D_AIX41 -D_LONG_LONG -D_IBMR2 -D_POWER
		-D_AIX -D_AIX32 -D_AIX41 -D_LONG_LONG -Asystem(unix)
		-Asystem(aix) -D__CHAR_UNSIGNED__ -D_ARCH_COM sc.c
		/tmp/ccsi7oqM.i

	GNU CPP version 2.95.2 19991024 (release)
	#include "..." search starts here:

	#include <...> search starts here:
		/usr/local/include
		/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/../../../../powerpc-ibm-aix4.2.1.0/include
		/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/include
		/usr/include
	End of search list.

	/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/cc1
		/tmp/ccsi7oqM.i -quiet -dumpbase sc.c -version -o
		/tmp/ccq6iCyD.s
	GNU C version 2.95.2 19991024 (release) (powerpc-ibm-aix4.2.1.0) compiled by GNU C version 2.95.2 19991024 (release).

	as -u -mcom -o /tmp/ccC3w7i0.o /tmp/ccq6iCyD.s

	/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/collect2
		-bpT:0x10000000 -bpD:0x20000000 -btextro -bnodelcsect
		/lib/crt0.o
		-L/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2
		-L/usr/local/powerpc-ibm-aix4.2.1.0/lib -L/usr/local/lib
		/tmp/ccC3w7i0.o
		/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/libgcc.a
		-lc
		/usr/local/lib/gcc-lib/powerpc-ibm-aix4.2.1.0/2.95.2/libgcc.a

***********************************************************************/

char *cpp[] = {
	LCCDIR "cpp",
	/* lcc settings */
	"-D_POSIX_SOURCE",
	"-D__STDC__=1",
	"-D__STRICT_ANSI__",
	"-U__GNUC__",

	/* Match the following -Dxxx=yyy values to what "gcc -v -E"
	   reports, commenting out those specific only to gcc, or
	   conflicting with lcc! */

	"-D_AIX",
	"-D_AIX32",
	"-D_AIX41",
	"-D_ARCH_COM",
	"-D_IBMR2",
	"-D_POWER",

	/* "-Asystem(aix)", */
	/* "-Asystem(unix)", */
	/* "-D_LONG_LONG", */	/* lcc does not support 64-bit long long type */
	/* "-D__CHAR_UNSIGNED__", */	/* lcc has signed chars by default */
	/* "-D__GNUC_MINOR__=95", */
	/* "-D__GNUC__=2", */

	"$1", "$2", "$3", 0 };

char *include[] = {
	"-I" LCCDIR "include",
	"-I" LOCALDIR "include",
	"-I" GCCDIR "../../../../powerpc-ibm-aix4.2.1.0/include", /* ARG... O/S release dependent! */
	"-I" GCCDIR "include",
	"-I/usr/include",
	0 };

char *com[] = {
	LCCDIR "rcc",
	"-target=ppc/aix",
	"$1",
	"$2",
	"$3",
	0 };

char *as[] = {
	"/usr/bin/as",
	"-o",
	"$3",
	"$1",
	"$2",
	0 };

char *ld[] = {
	/* 0 */ GCCDIR "collect2",
		"-bpT:0x10000000",
		"-bpD:0x20000000",
		"-btextro",
	/* 4 */	"-bnodelcsect",
		"-o",
		"$3",			/* slot for executable file */
		"$1",
	/* 8 */	"",			/* with -p: "-bI:/lib/syscalls.exp" */
		"",			/* with -g: "-bexport:/usr/lib/libg.exp" */
		"/lib/crt0.o",		/* with -p: "/lib/mcrt0.o"; with -pg: "/lib/gcrt0.o" */
		"",			/* with -static: "-bnso" */
	/* 12*/	"",			/* with -static: "-bI:/lib/syscalls.exp" */
		"",		  	/* slot for extra -Ldir option(s) */
		"-L" GCCDIR,
		"-L/usr/local/powerpc-ibm-aix4.2.1.0/lib",
	/* 16*/	"-L/usr/local/lib",
		"$2",			/* slot for user .o file(s) */
		"",			/* slot for user -lxxx */
		GCCDIR "libgcc.a",
	/* 20*/	"",			/* with -p: "-L/lib/profiled" */
		"",			/* with -p: "-L/usr/lib/profiled" */
		"",			/* with -g: -lg */
		"-lc",
	/* 24*/	GCCDIR "libgcc.a",
	0 };

extern char *concat(char *, char *);

int option(char *arg) {
	if (strncmp(arg, "-lccdir=", 8) == 0) {
	    cpp[0] = concat(&arg[8], "/cpp");
		include[0] = concat("-I", concat(&arg[8], "/include"));
		ld[13] =  concat("-L", &arg[8]);
		com[0] = concat(&arg[8], "/rcc");
	} else if (strncmp(arg, "-as=", 4) == 0) {
		as[0] = concat(&arg[4], "");
	} else if (strncmp(arg, "-cpp=", 5) == 0) {
		cpp[0] = concat(&arg[5], "");
	} else if (strncmp(arg, "-ld=", 4) == 0) {
		ld[0] = concat(&arg[4],"");
	} else if (strncmp(arg, "-rcc=", 5) == 0) {
		com[0] = concat(&arg[5],"");
	} else if (strcmp(arg, "-p") == 0) {
		ld[8] = "-bI:/lib/syscalls.exp";
		ld[10] = "/usr/lib/mcrt0.o";
		ld[20] = "-L/lib/profiled";
		ld[21] = "-L/usr/lib/profiled";
	} else if (strcmp(arg, "-p") == 0 || strcmp(arg, "-pg") == 0) {
		ld[8] = "-bI:/lib/syscalls.exp";
		ld[10] = "/lib/gcrt0.o";
		ld[20] = "-L/lib/profiled";
		ld[21] = "-L/usr/lib/profiled";
	} else if (strcmp(arg, "-b") == 0)
		;
	else if (strcmp(arg, "-g") == 0) {
		ld[9] = "-bexport:/usr/lib/libg.exp";
		ld[22] = "-lg";
	}
	else if (strcmp(arg, "-dynamic") == 0)
	        ;			/* default */
	else if (strcmp(arg, "-static") == 0) {
		ld[11] = "-bnso";
		ld[12] = "-bI:/lib/syscalls.exp";
	} else
		return 0;
	return 1;
}
