3 * Top-level build dispatcher.
8 #include <rpmio_internal.h>
10 #include "misc.h" /* XXX for makeTempFile */
14 static int _build_debug = 0;
18 static void doRmSource(rpmSpec spec)
25 rc = Unlink(spec->specFile);
28 for (p = spec->sources; p != NULL; p = p->next) {
29 if (! (p->flags & RPMBUILD_ISNO)) {
30 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
36 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
37 for (p = pkg->icon; p != NULL; p = p->next) {
38 if (! (p->flags & RPMBUILD_ISNO)) {
39 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
48 * @todo Single use by %%doc in files.c prevents static.
50 int doScript(rpmSpec spec, int what, const char *name, StringBuf sb, int test)
52 const char * rootURL = spec->rootURL;
54 const char *scriptName = NULL;
55 const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
56 const char * buildScript;
57 const char * buildCmd = NULL;
58 const char * buildTemplate = NULL;
59 const char * buildPost = NULL;
60 const char * mTemplate = NULL;
61 const char * mCmd = NULL;
62 const char * mPost = NULL;
64 const char **argv = NULL;
78 mTemplate = "%{__spec_prep_template}";
79 mPost = "%{__spec_prep_post}";
80 mCmd = "%{__spec_prep_cmd}";
85 mTemplate = "%{__spec_build_template}";
86 mPost = "%{__spec_build_post}";
87 mCmd = "%{__spec_build_cmd}";
89 case RPMBUILD_INSTALL:
92 mTemplate = "%{__spec_install_template}";
93 mPost = "%{__spec_install_post}";
94 mCmd = "%{__spec_install_cmd}";
99 mTemplate = "%{__spec_check_template}";
100 mPost = "%{__spec_check_post}";
101 mCmd = "%{__spec_check_cmd}";
106 mTemplate = "%{__spec_clean_template}";
107 mPost = "%{__spec_clean_post}";
108 mCmd = "%{__spec_clean_cmd}";
110 case RPMBUILD_RMBUILD:
112 mTemplate = "%{__spec_clean_template}";
113 mPost = "%{__spec_clean_post}";
114 mCmd = "%{__spec_clean_cmd}";
116 case RPMBUILD_STRINGBUF:
118 mTemplate = "%{___build_template}";
119 mPost = "%{___build_post}";
120 mCmd = "%{___build_cmd}";
123 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 rpmlog(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
141 (void)fchmod(Fileno(fd), 0600);
148 if (fdGetFp(fd) == NULL)
149 xfd = Fdopen(fd, "w.fpio");
154 if ((fp = fdGetFp(xfd)) == NULL) {
159 (void) urlPath(rootURL, &rootDir);
160 if (*rootDir == '\0') rootDir = "/";
162 (void) urlPath(scriptName, &buildScript);
164 buildTemplate = rpmExpand(mTemplate, NULL);
165 buildPost = rpmExpand(mPost, NULL);
167 (void) fputs(buildTemplate, fp);
169 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
170 fprintf(fp, "cd '%s'\n", spec->buildSubdir);
172 if (what == RPMBUILD_RMBUILD) {
173 if (spec->buildSubdir)
174 fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
175 } else if (sb != NULL)
176 fprintf(fp, "%s", getStringBuf(sb));
178 (void) fputs(buildPost, fp);
188 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
189 if (buildDirURL && buildDirURL[0] != '/' &&
190 (urlSplit(buildDirURL, &u) != 0)) {
195 switch (u->urltype) {
200 fprintf(stderr, "*** addMacros\n");
201 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
202 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
203 if (strcmp(rootDir, "/"))
204 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
215 buildCmd = rpmExpand(mCmd, " ", buildScript, NULL);
216 (void) poptParseArgvString(buildCmd, &argc, &argv);
218 rpmlog(RPMLOG_NOTICE, _("Executing(%s): %s\n"), name, buildCmd);
219 if (!(child = fork())) {
222 (void) execvp(argv[0], (char *const *)argv);
224 rpmlog(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
225 scriptName, name, strerror(errno));
230 rc = waitpid(child, &status, 0);
232 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
233 rpmlog(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
242 (void) Unlink(scriptName);
243 scriptName = _free(scriptName);
246 switch (u->urltype) {
251 fprintf(stderr, "*** delMacros\n");
252 delMacro(spec->macros, "_remsh");
253 delMacro(spec->macros, "_remhost");
254 if (strcmp(rootDir, "/"))
255 delMacro(spec->macros, "_remroot");
267 buildCmd = _free(buildCmd);
268 buildTemplate = _free(buildTemplate);
269 buildPost = _free(buildPost);
270 buildDirURL = _free(buildDirURL);
275 int buildSpec(rpmts ts, rpmSpec spec, int what, int test)
279 if (!spec->recursing && spec->BACount) {
281 /* When iterating over BANames, do the source */
282 /* packaging on the first run, and skip RMSOURCE altogether */
283 if (spec->BASpecs != NULL)
284 for (x = 0; x < spec->BACount; x++) {
285 if ((rc = buildSpec(ts, spec->BASpecs[x],
286 (what & ~RPMBUILD_RMSOURCE) |
287 (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 rpmlog(RPMLOG_NOTICE, _("\n\nRPM build errors:\n"));