On Thursday 28 November 2002 08:00 pm, Dimitrie O. Paun wrote:
> On November 28, 2002 01:35 pm, Alexandre Julliard wrote:
> > > -CC=gcc
> > > +CC=wcc
> > >
> > >   Q: Alexandre, would you accept such a utility in the tree?
> >
> > I don't know, I don't think you can write a one-size-fits-all script,
> > different apps will require different options, and then it will get
> > even more confusing IMO if you have two different ways of doing it
> > depending on your options. Maybe we should simply have a script
> > example in the documentation that people can adapt and ship with their
> > app if the makefile really makes it hard to set options.
>
> Sorry, maybe I was unclear. This wrapper will probably be a small C
> program that understands gcc's options. It needs to do the following:
>   -- add the include dirs first
>   -- add -fshort-wchar
>   -- add -D__int8=char -D__int16=short -D__int32=int "-D__int64=long long"
>   -- remove cygwin options not recognized by the Linux gcc
>
> If you think about them, it kindda makes sense, as under a Windowish
> environment these options are _implied_ in the invocation of gcc,
> just like -I /usr/include is implied when you invoke gcc under Linux.
>
> So to be honest, I don't see why it would be confusing, and second,
> why we can not have a one-size-fits-all wrapper.
>
> As I said, this is more a porting tool, to help you with the initial port.
> If the app has a configure script, you would want to instrument it to
> generate the correct flags for the compiler, and use gcc directly. But this
> is time consuming, and you have to worry about so many things at the
> beginning, I find it so much easier to have something working, and tweak
> only one thing at a time.

Some small ide from uClibc.
/*
 * Copyright (C) 2000 Manuel Novoa III
 *
 * This is a crude wrapper to use uClibc with gcc.
 * It was originally written to work around ./configure for ext2fs-utils.
 * It certainly can be improved, but it works for me in the normal cases.
 *
 * April 7, 2001
 *
 * A bug was fixed in building the gcc command line when dynamic linking.
 * The functions dlopen, etc. now work.  At this time, you must make sure
 * the correct libdl.so is included however.  It is safest to, for example,
 * add /lib/libdl.so.1 if using ld-linux.so.1 rather than adding -ldl to the
 * command line.
 *
 * Note: This is only a problem if devel and target archs are the same.  To
 * avoid the problem, you can use a customized dynamic linker.
 *
 *
 * April 18, 2001
 *
 * The wrapper now works with either installed and uninstalled uClibc versions.
 * If you want to use the uninstalled header files and libs, either include
 * the string "build" in the invocation name such as
 *       'ln -s <ARCH>-uclibc-gcc <ARCH>-uclibc-gcc-build'
 * or include it in the environment variable setting of UCLIBC_GCC.
 * Note: This automatically enables the "rpath" behavior described below.
 *
 * The wrapper will now pass the location of the uClibc shared libs used to
 * the linker with the "-rpath" option if the invocation name includes the
 * string "rpath" or if the environment variable UCLIBC_GCC include it (as
 * with "build" above).  This is primarily intended to be used on devel
 * platforms of the same arch as the target.  A good place to use this feature
 * would be in the uClibc/test directory.
 *
 * The wrapper now displays the command line passed to gcc when '-v' is used.
 *
 * May 31, 2001
 *
 * "rpath" and "build" behavior are now decoupled.  You can of course get
 * the old "build" behavior by setting UCLIBC_GCC="rpath-build".  Order
 * isn't important here, as only the substrings are searched for.
 *
 * Added environment variable check for UCLIBC_GCC_DLOPT to let user specify
 * an alternative dynamic linker at runtime without using command line args.
 * Since this wouldn't commonly be used, I made it easy on myself.  You have
 * to match the option you would have passed to the gcc wrapper.  As an
 * example,
 *
 *   export UCLIBC_GCC_DLOPT="-Wl,--dynamic-linker,/lib/ld-alt-linker.so.3"
 *
 * This is really only useful if target arch == devel arch and DEVEL_PREFIX
 * isn't empty.  It involves a recompile, but you can at least test apps
 * on your devel system if combined with the "rpath" behavor if by using
 * LD_LIBRARY_PATH, etc.
 *
 * Also added check for "-Wl,--dynamic-linker" on the command line.  The
 * use default dynamic linker or the envirnment-specified dynamic linker
 * is disabled in that case.
 *
 * Added options --uclibc-use-build-dir and --uclibc-use-rpath so that those
 * behaviors can be invoked from the command line.
 *
 */

/*
 *
 * TODO:
 * Check/modify gcc-specific environment variables?
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "gcc-uClibc.h"

static char *rpath_link[] = {
	"-Wl,-rpath-link,"UCLIBC_DEVEL_PREFIX"/lib",
	"-Wl,-rpath-link,"UCLIBC_BUILD_DIR"/lib"
};

static char *rpath[] = {
	"-Wl,-rpath,"UCLIBC_DEVEL_PREFIX"/lib",
	"-Wl,-rpath,"UCLIBC_BUILD_DIR"/lib"
};

static char *uClibc_inc[] = {
	"-I"UCLIBC_DEVEL_PREFIX"/include/",
	"-I"UCLIBC_BUILD_DIR"/include/"
};

static char *crt0_path[] = {
	UCLIBC_DEVEL_PREFIX"/lib/crt0.o",
	UCLIBC_BUILD_DIR"/lib/crt0.o"
};

static char *lib_path[] = {
	"-L"UCLIBC_DEVEL_PREFIX"/lib",
	"-L"UCLIBC_BUILD_DIR"/lib"
};

static char *usr_lib_path = "-L"UCLIBC_DEVEL_PREFIX"/lib";

static char static_linking[] = "-static";
static char nostdinc[] = "-nostdinc";
static char nostartfiles[] = "-nostartfiles";
static char nodefaultlibs[] = "-nodefaultlibs";
static char nostdlib[] = "-nostdlib";

int main(int argc, char **argv)
{
	int use_build_dir = 0, linking = 1, use_static_linking = 0;
	int use_stdinc = 1, use_start = 1, use_stdlib = 1;
	int source_count = 0, use_rpath = 0, verbose = 0;
	int i, j;
	char ** gcc_argv;
	char *dlstr;
	char *build_dlstr;
	char *ep;

	build_dlstr = "-Wl,--dynamic-linker," BUILD_DYNAMIC_LINKER;
	dlstr = getenv("UCLIBC_GCC_DLOPT");
	if (!dlstr) {
		dlstr = "-Wl,--dynamic-linker," DYNAMIC_LINKER;
	}

	ep = getenv("UCLIBC_GCC");
	if (!ep) {
		ep = "";
	}

	if (strstr(ep,"build") != 0) {
		use_build_dir = 1;
	}

	if (strstr(ep,"rpath") != 0) {
		use_rpath = 1;
	}

	for ( i = 1 ; i < argc ; i++ ) {
		if (argv[i][0] == '-') { /* option */
			switch (argv[i][1]) {
				case 'c':		/* compile or assemble */
				case 'S':		/* generate assembler code */
				case 'E':		/* preprocess only */
				case 'r':		/* partial-link */
				case 'i':		/* partial-link */
				case 'M':       /* map file generation */
					if (argv[i][2] == 0) linking = 0;
					break;
				case 'v':		/* verbose */
					if (argv[i][2] == 0) verbose = 1;
					break;
				case 'n':
					if (strcmp(nostdinc,argv[i]) == 0) {
						use_stdinc = 0;
					} else if (strcmp(nostartfiles,argv[i]) == 0) {
						use_start = 0;
					} else if (strcmp(nodefaultlibs,argv[i]) == 0) {
						use_stdlib = 0;
					} else if (strcmp(nostdlib,argv[i]) == 0) {
						use_start = 0;
						use_stdlib = 0;
					}
					break;
				case 's':
					if (strcmp(static_linking,argv[i]) == 0) {
						use_static_linking = 1;
					}
					break;
			    case 'W':		/* -static could be passed directly to ld */
					if (strncmp("-Wl,",argv[i],4) == 0) {
						if (strstr(argv[i],static_linking) != 0) {
							use_static_linking = 1;
						}
						if (strstr(argv[i],"--dynamic-linker") != 0) {
							dlstr = 0;
						}
					}
					break;
				case '-':
					if (strcmp(static_linking,argv[i]+1) == 0) {
						use_static_linking = 1;
					}
					break;
			}
		} else {				/* assume it is an existing source file */
			++source_count;
		}
	}

#if 1
	gcc_argv = __builtin_alloca(sizeof(char*) * (argc + 20));
#else
	if (!(gcc_argv = malloc(sizeof(char) * (argc + 20)))) {
		return EXIT_FAILURE;
	}
#endif

	i = 0;
	gcc_argv[i++] = GCC_BIN;
	for ( j = 1 ; j < argc ; j++ ) {
		if (strcmp("--uclibc-use-build-dir",argv[j]) == 0) {
			use_build_dir = 1;
		} else if (strcmp("--uclibc-use-rpath",argv[j]) == 0) {
			use_rpath = 1;
		} else {
			gcc_argv[i++] = argv[j];
		}
	}
	if (use_stdinc) {
		gcc_argv[i++] = nostdinc;
		gcc_argv[i++] = uClibc_inc[use_build_dir];
		gcc_argv[i++] = GCC_INCDIR;
	}
	if (linking && source_count) {
		if (!use_static_linking) {
			if (dlstr && use_build_dir) {
				gcc_argv[i++] = build_dlstr;
			} else if (dlstr) {
				gcc_argv[i++] = dlstr;
			}
			if (use_rpath) {
				gcc_argv[i++] = rpath[use_build_dir];
			}
		}
		gcc_argv[i++] = rpath_link[use_build_dir]; /* just to be safe */
		gcc_argv[i++] = lib_path[use_build_dir];
		if (!use_build_dir) {
			gcc_argv[i++] = usr_lib_path;
		}
		if (use_start) {
			gcc_argv[i++] = crt0_path[use_build_dir];
		}
		if (use_stdlib) {
			gcc_argv[i++] = nostdlib;
			gcc_argv[i++] = "-lc";
			gcc_argv[i++] = GCC_LIB;
		}
	}
	gcc_argv[i++] = NULL;

	if (verbose) {
		printf("Invoked as %s\n", argv[0]);
		for ( j = 0 ; gcc_argv[j] ; j++ ) {
			printf("arg[%2i] = %s\n", j, gcc_argv[j]);
		}
	}

	return execvp(GCC_BIN, gcc_argv);
}
/* this file was autogenerated by make */
#define UCLIBC_TARGET_PREFIX  "/"
#define UCLIBC_DEVEL_PREFIX  "/usr/local/uclibc"
#define UCLIBC_BUILD_DIR  "/home/ace/src/lib/uClibc/"
#define GCC_BIN  "/usr/bin/gcc"
#define GCC_LIB  "/usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/libgcc.a"
#define GCC_INCDIR  "-I/usr/lib/gcc-lib/i586-mandrake-linux-gnu/3.2/include/"
#define TARGET_ARCH  "i386"
#define DYNAMIC_LINKER  "/usr/local/uclibc/lib/ld-uClibc.so.0"
#define BUILD_DYNAMIC_LINKER  "/home/ace/src/lib/uClibc/lib/ld-uClibc.so.0"
TOPDIR = ../../
include $(TOPDIR)Rules.mak

UCLIBC_DIR = $(shell (cd ../.. ; /bin/pwd))
GCC_BIN = $(shell which $(CC))
LD_BIN = $(shell which $(LD))
GCC_LIB = $(shell $(CC) -print-libgcc-file-name )
#GCCINCDIR inherited from Rules.mak

all: gcc-uClibc ld-uClibc

gcc-uClibc.h: $(TOPDIR)/Config
	@echo "/* this file was autogenerated by make */" > gcc-uClibc.h
	@echo "#define UCLIBC_TARGET_PREFIX " \"$(TARGET_PREFIX)\" >> gcc-uClibc.h
	@echo "#define UCLIBC_DEVEL_PREFIX " \"$(DEVEL_PREFIX)\" >> gcc-uClibc.h
	@echo "#define UCLIBC_BUILD_DIR " \"$(UCLIBC_DIR)/\" >> gcc-uClibc.h
	@echo "#define GCC_BIN " \"$(GCC_BIN)\" >> gcc-uClibc.h
	@echo "#define GCC_LIB " \"$(GCC_LIB)\" >> gcc-uClibc.h
	@echo "#define GCC_INCDIR " \"-I$(GCCINCDIR)/\" >> gcc-uClibc.h
	@echo "#define TARGET_ARCH " \"$(TARGET_ARCH)\" >> gcc-uClibc.h
	@echo "#define DYNAMIC_LINKER " \"$(DYNAMIC_LINKER)\" >> gcc-uClibc.h
	@echo "#define BUILD_DYNAMIC_LINKER " \"$(UCLIBC_DIR)/lib/$(UCLIBC_LDSO)\" >> gcc-uClibc.h

gcc-uClibc: gcc-uClibc.h gcc-uClibc.c
	gcc -Wall -O2 -s gcc-uClibc.c -o $(TARGET_ARCH)-uclibc-gcc

ld-uClibc:
	@echo "#!/bin/sh" > $(TARGET_ARCH)-uclibc-ld
	@echo "# This file was autogenerated by make" >> $(TARGET_ARCH)-uclibc-ld
	@echo "$(LD_BIN) -L- -L$(DEVEL_PREFIX)/lib -L$(DEVEL_PREFIX)/usr/lib "\
		"-L$(UCLIBC_DIR) \$$@" >> $(TARGET_ARCH)-uclibc-ld
	chmod a+x $(TARGET_ARCH)-uclibc-ld

install: all
	install -d $(PREFIX)$(DEVEL_PREFIX)/bin;
	install -d $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin;
	install -m 755 $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/
	install -m 755 $(TARGET_ARCH)-uclibc-ld $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/
	ln -fs $(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-cc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_PREFIX)/bin/gcc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-gcc $(PREFIX)$(DEVEL_PREFIX)/bin/cc
	ln -fs $(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-ld $(PREFIX)$(DEVEL_PREFIX)/bin/ld
	for app in addr2line ar as cpp gasp nm objcopy \
	    objdump ranlib size strings strip; do \
	  ln -fs `which $(CROSS)$${app}` $(PREFIX)$(DEVEL_PREFIX)/bin/$${app}; \
	  ln -fs `which $(CROSS)$${app}` $(PREFIX)$(SYSTEM_DEVEL_PREFIX)/bin/$(TARGET_ARCH)-uclibc-$${app}; \
	done

clean:
	rm -f gcc-uClibc.h *-uclibc-gcc *-uclibc-ld core


Reply via email to