3 * Top-level build dispatcher.
8 #include <rpmio_internal.h>
14 static int _build_debug = 0;
16 /*@access StringBuf @*/
17 /*@access urlinfo @*/ /* XXX compared with NULL */
22 static void doRmSource(Spec spec)
23 /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
24 /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
31 rc = Unlink(spec->specFile);
34 for (p = spec->sources; p != NULL; p = p->next) {
35 if (! (p->flags & RPMBUILD_ISNO)) {
36 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
42 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
43 for (p = pkg->icon; p != NULL; p = p->next) {
44 if (! (p->flags & RPMBUILD_ISNO)) {
45 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
54 * @todo Single use by %%doc in files.c prevents static.
56 int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
58 const char * rootURL = spec->rootURL;
60 const char *scriptName = NULL;
61 const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
62 const char * buildScript;
63 const char * buildCmd = NULL;
64 const char * buildTemplate = NULL;
65 const char * buildPost = NULL;
66 const char * mTemplate = NULL;
67 const char * mPost = NULL;
69 const char **argv = NULL;
84 mTemplate = "%{__spec_prep_template}";
85 mPost = "%{__spec_prep_post}";
90 mTemplate = "%{__spec_build_template}";
91 mPost = "%{__spec_build_post}";
93 case RPMBUILD_INSTALL:
96 mTemplate = "%{__spec_install_template}";
97 mPost = "%{__spec_install_post}";
102 mTemplate = "%{__spec_check_template}";
103 mPost = "%{__spec_check_post}";
108 mTemplate = "%{__spec_clean_template}";
109 mPost = "%{__spec_clean_post}";
111 case RPMBUILD_RMBUILD:
113 mTemplate = "%{__spec_clean_template}";
114 mPost = "%{__spec_clean_post}";
116 case RPMBUILD_STRINGBUF:
118 mTemplate = "%{___build_template}";
119 mPost = "%{___build_post}";
122 if (name == NULL) /* XXX shouldn't happen */
126 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
131 if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
132 rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
141 (void)fchmod(Fileno(fd), 0600);
149 if (fdGetFp(fd) == NULL)
150 xfd = Fdopen(fd, "w.fpio");
155 /*@-type@*/ /* FIX: cast? */
156 if ((fp = fdGetFp(xfd)) == NULL) {
162 (void) urlPath(rootURL, &rootDir);
164 if (*rootDir == '\0') rootDir = "/";
167 (void) urlPath(scriptName, &buildScript);
169 buildTemplate = rpmExpand(mTemplate, NULL);
170 buildPost = rpmExpand(mPost, NULL);
172 (void) fputs(buildTemplate, fp);
174 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
175 fprintf(fp, "cd %s\n", spec->buildSubdir);
177 if (what == RPMBUILD_RMBUILD) {
178 if (spec->buildSubdir)
179 fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
180 } else if (sb != NULL)
181 fprintf(fp, "%s", getStringBuf(sb));
183 (void) fputs(buildPost, fp);
193 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
195 if (buildDirURL && buildDirURL[0] != '/' &&
196 (urlSplit(buildDirURL, &u) != 0)) {
202 switch (u->urltype) {
207 fprintf(stderr, "*** addMacros\n");
208 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
209 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
210 if (strcmp(rootDir, "/"))
211 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
218 buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
219 (void) poptParseArgvString(buildCmd, &argc, &argv);
221 rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
222 if (!(child = fork())) {
228 (void) execvp(argv[0], (char *const *)argv);
231 rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
232 scriptName, name, strerror(errno));
237 rc = waitpid(child, &status, 0);
239 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
240 rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
249 (void) Unlink(scriptName);
250 scriptName = _free(scriptName);
253 switch (u->urltype) {
258 fprintf(stderr, "*** delMacros\n");
259 delMacro(spec->macros, "_remsh");
260 delMacro(spec->macros, "_remhost");
261 if (strcmp(rootDir, "/"))
262 delMacro(spec->macros, "_remroot");
269 buildCmd = _free(buildCmd);
270 buildTemplate = _free(buildTemplate);
271 buildPost = _free(buildPost);
272 buildDirURL = _free(buildDirURL);
277 int buildSpec(rpmts ts, Spec spec, int what, int test)
281 if (!spec->recursing && spec->BACount) {
283 /* When iterating over BANames, do the source */
284 /* packaging on the first run, and skip RMSOURCE altogether */
285 if (spec->BASpecs != NULL)
286 for (x = 0; x < spec->BACount; x++) {
288 if ((rc = buildSpec(ts, spec->BASpecs[x],
289 (what & ~RPMBUILD_RMSOURCE) |
290 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
297 if ((what & RPMBUILD_PREP) &&
298 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
301 if ((what & RPMBUILD_BUILD) &&
302 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
305 if ((what & RPMBUILD_INSTALL) &&
306 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
309 if ((what & RPMBUILD_CHECK) &&
310 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
313 if ((what & RPMBUILD_PACKAGESOURCE) &&
314 (rc = processSourceFiles(spec)))
317 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
318 (what & RPMBUILD_FILECHECK)) &&
319 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
322 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
323 (rc = packageSources(spec)))
326 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
327 (rc = packageBinaries(spec)))
330 if ((what & RPMBUILD_CLEAN) &&
331 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
334 if ((what & RPMBUILD_RMBUILD) &&
335 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
339 if (what & RPMBUILD_RMSOURCE)
342 if (what & RPMBUILD_RMSPEC)
343 (void) Unlink(spec->specFile);
346 if (rc && rpmlogGetNrecs() > 0) {
347 rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));