~ubuntu-branches/ubuntu/karmic/erlang/karmic-security

« back to all changes in this revision

Viewing changes to erts/etc/win32/cygwin_tools/vc/cc_wrap.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <windows.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <errno.h>
 
6
#include <limits.h>
 
7
#include <dirent.h>
 
8
#include <sys/cygwin.h>
 
9
 
 
10
    
 
11
 
 
12
#ifdef CCP_POSIX_TO_WIN_A
 
13
#define NEW_CYGPATH_INTERFACE
 
14
#endif
 
15
 
 
16
#ifdef NEW_CYGPATH_INTERFACE
 
17
#define GET_WIN32_SIZE(Posix) \
 
18
cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE, (Posix), NULL, 0)
 
19
#define CONVERT_TO_WIN32(Posix,Win32,Size) \
 
20
cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE, (Posix), \
 
21
                  (Win32), (Size))
 
22
#else
 
23
#define GET_WIN32_SIZE(Posix) PATH_MAX
 
24
#define CONVERT_TO_WIN32(Posix,Win32,Size) \
 
25
((cygwin32_conv_to_full_win32_path((Posix),(Win32)) >= 0) ? 0 : -1)
 
26
#endif
 
27
 
 
28
/*#define HARDDEBUG 1*/
 
29
 
 
30
#ifdef HARDDEBUG
 
31
#define DEBUGF(X) printf X
 
32
#else
 
33
#define DEBUGF(X) /* noop */
 
34
#endif
 
35
char *tmpobjdir = "";
 
36
 
 
37
char *add_to(char *src,char *add) {
 
38
    int len = strlen(src)+strlen(add)+1;
 
39
    char *n;
 
40
 
 
41
    if (strlen(src) == 0) {
 
42
        n = malloc(len);
 
43
        strcpy(n,add);
 
44
        return n;
 
45
    }
 
46
    n = realloc(src,len);
 
47
    strcat(n,add);
 
48
    return n;
 
49
}
 
50
 
 
51
void maybe_cleanup(void)
 
52
{
 
53
    DIR *dir;
 
54
    struct dirent *dent;
 
55
    if (*tmpobjdir == '\0') {
 
56
        return;
 
57
    }
 
58
    if (!(dir = opendir(tmpobjdir))) {
 
59
        return;
 
60
    }
 
61
    while((dent = readdir(dir)) != NULL) {
 
62
        char *fullname = add_to("",tmpobjdir);
 
63
        fullname = add_to(fullname,"/");
 
64
        fullname = add_to(fullname,dent->d_name);
 
65
        unlink(fullname);
 
66
        free(fullname);
 
67
    }
 
68
    closedir(dir);
 
69
    rmdir(tmpobjdir);
 
70
}
 
71
 
 
72
    
 
73
    
 
74
 
 
75
void error(char *str) 
 
76
{
 
77
    fprintf(stderr,"%s\n",str);
 
78
    maybe_cleanup();
 
79
    exit(1);
 
80
}
 
81
 
 
82
 
 
83
char **add_to_src(char **srcarr, char *add)
 
84
{
 
85
    int num;
 
86
    if (srcarr == NULL) {
 
87
        srcarr = malloc(sizeof(char *)*2);
 
88
        srcarr[0]=malloc(strlen(add)+1);
 
89
        strcpy(srcarr[0],add);
 
90
        srcarr[1] = NULL;
 
91
    } else {
 
92
        for(num = 0; srcarr[num] != NULL; ++num)
 
93
            ;
 
94
        num +=1;
 
95
        srcarr = realloc(srcarr,sizeof(char *)*(num+1));
 
96
        srcarr[num-1] = malloc(strlen(add)+1);
 
97
        strcpy(srcarr[num-1],add);
 
98
        srcarr[num] = NULL;
 
99
    }
 
100
    return srcarr;
 
101
}
 
102
 
 
103
 
 
104
 
 
105
char *object_name(char *source) {
 
106
    char *tmp = add_to("",source);
 
107
    int j = strlen(tmp)-2;
 
108
    if (j < 0) {
 
109
        j = 0;
 
110
    }
 
111
    while(j>0 && tmp[j] != '.') {
 
112
        --j;
 
113
    }
 
114
    if (tmp[j] == '.') {
 
115
        ++j;
 
116
    }
 
117
    tmp[j++] = 'o';
 
118
    tmp[j] = '\0';
 
119
    return tmp;
 
120
}
 
121
 
 
122
char *exe_name(char *source) {
 
123
    char *tmp = add_to("",source);
 
124
    int j = strlen(tmp)-2;
 
125
    if (j < 0) {
 
126
        j = 0;
 
127
    }
 
128
    while(j>0 && tmp[j] != '.') {
 
129
        --j;
 
130
    }
 
131
    if (tmp[j] == '.') {
 
132
        ++j;
 
133
    }
 
134
    tmp[j] = '\0';
 
135
    return add_to(tmp,"exe");
 
136
}
 
137
 
 
138
char *dyn_get_short(char *longp) 
 
139
{
 
140
    int size;
 
141
    char *shortp;
 
142
    size = GetShortPathName(longp,NULL,0);
 
143
    if (size <= 0) {
 
144
        return NULL;
 
145
    }
 
146
    shortp = malloc(size);
 
147
    if (GetShortPathName(longp,shortp,size) != size - 1) {
 
148
        free(shortp);
 
149
        return NULL;
 
150
    }
 
151
    return shortp;
 
152
}
 
153
 
 
154
char *do_cyp(char *posix)
 
155
{
 
156
    ssize_t size;
 
157
    char *win32;
 
158
    size = GET_WIN32_SIZE(posix);
 
159
    char *ret = NULL;
 
160
    if (size < 0) {
 
161
        fprintf(stderr,"Could not cygpath %s, errno = %d\n",
 
162
                posix,errno);
 
163
    } else {
 
164
        win32 = (char *) malloc (size);
 
165
        if (CONVERT_TO_WIN32(posix,
 
166
                             win32, size)) {
 
167
            fprintf(stderr,"Could not cygpath %s, errno = %d\n",
 
168
                    posix,errno);
 
169
        } else {
 
170
            char *w32_short = dyn_get_short(win32);
 
171
            DEBUGF(("win32 = %s, w32_short = %s\n",win32, (w32_short == NULL) ? "NULL" : w32_short));
 
172
            if (w32_short == NULL) {
 
173
                char *rest = malloc(size);
 
174
                char *first = malloc(size);
 
175
                int x = 0;
 
176
                int y = strlen(win32) - 1;
 
177
                strcpy(first,win32);
 
178
                while (w32_short == NULL) {
 
179
                    while ( y > 0 && first[y] != '\\') {
 
180
                        rest[x++] = first[y--];
 
181
                    }
 
182
                    if (y > 0) {
 
183
                        rest[x++] = first[y];
 
184
                        first[y--] = '\0'; 
 
185
                    } else {
 
186
                        break;
 
187
                    }
 
188
                    w32_short = dyn_get_short(first);
 
189
                    DEBUGF(("first = %s, w32_short = %s\n",first, (w32_short == NULL) ? "NULL" : w32_short));
 
190
                }
 
191
                if (w32_short != NULL) {
 
192
                    y = strlen(w32_short);
 
193
                    w32_short = realloc(w32_short,y+1+x);
 
194
                    /* spool back */
 
195
                    while ( x > 0) {
 
196
                        w32_short[y++] = rest[--x];
 
197
                    }
 
198
                    w32_short[y] = '\0';
 
199
                } else {
 
200
                    w32_short = malloc(strlen(win32)+1);
 
201
                    strcpy(w32_short,win32); /* last resort */
 
202
                }
 
203
                free(first);
 
204
                free(rest);
 
205
            }   
 
206
            ret = w32_short;
 
207
            while (*ret) {
 
208
                if (*ret == '\\') {
 
209
                    *ret = '/';
 
210
                }
 
211
                ++ret;
 
212
            }
 
213
            ret = w32_short;
 
214
        }    
 
215
        free(win32);
 
216
    }
 
217
    return ret;
 
218
}
 
219
 
 
220
 
 
221
 
 
222
char *save = "";
 
223
 
 
224
void save_args(int argc, char **argv) 
 
225
{
 
226
    int i;
 
227
    for(i = 0; i < argc; ++i) {
 
228
        save = add_to(save,argv[i]);
 
229
        save = add_to(save," ");
 
230
    }
 
231
}
 
232
 
 
233
char *progname="cc_wrap";
 
234
 
 
235
int my_create_pipe(HANDLE *read_p, HANDLE *write_p)
 
236
{
 
237
    char name_buff[1000];
 
238
    SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
 
239
    static int counter = 0;
 
240
 
 
241
    ++counter;
 
242
 
 
243
    sprintf(name_buff,"\\\\.\\pipe\\%s_%d_%d",progname,getpid(),counter);
 
244
    sa.bInheritHandle = FALSE;
 
245
    if ((*read_p = CreateNamedPipe(name_buff,
 
246
                                   PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
 
247
                                   PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
 
248
                                   1,
 
249
                                   0,
 
250
                                   0,
 
251
                                   2000,
 
252
                                   &sa)) == INVALID_HANDLE_VALUE || 
 
253
        *read_p == NULL) {
 
254
        return 0;
 
255
    }
 
256
    sa.bInheritHandle = TRUE;
 
257
    if ((*write_p = CreateFile(name_buff,
 
258
                               GENERIC_WRITE,
 
259
                               0, /* No sharing */
 
260
                               &sa,
 
261
                               OPEN_EXISTING,
 
262
                               FILE_ATTRIBUTE_NORMAL,
 
263
                               NULL)) == INVALID_HANDLE_VALUE || 
 
264
        *write_p == NULL) {
 
265
        CloseHandle(*read_p);
 
266
        return 0;
 
267
    }
 
268
    return 1;
 
269
}
 
270
 
 
271
void forwardenv(void)
 
272
{
 
273
  char *(envs[]) = {"LIB","INCLUDE","LIBPATH", "LD_SH_DEBUG_LOG", NULL};
 
274
  char **p = envs;
 
275
  while (*p != NULL) {
 
276
    char *val = getenv(*p);
 
277
    if (val != NULL) {
 
278
      SetEnvironmentVariable(*p,val);
 
279
    }
 
280
    ++p;
 
281
  }
 
282
}
 
283
 
 
284
HANDLE do_run(char *commandline, HANDLE *out, HANDLE *err) 
 
285
{
 
286
    STARTUPINFO start;
 
287
    HANDLE write_pipe_stdout = NULL, read_pipe_stdout = NULL;
 
288
    HANDLE write_pipe_stderr = NULL, read_pipe_stderr = NULL;
 
289
    SECURITY_ATTRIBUTES pipe_security;
 
290
    SECURITY_ATTRIBUTES attr;
 
291
    PROCESS_INFORMATION info;
 
292
 
 
293
    memset(&start,0,sizeof(start));
 
294
    memset(&pipe_security,0,sizeof(pipe_security));
 
295
    memset(&attr,0,sizeof(attr));
 
296
    memset(&info,0,sizeof(info));
 
297
 
 
298
    pipe_security.nLength = sizeof(pipe_security);
 
299
    pipe_security.lpSecurityDescriptor = NULL;
 
300
    pipe_security.bInheritHandle = TRUE;
 
301
 
 
302
    if(!my_create_pipe(&read_pipe_stdout,&write_pipe_stdout)){
 
303
        error("Could not create stdout pipes!");
 
304
    }
 
305
    if(!my_create_pipe(&read_pipe_stderr,&write_pipe_stderr)){
 
306
        error("Could not create stderr pipes!");
 
307
    }
 
308
    start.cb = sizeof (start);
 
309
    start.dwFlags = STARTF_USESHOWWINDOW;
 
310
    start.wShowWindow = SW_HIDE;
 
311
    start.hStdOutput = write_pipe_stdout;
 
312
    start.hStdError = write_pipe_stderr;
 
313
    start.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
 
314
    start.dwFlags |= STARTF_USESTDHANDLES;
 
315
 
 
316
    attr.nLength = sizeof(attr);
 
317
    attr.lpSecurityDescriptor = NULL;
 
318
    attr.bInheritHandle = TRUE;
 
319
    forwardenv(); /* Cygwin and windows environment variables... sigh... */
 
320
    if(!CreateProcess(NULL,
 
321
                      commandline,
 
322
                      &attr,
 
323
                      NULL,
 
324
                      TRUE,
 
325
                      CREATE_DEFAULT_ERROR_MODE,
 
326
                      NULL,
 
327
                      NULL,
 
328
                      &start,
 
329
                      &info)){
 
330
        fprintf(stderr,"Could not run %s, last error: %d\n",commandline,GetLastError());
 
331
        error("Could not create process");
 
332
    }
 
333
    *out = read_pipe_stdout;
 
334
    *err = read_pipe_stderr;
 
335
    CloseHandle(write_pipe_stdout);
 
336
    CloseHandle(write_pipe_stderr);
 
337
    return info.hProcess;
 
338
}
 
339
#define HANDLE_STDOUT 0
 
340
#define HANDLE_STDERR 1
 
341
#define HANDLE_PROC 2
 
342
 
 
343
#ifdef HARDDEBUG
 
344
char *prefix = "";
 
345
#endif
 
346
 
 
347
int handle_overlapped(HANDLE fd, OVERLAPPED *ovp, char *buffer, 
 
348
                      int bufflen, int get_old, FILE *whereto, int *skip)
 
349
{
 
350
    DWORD res,read,err;
 
351
    char *ptr;
 
352
 
 
353
    DEBUGF(("In handle_overlapped(%d,0x%08x,0x%08x,%d,%d), prefix = %s\n",
 
354
            fd,ovp,buffer,bufflen,get_old,prefix));
 
355
    /* h�mta resultat av gamla f�rst */
 
356
    if (get_old) {
 
357
        res = GetOverlappedResult(fd,ovp,&read,TRUE);
 
358
        DEBUGF(("read = %d, res = %d, GetLastError() = %d\n",read,res,GetLastError()));
 
359
        if (!res) {
 
360
            return 0;
 
361
        }
 
362
        buffer[read] = '\0';
 
363
        ptr = buffer;
 
364
        while(*skip && *ptr != '\0') {
 
365
            if (*ptr == '\n') {
 
366
                --(*skip);
 
367
            }
 
368
            ++ptr;
 
369
        }
 
370
        if(*ptr != '\0') {
 
371
            fprintf(whereto,"%s",ptr);
 
372
        }
 
373
    }
 
374
 
 
375
    ResetEvent(ovp->hEvent);
 
376
 
 
377
    for(;;) {
 
378
        res = ReadFile(fd,buffer,bufflen-1,&read,ovp);
 
379
 
 
380
        if (!res) {
 
381
            err = GetLastError();
 
382
            if (err == ERROR_IO_PENDING) {
 
383
                DEBUGF(("Error I/O Pending\n"));
 
384
                return 1;
 
385
            } 
 
386
            DEBUGF(("ReadFileFailed for %s, %d\n",prefix,err));
 
387
            return 0;
 
388
        }
 
389
        buffer[read] = '\0';
 
390
        ptr = buffer;
 
391
        while(*skip && *ptr != '\0') {
 
392
            if (*ptr == '\n') {
 
393
                --(*skip);
 
394
            }
 
395
            ++ptr;
 
396
        }
 
397
        if(*ptr != '\0') {
 
398
            fprintf(whereto,"%s",ptr);
 
399
        }
 
400
    }
 
401
}
 
402
 
 
403
 
 
404
int run(char *commandline,int skipout,int skiperr)
 
405
{
 
406
    HANDLE harr[3];
 
407
    HANDLE real_stdout,real_stderr;
 
408
    OVERLAPPED ov_out,ov_err;
 
409
    char outbuff[1024],errbuff[1024];
 
410
    DWORD ret,exitcode;
 
411
    HANDLE wait[3];
 
412
    int map[3];
 
413
    DWORD nwait = 3;
 
414
    int i,j;
 
415
    unsigned living_handles = 0x7;
 
416
 
 
417
    harr[HANDLE_STDOUT] = CreateEvent(NULL,
 
418
                                      TRUE,
 
419
                                      FALSE, /*not signalled */
 
420
                                      NULL);  
 
421
    harr[HANDLE_STDERR] = CreateEvent(NULL,
 
422
                                      TRUE,
 
423
                                      FALSE,/*not signalled */
 
424
                                      NULL);  
 
425
 
 
426
    memset(&ov_out,0,sizeof(ov_out));
 
427
    memset(&ov_err,0,sizeof(ov_err));
 
428
 
 
429
    ov_out.hEvent = harr[HANDLE_STDOUT];
 
430
    ov_err.hEvent = harr[HANDLE_STDERR];
 
431
 
 
432
    harr[HANDLE_PROC] = do_run(commandline,&real_stdout,&real_stderr);
 
433
 
 
434
#ifdef HARDDEBUG
 
435
    prefix = "STDOUT";
 
436
#endif
 
437
    handle_overlapped(real_stdout,&ov_out,outbuff,1024,0,stdout,&skipout);
 
438
#ifdef HARDDEBUG
 
439
    prefix = "STDERR";
 
440
#endif
 
441
    handle_overlapped(real_stderr,&ov_err,errbuff,1024,0,stderr,&skiperr);
 
442
 
 
443
    for(;;) {
 
444
        nwait = 0;
 
445
        for(i=0;i<3;++i) {
 
446
            if ((living_handles & (1U << i))) {
 
447
                map[nwait] = i;
 
448
                wait[nwait++] = harr[i];
 
449
            }
 
450
        }
 
451
 
 
452
        ret = WaitForMultipleObjects(nwait,
 
453
                                     wait,
 
454
                                     FALSE,
 
455
                                     INFINITE);
 
456
        DEBUGF(("Wait returned %d\n",ret));
 
457
 
 
458
        if (ret == WAIT_FAILED) {
 
459
            error("Wait failed");
 
460
        }
 
461
 
 
462
        ret -= WAIT_OBJECT_0;
 
463
        
 
464
        switch (map[ret]) {
 
465
        case HANDLE_PROC:
 
466
            
 
467
            DEBUGF(("Process died!\n"));
 
468
            GetExitCodeProcess(harr[HANDLE_PROC],&exitcode);
 
469
            if ((living_handles &= (~(1U<<HANDLE_PROC))) == 0) {
 
470
                goto done;
 
471
            }
 
472
            --nwait;
 
473
            break;
 
474
        case HANDLE_STDOUT:
 
475
#ifdef HARDDEBUG
 
476
            prefix = "STDOUT";
 
477
#endif
 
478
            if (!handle_overlapped(real_stdout,&ov_out, outbuff,1024,1,stdout,&skipout)) {
 
479
                if ((living_handles &= (~(1U<<HANDLE_STDOUT))) == 0) {
 
480
                    goto done;
 
481
                }
 
482
            }
 
483
            break;
 
484
        case HANDLE_STDERR:
 
485
#ifdef HARDDEBUG
 
486
            prefix = "STDERR";
 
487
#endif
 
488
            if (!handle_overlapped(real_stderr,&ov_err, errbuff,1024,1,stderr,&skiperr)){
 
489
                if ((living_handles &= (~(1U<<HANDLE_STDERR))) == 0) {
 
490
                    goto done;
 
491
                }
 
492
            }
 
493
            break;
 
494
        default:
 
495
            error("Unexpected wait result");
 
496
        }
 
497
    }
 
498
 done:
 
499
    CloseHandle(harr[HANDLE_PROC]);
 
500
    CloseHandle(harr[HANDLE_STDOUT]);
 
501
    CloseHandle(harr[HANDLE_STDERR]);
 
502
    CloseHandle(real_stdout);
 
503
    CloseHandle(real_stderr);
 
504
    return (int) exitcode;
 
505
}
 
506
 
 
507
int main(int argc, char **argv)
 
508
{
 
509
    int i;
 
510
    int x;
 
511
    char *s;
 
512
    char *mpath;
 
513
    char *debuglog;
 
514
    FILE *debugfile;
 
515
 
 
516
    char *common_cflags="-nologo -D__WIN32__ -DWIN32 -DWINDOWS -D_WIN32 -DNT -D_CRT_SECURE_NO_DEPRECATE";
 
517
    char *md = "-MD";
 
518
    char *debug_flags = "";
 
519
    char *optimize_flags = "";
 
520
    char *outfile = "";
 
521
    char *cmd = "";
 
522
    char **sources = NULL;
 
523
    char *accum_objects = "";
 
524
    char *linkcmd = "";
 
525
    
 
526
    int md_forced = 0;
 
527
    int preprocessing = 0;
 
528
    int debug_build = 0;
 
529
    int optimized_build = 0;
 
530
    int linking = 1;
 
531
    int retval;
 
532
 
 
533
    save_args(argc,argv);
 
534
    
 
535
    for(i = 1; i < argc; ++i) {
 
536
        if (argv[i][0] == '-') {
 
537
            char *opt = argv[i]+1;
 
538
            switch(*opt) {
 
539
            case 'W':
 
540
                if(strcmp(opt,"Wall")) {
 
541
                    goto filename;
 
542
                }
 
543
                break;
 
544
            case 'c':
 
545
                if(strlen(opt) > 1) {
 
546
                    goto filename;
 
547
                }
 
548
                linking = 0;
 
549
                break;
 
550
            case 'E':
 
551
                if(strlen(opt) > 1) {
 
552
                    if (opt[1] == 'H') {
 
553
                        cmd = add_to(cmd," ");
 
554
                        cmd = add_to(cmd,opt);
 
555
                    } else {
 
556
                        goto filename;
 
557
                    }
 
558
                }
 
559
                preprocessing = 1;
 
560
                linking = 0;
 
561
                break;
 
562
            case 'O':
 
563
                /* ignore what opt is requested, set hard */
 
564
                optimize_flags = "-Ox -Zi";
 
565
                debug_flags = "";
 
566
                debug_build = 0;
 
567
                if (!md_forced) {
 
568
                    md = "-MD";
 
569
                }
 
570
                optimized_build = 1;
 
571
                break;
 
572
            case 'g':
 
573
                if (strcmp(opt,"g") && strcmp(opt,"ggdb")) {
 
574
                    goto filename;
 
575
                }
 
576
                if (!optimized_build) {
 
577
                    debug_flags = "-Z7";
 
578
                    if (!md_forced) {
 
579
                        md = "-MDd";
 
580
                    }
 
581
                    linkcmd = add_to(linkcmd," -g");
 
582
                    debug_build = 1;
 
583
                }
 
584
                break;
 
585
            case 'm':
 
586
            case 'M':
 
587
                if(!strcmp(opt,"mt") || !strcmp(opt,"MT")) {
 
588
                    md = "-MT";
 
589
                } else if (!strcmp(opt,"md") || !strcmp(opt,"MD")) {
 
590
                    md = "-MD";
 
591
                } else if (!strcmp(opt,"ml") || !strcmp(opt,"ML")) {
 
592
                    md = "-ML";
 
593
                } else if (!strcmp(opt,"mdd") || !strcmp(opt,"MDd") || 
 
594
                           !strcmp(opt,"MDD")) {
 
595
                    md = "-MDd";
 
596
                } else if (!strcmp(opt,"mtd") || !strcmp(opt,"MTd") || 
 
597
                           !strcmp(opt,"MTD")) {
 
598
                    md = "-MTd";
 
599
                } else if (!strcmp(opt,"mld") || !strcmp(opt,"MLd") || 
 
600
                           !strcmp(opt,"MLD")) {
 
601
                    md = "-MLd";
 
602
                } else {
 
603
                    goto filename;
 
604
                }
 
605
                md_forced = 1;
 
606
                break;
 
607
            case 'o':
 
608
                if (!strcmp(opt,"o")) {
 
609
                    ++i;
 
610
                    if (i >= argc) {
 
611
                        error("-o without filename");
 
612
                    }
 
613
                    outfile = argv[i];
 
614
                } else {
 
615
                    outfile = opt+1;
 
616
                }
 
617
                break;
 
618
            case 'I':
 
619
                if(opt[1] == '/') {
 
620
                    mpath = do_cyp(opt+1);
 
621
                    cmd = add_to(cmd," -I\"");
 
622
                    cmd = add_to(cmd,mpath);
 
623
                    cmd = add_to(cmd,"\"");
 
624
                    free(mpath);
 
625
                } else {
 
626
                    cmd = add_to(cmd," ");
 
627
                    cmd = add_to(cmd,opt);
 
628
                }
 
629
                break;
 
630
            case 'D':
 
631
                cmd = add_to(cmd," -");
 
632
                cmd = add_to(cmd,opt);
 
633
            case 'l':
 
634
                linkcmd = add_to(linkcmd," -");
 
635
                linkcmd = add_to(linkcmd,opt);
 
636
                break;
 
637
            default:
 
638
                goto filename;
 
639
            }
 
640
            continue;
 
641
        }
 
642
    filename:
 
643
        s = argv[i];
 
644
        x = strlen(s);
 
645
        if (x > 1 && s[x-1] == 'c' && s[x-2] == '.') {
 
646
            /* C source */
 
647
            sources = add_to_src(sources,s);
 
648
        } else if (x > 3 && !strcmp(s + (x - 4),".cpp")) {
 
649
            /* C++ */
 
650
            sources = add_to_src(sources,s);
 
651
        } else  if (x > 1 && s[x-1] == 'o' && s[x-2] == '.') { 
 
652
            linkcmd = add_to(linkcmd," ");
 
653
            linkcmd = add_to(linkcmd,s);
 
654
        } else {
 
655
            /* Pass rest to linker */
 
656
            linkcmd = add_to(linkcmd," ");
 
657
            linkcmd = add_to(linkcmd,s);
 
658
        }
 
659
    }
 
660
    if ((debuglog = getenv("CC_SH_DEBUG_LOG")) != NULL) {
 
661
        debugfile = fopen(debuglog,"wb+");
 
662
        if (debugfile) {
 
663
            fprintf(debugfile,"----------------\n");
 
664
        }
 
665
    } else {
 
666
        debugfile = NULL;
 
667
    }
 
668
 
 
669
    tmpobjdir = add_to("","/tmp/tmpobj");
 
670
    {
 
671
        char pidstr[100];
 
672
        pid_t pid = getpid();
 
673
        sprintf(pidstr,"%d",pid);
 
674
        tmpobjdir = add_to(tmpobjdir,pidstr);
 
675
    }
 
676
    mkdir(tmpobjdir,0777);
 
677
    if (sources != NULL) { 
 
678
        char *output_filename;
 
679
        char *output_flag;
 
680
        char *params;
 
681
        for (i=0;sources[i] != NULL; ++i) {
 
682
            if (!linking) {
 
683
                int x = strlen(outfile);
 
684
                if (x > 1 && outfile[x-1] == 'o' && outfile[x-2] == '.') {
 
685
                    if (*outfile != '/') {
 
686
                        /* non absolute object */
 
687
                        if (i > 0) {
 
688
                            error("Single object multiple sources");
 
689
                        }
 
690
                        output_filename = add_to("",outfile);
 
691
                    } else {
 
692
                        if (i > 0) {
 
693
                            error("Single object multiple sources");
 
694
                        }
 
695
                        output_filename = do_cyp(outfile);
 
696
                    }
 
697
                } else {
 
698
                    char *tmp = object_name(sources[i]);
 
699
 
 
700
                    /*fprintf(stderr,"sources[i] = %s\ntmp = %s\n",
 
701
                      sources[i],tmp);*/
 
702
 
 
703
                    if (!x || outfile[0] != '/') {
 
704
                        /* non absolute directory */
 
705
                        output_filename = add_to("",outfile);
 
706
                    } else {
 
707
                        output_filename = do_cyp(outfile);
 
708
                    }
 
709
                    /*fprintf(stderr,"output_filename = %s\n",output_filename);*/
 
710
                    if (*output_filename != '\0') {
 
711
                        output_filename = add_to(output_filename,"/");
 
712
                    }
 
713
                    output_filename = add_to(output_filename,tmp);
 
714
                    free(tmp);
 
715
                }
 
716
            } else {
 
717
                char *tmp = object_name(sources[i]);
 
718
                output_filename = add_to("",tmpobjdir);
 
719
                output_filename = add_to(output_filename,"/");
 
720
                output_filename = add_to(output_filename,tmp);
 
721
                accum_objects = add_to(accum_objects," ");
 
722
                accum_objects = add_to(accum_objects,output_filename);
 
723
                /* reform to dos path */
 
724
                free(output_filename);
 
725
                output_filename = do_cyp(tmpobjdir);
 
726
                output_filename = add_to(output_filename,"/");
 
727
                output_filename = add_to(output_filename,tmp);
 
728
            }
 
729
            mpath = do_cyp(sources[i]);
 
730
            if (preprocessing) {
 
731
                output_flag = add_to("","-E");
 
732
            } else {
 
733
                output_flag = add_to("","-c -Fo");
 
734
                output_flag = add_to(output_flag,output_filename);
 
735
            }
 
736
            params = add_to("","cl.exe ");
 
737
            params = add_to(params,common_cflags);
 
738
            params = add_to(params," ");
 
739
            params = add_to(params,md);
 
740
            params = add_to(params," ");
 
741
            params = add_to(params,debug_flags);
 
742
            params = add_to(params," ");
 
743
            params = add_to(params,optimize_flags);
 
744
            params = add_to(params," ");
 
745
            params = add_to(params,cmd);
 
746
            params = add_to(params," ");
 
747
            params = add_to(params,output_flag);
 
748
            params = add_to(params," ");
 
749
            params = add_to(params,mpath);
 
750
            free(output_filename);
 
751
            free(output_flag);
 
752
            free(mpath);
 
753
            
 
754
            if (debugfile) {
 
755
                fprintf(debugfile,"%s\n",save);
 
756
                fprintf(debugfile,"%s\n",params);
 
757
            }
 
758
            if (preprocessing) {
 
759
                retval = run(params,0,1);
 
760
            } else {
 
761
                retval = run(params,1,0);
 
762
            }
 
763
            if (retval != 0) {
 
764
                maybe_cleanup();
 
765
                return retval;
 
766
            }
 
767
            free(params);
 
768
        }
 
769
    }
 
770
    if (linking) {
 
771
        char *out_spec;
 
772
        char *stdlib;
 
773
        char *params;
 
774
        if (strlen(outfile) == 0) {
 
775
            if (sources != NULL && sources[0] != NULL) {
 
776
                char *tmp = exe_name(sources[0]);
 
777
                out_spec = add_to("","-o ");
 
778
                out_spec = add_to(out_spec,tmp);
 
779
                free(tmp);
 
780
            } else {
 
781
                out_spec = add_to("","");
 
782
            }
 
783
        } else {
 
784
            out_spec = add_to("","-o ");
 
785
            out_spec = add_to(out_spec,outfile);
 
786
        }
 
787
        if (!strcmp(md,"-ML")) {
 
788
            stdlib="-lLIBC";
 
789
        } else if (!strcmp(md,"-MLd")) {
 
790
            stdlib="-lLIBCD";
 
791
        } else if (!strcmp(md,"-MD")) {
 
792
            stdlib="-lMSVCRT";
 
793
        } else if (!strcmp(md,"-MDd")) {
 
794
            stdlib="-lMSVCRTD";
 
795
        } else if (!strcmp(md,"-MT")) {
 
796
            stdlib="-lLIBCMT";
 
797
        } else if (!strcmp(md,"-MTd")) {
 
798
            stdlib="-lLIBMTD";
 
799
        } else {
 
800
            stdlib = "";
 
801
        }
 
802
#if 0
 
803
        params = add_to("","bash ld.sh ");
 
804
#else
 
805
        params = add_to("","ld_wrap.exe ");
 
806
#endif
 
807
        params = add_to(params,accum_objects);
 
808
        params = add_to(params," ");
 
809
        params = add_to(params,out_spec);
 
810
        params = add_to(params," ");
 
811
        params = add_to(params,linkcmd);
 
812
        params = add_to(params," ");
 
813
        params = add_to(params,stdlib);
 
814
        free(out_spec);
 
815
        free(accum_objects);
 
816
        if (debugfile) {
 
817
            fprintf(debugfile,"%s\n",params);
 
818
        }
 
819
        if (retval = run(params,0,0) != 0) {
 
820
            maybe_cleanup();
 
821
            return retval;
 
822
        }
 
823
        free(params);
 
824
    }
 
825
    maybe_cleanup();
 
826
    return 0;
 
827
}
 
828
            
 
829
            
 
830
                
 
831
 
 
832
                        
 
833
                    
 
834
                    
 
835
                
 
836
            
 
837
                    
 
838
 
 
839
 
 
840
 
 
841
 
 
842
 
 
843
 
 
844
 
 
845