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         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, 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         const 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         /* XXX Using mkstemp is difficult here. */
00139         /* XXX FWIW, default %{_specdir} is root.root 0755 */
00140         {   char tfn[64];
00141             strcpy(tfn, "rpm-spec.XXXXXX");
00142             /*@-unrecog@*/
00143             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
00144             /*@=unrecog@*/
00145         }
00146 
00147         (void) isCompressed(arg, &res);
00148 
00149         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
00150         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
00151                         zcmds[res & 0x3], arg, tmpSpecFile);
00152         if (!(fp = popen(cmd, "r"))) {
00153             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00154             specDir = _free(specDir);
00155             tmpSpecFile = _free(tmpSpecFile);
00156             return 1;
00157         }
00158         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
00159             /* Try again */
00160             (void) pclose(fp);
00161 
00162             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
00163                     zcmds[res & 0x3], arg, tmpSpecFile);
00164             if (!(fp = popen(cmd, "r"))) {
00165                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
00166                 specDir = _free(specDir);
00167                 tmpSpecFile = _free(tmpSpecFile);
00168                 return 1;
00169             }
00170             if (!fgets(buf, sizeof(buf) - 1, fp)) {
00171                 /* Give up */
00172                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
00173                         arg);
00174                 (void) unlink(tmpSpecFile);
00175                 specDir = _free(specDir);
00176                 tmpSpecFile = _free(tmpSpecFile);
00177                 return 1;
00178             }
00179         }
00180         (void) pclose(fp);
00181 
00182         cmd = s = buf;
00183         while (*cmd != '\0') {
00184             if (*cmd == '/') s = cmd + 1;
00185             cmd++;
00186         }
00187 
00188         cmd = s;
00189 
00190         /* remove trailing \n */
00191         s = cmd + strlen(cmd) - 1;
00192         *s = '\0';
00193 
00194         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
00195         sprintf(s, "%s/%s", specDir, cmd);
00196         res = rename(tmpSpecFile, s);
00197         specDir = _free(specDir);
00198         
00199         if (res) {
00200             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
00201                         tmpSpecFile, s);
00202             (void) unlink(tmpSpecFile);
00203             tmpSpecFile = _free(tmpSpecFile);
00204             return 1;
00205         }
00206         tmpSpecFile = _free(tmpSpecFile);
00207 
00208         /* Make the directory which contains the tarball the source 
00209            directory for this run */
00210 
00211         if (*arg != '/') {
00212             (void)getcwd(buf, BUFSIZ);
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         (void)getcwd(s, BUFSIZ);
00232         strcat(s, "/");
00233         strcat(s, arg);
00234         specURL = s;
00235     }
00236 
00237     if (specut != URL_IS_DASH) {
00238         struct stat st;
00239         if (Stat(specURL, &st) < 0) {
00240             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
00241             rc = 1;
00242             goto exit;
00243         }
00244         if (! S_ISREG(st.st_mode)) {
00245             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
00246                 specURL);
00247             rc = 1;
00248             goto exit;
00249         }
00250 
00251         /* Try to verify that the file is actually a specfile */
00252         if (!isSpecFile(specURL)) {
00253             rpmError(RPMERR_BADSPEC,
00254                 _("File %s does not appear to be a specfile.\n"), specURL);
00255             rc = 1;
00256             goto exit;
00257         }
00258     }
00259     
00260     /* Parse the spec file */
00261 #define _anyarch(_f)    \
00262 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
00263     if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
00264                 cookie, _anyarch(buildAmount), ba->force))
00265     {
00266         rc = 1;
00267         goto exit;
00268     }
00269 #undef  _anyarch
00270     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
00271         rc = 1;
00272         goto exit;
00273     }
00274 
00275     /* Assemble source header from parsed components */
00276     initSourceHeader(spec);
00277 
00278     /* Check build prerequisites */
00279     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
00280         rc = 1;
00281         goto exit;
00282     }
00283 
00284     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
00285         rc = 1;
00286         goto exit;
00287     }
00288     
00289     if (ba->buildMode == 't')
00290         (void) Unlink(specURL);
00291     rc = 0;
00292 
00293 exit:
00294     spec = freeSpec(spec);
00295     buildRootURL = _free(buildRootURL);
00296     return rc;
00297 }
00298 /*@=boundswrite@*/
00299 
00300 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
00301 {
00302     char *t, *te;
00303     int rc = 0;
00304     char * targets = ba->targets;
00305 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
00306     int cleanFlags = ba->buildAmount & buildCleanMask;
00307     rpmVSFlags vsflags, ovsflags;
00308 
00309     vsflags = rpmExpandNumeric("%{_vsflags_build}");
00310     if (ba->qva_flags & VERIFY_DIGEST)
00311         vsflags |= _RPMVSF_NODIGESTS;
00312     if (ba->qva_flags & VERIFY_SIGNATURE)
00313         vsflags |= _RPMVSF_NOSIGNATURES;
00314     if (ba->qva_flags & VERIFY_HDRCHK)
00315         vsflags |= RPMVSF_NOHDRCHK;
00316     ovsflags = rpmtsSetVSFlags(ts, vsflags);
00317 
00318     if (targets == NULL) {
00319         rc =  buildForTarget(ts, arg, ba);
00320         goto exit;
00321     }
00322 
00323     /* parse up the build operators */
00324 
00325     printf(_("Building target platforms: %s\n"), targets);
00326 
00327     ba->buildAmount &= ~buildCleanMask;
00328     for (t = targets; *t != '\0'; t = te) {
00329         char *target;
00330         if ((te = strchr(t, ',')) == NULL)
00331             te = t + strlen(t);
00332         target = alloca(te-t+1);
00333         strncpy(target, t, (te-t));
00334         target[te-t] = '\0';
00335         if (*te != '\0')
00336             te++;
00337         else    /* XXX Perform clean-up after last target build. */
00338             ba->buildAmount |= cleanFlags;
00339 
00340         printf(_("Building for target %s\n"), target);
00341 
00342         /* Read in configuration for target. */
00343         rpmFreeMacros(NULL);
00344         (void) rpmReadConfigFiles(rcfile, target);
00345         rc = buildForTarget(ts, arg, ba);
00346         if (rc)
00347             break;
00348     }
00349 
00350 exit:
00351     vsflags = rpmtsSetVSFlags(ts, ovsflags);
00352     /* Restore original configuration. */
00353     rpmFreeMacros(NULL);
00354     (void) rpmReadConfigFiles(rcfile, NULL);
00355 
00356     return rc;
00357 }

Generated on Thu Aug 5 04:57:50 2004 for rpm by doxygen 1.3.4