3 * Top-level build dispatcher.
8 #include <rpmio_internal.h>
11 #include "misc.h" /* XXX for makeTempFile */
15 static int _build_debug = 0;
19 static void doRmSource(rpmSpec spec)
26 rc = Unlink(spec->specFile);
29 for (p = spec->sources; p != NULL; p = p->next) {
30 if (! (p->flags & RPMBUILD_ISNO)) {
31 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
37 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
38 for (p = pkg->icon; p != NULL; p = p->next) {
39 if (! (p->flags & RPMBUILD_ISNO)) {
40 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
49 * @todo Single use by %%doc in files.c prevents static.
51 int doScript(rpmSpec spec, rpmBuildFlags what, const char *name, StringBuf sb, int test)
53 const char * rootURL = spec->rootURL;
55 const char *scriptName = NULL;
56 const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
57 const char * buildScript;
58 const char * buildCmd = NULL;
59 const char * buildTemplate = NULL;
60 const char * buildPost = NULL;
61 const char * mTemplate = NULL;
62 const char * mCmd = NULL;
63 const char * mPost = NULL;
65 const char **argv = NULL;
79 mTemplate = "%{__spec_prep_template}";
80 mPost = "%{__spec_prep_post}";
81 mCmd = "%{__spec_prep_cmd}";
86 mTemplate = "%{__spec_build_template}";
87 mPost = "%{__spec_build_post}";
88 mCmd = "%{__spec_build_cmd}";
90 case RPMBUILD_INSTALL:
93 mTemplate = "%{__spec_install_template}";
94 mPost = "%{__spec_install_post}";
95 mCmd = "%{__spec_install_cmd}";
100 mTemplate = "%{__spec_check_template}";
101 mPost = "%{__spec_check_post}";
102 mCmd = "%{__spec_check_cmd}";
107 mTemplate = "%{__spec_clean_template}";
108 mPost = "%{__spec_clean_post}";
109 mCmd = "%{__spec_clean_cmd}";
111 case RPMBUILD_RMBUILD:
113 mTemplate = "%{__spec_clean_template}";
114 mPost = "%{__spec_clean_post}";
115 mCmd = "%{__spec_clean_cmd}";
117 case RPMBUILD_STRINGBUF:
119 mTemplate = "%{___build_template}";
120 mPost = "%{___build_post}";
121 mCmd = "%{___build_cmd}";
124 if (name == NULL) /* XXX shouldn't happen */
127 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
132 if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
133 rpmlog(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
138 if (fdGetFp(fd) == NULL)
139 xfd = Fdopen(fd, "w.fpio");
144 if ((fp = fdGetFp(xfd)) == NULL) {
149 (void) urlPath(rootURL, &rootDir);
150 if (*rootDir == '\0') rootDir = "/";
152 (void) urlPath(scriptName, &buildScript);
154 buildTemplate = rpmExpand(mTemplate, NULL);
155 buildPost = rpmExpand(mPost, NULL);
157 (void) fputs(buildTemplate, fp);
159 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
160 fprintf(fp, "cd '%s'\n", spec->buildSubdir);
162 if (what == RPMBUILD_RMBUILD) {
163 if (spec->buildSubdir)
164 fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
165 } else if (sb != NULL)
166 fprintf(fp, "%s", getStringBuf(sb));
168 (void) fputs(buildPost, fp);
178 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
179 if (buildDirURL && buildDirURL[0] != '/' &&
180 (urlSplit(buildDirURL, &u) != 0)) {
185 switch (u->urltype) {
190 fprintf(stderr, "*** addMacros\n");
191 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
192 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
193 if (strcmp(rootDir, "/"))
194 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
205 buildCmd = rpmExpand(mCmd, " ", buildScript, NULL);
206 (void) poptParseArgvString(buildCmd, &argc, &argv);
208 rpmlog(RPMLOG_NOTICE, _("Executing(%s): %s\n"), name, buildCmd);
209 if (!(child = fork())) {
212 (void) execvp(argv[0], (char *const *)argv);
214 rpmlog(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
215 scriptName, name, strerror(errno));
220 rc = waitpid(child, &status, 0);
222 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
223 rpmlog(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
232 (void) Unlink(scriptName);
233 scriptName = _free(scriptName);
236 switch (u->urltype) {
241 fprintf(stderr, "*** delMacros\n");
242 delMacro(spec->macros, "_remsh");
243 delMacro(spec->macros, "_remhost");
244 if (strcmp(rootDir, "/"))
245 delMacro(spec->macros, "_remroot");
257 buildCmd = _free(buildCmd);
258 buildTemplate = _free(buildTemplate);
259 buildPost = _free(buildPost);
260 buildDirURL = _free(buildDirURL);
265 int buildSpec(rpmts ts, rpmSpec spec, int what, int test)
269 if (!spec->recursing && spec->BACount) {
271 /* When iterating over BANames, do the source */
272 /* packaging on the first run, and skip RMSOURCE altogether */
273 if (spec->BASpecs != NULL)
274 for (x = 0; x < spec->BACount; x++) {
275 if ((rc = buildSpec(ts, spec->BASpecs[x],
276 (what & ~RPMBUILD_RMSOURCE) |
277 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
283 if ((what & RPMBUILD_PREP) &&
284 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
287 if ((what & RPMBUILD_BUILD) &&
288 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
291 if ((what & RPMBUILD_INSTALL) &&
292 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
295 if ((what & RPMBUILD_CHECK) &&
296 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
299 if ((what & RPMBUILD_PACKAGESOURCE) &&
300 (rc = processSourceFiles(spec)))
303 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
304 (what & RPMBUILD_FILECHECK)) &&
305 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
308 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
309 (rc = packageSources(spec)))
312 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
313 (rc = packageBinaries(spec)))
316 if ((what & RPMBUILD_CLEAN) &&
317 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
320 if ((what & RPMBUILD_RMBUILD) &&
321 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
325 if (what & RPMBUILD_RMSOURCE)
328 if (what & RPMBUILD_RMSPEC)
329 (void) Unlink(spec->specFile);
332 if (rc && rpmlogGetNrecs() > 0) {
333 rpmlog(RPMLOG_NOTICE, _("\n\nRPM build errors:\n"));