int dispatchFile(const char *name) { const char *ext; if ( (ext = file_name_extension(name)) ) { extdef *d = extdefs; for( ; d->extension; d++ ) { if ( strfeq(d->extension, ext) ) { if ( d->list == &plfiles ) nostate = FALSE; appendArgList(d->list, name); return TRUE; } } if ( soext && strfeq(soext, ext) ) { if ( d->list == &plfiles ) nostate = FALSE; appendArgList(&libs, name); return TRUE; } } return FALSE; }
void linkBaseExecutable() { char *cout = out; #if !defined(HOST_OS_WINDOWS) /* bit of a hack ... */ if ( embed_shared ) { linkSharedObject(); return; } #endif #if defined(HOST_TOOLCHAIN_MSC) { char tmp[MAXPATHLEN]; sprintf(tmp, "/out:%s", cout); prependArgList(&ldoptions, tmp); } concatArgList(&ldoptions, "", &ofiles); /* object files */ exportlibdirs(); if ( !nolibswipl ) { appendArgList(&ldoptions, pllib); /* -lswipl */ addOptionString(pllibs); } concatArgList(&ldoptions, "", &libs); /* libraries */ concatArgList(&ldoptions, "", &lastlibs); /* libraries */ #else /* !defined(HOST_TOOLCHAIN_MSC) */ prependArgList(&ldoptions, cout); prependArgList(&ldoptions, "-o"); /* -o ctmp */ concatArgList(&ldoptions, "", &ofiles); /* object files */ concatArgList(&ldoptions, "-L", &libdirs); /* library directories */ if ( !nolibswipl ) { appendArgList(&ldoptions, pllib); /* -lswipl */ addOptionString(pllibs); } concatArgList(&ldoptions, "", &libs); /* libraries */ concatArgList(&ldoptions, "", &lastlibs); /* libraries */ #endif /* !defined(HOST_TOOLCHAIN_MSC) */ if ( !nostate ) { #if defined(HOST_TOOLCHAIN_MSC) if ( !embed_shared ) { char buf[MAXPATHLEN]; appendArgList(&tmpfiles, replaceExtension(cout, "exp", buf)); appendArgList(&tmpfiles, replaceExtension(cout, "lib", buf)); } #endif } callprog(ld, &ldoptions); }
void createSavedState() { char buf[1024]; char *e; int n; strcpy(buf, "consult(["); e = buf + strlen(buf); for(n=0; n<plfiles.size; n++) { if ( n > 0 ) *e++ = ','; quoted_name(plfiles.list[n], e); e += strlen(e); } for(n=0; n<qlfiles.size; n++) { if ( n > 0 ) *e++ = ','; quoted_name(qlfiles.list[n], e); e += strlen(e); } strcpy(e, "]),qsave_program("); e += strlen(e); quoted_name(pltmp, e); e += strlen(e); strcpy(e, ",["); e += strlen(e); e = put_pl_option(e, "goal", plgoal); *e++ = ','; e = put_pl_option(e, "toplevel", pltoplevel); *e++ = ','; e = put_pl_option(e, "init_file", plinitfile); if ( plclass ) { *e++ = ','; e = put_pl_option(e, "class", plclass); } strcpy(e, "])"); e += strlen(e); appendArgList(&ploptions, "-f"); appendArgList(&ploptions, "none"); appendArgList(&ploptions, "-F"); appendArgList(&ploptions, plsysinit); appendArgList(&ploptions, "-g"); appendArgList(&ploptions, "true"); appendArgList(&ploptions, "-t"); appendArgList(&ploptions, buf); appendArgList(&tmpfiles, pltmp); callprog(pl, &ploptions); }
void ensureOption(arglist *args, const char *opt) { int n; for(n=0; n<args->size; n++) { if ( streq(args->list[n], opt) ) return; } appendArgList(args, opt); }
void concatArgList(arglist *to, const char *prefix, arglist *from) { int n; for(n=0; n<from->size; n++) { char buf[1024]; buf[0] = UNQUOTED; if ( strchr(from->list[n], ' ') ) sprintf(buf+1, "%s\"%s\"", prefix, from->list[n]); else sprintf(buf+1, "%s%s", prefix, from->list[n]); appendArgList(to, buf); } }
int dispatchFile(const char *name) { const char *ext; if ( (ext = file_name_extension(name)) ) { extdef *d = extdefs; for( ; d->extension; d++ ) { if ( strfeq(d->extension, ext) ) { appendArgList(d->list, name); return TRUE; } } } return FALSE; }
static void compileFile(const char *compiler, arglist *options, const char *cfile) { char ofile[MAXPATHLEN]; char *ext; arglist *args = copyArgList(options); if ( opt_o && nolink ) { strcpy(ofile, out); } else { strcpy(ofile, cfile); if ( (ext = (char *)file_name_extension(ofile)) ) strcpy(ext, EXT_OBJ); } if ( !opt_E ) prependArgList(args, "-c"); #if defined(HOST_OS_WINDOWS) appendArgList(args, "-D__WINDOWS__"); appendArgList(args, "-D_WINDOWS"); #endif appendArgList(args, "-D__SWI_PROLOG__"); if ( !shared ) appendArgList(args, "-D__SWI_EMBEDDED__"); concatArgList(args, "-I", &includedirs); if ( opt_o || !opt_E ) { appendArgList(args, "-o"); appendArgList(args, ofile); } appendArgList(args, cfile); callprog(compiler, args); appendArgList(&ofiles, ofile); if ( !nolink ) appendArgList(&tmpfiles, ofile); freeArgList(args); }
void appendOptions(arglist *args, const char *from) { int sep = *from++; const char *f; char tmp[1024]; while(*from) { f = from; while(*from && *from != sep) from++; if ( from > f ) { strncpy(tmp, f, from-f); tmp[from-f] = '\0'; appendArgList(args, tmp); } if ( *from == sep ) from++; } }
static void parseOptions(int argc, char **argv) { for( ; argc > 0; argc--, argv++ ) { char *opt = argv[0]; if ( dispatchFile(opt) ) continue; if ( streq(opt, "-help") ) /* -help */ { usage(); } else if ( streq(opt, "-v") ) /* -v */ { verbose++; } else if ( streq(opt, "--version") ) /* --version */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); show_version = TRUE; } else if ( streq(opt, "-f") ) /* -f */ { fake++; } else if ( streq(opt, "-c") ) /* -c */ { nolink++; } else if ( streq(opt, "-S") ) /* -S */ { nolink++; appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( streq(opt, "-E") ) /* -E */ { nolink++; opt_E = TRUE; appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( streq(opt, "-g") ) /* -g */ { appendArgList(&coptions, OPT_DEBUG); appendArgList(&cppoptions, OPT_DEBUG); #if defined(HOST_TOOLCHAIN_MSC) /* MSVC DEBUG OPTIONS */ appendArgList(&coptions, "/ZI"); appendArgList(&coptions, "/Od"); appendArgList(&cppoptions, "/ZI"); appendArgList(&cppoptions, "/Od"); #endif appendArgList(&ldoptions, OPT_DEBUG); #ifdef LIB_PL_DEBUG pllib = LIB_PL_DEBUG; #endif } else if ( strprefix(opt, "-pg") ) /* -pg* */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( streq(opt, "-g3") ) /* -g3 */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( strprefix(opt, "gdwarf-") ) /* -gdwarf-* */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( strprefix(opt, "-O") ) /* -O* */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( strprefix(opt, "-Wl,") ) /* -Wl,* */ { appendArgList(&ldoptions, opt); } else if ( strprefix(opt, "-W") ) /* -W* */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( streq(opt, "-build-defaults") ) /* -build-defaults */ { build_defaults = TRUE; } else if ( streq(opt, "-nostate") ) /* -nostate */ { nostate = TRUE; } else if ( streq(opt, "-state") ) /* -state */ { nostate = FALSE; } else if ( streq(opt, "-nolibswipl") ) /* -nolibswipl */ { nolibswipl = TRUE; } else if ( streq(opt, "-dll") || /* -dll */ streq(opt, "-embed-shared") ) /* -embed-shared */ { embed_shared = TRUE; #if defined(HOST_TOOLCHAIN_MSC) appendArgList(&ldoptions, "/DLL"); #else #ifdef SO_pic appendArgList(&coptions, SO_pic); appendArgList(&cppoptions, SO_pic); #endif #endif } else if ( streq(opt, "-shared") ) /* -shared */ { shared = TRUE; nostate = TRUE; #ifdef SO_pic appendArgList(&coptions, SO_pic); appendArgList(&cppoptions, SO_pic); #endif } else if ( streq(opt, "-SHARED") ) /* -SHARED */ { shared = TRUE; nostate = TRUE; #ifdef SO_PIC appendArgList(&coptions, SO_PIC); appendArgList(&cppoptions, SO_PIC); #else #ifdef SO_pic appendArgList(&coptions, SO_pic); appendArgList(&cppoptions, SO_pic); #endif #endif } else if ( streq(opt, "-fpic") ) /* -fpic */ { #ifdef SO_pic appendArgList(&coptions, SO_pic); appendArgList(&cppoptions, SO_pic); #endif } else if ( streq(opt, "-fPIC") ) /* -fPIC */ { #ifdef SO_PIC appendArgList(&coptions, SO_PIC); appendArgList(&cppoptions, SO_PIC); #endif } else if ( streq(opt, "-o") ) /* -o out */ { if ( argc > 1 ) { out = argv[1]; opt_o = TRUE; argc--, argv++; } else usage(); } else if ( streq(opt, "-goal") ) /* -goal goal */ { if ( argc > 1 ) { plgoal = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-toplevel") ) /* -toplevel goal */ { if ( argc > 1 ) { pltoplevel = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-initfile") ) /* -initfile goal */ { if ( argc > 1 ) { plinitfile = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-F") ) /* -F base */ { if ( argc > 1 ) { plsysinit = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-class") ) /* -class runtime,kernel, development */ { if ( argc > 1 ) { plclass = argv[1]; if ( !streq(plclass, "runtime") && !streq(plclass, "kernel") && !streq(plclass, "development") ) usage(); argc--, argv++; } else usage(); } else if ( streq(opt, "-pl") ) /* -pl prolog */ { if ( argc > 1 ) { pl = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-cc") ) /* -cc compiler */ { if ( argc > 1 ) { cc = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-c++") ) /* -c++ compiler */ { if ( argc > 1 ) { cxx = argv[1]; argc--, argv++; } else usage(); } else if ( streq(opt, "-ld") ) /* -ld linker */ { if ( argc > 1 ) { ld = argv[1]; argc--, argv++; } else usage(); } else if ( strprefix(opt, "-cc-options") ) { appendOptions(&coptions, opt+strlen("-cc-options")); appendOptions(&cppoptions, opt+strlen("-cc-options")); } else if ( strprefix(opt, "-ld-options") ) { appendOptions(&ldoptions, opt+strlen("-ld-options")); } else if ( strprefix(opt, "-pl-options") ) { appendOptions(&ploptions, opt+strlen("-pl-options")); } else if ( strprefix(opt, "-I") ) /* -I<include> */ { appendArgList(&includedirs, &opt[2]); } else if ( strprefix(opt, "-D") ) /* -D<def> */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( strprefix(opt, "-U") ) /* -U<def> */ { appendArgList(&coptions, opt); appendArgList(&cppoptions, opt); } else if ( strprefix(opt, "-L") ) /* -L<libdir> */ { appendArgList(&libdirs, &opt[2]); } else if ( streq(opt, "-lccmalloc") ) /* -lccmalloc */ { appendArgList(&lastlibs, opt); } else if ( strprefix(opt, "-l") ) /* -l<lib> */ { appendArgList(&libs, opt); } } }
int main(int argc, char **argv) { int special; plld = argv[0]; argc--; argv++; catchSignals(); if ( argc == 0 ) { fprintf(stderr, "No input files. Use %s -help.\n", plld); exit(0); } putenv("PLLD=true"); /* for subprograms */ verbose = FALSE; if ( argc > 2 && streq(argv[0], "-pl") ) special = 2; else special = 0; /* swipl-ld [-pl x] -v: verbose */ if ( argc-special == 1 && streq(argv[special], "-v") ) { arglist coptions; int i; memset(&coptions, 0, sizeof(coptions)); for(i=special; i < argc; i++) appendArgList(&coptions, argv[i]); callprog(PROG_CC, &coptions); return 0; } parseOptions(argc, argv); defaultProgram(&pl, PROG_PL); if ( build_defaults ) { nostate = TRUE; /* not needed and Prolog won't run */ defaultProgram(&cc, C_CC); #ifdef PLBASE defaultPath(&plbase, PLBASE); #else defaultPath(&plbase, PLHOME); #endif defaultPath(&plarch, PLARCH); defaultProgram(&pllib, C_PLLIB); addOptionString(C_LIBS); appendArgList(&ldoptions, C_LDFLAGS); appendArgList(&coptions, C_CFLAGS); appendArgList(&cppoptions, C_CFLAGS); #ifdef SO_EXT soext = strdup(SO_EXT); #endif #ifdef O_PLMT ensureOption(&coptions, "-D_REENTRANT"); ensureOption(&cppoptions, "-D_REENTRANT"); #ifdef _THREAD_SAFE /* FreeBSD */ ensureOption(&coptions, "-D_THREAD_SAFE"); ensureOption(&cppoptions, "-D_THREAD_SAFE"); #endif #endif } else { getPrologOptions(); } fillDefaultOptions(); if ( show_version ) { callprog(cc, &coptions); exit(0); } compileObjectFiles(); if ( !nolink ) { if ( shared ) linkSharedObject(); else { linkBaseExecutable(); if ( !nostate ) { createSavedState(); createOutput(); } } } removeTempFiles(); return 0; }
void linkSharedObject() { char soname[MAXPATHLEN]; char *soout; if ( !soext ) soext = SO_EXT; if ( file_name_extension(out) ) { soout = out; } else { soout = replaceExtension(out, soext, soname); } #if defined(HOST_TOOLCHAIN_MSC) prependArgList(&ldoptions, "/dll"); { char tmp[MAXPATHLEN]; sprintf(tmp, "/out:%s", soout); prependArgList(&ldoptions, tmp); } concatArgList(&ldoptions, "", &ofiles); /* object files */ exportlibdirs(); if ( !nolibswipl ) { appendArgList(&ldoptions, pllib); /* swipl.lib */ addOptionString(pllibs); } concatArgList(&ldoptions, "", &libs); /* libraries */ concatArgList(&ldoptions, "", &lastlibs); /* libraries */ #else /* !defined(HOST_TOOLCHAIN_MSC) */ #ifdef __CYGWIN__ prependArgList(&ldoptions, SO_LDFLAGS); prependArgList(&ldoptions, soout); prependArgList(&ldoptions, "-o"); /* -o ctmp */ concatArgList(&ldoptions, "", &ofiles); /* object files */ if ( !nolibswipl ) { appendArgList(&ldoptions, pllib); /* -lswipl */ addOptionString(pllibs); } concatArgList(&ldoptions, "-L", &libdirs); /* library directories */ concatArgList(&ldoptions, "", &libs); /* libraries */ concatArgList(&ldoptions, "", &lastlibs); /* libraries */ #else /*__CYGWIN__*/ #ifdef SO_FORMAT_LDFLAGS /* must specify output too */ { char tmp[MAXPATHLEN]; tmp[0] = UNQUOTED; sprintf(&tmp[1], SO_FORMAT_LDFLAGS); prependArgList(&ldoptions, tmp); } #else prependArgList(&ldoptions, SO_LDFLAGS); prependArgList(&ldoptions, soout); prependArgList(&ldoptions, "-o"); /* -o ctmp */ #endif /*SO_FORMAT_LDFLAGS*/ concatArgList(&ldoptions, "", &ofiles); /* object files */ concatArgList(&ldoptions, "-L", &libdirs); /* library directories */ concatArgList(&ldoptions, "", &libs); /* libraries */ #ifdef O_SHARED_KERNEL if ( !nolibswipl ) #endif { appendArgList(&ldoptions, pllib); /* -lswipl */ } concatArgList(&ldoptions, "", &lastlibs); /* libraries */ #ifdef __BEOS__ appendArgList(&ldoptions, plexe); /* last is executable */ #endif #endif /*__CYGWIN__*/ #endif /* !defined(HOST_TOOLCHAIN_MSC) */ callprog(ld, &ldoptions); }
/******************************* * PROLOG OPTIONS * *******************************/ static void getPrologOptions() { FILE *fd; char cmd[512]; sprintf(cmd, "%s --dump-runtime-variables", pl); if ( verbose ) printf("\teval `%s`\n", cmd); if ( (fd = popen(cmd, "r")) ) { char buf[256]; while( fgets(buf, sizeof(buf), fd) ) { char name[100]; char value[256]; char *v; if ( sscanf(buf, "%[^=]=%[^;\n]", name, value) == 2 ) { v = value; if ( *v == '"' ) { char *e = ++v; while(*e && *e != '"') e++; while(e>v && isspace(CTOI(e[-1]))) e--; *e = '\0'; } if ( streq(name, "CC") ) defaultProgram(&cc, v); else if ( streq(name, "PLBASE") ) defaultPath(&plbase, v); else if ( streq(name, "PLARCH") ) defaultPath(&plarch, v); else if ( streq(name, "PLLIBS") ) /* Always required. */ pllibs = strdup(v); else if ( streq(name, "PLLIB") ) defaultProgram(&pllib, v); else if ( streq(name, "PLLDFLAGS") ) appendArgList(&ldoptions, v); else if ( streq(name, "PLCFLAGS") ) { appendArgList(&coptions, v); appendArgList(&cppoptions, v); } else if ( streq(name, "PLSOEXT") ) soext = strdup(v); else if ( streq(name, "PLTHREADS") && streq(v, "yes") ) { ensureOption(&coptions, "-D_REENTRANT"); ensureOption(&cppoptions, "-D_REENTRANT"); #ifdef _THREAD_SAFE /* FreeBSD */ ensureOption(&coptions, "-D_THREAD_SAFE"); ensureOption(&cppoptions, "-D_THREAD_SAFE"); #endif } else continue; if ( verbose ) fprintf(stderr, "\t\t%s=\"%s\"\n", name, v); } else { fprintf(stderr, "Unparsed Prolog option: %s\n", buf); } } pclose(fd); #if defined(__WINDOWS__) && defined(HOST_OS_WINDOWS) sprintf(buf, "%s/bin/%s", plbase, PROG_PL); #else sprintf(buf, "%s/bin/%s/%s", plbase, plarch, PROG_PL); #endif defaultPath(&plexe, buf); } else { fprintf(stderr, "%s: failed to run %s: %s", plld, cmd, oserror()); error(1); } }