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 
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 h_errno, 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         case ':':
00087             checking = 0;
00088             /*@switchbreak@*/ break;
00089 /*@-boundsread@*/
00090         default:
00091             if (checking && !(isprint(*s) || isspace(*s))) return 0;
00092             /*@switchbreak@*/ break;
00093 /*@=boundsread@*/
00094         }
00095     }
00096     return 1;
00097 }
00098 
00101 /*@-boundswrite@*/
00102 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
00103         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00104         /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/
00105 {
00106     const char * passPhrase = ba->passPhrase;
00107     const char * cookie = ba->cookie;
00108     int buildAmount = ba->buildAmount;
00109     const char * buildRootURL = NULL;
00110     const char * specFile;
00111     const char * specURL;
00112     int specut;
00113     char buf[BUFSIZ];
00114     Spec spec = NULL;
00115     int rc;
00116 
00117 #ifndef DYING
00118     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
00119 #endif
00120 
00121     /*@-branchstate@*/
00122     if (ba->buildRootOverride)
00123         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
00124     /*@=branchstate@*/
00125 
00126     /*@-compmempass@*/ /* FIX: static zcmds heartburn */
00127     if (ba->buildMode == 't') {
00128         FILE *fp;
00129         const char * specDir;
00130         char * tmpSpecFile;
00131         char * cmd, * s;
00132         rpmCompressedMagic res = COMPRESSED_OTHER;
00133         /*@observer@*/ static const char *zcmds[] =
00134                 { "cat", "gunzip", "bunzip2", "cat" };
00135 
00136         specDir = rpmGetPath("%{_specdir}", NULL);
00137 
00138         tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
00139 #if defined(HAVE_MKSTEMP)
00140         (void) close(mkstemp(tmpSpecFile));
00141 #else
00142         (void) mktemp(tmpSpecFile);
00143 #endif
00144 
00145         (void) isCompressed(arg, &res);
00146 
00147         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
00148         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
00149                         zcmds[res & 0x3], arg, tmpSpecFile);
00150         if (!(fp = popen(cmd, "r"))) {
00151             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00152             specDir = _free(specDir);
00153             tmpSpecFile = _free(tmpSpecFile);
00154             return 1;
00155         }
00156         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
00157             /* Try again */
00158             (void) pclose(fp);
00159 
00160             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
00161                     zcmds[res & 0x3], arg, tmpSpecFile);
00162             if (!(fp = popen(cmd, "r"))) {
00163                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00164                 specDir = _free(specDir);
00165                 tmpSpecFile = _free(tmpSpecFile);
00166                 return 1;
00167             }
00168             if (!fgets(buf, sizeof(buf) - 1, fp)) {
00169                 /* Give up */
00170                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
00171                         arg);
00172                 (void) unlink(tmpSpecFile);
00173                 specDir = _free(specDir);
00174                 tmpSpecFile = _free(tmpSpecFile);
00175                 return 1;
00176             }
00177         }
00178         (void) pclose(fp);
00179 
00180         cmd = s = buf;
00181         while (*cmd != '\0') {
00182             if (*cmd == '/') s = cmd + 1;
00183             cmd++;
00184         }
00185 
00186         cmd = s;
00187 
00188         /* remove trailing \n */
00189         s = cmd + strlen(cmd) - 1;
00190         *s = '\0';
00191 
00192         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
00193         sprintf(s, "%s/%s", specDir, cmd);
00194         res = rename(tmpSpecFile, s);
00195         specDir = _free(specDir);
00196         
00197         if (res) {
00198             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
00199                         tmpSpecFile, s);
00200             (void) unlink(tmpSpecFile);
00201             tmpSpecFile = _free(tmpSpecFile);
00202             return 1;
00203         }
00204         tmpSpecFile = _free(tmpSpecFile);
00205 
00206         /* Make the directory which contains the tarball the source 
00207            directory for this run */
00208 
00209         if (*arg != '/') {
00210             (void)getcwd(buf, BUFSIZ);
00211             strcat(buf, "/");
00212             strcat(buf, arg);
00213         } else 
00214             strcpy(buf, arg);
00215 
00216         cmd = buf + strlen(buf) - 1;
00217         while (*cmd != '/') cmd--;
00218         *cmd = '\0';
00219 
00220         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
00221     } else {
00222         specURL = arg;
00223     }
00224     /*@=compmempass@*/
00225 
00226     specut = urlPath(specURL, &specFile);
00227     if (*specFile != '/') {
00228         char *s = alloca(BUFSIZ);
00229         (void)getcwd(s, BUFSIZ);
00230         strcat(s, "/");
00231         strcat(s, arg);
00232         specURL = s;
00233     }
00234 
00235     if (specut != URL_IS_DASH) {
00236         struct stat st;
00237         if (Stat(specURL, &st) < 0) {
00238             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00239             rc = 1;
00240             goto exit;
00241         }
00242         if (! S_ISREG(st.st_mode)) {
00243             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00244                 specURL);
00245             rc = 1;
00246             goto exit;
00247         }
00248 
00249         /* Try to verify that the file is actually a specfile */
00250         if (!isSpecFile(specURL)) {
00251             rpmError(RPMERR_BADSPEC,
00252                 _("File %s does not appear to be a specfile.\n"), specURL);
00253             rc = 1;
00254             goto exit;
00255         }
00256     }
00257     
00258     /* Parse the spec file */
00259 #define _anyarch(_f)    \
00260 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00261     if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00262                 cookie, _anyarch(buildAmount), ba->force))
00263     {
00264         rc = 1;
00265         goto exit;
00266     }
00267 #undef  _anyarch
00268     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
00269         rc = 1;
00270         goto exit;
00271     }
00272 
00273     /* Assemble source header from parsed components */
00274     initSourceHeader(spec);
00275 
00276     /* Check build prerequisites */
00277     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
00278         rc = 1;
00279         goto exit;
00280     }
00281 
00282     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
00283         rc = 1;
00284         goto exit;
00285     }
00286     
00287     if (ba->buildMode == 't')
00288         (void) Unlink(specURL);
00289     rc = 0;
00290 
00291 exit:
00292     spec = freeSpec(spec);
00293     buildRootURL = _free(buildRootURL);
00294     return rc;
00295 }
00296 /*@=boundswrite@*/
00297 
00298 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
00299 {
00300     char *t, *te;
00301     int rc = 0;
00302     char * targets = ba->targets;
00303 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00304     int cleanFlags = ba->buildAmount & buildCleanMask;
00305     rpmVSFlags vsflags, ovsflags;
00306 
00307     vsflags = rpmExpandNumeric("%{_vsflags_build}");
00308     if (ba->qva_flags & VERIFY_DIGEST)
00309         vsflags |= _RPMVSF_NODIGESTS;
00310     if (ba->qva_flags & VERIFY_SIGNATURE)
00311         vsflags |= _RPMVSF_NOSIGNATURES;
00312     if (ba->qva_flags & VERIFY_HDRCHK)
00313         vsflags |= RPMVSF_NOHDRCHK;
00314     ovsflags = rpmtsSetVSFlags(ts, vsflags);
00315 
00316     if (targets == NULL) {
00317         rc =  buildForTarget(ts, arg, ba);
00318         goto exit;
00319     }
00320 
00321     /* parse up the build operators */
00322 
00323     printf(_("Building target platforms: %s\n"), targets);
00324 
00325     ba->buildAmount &= ~buildCleanMask;
00326     for (t = targets; *t != '\0'; t = te) {
00327         char *target;
00328         if ((te = strchr(t, ',')) == NULL)
00329             te = t + strlen(t);
00330         target = alloca(te-t+1);
00331         strncpy(target, t, (te-t));
00332         target[te-t] = '\0';
00333         if (*te != '\0')
00334             te++;
00335         else    /* XXX Perform clean-up after last target build. */
00336             ba->buildAmount |= cleanFlags;
00337 
00338         printf(_("Building for target %s\n"), target);
00339 
00340         /* Read in configuration for target. */
00341         rpmFreeMacros(NULL);
00342         (void) rpmReadConfigFiles(rcfile, target);
00343         rc = buildForTarget(ts, arg, ba);
00344         if (rc)
00345             break;
00346     }
00347 
00348 exit:
00349     vsflags = rpmtsSetVSFlags(ts, ovsflags);
00350     /* Restore original configuration. */
00351     rpmFreeMacros(NULL);
00352     (void) rpmReadConfigFiles(rcfile, NULL);
00353 
00354     return rc;
00355 }

Generated on Fri Feb 24 08:33:28 2006 for rpm by  doxygen 1.4.5