summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/dietlibc/diet.c
blob: 1de6befc8a05599a46143028e12e5ed957798247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

/* goal:
 *   when invoked as
 * "diet gcc -c t.c"
 *   exec
 * "gcc -I/path/to/dietlibc/include -c t.c"
 *
 *   when invoked as
 * "diet sparc-linux-gcc -o t t.o"
 *   exec
 * "sparc-linux-gcc -nostdlib -static -o t t.o /path/to/dietlibc/bin-sparc/start.o /path/to/dietlibc/bin-sparc/dietlibc.a"
*/

void error(const char *message) {
  write(2,message,strlen(message));
  exit(1);
}

int main(int argc,char *argv[]) {
  int link=0;
  int compile=0;
  char diethome[]=DIETHOME;
  char platform[1000]=DIETHOME "/bin-";
  char *nostdlib="-nostdlib";
  char *libgcc="-lgcc";
  char dashL[1000]="-L";
  int i;

  if (argc<2) {
    error("usage: diet [gcc command line]\n"
	  "e.g.   diet gcc -c t.c\n"
	  "or     diet sparc-linux-gcc -o foo foo.c bar.o\n");
  }
  {
    char *tmp=strchr(argv[1],0)-2;
    char *tmp2;
    char *cc=argv[1];
    if (tmp<cc) goto donttouch;
    if ((tmp2=strchr(cc,'-'))) {	/* cross compiling? */
      int len=strlen(platform);
      if (tmp2-cc>90) error("platform name too long!\n");
      memmove(platform+len,argv[1],tmp2-cc);
      platform[tmp2-cc+len]=0;
/*      printf("found platform %s\n",platform); */
    } else {
#ifdef __sparc__
      strcat(platform,"sparc");
#endif
#ifdef __ppc__
      strcat(platform,"ppc");
#endif
#ifdef __i386__
      strcat(platform,"i386");
#endif
#ifdef __alpha__
      strcat(platform,"alpha");
#endif
#ifdef __arm__
      strcat(platform,"arm");
#endif
#ifdef __mips__
      strcat(platform,"mips");
#endif
    }
    strcat(dashL,platform);
    if (!strcmp(tmp,"cc")) {
      char **newargv;
      char **dest;
      char *a,*b,*c;
/* we need to add -I... if the command line contains -c, -S or -E */
      for (i=2; i<argc; ++i)
	if (!strcmp(argv[i],"-c") || !strcmp(argv[i],"-S") || !strcmp(argv[i],"-E"))
	  compile=1;
/* we need to add -nostdlib if we are not compiling*/
      link=!compile;
#if 0
      for (i=2; i<argc; ++i)
	if (!strcmp(argv[i],"-o"))
	  if (!compile) link=1;
#endif
      newargv=alloca(sizeof(char*)*(argc+6));
      a=alloca(strlen(diethome)+20);
      b=alloca(strlen(platform)+20);
      c=alloca(strlen(platform)+20);

      strcpy(a,"-I"); strcat(a,diethome); strcat(a,"/include");
      strcpy(b,platform); strcat(b,"/start.o");
      strcpy(c,platform); strcat(c,"/dietlibc.a");

      dest=newargv;
      *dest++=argv[1];
      if (link) { *dest++=nostdlib; *dest++=dashL; }
      if (compile || link) *dest++=a;
      for (i=2; i<argc; ++i)
	*dest++=argv[i];
      if (link) { *dest++=b; *dest++=c; *dest++=libgcc; }
      *dest=0;
      execvp(newargv[0],newargv);
      goto error;
    } else if (!strcmp(tmp,"ld")) {
    }
  }
donttouch:
  execvp(argv[1],argv+1);
error:
  error("execvp failed!\n");
  return 1;
}