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

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