int main()

in erts/etc/win32/cygwin_tools/vc/ld_wrap.c [448:779]


int main(int argc, char **argv)
{
    int i;
    int x;
    char *s;
    char *mpath;
    char *debuglog;
    char *remove;
    FILE *debugfile;
    FILE *tmpfile;
    int filefound;
    int retval = 0;

    char *kernel_libs="kernel32.lib advapi32.lib";
    char *gdi_libs="gdi32.lib user32.lib comctl32.lib comdlg32.lib shell32.lib";
    char *default_libraries = "";
    char *cmd = "";
    char *stdlib = "MSVCRT.LIB";
    int debug_build = 0;
    int stdlib_forced = 0;
    int build_dll = 0;
    char *output_filename = "";
    char *linkadd_pdb = "";
    char *linktype = "";
    char *manifest = "";
    char *outputres = "";

    save_args(argc,argv);
    //fprintf(stderr,"ld_wrap!\n");

    default_libraries = add_to(default_libraries,kernel_libs);
    default_libraries = add_to(default_libraries," ");
    default_libraries = add_to(default_libraries,gdi_libs);
    
    for(i = 1; i < argc; ++i) {
	if (argv[i][0] == '-') {
	    char *opt = argv[i]+1;
	    switch(*opt) {
	    case 'D':
		if(strcmp(opt,"DLL")) {
		    goto filename;
		}
		build_dll = 1;
		break;
	    case 'd':
		if(!strncmp(opt,"def:",4)) {
		    mpath = do_cyp(opt+4);
		    cmd = add_to(cmd," -def:\"");
		    cmd = add_to(cmd,mpath);
		    cmd = add_to(cmd,"\"");
		    free(mpath);
		} else if(strcmp(opt,"dll")) {
		    goto filename;
		} else {
		    build_dll = 1;
		}
		break;
	    case 'L':
		mpath = do_cyp(opt+1);
		cmd = add_to(cmd," -libpath:\"");
		cmd = add_to(cmd,mpath);
		cmd = add_to(cmd,"\"");
		free(mpath);
		break;
	    case 'l':
		if(!strcmp(opt,"lMSVCRT") || !strcmp(opt,"lmsvcrt")) {
		    stdlib = "MSVCRT.LIB";
		    stdlib_forced = 1;
		} else if(!strcmp(opt,"lMSVCRTD") || !strcmp(opt,"lmsvcrtd")) {
		    stdlib = "MSVCRTD.LIB";
		    stdlib_forced = 1;
		} else if(!strcmp(opt,"lLIBCMT") || !strcmp(opt,"llibcmt")) {
		    stdlib = "LIBCMT.LIB";
		    stdlib_forced = 1;
		} else if(!strcmp(opt,"lLIBCMTD") || !strcmp(opt,"llibcmtd")) {
		    stdlib = "LIBCMTD.LIB";
		    stdlib_forced = 1;
		} else if(!strcmp(opt,"lsocket")) {
		    default_libraries = add_to(default_libraries," ");
		    default_libraries = add_to(default_libraries,"WS2_32.LIB");
		} else {
		    mpath = do_cyp(opt+1);
		    cmd = add_to(cmd," \"");
		    cmd = add_to(cmd,mpath);
		    cmd = add_to(cmd,"\"");
		    free(mpath);
		}
		break;
	    case 'g':
		debug_build = 1;
		break;
	    case 'p':
		if (strcmp(opt,"pdb:none")) {
		    goto filename;
		}
		break;
	    case 'i':
		if (!strncmp(opt,"implib:",7)) {
		    mpath = do_cyp(opt+7);
		    cmd = add_to(cmd," -implib:\"");
		    cmd = add_to(cmd,mpath);
		    cmd = add_to(cmd,"\"");
		    free(mpath);
		} else if (strcmp(opt,"incremental:no")) {
		    goto filename;
		}
		break;
	    case 'o':
		if (!strcmp(opt,"o")) {
		    ++i;
		    if (i >= argc) {
			error("-o without filename");
		    }
		    output_filename = do_cyp(argv[i]);
		} else {
		    output_filename = do_cyp(opt+1);
		}
		break;
	    default:
		goto filename;
	    }
	    continue;
	}
    filename:
	s = argv[i];
	if (*s == '/') {
	    mpath = do_cyp(s);
	    cmd = add_to(cmd," \"");
	    cmd = add_to(cmd,mpath);
	    cmd = add_to(cmd,"\"");
	    free(mpath);
	} else {
	    cmd = add_to(cmd," \"");
	    cmd = add_to(cmd,s);
	    cmd = add_to(cmd,"\"");
	}
    }
    if ((debuglog = getenv("LD_SH_DEBUG_LOG")) != NULL) {
	debugfile = fopen(debuglog,"wb+");
	if (debugfile) {
	    fprintf(debugfile,"----------------\n");
	}
    } else {
	debugfile = NULL;
    }

    if (debug_build) {
	if (!stdlib_forced) {
	    stdlib = "MSVCRTD.LIB";
	}
    }

    s = add_to("",output_filename);
    x = strlen(s);
    
    if (x >= 4 && (!strcmp(s+x-4,".exe") || !strcmp(s+x-4,".EXE") ||
		   !strcmp(s+x-4,".dll") || !strcmp(s+x-4,".DLL"))) {
	*(s+x-3) = '\0';
	linkadd_pdb = add_to(linkadd_pdb,"-pdb:\"");
	linkadd_pdb = add_to(linkadd_pdb,s);
	linkadd_pdb = add_to(linkadd_pdb,"pdb\"");
    } else if (!x) {
	linkadd_pdb = add_to(linkadd_pdb,"-pdb:\"a.pdb\"");
    } else {
	linkadd_pdb = add_to(linkadd_pdb,"-pdb:\"");
	linkadd_pdb = add_to(linkadd_pdb,s);
	linkadd_pdb = add_to(linkadd_pdb,".pdb\"");
    }
    free(s);

    
    linktype = add_to(linktype,"-debug ");
    linktype = add_to(linktype,linkadd_pdb);

    free(linkadd_pdb);

    s = add_to("",output_filename);
    x = strlen(s);

    if (build_dll) {
	if (x >= 4 && (!strcmp(s+x-4,".exe") || !strcmp(s+x-4,".EXE") ||
		       !strcmp(s+x-4,".dll") || !strcmp(s+x-4,".DLL"))) {
	    
	    if (!strcmp(s+x-4,".exe") || !strcmp(s+x-4,".EXE")) {
		fprintf(stderr,"Warning, output set to .exe when building DLL");
	    }
	    mpath = cmd;
	    cmd = add_to("","-dll -out:\"");
	    cmd = add_to(cmd,s);
	    cmd = add_to(cmd,"\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,output_filename);
	    outputres = add_to(outputres,";2");
	    manifest = add_to(manifest,output_filename);
	    manifest = add_to(manifest,".manifest");
	} else if (x == 0) {
	    mpath = cmd;
	    cmd = add_to("","-dll -out:\"a.dll\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,"a.dll;2");
	    manifest = add_to(manifest,"a.dll.manifest");
	} else {
	    mpath = cmd;
	    cmd = add_to("","-dll -out:\"");
	    cmd = add_to(cmd,s);
	    cmd = add_to(cmd,".dll\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,output_filename);
	    outputres = add_to(outputres,".dll;2");
	    manifest = add_to(manifest,output_filename);
	    manifest = add_to(manifest,".dll.manifest");
	}
    } else {
	if (x >= 4 && (!strcmp(s+x-4,".exe") || !strcmp(s+x-4,".EXE") ||
		       !strcmp(s+x-4,".dll") || !strcmp(s+x-4,".DLL"))) {
	    
	    if (!strcmp(s+x-4,".dll") || !strcmp(s+x-4,".DLL")) {
		fprintf(stderr,"Warning, output set to .exe when building DLL");
	    }
	    mpath = cmd;
	    cmd = add_to("","-out:\"");
	    cmd = add_to(cmd,s);
	    cmd = add_to(cmd,"\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,output_filename);
	    outputres = add_to(outputres,";1");
	    manifest = add_to(manifest,output_filename);
	    manifest = add_to(manifest,".manifest");
	} else if (x == 0) {
	    mpath = cmd;
	    cmd = add_to("","-out:\"a.exe\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,"a.exe;1");
	    manifest = add_to(manifest,"a.exe.manifest");
	} else {
	    mpath = cmd;
	    cmd = add_to("","-out:\"");
	    cmd = add_to(cmd,s);
	    cmd = add_to(cmd,".exe\" ");
	    cmd = add_to(cmd,mpath);
	    if (*mpath) {
		free(mpath);
	    }

	    outputres = add_to(outputres,output_filename);
	    outputres = add_to(outputres,".exe;1");
	    manifest = add_to(manifest,output_filename);
	    manifest = add_to(manifest,".exe.manifest");
	}
    }
	    
    s = cmd;
    cmd = add_to("","link.exe ");
    cmd = add_to(cmd,linktype);
    cmd = add_to(cmd," -nologo -incremental:no ");
    cmd = add_to(cmd,s);
    cmd = add_to(cmd," ");
    cmd = add_to(cmd,stdlib);
    cmd = add_to(cmd," ");
    cmd = add_to(cmd,default_libraries);

    if (*s) {
	free(s);
    }
	

    if (debugfile) {
	fprintf(debugfile,"%s\n",save);
	fprintf(debugfile,"%s\n",cmd);
    }
    
    retval = run(cmd,0,0);
    
    
    mpath = do_cyp(manifest);
    filefound = 0;
    tmpfile = fopen(mpath,"rb");
    if (tmpfile != NULL) {
	filefound = 1;
	fclose(tmpfile);
    }
    if (retval == 0 && filefound) {
	s = add_to("","mt.exe -nologo -manifest \"");
	s = add_to(s,manifest);
	s = add_to(s,"\" -outputresource:\"");
	s = add_to(s,outputres);
	s = add_to(s,"\"");
	if (debugfile) {
	    fprintf(debugfile,"%s\n",s);
	}
	retval = run(s,0,0);
	if (*s) {
	    free(s);
	}
	if (retval) {
	    /* cleanup needed */
	    remove = add_to("",outputres);
	    x = strlen(remove);
	    remove[x-2] = '\0';
	    if (debugfile) {
		fprintf(debugfile,"remove %s\n",remove);
	    }
	    DeleteFile(remove);
	    free(remove);
	}
	if (debugfile) {
	    fprintf(debugfile,"remove %s\n",manifest);
	}
	DeleteFile(manifest);
    }
    return retval;
}