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 
00227 #if defined(__GLIBC__)
00228 
00232      {
00233         char* bypassVar = (char*) malloc(1024*sizeof(char));
00234         if (bypassVar != NULL)
00235         {
00236            snprintf(bypassVar,1024*sizeof(char), "__PASSTHROUGH_LD_ASSUME_KERNEL_%d", getppid());
00237            bypassVar[1023] = '\0';
00238            if (getenv(bypassVar) != NULL)
00239            {
00240               char* bypassVal = (char*) malloc(1024*sizeof(char));
00241               if (bypassVal != NULL)
00242               {
00243                  rpmMessage(RPMMESS_DEBUG, _("Restoring LD_ASSUME_KERNEL for child scripts.\n"));
00244                  snprintf(bypassVal, 1024*sizeof(char), "%s", getenv(bypassVar));
00245                  unsetenv(bypassVar);
00246                  snprintf(bypassVar, 1024*sizeof(char), "LD_ASSUME_KERNEL=%s", bypassVal);
00247                  bypassVar[1023] = '\0';
00248                  putenv(bypassVar);
00249                  free(bypassVal);
00250               }
00251               else
00252               {
00253                  free(bypassVar);
00254               }
00255            }
00256         }
00257      }
00258 #endif
00259 
00260 /*@-boundsread@*/
00261         (void) execvp(argv[0], (char *const *)argv);
00262 /*@=boundsread@*/
00263 
00264         rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
00265                 scriptName, name, strerror(errno));
00266 
00267         _exit(-1);
00268     }
00269 
00270     rc = waitpid(child, &status, 0);
00271 
00272     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
00273         rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
00274                  scriptName, name);
00275         rc = RPMERR_SCRIPT;
00276     } else
00277         rc = 0;
00278     
00279 exit:
00280     if (scriptName) {
00281         if (!rc)
00282             (void) Unlink(scriptName);
00283         scriptName = _free(scriptName);
00284     }
00285     if (u != NULL) {
00286         switch (u->urltype) {
00287         case URL_IS_FTP:
00288         case URL_IS_HTTP:
00289 if (_build_debug)
00290 fprintf(stderr, "*** delMacros\n");
00291             delMacro(spec->macros, "_remsh");
00292             delMacro(spec->macros, "_remhost");
00293             if (strcmp(rootDir, "/"))
00294                 delMacro(spec->macros, "_remroot");
00295             break;
00296         default:
00297             break;
00298         }
00299     }
00300     argv = _free(argv);
00301     buildCmd = _free(buildCmd);
00302     buildTemplate = _free(buildTemplate);
00303     buildPost = _free(buildPost);
00304     buildDirURL = _free(buildDirURL);
00305 
00306     return rc;
00307 }
00308 
00309 int buildSpec(rpmts ts, Spec spec, int what, int test)
00310 {
00311     int rc = 0;
00312 
00313     if (!spec->recursing && spec->BACount) {
00314         int x;
00315         /* When iterating over BANames, do the source    */
00316         /* packaging on the first run, and skip RMSOURCE altogether */
00317         if (spec->BASpecs != NULL)
00318         for (x = 0; x < spec->BACount; x++) {
00319 /*@-boundsread@*/
00320             if ((rc = buildSpec(ts, spec->BASpecs[x],
00321                                 (what & ~RPMBUILD_RMSOURCE) |
00322                                 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
00323                                 test))) {
00324                 goto exit;
00325             }
00326 /*@=boundsread@*/
00327         }
00328     } else {
00329         if ((what & RPMBUILD_PREP) &&
00330             (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
00331                 goto exit;
00332 
00333         if ((what & RPMBUILD_BUILD) &&
00334             (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
00335                 goto exit;
00336 
00337         if ((what & RPMBUILD_INSTALL) &&
00338             (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
00339                 goto exit;
00340 
00341         if ((what & RPMBUILD_CHECK) &&
00342             (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
00343                 goto exit;
00344 
00345         if ((what & RPMBUILD_PACKAGESOURCE) &&
00346             (rc = processSourceFiles(spec)))
00347                 goto exit;
00348 
00349         if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
00350             (what & RPMBUILD_FILECHECK)) &&
00351             (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
00352                 goto exit;
00353 
00354         if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
00355             (rc = packageSources(spec)))
00356                 return rc;
00357 
00358         if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
00359             (rc = packageBinaries(spec)))
00360                 goto exit;
00361         
00362         if ((what & RPMBUILD_CLEAN) &&
00363             (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
00364                 goto exit;
00365 
00366         if ((what & RPMBUILD_RMBUILD) &&
00367             (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
00368                 goto exit;
00369     }
00370 
00371     if (what & RPMBUILD_RMSOURCE)
00372         doRmSource(spec);
00373 
00374     if (what & RPMBUILD_RMSPEC)
00375         (void) Unlink(spec->specFile);
00376 
00377 exit:
00378     if (rc && rpmlogGetNrecs() > 0) {
00379         rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
00380         rpmlogPrint(NULL);
00381     }
00382 
00383     return rc;
00384 }

Generated on Sat Jun 10 14:25:35 2006 for rpm by doxygen 1.3.5