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, 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}";
124 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
129 if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
130 rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
139 (void)fchmod(Fileno(fd), 0600);
147 if (fdGetFp(fd) == NULL)
148 xfd = Fdopen(fd, "w.fpio");
153 /*@-type@*/ /* FIX: cast? */
154 if ((fp = fdGetFp(xfd)) == NULL) {
160 (void) urlPath(rootURL, &rootDir);
162 if (*rootDir == '\0') rootDir = "/";
165 (void) urlPath(scriptName, &buildScript);
167 buildTemplate = rpmExpand(mTemplate, NULL);
168 buildPost = rpmExpand(mPost, NULL);
170 (void) fputs(buildTemplate, fp);
172 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
173 fprintf(fp, "cd %s\n", spec->buildSubdir);
175 if (what == RPMBUILD_RMBUILD) {
176 if (spec->buildSubdir)
177 fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
179 fprintf(fp, "%s", getStringBuf(sb));
181 (void) fputs(buildPost, fp);
191 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
193 if (buildDirURL && buildDirURL[0] != '/' &&
194 (urlSplit(buildDirURL, &u) != 0)) {
200 switch (u->urltype) {
203 fprintf(stderr, "*** addMacros\n");
204 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
205 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
206 if (strcmp(rootDir, "/"))
207 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
215 buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
216 (void) poptParseArgvString(buildCmd, &argc, &argv);
218 rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
219 if (!(child = fork())) {
225 (void) execvp(argv[0], (char *const *)argv);
228 rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
229 scriptName, name, strerror(errno));
234 rc = waitpid(child, &status, 0);
236 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
237 rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
246 (void) Unlink(scriptName);
247 scriptName = _free(scriptName);
250 switch (u->urltype) {
254 fprintf(stderr, "*** delMacros\n");
255 delMacro(spec->macros, "_remsh");
256 delMacro(spec->macros, "_remhost");
257 if (strcmp(rootDir, "/"))
258 delMacro(spec->macros, "_remroot");
265 buildCmd = _free(buildCmd);
266 buildTemplate = _free(buildTemplate);
267 buildPost = _free(buildPost);
268 buildDirURL = _free(buildDirURL);
273 int buildSpec(rpmts ts, Spec spec, int what, int test)
277 if (!spec->recursing && spec->BACount) {
279 /* When iterating over BANames, do the source */
280 /* packaging on the first run, and skip RMSOURCE altogether */
281 if (spec->BASpecs != NULL)
282 for (x = 0; x < spec->BACount; x++) {
284 if ((rc = buildSpec(ts, spec->BASpecs[x],
285 (what & ~RPMBUILD_RMSOURCE) |
286 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
293 if ((what & RPMBUILD_PREP) &&
294 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
297 if ((what & RPMBUILD_BUILD) &&
298 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
301 if ((what & RPMBUILD_INSTALL) &&
302 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
305 if ((what & RPMBUILD_CHECK) &&
306 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
309 if ((what & RPMBUILD_PACKAGESOURCE) &&
310 (rc = processSourceFiles(spec)))
313 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
314 (what & RPMBUILD_FILECHECK)) &&
315 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
318 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
319 (rc = packageSources(spec)))
322 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
323 (rc = packageBinaries(spec)))
326 if ((what & RPMBUILD_CLEAN) &&
327 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
330 if ((what & RPMBUILD_RMBUILD) &&
331 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
335 if (what & RPMBUILD_RMSOURCE)
338 if (what & RPMBUILD_RMSPEC)
339 (void) Unlink(spec->specFile);
342 if (rc && rpmlogGetNrecs() > 0) {
343 rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));