build/build.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <rpmio_internal.h>
00009 #include <rpmbuild.h>
00010 
00011 #include "debug.h"
00012 
00013 /*@unchecked@*/
00014 static int _build_debug = 0;
00015 
00016 /*@access StringBuf @*/
00017 /*@access urlinfo @*/           /* XXX compared with NULL */
00018 /*@access FD_t @*/
00019 
00022 static void doRmSource(Spec spec)
00023         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00024         /*@modifies rpmGlobalMacroContext, fileSystem, internalState  @*/
00025 {
00026     struct Source *sp;
00027     int rc;
00028     
00029 #if 0
00030     rc = Unlink(spec->specFile);
00031 #endif
00032 
00033     for (sp = spec->sources; sp != NULL; sp = sp->next) {
00034         const char *dn, *fn;
00035         if (sp->flags & RPMFILE_GHOST)
00036             continue;
00037         if (sp->flags & RPMFILE_SOURCE)
00038             dn = "%{_sourcedir}/";
00039         else if (sp->flags & RPMFILE_PATCH)
00040             dn = "%{_patchdir}/";
00041         else if (sp->flags & RPMFILE_ICON)
00042             dn = "%{_icondir}/";
00043         else
00044             continue;
00045         fn = rpmGenPath(NULL, dn, sp->source);
00046         rc = Unlink(fn);
00047         fn = _free(fn);
00048     }
00049 }
00050 
00051 /*
00052  * @todo Single use by %%doc in files.c prevents static.
00053  */
00054 int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
00055 {
00056     const char * rootURL = spec->rootURL;
00057     const char * rootDir;
00058     const char *scriptName = NULL;
00059     const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
00060     const char * buildScript;
00061     const char * buildCmd = NULL;
00062     const char * buildTemplate = NULL;
00063     const char * buildPost = NULL;
00064     const char * mTemplate = NULL;
00065     const char * mCmd = NULL;
00066     const char * mPost = NULL;
00067     int argc = 0;
00068     const char **argv = NULL;
00069     FILE * fp = NULL;
00070     urlinfo u = NULL;
00071 
00072     FD_t fd;
00073     FD_t xfd;
00074     int child;
00075     int status;
00076     int rc;
00077     
00078     /*@-branchstate@*/
00079     switch (what) {
00080     case RPMBUILD_PREP:
00081         name = "%prep";
00082         sb = spec->prep;
00083         mTemplate = "%{__spec_prep_template}";
00084         mPost = "%{__spec_prep_post}";
00085         mCmd = "%{__spec_prep_cmd}";
00086         break;
00087     case RPMBUILD_BUILD:
00088         name = "%build";
00089         sb = spec->build;
00090         mTemplate = "%{__spec_build_template}";
00091         mPost = "%{__spec_build_post}";
00092         mCmd = "%{__spec_build_cmd}";
00093         break;
00094     case RPMBUILD_INSTALL:
00095         name = "%install";
00096         sb = spec->install;
00097         mTemplate = "%{__spec_install_template}";
00098         mPost = "%{__spec_install_post}";
00099         mCmd = "%{__spec_install_cmd}";
00100         break;
00101     case RPMBUILD_CHECK:
00102         name = "%check";
00103         sb = spec->check;
00104         mTemplate = "%{__spec_check_template}";
00105         mPost = "%{__spec_check_post}";
00106         mCmd = "%{__spec_check_cmd}";
00107         break;
00108     case RPMBUILD_CLEAN:
00109         name = "%clean";
00110         sb = spec->clean;
00111         mTemplate = "%{__spec_clean_template}";
00112         mPost = "%{__spec_clean_post}";
00113         mCmd = "%{__spec_clean_cmd}";
00114         break;
00115     case RPMBUILD_RMBUILD:
00116         name = "--clean";
00117         mTemplate = "%{__spec_clean_template}";
00118         mPost = "%{__spec_clean_post}";
00119         mCmd = "%{__spec_clean_cmd}";
00120         break;
00121     case RPMBUILD_STRINGBUF:
00122     default:
00123         mTemplate = "%{___build_template}";
00124         mPost = "%{___build_post}";
00125         mCmd = "%{___build_cmd}";
00126         break;
00127     }
00128     if (name == NULL)   /* XXX shouldn't happen */
00129         name = "???";
00130     /*@=branchstate@*/
00131 
00132     if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
00133         rc = 0;
00134         goto exit;
00135     }
00136     
00137     if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
00138         rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
00139         rc = RPMERR_SCRIPT;
00140         goto exit;
00141     }
00142 
00143 #ifdef HAVE_FCHMOD
00144     switch (rootut) {
00145     case URL_IS_PATH:
00146     case URL_IS_UNKNOWN:
00147         (void)fchmod(Fileno(fd), 0600);
00148         break;
00149     default:
00150         break;
00151     }
00152 #endif
00153 
00154     /*@-branchstate@*/
00155     if (fdGetFp(fd) == NULL)
00156         xfd = Fdopen(fd, "w.fpio");
00157     else
00158         xfd = fd;
00159     /*@=branchstate@*/
00160 
00161     /*@-type@*/ /* FIX: cast? */
00162     if ((fp = fdGetFp(xfd)) == NULL) {
00163         rc = RPMERR_SCRIPT;
00164         goto exit;
00165     }
00166     /*@=type@*/
00167     
00168     (void) urlPath(rootURL, &rootDir);
00169     /*@-branchstate@*/
00170     if (*rootDir == '\0') rootDir = "/";
00171     /*@=branchstate@*/
00172 
00173     (void) urlPath(scriptName, &buildScript);
00174 
00175     buildTemplate = rpmExpand(mTemplate, NULL);
00176     buildPost = rpmExpand(mPost, NULL);
00177 
00178     (void) fputs(buildTemplate, fp);
00179 
00180     if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
00181         fprintf(fp, "cd '%s'\n", spec->buildSubdir);
00182 
00183     if (what == RPMBUILD_RMBUILD) {
00184         if (spec->buildSubdir)
00185             fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
00186     } else if (sb != NULL)
00187         fprintf(fp, "%s", getStringBuf(sb));
00188 
00189     (void) fputs(buildPost, fp);
00190     
00191     (void) Fclose(xfd);
00192 
00193     if (test) {
00194         rc = 0;
00195         goto exit;
00196     }
00197     
00198 if (_build_debug)
00199 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
00200 /*@-boundsread@*/
00201     if (buildDirURL && buildDirURL[0] != '/' &&
00202         (urlSplit(buildDirURL, &u) != 0)) {
00203         rc = RPMERR_SCRIPT;
00204         goto exit;
00205     }
00206 /*@=boundsread@*/
00207     if (u != NULL) {
00208         switch (u->urltype) {
00209         case URL_IS_HTTPS:
00210         case URL_IS_HTTP:
00211         case URL_IS_FTP:
00212 if (_build_debug)
00213 fprintf(stderr, "*** addMacros\n");
00214             addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
00215             addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
00216             if (strcmp(rootDir, "/"))
00217                 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
00218             break;
00219         case URL_IS_UNKNOWN:
00220         case URL_IS_DASH:
00221         case URL_IS_PATH:
00222         case URL_IS_HKP:
00223         default:
00224             break;
00225         }
00226     }
00227 
00228     buildCmd = rpmExpand(mCmd, " ", buildScript, NULL);
00229     (void) poptParseArgvString(buildCmd, &argc, &argv);
00230 
00231     rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
00232     if (!(child = fork())) {
00233 
00234         /*@-mods@*/
00235         errno = 0;
00236         /*@=mods@*/
00237 /*@-boundsread@*/
00238         (void) execvp(argv[0], (char *const *)argv);
00239 /*@=boundsread@*/
00240 
00241         rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
00242                 scriptName, name, strerror(errno));
00243 
00244         _exit(-1);
00245     }
00246 
00247     rc = waitpid(child, &status, 0);
00248 
00249     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
00250         rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
00251                  scriptName, name);
00252         rc = RPMERR_SCRIPT;
00253     } else
00254         rc = 0;
00255     
00256 exit:
00257     if (scriptName) {
00258         if (!rc)
00259             (void) Unlink(scriptName);
00260         scriptName = _free(scriptName);
00261     }
00262     if (u != NULL) {
00263         switch (u->urltype) {
00264         case URL_IS_HTTPS:
00265         case URL_IS_HTTP:
00266         case URL_IS_FTP:
00267 if (_build_debug)
00268 fprintf(stderr, "*** delMacros\n");
00269             delMacro(spec->macros, "_remsh");
00270             delMacro(spec->macros, "_remhost");
00271             if (strcmp(rootDir, "/"))
00272                 delMacro(spec->macros, "_remroot");
00273             break;
00274         case URL_IS_UNKNOWN:
00275         case URL_IS_DASH:
00276         case URL_IS_PATH:
00277         case URL_IS_HKP:
00278         default:
00279             break;
00280         }
00281     }
00282     argv = _free(argv);
00283     buildCmd = _free(buildCmd);
00284     buildTemplate = _free(buildTemplate);
00285     buildPost = _free(buildPost);
00286     buildDirURL = _free(buildDirURL);
00287 
00288     return rc;
00289 }
00290 
00291 int buildSpec(rpmts ts, Spec spec, int what, int test)
00292 {
00293     int rc = 0;
00294 
00295     if (!spec->recursing && spec->BACount) {
00296         int x;
00297         /* When iterating over BANames, do the source    */
00298         /* packaging on the first run, and skip RMSOURCE altogether */
00299         if (spec->BASpecs != NULL)
00300         for (x = 0; x < spec->BACount; x++) {
00301 /*@-boundsread@*/
00302             if ((rc = buildSpec(ts, spec->BASpecs[x],
00303                                 (what & ~RPMBUILD_RMSOURCE) |
00304                                 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
00305                                 test))) {
00306                 goto exit;
00307             }
00308 /*@=boundsread@*/
00309         }
00310     } else {
00311         if ((what & RPMBUILD_PREP) &&
00312             (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
00313                 goto exit;
00314 
00315         if ((what & RPMBUILD_BUILD) &&
00316             (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
00317                 goto exit;
00318 
00319         if ((what & RPMBUILD_INSTALL) &&
00320             (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
00321                 goto exit;
00322 
00323         if ((what & RPMBUILD_CHECK) &&
00324             (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
00325                 goto exit;
00326 
00327         if ((what & RPMBUILD_PACKAGESOURCE) &&
00328             (rc = processSourceFiles(spec)))
00329                 goto exit;
00330 
00331         if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
00332             (what & RPMBUILD_FILECHECK)) &&
00333             (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
00334                 goto exit;
00335 
00336         if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
00337             (rc = packageSources(spec)))
00338                 return rc;
00339 
00340         if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
00341             (rc = packageBinaries(spec)))
00342                 goto exit;
00343         
00344         if ((what & RPMBUILD_CLEAN) &&
00345             (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
00346                 goto exit;
00347 
00348         if ((what & RPMBUILD_RMBUILD) &&
00349             (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
00350                 goto exit;
00351     }
00352 
00353     if (what & RPMBUILD_RMSOURCE)
00354         doRmSource(spec);
00355 
00356     if (what & RPMBUILD_RMSPEC)
00357         (void) Unlink(spec->specFile);
00358 
00359 exit:
00360     if (rc && rpmlogGetNrecs() > 0) {
00361         rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
00362         rpmlogPrint(NULL);
00363     }
00364 
00365     return rc;
00366 }

Generated on Fri May 25 21:18:11 2007 for rpm by  doxygen 1.5.2