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

Generated at Thu Apr 19 15:29:41 2001 for rpm by doxygen1.2.6-20010408 written by Dimitri van Heesch, © 1997-2001