/* PA-RISC systems running HP-UX */
/* NB: More work is needed to correctly fill in array slots in option() below! */

#include <lcc-conf.h>

#include <string.h>
#include <stdio.h>			/* for fprintf() calls in option() */
#include <unistd.h>			/* for access() prototype and R_OK */

static char rcsid[] = "$Id: hpux.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/hppa1.1-hp-hpux10.20/2.95.2/"
#endif

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

#ifndef LIBDIR
#define LIBDIR "/usr/lib/"
#endif

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

char inputs[256] = "";

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

	"-Dhppa",			/* deprecated, but supplied by older HP-UX compilers */
	"-Dhpux",			/* deprecated, but supplied by older HP-UX compilers */
	"-Dunix",			/* deprecated, but supplied by older HP-UX compilers */

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

	"-DPWB",
	"-D_HIUX_SOURCE",
	"-D_HPUX_SOURCE",
	"-D_PA_RISC1_1",
	"-D__PWB",
	"-D__PWB__",
	"-D__STDC_EXT__",
	"-D__hp9000s700",
	"-D__hp9000s800",
	"-D__hp9000s800",
	"-D__hp9000s800__",
	"-D__hp9k8",
	"-D__hp9k8__",
	"-D__hppa",
	"-D__hppa__",
	"-D__hpux",
	"-D__hpux__",
	"-D__unix",
	"-D__unix__",
	"-Dhp9000s800",
	"-Dhp9k8",

	/* "-Acpu(hppa)", */
	/* "-Amachine(hppa)", */
	/* "-Asystem(hpux)", */
	/* "-Asystem(unix)", */
	/* "-D__GNUC_MINOR__=95", */
	/* "-D__GNUC__=2", */

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

char *include[] = {
    "-I" LCCDIR "include",
    "-I" LOCALDIR "include",
    "-I" GCCDIR "../../../../hppa1.1-hp-hpux10.20/include", /* ARG... O/S release dependent! */
    "-I" GCCDIR "include",
    "-I/usr/include",
    0 };

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

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

/***********************************************************************
   NB: On this system, there are three locations of startup .o files,
   but only one is unique:

	$ ls -l /usr/ccs/lib/crt0.o /lib/crt0.o /usr/lib/crt0.o
	lr--r--r-T    1 root     sys            19 Jun 10  1996 /lib/crt0.o -> /usr/ccs/lib/crt0.o
	-r--r--r--    1 bin      bin          6880 Jun 10  1996 /usr/ccs/lib/crt0.o
	lr--r--r-T    1 root     sys            19 Jun 10  1996 /usr/lib/crt0.o -> /usr/ccs/lib/crt0.o

   Thus, LIBDIR could be /lib, /usr/lib, or /usr/ccs/lib.

   We could also use the native /usr/ccs/bin/ld, but we still need gcc
   preinstalled for two reasons:

   (1) lcc and gcc use the same startup symbol, __main(), resolved
       from -lgcc; cc does not use this symbol.  Thus, -lgcc is always
       needed.

   (2) the HP native cc compiler supports only K&R C, and thus, will not
       compile lcc, so gcc is needed for the bootstrap, unless the
       site has licensed, and paid extra for, the HP c89
       compiler. That compiler was not available on the HP-UX 10.20
       development system.

   Unfortunately, the /usr/lib/gcrt0.o and /usr/lib/mcrt0.o files
   needed for -p and -pg support are symlinks to nonexistent files,
   unless the c89 product has been installed, sigh...  We therefore
   support those flags, but the user would get a loader error for the
   missing file, so we make sure they exist before installing them in
   ld[].
***********************************************************************/
char *ld[] = {
	/* 0 */ GCCDIR "collect2",
		"-L/lib/pa1.1",
		"-L/usr/lib/pa1.1",
		"-L/usr/lib",
	/* 4 */	"-L/opt/langtools/lib",
		"-z",		/* to catch NULL-pointer dereferencing */
		"-u",
		"main",
	/* 8 */	"-a",
		"shared_archive",	/* -static: "archive_shared" */
		LIBDIR "crt0.o", /* with -p: LIBDIR "mcrt0.o"; with -pg: LIBDIR "gcrt0.o" */
		"-o",
	/* 12*/	"$3",
		"",
		"",
		"-L" GCCDIR,
	/* 16*/	"-L/usr/ccs/bin",
		"-L/usr/ccs/lib",
		"-L" LOCALDIR "lib",
		"",
	/* 20*/	"",
		"$1",
		"$2",
		"-L" LCCDIR,
	/* 24*/	"-llcc",
		"-lgcc",		/* need for __main() symbol */
		"",			/* with -pg: -L/lib/libp/ */
		"-lc",
	/* 28*/	"-lm",
		"",
		"",
		"",
	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) {
		if (access((LIBDIR "mcrt0.o"),R_OK) == 0)
			ld[10] = LIBDIR "mcrt0.o";
		else
			(void)fprintf(stderr,
				"WARNING: support for -p profiling is not installed on this system (needs HP's c89 compiler product)\n");
	} else if (strcmp(arg, "-pg") == 0) {
		if (access((LIBDIR "gcrt0.o"),R_OK) == 0)
		{
			ld[10] = LIBDIR "gcrt0.o";
			ld[26] = "-L/lib/libp";
		}
		else
			(void)fprintf(stderr,
				"WARNING: support for -pg profiling is not installed on this system (needs HP's c89 compiler product)\n");
	} else if (strcmp(arg, "-b") == 0)
		;
	else if (strcmp(arg, "-g") == 0)
		;
	else if (strcmp(arg, "-dynamic") == 0)
	        ld[9] = "shared_archive";
	else if (strcmp(arg, "-static") == 0)
	        ld[9] = "archive_shared";
	else
		return 0;
	return 1;
}
