int main()

in erts/etc/win32/cygwin_tools/vc/cc_wrap.c [527:847]


int main(int argc, char **argv)
{
    int i;
    int x;
    char *s;
    char *mpath;
    char *debuglog;
    FILE *debugfile;

    char *common_cflags="-nologo -D__WIN32__ -DWIN32 -DWINDOWS -D_WIN32 -DNT -D_CRT_SECURE_NO_DEPRECATE";
    char *md = "-MD";
    char *debug_flags = "";
    char *optimize_flags = "";
    char *outfile = "";
    char *cmd = "";
    char **sources = NULL;
    char *accum_objects = "";
    char *linkcmd = "";
    
    int md_forced = 0;
    int preprocessing = 0;
    int debug_build = 0;
    int optimized_build = 0;
    int linking = 1;
    int retval;

    save_args(argc,argv);
    
    for(i = 1; i < argc; ++i) {
	if (argv[i][0] == '-') {
	    char *opt = argv[i]+1;
	    switch(*opt) {
	    case 'W':
		if(strcmp(opt,"Wall")) {
		    goto filename;
		}
		break;
	    case 'c':
		if(strlen(opt) > 1) {
		    goto filename;
		}
		linking = 0;
		break;
	    case 'E':
		if(strlen(opt) > 1) {
		    if (opt[1] == 'H') {
			cmd = add_to(cmd," ");
			cmd = add_to(cmd,opt);
		    } else {
			goto filename;
		    }
		}
		preprocessing = 1;
		linking = 0;
		break;
	    case 'O':
		/* ignore what opt is requested, set hard */
		optimize_flags = "-Ox -Zi";
		debug_flags = "";
		debug_build = 0;
		if (!md_forced) {
		    md = "-MD";
		}
		optimized_build = 1;
		break;
	    case 'g':
		if (strcmp(opt,"g") && strcmp(opt,"ggdb")) {
		    goto filename;
		}
		if (!optimized_build) {
		    debug_flags = "-Z7";
		    if (!md_forced) {
			md = "-MDd";
		    }
		    linkcmd = add_to(linkcmd," -g");
		    debug_build = 1;
		}
		break;
	    case 'm':
	    case 'M':
		if(!strcmp(opt,"mt") || !strcmp(opt,"MT")) {
		    md = "-MT";
		} else if (!strcmp(opt,"md") || !strcmp(opt,"MD")) {
		    md = "-MD";
		} else if (!strcmp(opt,"ml") || !strcmp(opt,"ML")) {
		    md = "-ML";
		} else if (!strcmp(opt,"mdd") || !strcmp(opt,"MDd") || 
			   !strcmp(opt,"MDD")) {
		    md = "-MDd";
		} else if (!strcmp(opt,"mtd") || !strcmp(opt,"MTd") || 
			   !strcmp(opt,"MTD")) {
		    md = "-MTd";
		} else if (!strcmp(opt,"mld") || !strcmp(opt,"MLd") || 
			   !strcmp(opt,"MLD")) {
		    md = "-MLd";
		} else {
		    goto filename;
		}
		md_forced = 1;
		break;
	    case 'o':
		if (!strcmp(opt,"o")) {
		    ++i;
		    if (i >= argc) {
			error("-o without filename");
		    }
		    outfile = argv[i];
		} else {
		    outfile = opt+1;
		}
		break;
	    case 'I':
		if(opt[1] == '/') {
		    mpath = do_cyp(opt+1);
		    cmd = add_to(cmd," -I\"");
		    cmd = add_to(cmd,mpath);
		    cmd = add_to(cmd,"\"");
		    free(mpath);
		} else {
		    cmd = add_to(cmd," ");
		    cmd = add_to(cmd,opt);
		}
		break;
	    case 'D':
		cmd = add_to(cmd," -");
		cmd = add_to(cmd,opt);
	    case 'l':
		linkcmd = add_to(linkcmd," -");
		linkcmd = add_to(linkcmd,opt);
		break;
	    default:
		goto filename;
	    }
	    continue;
	}
    filename:
	s = argv[i];
	x = strlen(s);
	if (x > 1 && s[x-1] == 'c' && s[x-2] == '.') {
	    /* C source */
	    sources = add_to_src(sources,s);
	} else if (x > 3 && !strcmp(s + (x - 4),".cpp")) {
	    /* C++ */
	    sources = add_to_src(sources,s);
	} else 	if (x > 1 && s[x-1] == 'o' && s[x-2] == '.') { 
	    linkcmd = add_to(linkcmd," ");
	    linkcmd = add_to(linkcmd,s);
	} else {
	    /* Pass rest to linker */
	    linkcmd = add_to(linkcmd," ");
	    linkcmd = add_to(linkcmd,s);
	}
    }
    if ((debuglog = getenv("CC_SH_DEBUG_LOG")) != NULL) {
	debugfile = fopen(debuglog,"wb+");
	if (debugfile) {
	    fprintf(debugfile,"----------------\n");
	}
    } else {
	debugfile = NULL;
    }

    tmpobjdir = add_to("","/tmp/tmpobj");
    {
	char pidstr[100];
	pid_t pid = getpid();
	sprintf(pidstr,"%d",pid);
	tmpobjdir = add_to(tmpobjdir,pidstr);
    }
    mkdir(tmpobjdir,0777);
    if (sources != NULL) { 
	char *output_filename;
	char *output_flag;
	char *params;
	for (i=0;sources[i] != NULL; ++i) {
	    if (!linking) {
		int x = strlen(outfile);
		if (x > 1 && outfile[x-1] == 'o' && outfile[x-2] == '.') {
		    if (*outfile != '/') {
			/* non absolute object */
			if (i > 0) {
			    error("Single object multiple sources");
			}
			output_filename = add_to("",outfile);
		    } else {
			if (i > 0) {
			    error("Single object multiple sources");
			}
			output_filename = do_cyp(outfile);
		    }
		} else {
		    char *tmp = object_name(sources[i]);

		    /*fprintf(stderr,"sources[i] = %s\ntmp = %s\n",
		      sources[i],tmp);*/

		    if (!x || outfile[0] != '/') {
			/* non absolute directory */
			output_filename = add_to("",outfile);
		    } else {
			output_filename = do_cyp(outfile);
		    }
		    /*fprintf(stderr,"output_filename = %s\n",output_filename);*/
		    if (*output_filename != '\0') {
			output_filename = add_to(output_filename,"/");
		    }
		    output_filename = add_to(output_filename,tmp);
		    free(tmp);
		}
	    } else {
		char *tmp = object_name(sources[i]);
		output_filename = add_to("",tmpobjdir);
		output_filename = add_to(output_filename,"/");
		output_filename = add_to(output_filename,tmp);
		accum_objects = add_to(accum_objects," ");
		accum_objects = add_to(accum_objects,output_filename);
		/* reform to dos path */
		free(output_filename);
		output_filename = do_cyp(tmpobjdir);
		output_filename = add_to(output_filename,"/");
		output_filename = add_to(output_filename,tmp);
	    }
	    mpath = do_cyp(sources[i]);
	    if (preprocessing) {
		output_flag = add_to("","-E");
	    } else {
		output_flag = add_to("","-c -Fo");
		output_flag = add_to(output_flag,output_filename);
	    }
	    params = add_to("","cl.exe ");
	    params = add_to(params,common_cflags);
	    params = add_to(params," ");
	    params = add_to(params,md);
	    params = add_to(params," ");
	    params = add_to(params,debug_flags);
	    params = add_to(params," ");
	    params = add_to(params,optimize_flags);
	    params = add_to(params," ");
	    params = add_to(params,cmd);
	    params = add_to(params," ");
	    params = add_to(params,output_flag);
	    params = add_to(params," ");
	    params = add_to(params,mpath);
	    free(output_filename);
	    free(output_flag);
	    free(mpath);
	    
	    if (debugfile) {
		fprintf(debugfile,"%s\n",save);
		fprintf(debugfile,"%s\n",params);
	    }
	    if (preprocessing) {
		retval = run(params,0,1);
	    } else {
		retval = run(params,1,0);
	    }
	    if (retval != 0) {
		maybe_cleanup();
		return retval;
	    }
	    free(params);
	}
    }
    if (linking) {
	char *out_spec;
	char *stdlib;
	char *params;
	if (strlen(outfile) == 0) {
	    if (sources != NULL && sources[0] != NULL) {
		char *tmp = exe_name(sources[0]);
		out_spec = add_to("","-o ");
		out_spec = add_to(out_spec,tmp);
		free(tmp);
	    } else {
		out_spec = add_to("","");
	    }
	} else {
	    out_spec = add_to("","-o ");
	    out_spec = add_to(out_spec,outfile);
	}
	if (!strcmp(md,"-ML")) {
	    stdlib="-lLIBC";
	} else if (!strcmp(md,"-MLd")) {
	    stdlib="-lLIBCD";
	} else if (!strcmp(md,"-MD")) {
	    stdlib="-lMSVCRT";
	} else if (!strcmp(md,"-MDd")) {
	    stdlib="-lMSVCRTD";
	} else if (!strcmp(md,"-MT")) {
	    stdlib="-lLIBCMT";
	} else if (!strcmp(md,"-MTd")) {
	    stdlib="-lLIBMTD";
	} else {
	    stdlib = "";
	}
#if 0
	params = add_to("","bash ld.sh ");
#else
	params = add_to("","ld_wrap.exe ");
#endif
	params = add_to(params,accum_objects);
	params = add_to(params," ");
	params = add_to(params,out_spec);
	params = add_to(params," ");
	params = add_to(params,linkcmd);
	params = add_to(params," ");
	params = add_to(params,stdlib);
	free(out_spec);
	free(accum_objects);
	if (debugfile) {
	    fprintf(debugfile,"%s\n",params);
	}
	if (retval = run(params,0,0) != 0) {
	    maybe_cleanup();
	    return retval;
	}
	free(params);
    }
    maybe_cleanup();
    return 0;
}