Main Page Modules Data Structures File List Data Fields Globals Related Pages

# build.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #include <rpmcli.h>
    00008 #include <rpmbuild.h>
    00009
    00010 #include "rpmps.h"
    00011 #include "rpmte.h"
    00012 #include "rpmts.h"
    00013
    00014 #include "build.h"
    00015 #include "debug.h"
    00016
    00017 /*@access rpmts @*/     /* XXX compared with NULL @*/
    00018 /*@access rpmdb @*/             /* XXX compared with NULL @*/
    00019 /*@access FD_t @*/              /* XXX compared with NULL @*/
    00020
    00023 static int checkSpec(rpmts ts, Header h)
    00024         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
    00025         /*@modifies ts, h, rpmGlobalMacroContext, fileSystem, internalState @*/
    00026 {
    00027     rpmps ps;
    00028     int rc;
    00029
    00030     if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
    00031      && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
    00032         return 0;
    00033
    00034     rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
    00035
    00036     rc = rpmtsCheck(ts);
    00037
    00038     ps = rpmtsProblems(ts);
    00039     if (rc == 0 && rpmpsNumProblems(ps) > 0) {
    00040         rpmMessage(RPMMESS_ERROR, _("Failed build dependencies:\n"));
    00041         rpmpsPrint(NULL, ps);
    00042         rc = 1;
    00043     }
    00044     ps = rpmpsFree(ps);
    00045
    00046     /* XXX nuke the added package. */
    00047     rpmtsClean(ts);
    00048
    00049     return rc;
    00050 }
    00051
    00052 /*
    00053  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
    00054  * angielsku...
    00055  */
    00056 /* XXX this is still a dumb test but at least it's i18n aware */
    00059 static int isSpecFile(const char * specfile)
    00060         /*@globals fileSystem, internalState @*/
    00061         /*@modifies fileSystem, internalState @*/
    00062 {
    00063     char buf[256];
    00064     const char * s;
    00065     FD_t fd;
    00066     int count;
    00067     int checking;
    00068
    00069     fd = Fopen(specfile, "r.ufdio");
    00070     if (fd == NULL || Ferror(fd)) {
    00071         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
    00072                 specfile, Fstrerror(fd));
    00073         return 0;
    00074     }
    00075     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
    00076     (void) Fclose(fd);
    00077
    00078     checking = 1;
    00079     for (s = buf; count--; s++) {
    00080         switch (*s) {
    00081         case '\r':
    00082         case '\n':
    00083             checking = 1;
    00084             /*@switchbreak@*/ break;
    00085         case ':':
    00086             checking = 0;
    00087             /*@switchbreak@*/ break;
    00088 /*@-boundsread@*/
    00089         default:
    00090             if (checking && !(isprint(*s) || isspace(*s))) return 0;
    00091             /*@switchbreak@*/ break;
    00092 /*@=boundsread@*/
    00093         }
    00094     }
    00095     return 1;
    00096 }
    00097
    00100 /*@-boundswrite@*/
    00101 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
    00102         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
    00103         /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/
    00104 {
    00105     const char * passPhrase = ba->passPhrase;
    00106     const char * cookie = ba->cookie;
    00107     int buildAmount = ba->buildAmount;
    00108     const char * buildRootURL = NULL;
    00109     const char * specFile;
    00110     const char * specURL;
    00111     int specut;
    00112     char buf[BUFSIZ];
    00113     Spec spec = NULL;
    00114     int rc;
    00115
    00116 #ifndef DYING
    00117     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
    00118 #endif
    00119
    00120     /*@-branchstate@*/
    00121     if (ba->buildRootOverride)
    00122         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
    00123     /*@=branchstate@*/
    00124
    00125     /*@-compmempass@*/ /* FIX: static zcmds heartburn */
    00126     if (ba->buildMode == 't') {
    00127         FILE *fp;
    00128         const char * specDir;
    00129         const char * tmpSpecFile;
    00130         char * cmd, * s;
    00131         rpmCompressedMagic res = COMPRESSED_OTHER;
    00132         /*@observer@*/ static const char *zcmds[] =
    00133                 { "cat", "gunzip", "bunzip2", "cat" };
    00134
    00135         specDir = rpmGetPath("%{_specdir}", NULL);
    00136
    00137         /* XXX Using mkstemp is difficult here. */
    00138         /* XXX FWIW, default %{_specdir} is root.root 0755 */
    00139         {   char tfn[64];
    00140             strcpy(tfn, "rpm-spec.XXXXXX");
    00141             /*@-unrecog@*/
    00142             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
    00143             /*@=unrecog@*/
    00144         }
    00145
    00146         (void) isCompressed(arg, &res);
    00147
    00148         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
    00149         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
    00150                         zcmds[res & 0x3], arg, tmpSpecFile);
    00151         if (!(fp = popen(cmd, "r"))) {
    00152             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
    00153             specDir = _free(specDir);
    00154             tmpSpecFile = _free(tmpSpecFile);
    00155             return 1;
    00156         }
    00157         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
    00158             /* Try again */
    00159             (void) pclose(fp);
    00160
    00161             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
    00162                     zcmds[res & 0x3], arg, tmpSpecFile);
    00163             if (!(fp = popen(cmd, "r"))) {
    00164                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
    00165                 specDir = _free(specDir);
    00166                 tmpSpecFile = _free(tmpSpecFile);
    00167                 return 1;
    00168             }
    00169             if (!fgets(buf, sizeof(buf) - 1, fp)) {
    00170                 /* Give up */
    00171                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
    00172                         arg);
    00173                 (void) unlink(tmpSpecFile);
    00174                 specDir = _free(specDir);
    00175                 tmpSpecFile = _free(tmpSpecFile);
    00176                 return 1;
    00177             }
    00178         }
    00179         (void) pclose(fp);
    00180
    00181         cmd = s = buf;
    00182         while (*cmd != '\0') {
    00183             if (*cmd == '/') s = cmd + 1;
    00184             cmd++;
    00185         }
    00186
    00187         cmd = s;
    00188
    00189         /* remove trailing \n */
    00190         s = cmd + strlen(cmd) - 1;
    00191         *s = '\0';
    00192
    00193         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
    00194         sprintf(s, "%s/%s", specDir, cmd);
    00195         res = rename(tmpSpecFile, s);
    00196         specDir = _free(specDir);
    00197
    00198         if (res) {
    00199             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
    00200                         tmpSpecFile, s);
    00201             (void) unlink(tmpSpecFile);
    00202             tmpSpecFile = _free(tmpSpecFile);
    00203             return 1;
    00204         }
    00205         tmpSpecFile = _free(tmpSpecFile);
    00206
    00207         /* Make the directory which contains the tarball the source
    00208            directory for this run */
    00209
    00210         if (*arg != '/') {
    00211             (void)getcwd(buf, BUFSIZ);
    00212             strcat(buf, "/");
    00213             strcat(buf, arg);
    00214         } else
    00215             strcpy(buf, arg);
    00216
    00217         cmd = buf + strlen(buf) - 1;
    00218         while (*cmd != '/') cmd--;
    00219         *cmd = '\0';
    00220
    00221         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
    00222     } else {
    00223         specURL = arg;
    00224     }
    00225     /*@=compmempass@*/
    00226
    00227     specut = urlPath(specURL, &specFile);
    00228     if (*specFile != '/') {
    00229         char *s = alloca(BUFSIZ);
    00230         (void)getcwd(s, BUFSIZ);
    00231         strcat(s, "/");
    00232         strcat(s, arg);
    00233         specURL = s;
    00234     }
    00235
    00236     if (specut != URL_IS_DASH) {
    00237         struct stat st;
    00238         if (Stat(specURL, &st) < 0) {
    00239             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
    00240             rc = 1;
    00241             goto exit;
    00242         }
    00243         if (! S_ISREG(st.st_mode)) {
    00244             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
    00245                 specURL);
    00246             rc = 1;
    00247             goto exit;
    00248         }
    00249
    00250         /* Try to verify that the file is actually a specfile */
    00251         if (!isSpecFile(specURL)) {
    00252             rpmError(RPMERR_BADSPEC,
    00253                 _("File %s does not appear to be a specfile.\n"), specURL);
    00254             rc = 1;
    00255             goto exit;
    00256         }
    00257     }
    00258
    00259     /* Parse the spec file */
    00260 #define _anyarch(_f)    \
    00261 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
    00262     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
    00263                 cookie, _anyarch(buildAmount), ba->force)) {
    00264         rc = 1;
    00265         goto exit;
    00266     }
    00267 #undef  _anyarch
    00268
    00269     /* Assemble source header from parsed components */
    00270     initSourceHeader(spec);
    00271
    00272     /* Check build prerequisites */
    00273     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
    00274         rc = 1;
    00275         goto exit;
    00276     }
    00277
    00278     if (buildSpec(spec, buildAmount, ba->noBuild)) {
    00279         rc = 1;
    00280         goto exit;
    00281     }
    00282
    00283     if (ba->buildMode == 't')
    00284         (void) Unlink(specURL);
    00285     rc = 0;
    00286
    00287 exit:
    00288     spec = freeSpec(spec);
    00289     buildRootURL = _free(buildRootURL);
    00290     return rc;
    00291 }
    00292 /*@=boundswrite@*/
    00293
    00294 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
    00295 {
    00296     char *t, *te;
    00297     int rc = 0;
    00298     char * targets = ba->targets;
    00299 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
    00300     int cleanFlags = ba->buildAmount & buildCleanMask;
    00301     rpmVSFlags vsflags, ovsflags;
    00302
    00303     vsflags = rpmExpandNumeric("%{_vsflags_build}");
    00304     if (ba->qva_flags & VERIFY_DIGEST)
    00305         vsflags |= _RPMVSF_NODIGESTS;
    00306     if (ba->qva_flags & VERIFY_SIGNATURE)
    00307         vsflags |= _RPMVSF_NOSIGNATURES;
    00308     if (ba->qva_flags & VERIFY_HDRCHK)
    00309         vsflags |= RPMVSF_NOHDRCHK;
    00310     ovsflags = rpmtsSetVSFlags(ts, vsflags);
    00311
    00312     if (targets == NULL) {
    00313         rc =  buildForTarget(ts, arg, ba);
    00314         goto exit;
    00315     }
    00316
    00317     /* parse up the build operators */
    00318
    00319     printf(_("Building target platforms: %s\n"), targets);
    00320
    00321     ba->buildAmount &= ~buildCleanMask;
    00322     for (t = targets; *t != '\0'; t = te) {
    00323         char *target;
    00324         if ((te = strchr(t, ',')) == NULL)
    00325             te = t + strlen(t);
    00326         target = alloca(te-t+1);
    00327         strncpy(target, t, (te-t));
    00328         target[te-t] = '\0';
    00329         if (*te != '\0')
    00330             te++;
    00331         else    /* XXX Perform clean-up after last target build. */
    00332             ba->buildAmount |= cleanFlags;
    00333
    00334         printf(_("Building for target %s\n"), target);
    00335
    00336         /* Read in configuration for target. */
    00337         rpmFreeMacros(NULL);
    00338         (void) rpmReadConfigFiles(rcfile, target);
    00339         rc = buildForTarget(ts, arg, ba);
    00340         if (rc)
    00341             break;
    00342     }
    00343
    00344 exit:
    00345     vsflags = rpmtsSetVSFlags(ts, ovsflags);
    00346     /* Restore original configuration. */
    00347     rpmFreeMacros(NULL);
    00348     (void) rpmReadConfigFiles(rcfile, NULL);
    00349
    00350     return rc;
    00351 }