Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

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

Generated on Mon Sep 13 15:05:57 2004 for rpm by doxygen 1.3.5