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

Generated at Sun Apr 8 18:42:58 2001 for rpm by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000