/***********************************************************************
 ***********************************************************************
 ** NB: This code is a placeholder, and will require updating if/when **
 **     this architecture/os platform becomes available.              **
 ***********************************************************************
 ***********************************************************************/

/* HP/Intel IA-64 systems running FreeBSD */

#include <lcc-conf.h>

#include <string.h>

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

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

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

#ifndef GCCDIR
#define GCCDIR LOCALDIR "lib/gcc-lib/i386-unknown-freebsd3.0/2.8.1/"
#undef GCCDIR
#define GCCDIR "/usr/lib/"
#endif

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


char *suffixes[] = { ".c", ".i", ".s", ".o", ".out", 0 };
char inputs[256] = "";
char *cpp[] = { LCCDIR "cpp",
	"-U__GNUC__", "-D_POSIX_SOURCE", "-D__STDC__=1", "-D__STRICT_ANSI__",
	"-Dunix", "-Di386", "-Dlinux",
	"-D__unix__", "-D__i386__", "-D__linux__",


	"-D__signed=signed",	/* to fix mistake in <machine/ansi.h> (included by many other header files) */

	/* These gcc-specific symbols confuse lcc: define them differently */
	"-D__NORETURN=",
	"-D__NORETURN2=",
	"-D__CONSTVALUE=",
	"-D__CONSTVALUE2=",

	"$1", "$2", "$3", 0 };
char *include[] = {"-I" LCCDIR "include", "-I/usr/include", "-I/usr/i386-glibc20-linux/include",  "-I" GCCDIR "include", 0 };
char *com[] = {LCCDIR "rcc", "-target=ia64/freebsd", "$1", "$2", "$3", 0 };
char *as[] = { "/usr/bin/as", "-o", "$3", "$1", "$2", 0 };
char *ld[] = {
	/*  0 */ "/usr/bin/ld", "-m", "elf_i386", "-dynamic-linker",
	/*  4 */ "/usr/libexec/ld-elf.so.1", "-o", "$3",
	/*  7 */ LIBDIR "crt1.o", LIBDIR "crti.o", GCCDIR "crtbegin.o", 
                 "$1", "$2",
#if 0
	/* 12 */ "-L/usr/libexec/elf"
		 " -L/usr/libexec"
		 " -L" LCCDIR,
	/* 13 */ "-llcc",
   	/* 14 */ "", "", "-lc", "-lm",
#else
	/* For reasons that are not clear, but appear to be a bug in
	   the FreeBSD 4.1.1 loader, the lcc library must be specified
	   explicitly, rather than be found in the path search.  With
	   the then-branch above, the loader complains
	   "/usr/libexec/elf/ld: cannot find -llcc".  However, one can
	   feed the output of "lcc -v foo.c" into a shell, after
	   supplying suitable replacements for the /tmp/lcc*.o files,
	   and it works just fine! */
	/* 12 */ "-L/usr/libexec/elf"
		 " -L/usr/libexec"
		 " -L/usr/lib"
		 " " LCCDIR "liblcc.a" ,
	/* 13 */ "",
	/* NB: Note the reversed order here: need -lm BEFORE -lc on FreeBSD! */
	/* 14 */ "", "", "-lm", "-lc", 
#endif
	/* 18 */ "", GCCDIR "crtend.o", LIBDIR "crtn.o",
	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[12] =  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 || strcmp(arg, "-pg") == 0) {
		ld[7] = LIBDIR "gcrt1.o";
   		ld[15] = "";
      		ld[16] = "-L/usr/lib";
		ld[17] = "-lc_p";
	} else if (strcmp(arg, "-b") == 0) 
		;
	else if (strcmp(arg, "-g") == 0)
		;
	else if (strcmp(arg, "-dynamic") == 0)
	        ;			/* default */
	else if (strcmp(arg, "-static") == 0) {
	        ld[3] = "-static";
	        ld[4] = "";
	        ld[14] = "-L" GCCDIR;
	        ld[15] = "-lgcc";
	} else
		return 0;
	return 1;
}
