int spawn(const strarray* prefix, const strarray* args, int ignore_errors) { unsigned int i; int status; strarray* arr = strarray_dup(args); const char** argv; char* prog = 0; strarray_add(arr, NULL); argv = arr->base; if (prefix) { const char *p = strrchr(argv[0], '/'); if (!p) p = argv[0]; else p++; for (i = 0; i < prefix->size; i++) { struct stat st; free( prog ); prog = strmake("%s/%s%s", prefix->base[i], p, EXEEXT); if (stat(prog, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111)) { argv[0] = prog; break; } } } if (verbose) { for(i = 0; argv[i]; i++) printf("%s ", argv[i]); printf("\n"); } if ((status = _spawnvp( _P_WAIT, argv[0], argv)) && !ignore_errors) { if (status > 0) error("%s failed\n", argv[0]); else perror("winegcc"); exit(3); } free(prog); strarray_free(arr); return status; }
static void build(struct options* opts) { static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" }; strarray *lib_dirs, *files; strarray *spec_args, *link_args; char *output_file; const char *spec_o_name; const char *output_name, *spec_file, *lang; const char* winebuild = getenv("WINEBUILD"); int generate_app_loader = 1; unsigned int j; /* NOTE: for the files array we'll use the following convention: * -axxx: xxx is an archive (.a) * -dxxx: xxx is a DLL (.def) * -lxxx: xxx is an unsorted library * -oxxx: xxx is an object (.o) * -rxxx: xxx is a resource (.res) * -sxxx: xxx is a shared lib (.so) * -xlll: lll is the language (c, c++, etc.) */ if (!winebuild) winebuild = "winebuild"; output_file = strdup( opts->output_name ? opts->output_name : "a.out" ); /* 'winegcc -o app xxx.exe.so' only creates the load script */ if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so")) { create_file(output_file, 0755, app_loader_template, opts->files->base[0]); return; } /* generate app loader only for .exe */ if (opts->shared || strendswith(output_file, ".exe.so")) generate_app_loader = 0; /* normalize the filename a bit: strip .so, ensure it has proper ext */ if (strendswith(output_file, ".so")) output_file[strlen(output_file) - 3] = 0; if (opts->shared) { if ((output_name = strrchr(output_file, '/'))) output_name++; else output_name = output_file; if (!strchr(output_name, '.')) output_file = strmake("%s.dll", output_file); } else if (!strendswith(output_file, ".exe")) output_file = strmake("%s.exe", output_file); /* get the filename from the path */ if ((output_name = strrchr(output_file, '/'))) output_name++; else output_name = output_file; /* prepare the linking path */ if (!opts->wine_objdir) { lib_dirs = strarray_dup(opts->lib_dirs); for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ ) strarray_add(lib_dirs, stdlibpath[j]); } else { lib_dirs = strarray_alloc(); strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir)); strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir)); strarray_addall(lib_dirs, opts->lib_dirs); } /* mark the files with their appropriate type */ spec_file = lang = 0; files = strarray_alloc(); for ( j = 0; j < opts->files->size; j++ ) { const char* file = opts->files->base[j]; if (file[0] != '-') { switch(get_file_type(file)) { case file_def: case file_spec: if (spec_file) error("Only one spec file can be specified\n"); spec_file = file; break; case file_rc: /* FIXME: invoke wrc to build it */ error("Can't compile .rc file at the moment: %s\n", file); break; case file_res: strarray_add(files, strmake("-r%s", file)); break; case file_obj: strarray_add(files, strmake("-o%s", file)); break; case file_arh: strarray_add(files, strmake("-a%s", file)); break; case file_so: strarray_add(files, strmake("-s%s", file)); break; case file_na: error("File does not exist: %s\n", file); break; default: file = compile_to_object(opts, file, lang); strarray_add(files, strmake("-o%s", file)); break; } } else if (file[1] == 'l') add_library( lib_dirs, files, file + 2 ); else if (file[1] == 'x') lang = file; } if (opts->shared && !spec_file) error("A spec file is currently needed in shared mode\n"); /* add the default libraries, if needed */ if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt"); if (!opts->wine_objdir && !opts->nodefaultlibs) { if (opts->gui_app) { add_library(lib_dirs, files, "shell32"); add_library(lib_dirs, files, "comdlg32"); add_library(lib_dirs, files, "gdi32"); } add_library(lib_dirs, files, "advapi32"); add_library(lib_dirs, files, "user32"); add_library(lib_dirs, files, "kernel32"); } if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0"); if (!opts->nostdlib) add_library(lib_dirs, files, "wine"); /* run winebuild to generate the .spec.o file */ spec_args = strarray_alloc(); spec_o_name = get_temp_file(output_name, ".spec.o"); strarray_add(spec_args, winebuild); if (verbose) strarray_add(spec_args, "-v"); if (keep_generated) strarray_add(spec_args, "--save-temps"); strarray_add(spec_args, "--as-cmd"); strarray_add(spec_args, AS); strarray_add(spec_args, "--ld-cmd"); strarray_add(spec_args, LD); strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " ")); strarray_add(spec_args, opts->shared ? "--dll" : "--exe"); strarray_add(spec_args, "-o"); strarray_add(spec_args, spec_o_name); if (spec_file) { strarray_add(spec_args, "-E"); strarray_add(spec_args, spec_file); } if (!opts->shared) { strarray_add(spec_args, "-F"); strarray_add(spec_args, output_name); strarray_add(spec_args, "--subsystem"); strarray_add(spec_args, opts->gui_app ? "windows" : "console"); if (opts->unicode_app) { strarray_add(spec_args, "--entry"); strarray_add(spec_args, "__wine_spec_exe_wentry"); } } for ( j = 0; j < lib_dirs->size; j++ ) strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j])); for ( j = 0 ; j < opts->winebuild_args->size ; j++ ) strarray_add(spec_args, opts->winebuild_args->base[j]); for ( j = 0; j < files->size; j++ ) { const char* name = files->base[j] + 2; switch(files->base[j][1]) { case 'r': strarray_add(spec_args, files->base[j]); break; case 'd': case 'a': case 'o': strarray_add(spec_args, name); break; } } spawn(opts->prefix, spec_args, 0); /* link everything together now */ link_args = strarray_alloc(); strarray_addall(link_args, get_translator(opts->processor)); strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " ")); strarray_add(link_args, "-o"); strarray_add(link_args, strmake("%s.so", output_file)); for ( j = 0 ; j < opts->linker_args->size ; j++ ) strarray_add(link_args, opts->linker_args->base[j]); #ifdef __APPLE__ if (opts->image_base) { strarray_add(link_args, "-image_base"); strarray_add(link_args, opts->image_base); } #endif for ( j = 0; j < lib_dirs->size; j++ ) strarray_add(link_args, strmake("-L%s", lib_dirs->base[j])); strarray_add(link_args, spec_o_name); for ( j = 0; j < files->size; j++ ) { const char* name = files->base[j] + 2; switch(files->base[j][1]) { case 'l': case 's': strarray_add(link_args, strmake("-l%s", name)); break; case 'a': case 'o': strarray_add(link_args, name); break; } } if (!opts->nostdlib) { strarray_add(link_args, "-lm"); strarray_add(link_args, "-lc"); } spawn(opts->prefix, link_args, 0); /* set the base address */ if (opts->image_base) { const char *prelink = PRELINK; if (prelink[0] && strcmp(prelink,"false")) { strarray *prelink_args = strarray_alloc(); strarray_add(prelink_args, prelink); strarray_add(prelink_args, "--reloc-only"); strarray_add(prelink_args, opts->image_base); strarray_add(prelink_args, strmake("%s.so", output_file)); spawn(opts->prefix, prelink_args, 1); strarray_free(prelink_args); } } /* create the loader script */ if (generate_app_loader) { if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0; create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name)); } }