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, h_errno, 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 
00054 static int isSpecFile(const char * specfile)
00055         /*@globals h_errno, fileSystem, internalState @*/
00056         /*@modifies fileSystem, internalState @*/
00057 {
00058     char buf[256];
00059     const char * s;
00060     FD_t fd;
00061     int count;
00062     int checking;
00063 
00064     fd = Fopen(specfile, "r.ufdio");
00065     if (fd == NULL || Ferror(fd)) {
00066         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
00067                 specfile, Fstrerror(fd));
00068         return 0;
00069     }
00070     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
00071     (void) Fclose(fd);
00072 
00073     checking = 1;
00074     for (s = buf; count--; s++) {
00075         switch (*s) {
00076         case '\r':
00077         case '\n':
00078             checking = 1;
00079             /*@switchbreak@*/ break;
00080         case ':':
00081             checking = 0;
00082             /*@switchbreak@*/ break;
00083 /*@-boundsread@*/
00084         default:
00085 #if 0
00086             if (checking && !(isprint(*s) || isspace(*s))) return 0;
00087             /*@switchbreak@*/ break;
00088 #else
00089             if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
00090             /*@switchbreak@*/ break;
00091 #endif
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, h_errno, 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         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         tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
00138 #if defined(HAVE_MKSTEMP)
00139         (void) close(mkstemp(tmpSpecFile));
00140 #else
00141         (void) mktemp(tmpSpecFile);
00142 #endif
00143 
00144         (void) isCompressed(arg, &res);
00145 
00146         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
00147         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
00148                         zcmds[res & 0x3], arg, tmpSpecFile);
00149         if (!(fp = popen(cmd, "r"))) {
00150             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00151             specDir = _free(specDir);
00152             tmpSpecFile = _free(tmpSpecFile);
00153             return 1;
00154         }
00155         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
00156             /* Try again */
00157             (void) pclose(fp);
00158 
00159             sprintf(cmd, "%s < %s | tar xOvf - --wildcards \\*.spec 2>&1 > %s",
00160                     zcmds[res & 0x3], arg, tmpSpecFile);
00161             if (!(fp = popen(cmd, "r"))) {
00162                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00163                 specDir = _free(specDir);
00164                 tmpSpecFile = _free(tmpSpecFile);
00165                 return 1;
00166             }
00167             if (!fgets(buf, sizeof(buf) - 1, fp)) {
00168                 /* Give up */
00169                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
00170                         arg);
00171                 (void) unlink(tmpSpecFile);
00172                 specDir = _free(specDir);
00173                 tmpSpecFile = _free(tmpSpecFile);
00174                 return 1;
00175             }
00176         }
00177         (void) pclose(fp);
00178 
00179         cmd = s = buf;
00180         while (*cmd != '\0') {
00181             if (*cmd == '/') s = cmd + 1;
00182             cmd++;
00183         }
00184 
00185         cmd = s;
00186 
00187         /* remove trailing \n */
00188         s = cmd + strlen(cmd) - 1;
00189         *s = '\0';
00190 
00191         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
00192         sprintf(s, "%s/%s", specDir, cmd);
00193         res = rename(tmpSpecFile, s);
00194         specDir = _free(specDir);
00195         
00196         if (res) {
00197             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
00198                         tmpSpecFile, s);
00199             (void) unlink(tmpSpecFile);
00200             tmpSpecFile = _free(tmpSpecFile);
00201             return 1;
00202         }
00203         tmpSpecFile = _free(tmpSpecFile);
00204 
00205         /* Make the directory which contains the tarball the source 
00206            directory for this run */
00207 
00208         if (*arg != '/') {
00209             if (!getcwd(buf, BUFSIZ)) {
00210                 rpmError(RPMERR_STAT, "getcwd failed: %m\n");
00211                 return 1;
00212             }
00213             strcat(buf, "/");
00214             strcat(buf, arg);
00215         } else 
00216             strcpy(buf, arg);
00217 
00218         cmd = buf + strlen(buf) - 1;
00219         while (*cmd != '/') cmd--;
00220         *cmd = '\0';
00221 
00222         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
00223     } else {
00224         specURL = arg;
00225     }
00226     /*@=compmempass@*/
00227 
00228     specut = urlPath(specURL, &specFile);
00229     if (*specFile != '/') {
00230         char *s = alloca(BUFSIZ);
00231         if (!getcwd(s, BUFSIZ)) {
00232             rpmError(RPMERR_STAT, "getcwd failed: %m\n");
00233             rc = 1;
00234             goto exit;
00235         }
00236         strcat(s, "/");
00237         strcat(s, arg);
00238         specURL = s;
00239     }
00240 
00241     if (specut != URL_IS_DASH) {
00242         struct stat st;
00243         if (Stat(specURL, &st) < 0) {
00244             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00245             rc = 1;
00246             goto exit;
00247         }
00248         if (! S_ISREG(st.st_mode)) {
00249             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00250                 specURL);
00251             rc = 1;
00252             goto exit;
00253         }
00254 
00255         /* Try to verify that the file is actually a specfile */
00256         if (!isSpecFile(specURL)) {
00257             rpmError(RPMERR_BADSPEC,
00258                 _("File %s does not appear to be a specfile.\n"), specURL);
00259             rc = 1;
00260             goto exit;
00261         }
00262     }
00263     
00264     /* Parse the spec file */
00265 #define _anyarch(_f)    \
00266 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00267     if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00268                 cookie, _anyarch(buildAmount), ba->force))
00269     {
00270         rc = 1;
00271         goto exit;
00272     }
00273 #undef  _anyarch
00274     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
00275         rc = 1;
00276         goto exit;
00277     }
00278 
00279     /* Assemble source header from parsed components */
00280     initSourceHeader(spec);
00281 
00282     /* Check build prerequisites */
00283     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
00284         rc = 1;
00285         goto exit;
00286     }
00287 
00288     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
00289         rc = 1;
00290         goto exit;
00291     }
00292     
00293     if (ba->buildMode == 't')
00294         (void) Unlink(specURL);
00295     rc = 0;
00296 
00297 exit:
00298     spec = freeSpec(spec);
00299     buildRootURL = _free(buildRootURL);
00300     return rc;
00301 }
00302 /*@=boundswrite@*/
00303 
00304 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
00305 {
00306     char *t, *te;
00307     int rc = 0;
00308     char * targets = ba->targets;
00309 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00310     int cleanFlags = ba->buildAmount & buildCleanMask;
00311     rpmVSFlags vsflags, ovsflags;
00312 
00313     vsflags = rpmExpandNumeric("%{_vsflags_build}");
00314     if (ba->qva_flags & VERIFY_DIGEST)
00315         vsflags |= _RPMVSF_NODIGESTS;
00316     if (ba->qva_flags & VERIFY_SIGNATURE)
00317         vsflags |= _RPMVSF_NOSIGNATURES;
00318     if (ba->qva_flags & VERIFY_HDRCHK)
00319         vsflags |= RPMVSF_NOHDRCHK;
00320     ovsflags = rpmtsSetVSFlags(ts, vsflags);
00321 
00322     if (targets == NULL) {
00323         rc =  buildForTarget(ts, arg, ba);
00324         goto exit;
00325     }
00326 
00327     /* parse up the build operators */
00328 
00329     printf(_("Building target platforms: %s\n"), targets);
00330 
00331     ba->buildAmount &= ~buildCleanMask;
00332     for (t = targets; *t != '\0'; t = te) {
00333         char *target;
00334         if ((te = strchr(t, ',')) == NULL)
00335             te = t + strlen(t);
00336         target = alloca(te-t+1);
00337         strncpy(target, t, (te-t));
00338         target[te-t] = '\0';
00339         if (*te != '\0')
00340             te++;
00341         else    /* XXX Perform clean-up after last target build. */
00342             ba->buildAmount |= cleanFlags;
00343 
00344         printf(_("Building for target %s\n"), target);
00345 
00346         /* Read in configuration for target. */
00347         rpmFreeMacros(NULL);
00348         rpmFreeRpmrc();
00349         (void) rpmReadConfigFiles(rcfile, target);
00350         rc = buildForTarget(ts, arg, ba);
00351         if (rc)
00352             break;
00353     }
00354 
00355 exit:
00356     vsflags = rpmtsSetVSFlags(ts, ovsflags);
00357     /* Restore original configuration. */
00358     rpmFreeMacros(NULL);
00359     rpmFreeRpmrc();
00360     (void) rpmReadConfigFiles(rcfile, NULL);
00361 
00362     return rc;
00363 }

Generated on Fri Oct 12 08:44:53 2007 for rpm by  doxygen 1.5.2