00001
00006 #include "system.h"
00007
00008 #include <rpmio_internal.h>
00009 #include <rpmbuild.h>
00010
00011 #include "debug.h"
00012
00013
00014 static int _build_debug = 0;
00015
00016
00017
00018
00019
00022 static void doRmSource(Spec spec)
00023
00024
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
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
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
00123
00124 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
00125 rc = 0;
00126 goto exit;
00127 }
00128
00129 if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
00130 rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
00131 rc = RPMERR_SCRIPT;
00132 goto exit;
00133 }
00134
00135 #ifdef HAVE_FCHMOD
00136 switch (rootut) {
00137 case URL_IS_PATH:
00138 case URL_IS_UNKNOWN:
00139 (void)fchmod(Fileno(fd), 0600);
00140 break;
00141 default:
00142 break;
00143 }
00144 #endif
00145
00146
00147 if (fdGetFp(fd) == NULL)
00148 xfd = Fdopen(fd, "w.fpio");
00149 else
00150 xfd = fd;
00151
00152
00153
00154 if ((fp = fdGetFp(xfd)) == NULL) {
00155 rc = RPMERR_SCRIPT;
00156 goto exit;
00157 }
00158
00159
00160 (void) urlPath(rootURL, &rootDir);
00161
00162 if (*rootDir == '\0') rootDir = "/";
00163
00164
00165 (void) urlPath(scriptName, &buildScript);
00166
00167 buildTemplate = rpmExpand(mTemplate, NULL);
00168 buildPost = rpmExpand(mPost, NULL);
00169
00170 (void) fputs(buildTemplate, fp);
00171
00172 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
00173 fprintf(fp, "cd %s\n", spec->buildSubdir);
00174
00175 if (what == RPMBUILD_RMBUILD) {
00176 if (spec->buildSubdir)
00177 fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
00178 } else
00179 fprintf(fp, "%s", getStringBuf(sb));
00180
00181 (void) fputs(buildPost, fp);
00182
00183 (void) Fclose(xfd);
00184
00185 if (test) {
00186 rc = 0;
00187 goto exit;
00188 }
00189
00190 if (_build_debug)
00191 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
00192
00193 if (buildDirURL && buildDirURL[0] != '/' &&
00194 (urlSplit(buildDirURL, &u) != 0)) {
00195 rc = RPMERR_SCRIPT;
00196 goto exit;
00197 }
00198
00199 if (u != NULL) {
00200 switch (u->urltype) {
00201 case URL_IS_FTP:
00202 if (_build_debug)
00203 fprintf(stderr, "*** addMacros\n");
00204 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
00205 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
00206 if (strcmp(rootDir, "/"))
00207 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
00208 break;
00209 case URL_IS_HTTP:
00210 default:
00211 break;
00212 }
00213 }
00214
00215 buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
00216 (void) poptParseArgvString(buildCmd, &argc, &argv);
00217
00218 rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
00219 if (!(child = fork())) {
00220
00221
00222 errno = 0;
00223
00224
00225 (void) execvp(argv[0], (char *const *)argv);
00226
00227
00228 rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
00229 scriptName, name, strerror(errno));
00230
00231 _exit(-1);
00232 }
00233
00234 rc = waitpid(child, &status, 0);
00235
00236 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
00237 rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
00238 scriptName, name);
00239 rc = RPMERR_SCRIPT;
00240 } else
00241 rc = 0;
00242
00243 exit:
00244 if (scriptName) {
00245 if (!rc)
00246 (void) Unlink(scriptName);
00247 scriptName = _free(scriptName);
00248 }
00249 if (u != NULL) {
00250 switch (u->urltype) {
00251 case URL_IS_FTP:
00252 case URL_IS_HTTP:
00253 if (_build_debug)
00254 fprintf(stderr, "*** delMacros\n");
00255 delMacro(spec->macros, "_remsh");
00256 delMacro(spec->macros, "_remhost");
00257 if (strcmp(rootDir, "/"))
00258 delMacro(spec->macros, "_remroot");
00259 break;
00260 default:
00261 break;
00262 }
00263 }
00264 argv = _free(argv);
00265 buildCmd = _free(buildCmd);
00266 buildTemplate = _free(buildTemplate);
00267 buildPost = _free(buildPost);
00268 buildDirURL = _free(buildDirURL);
00269
00270 return rc;
00271 }
00272
00273 int buildSpec(rpmts ts, Spec spec, int what, int test)
00274 {
00275 int rc = 0;
00276
00277 if (!spec->recursing && spec->BACount) {
00278 int x;
00279
00280
00281 if (spec->BASpecs != NULL)
00282 for (x = 0; x < spec->BACount; x++) {
00283
00284 if ((rc = buildSpec(ts, spec->BASpecs[x],
00285 (what & ~RPMBUILD_RMSOURCE) |
00286 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
00287 test))) {
00288 goto exit;
00289 }
00290
00291 }
00292 } else {
00293 if ((what & RPMBUILD_PREP) &&
00294 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
00295 goto exit;
00296
00297 if ((what & RPMBUILD_BUILD) &&
00298 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
00299 goto exit;
00300
00301 if ((what & RPMBUILD_INSTALL) &&
00302 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
00303 goto exit;
00304
00305 if ((what & RPMBUILD_CHECK) &&
00306 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
00307 goto exit;
00308
00309 if ((what & RPMBUILD_PACKAGESOURCE) &&
00310 (rc = processSourceFiles(spec)))
00311 goto exit;
00312
00313 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
00314 (what & RPMBUILD_FILECHECK)) &&
00315 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
00316 goto exit;
00317
00318 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
00319 (rc = packageSources(spec)))
00320 return rc;
00321
00322 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
00323 (rc = packageBinaries(spec)))
00324 goto exit;
00325
00326 if ((what & RPMBUILD_CLEAN) &&
00327 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
00328 goto exit;
00329
00330 if ((what & RPMBUILD_RMBUILD) &&
00331 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
00332 goto exit;
00333 }
00334
00335 if (what & RPMBUILD_RMSOURCE)
00336 doRmSource(spec);
00337
00338 if (what & RPMBUILD_RMSPEC)
00339 (void) Unlink(spec->specFile);
00340
00341 exit:
00342 if (rc && rpmlogGetNrecs() > 0) {
00343 rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
00344 rpmlogPrint(NULL);
00345 }
00346
00347 return rc;
00348 }