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,
25 /*@modifies fileSystem @*/
32 rc = Unlink(spec->specFile);
35 for (p = spec->sources; p != NULL; p = p->next) {
36 if (! (p->flags & RPMBUILD_ISNO)) {
37 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
43 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
44 for (p = pkg->icon; p != NULL; p = p->next) {
45 if (! (p->flags & RPMBUILD_ISNO)) {
46 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
55 * @todo Single use by %%doc in files.c prevents static.
57 int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
59 const char * rootURL = spec->rootURL;
61 const char *scriptName = NULL;
62 const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
63 const char * buildScript;
64 const char * buildCmd = NULL;
65 const char * buildTemplate = NULL;
66 const char * buildPost = NULL;
67 const char * mTemplate = NULL;
68 const char * mPost = NULL;
70 const char **argv = NULL;
85 mTemplate = "%{__spec_prep_template}";
86 mPost = "%{__spec_prep_post}";
91 mTemplate = "%{__spec_build_template}";
92 mPost = "%{__spec_build_post}";
94 case RPMBUILD_INSTALL:
97 mTemplate = "%{__spec_install_template}";
98 mPost = "%{__spec_install_post}";
103 mTemplate = "%{__spec_clean_template}";
104 mPost = "%{__spec_clean_post}";
106 case RPMBUILD_RMBUILD:
108 mTemplate = "%{__spec_clean_template}";
109 mPost = "%{__spec_clean_post}";
111 case RPMBUILD_STRINGBUF:
113 mTemplate = "%{___build_template}";
114 mPost = "%{___build_post}";
119 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
124 if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
125 rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
134 (void)fchmod(Fileno(fd), 0600);
142 if (fdGetFp(fd) == NULL)
143 xfd = Fdopen(fd, "w.fpio");
148 if ((fp = fdGetFp(xfd)) == NULL) {
153 (void) urlPath(rootURL, &rootDir);
155 if (*rootDir == '\0') rootDir = "/";
158 (void) urlPath(scriptName, &buildScript);
160 buildTemplate = rpmExpand(mTemplate, NULL);
161 buildPost = rpmExpand(mPost, NULL);
163 (void) fputs(buildTemplate, fp);
165 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
166 fprintf(fp, "cd %s\n", spec->buildSubdir);
168 if (what == RPMBUILD_RMBUILD) {
169 if (spec->buildSubdir)
170 fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
172 fprintf(fp, "%s", getStringBuf(sb));
174 (void) fputs(buildPost, fp);
184 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
185 if (buildDirURL && buildDirURL[0] != '/' &&
186 (urlSplit(buildDirURL, &u) != 0)) {
191 switch (u->urltype) {
194 fprintf(stderr, "*** addMacros\n");
195 addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
196 addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
197 if (strcmp(rootDir, "/"))
198 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
206 buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
207 (void) poptParseArgvString(buildCmd, &argc, &argv);
209 rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
210 if (!(child = fork())) {
213 (void) execvp(argv[0], (char *const *)argv);
215 rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
216 scriptName, name, strerror(errno));
221 rc = waitpid(child, &status, 0);
223 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
224 rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
233 (void) Unlink(scriptName);
234 scriptName = _free(scriptName);
237 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");
252 buildCmd = _free(buildCmd);
253 buildTemplate = _free(buildTemplate);
254 buildPost = _free(buildPost);
255 buildDirURL = _free(buildDirURL);
260 int buildSpec(Spec spec, int what, int test)
264 if (!spec->recursing && spec->BACount) {
266 /* When iterating over BANames, do the source */
267 /* packaging on the first run, and skip RMSOURCE altogether */
268 if (spec->BASpecs != NULL)
269 for (x = 0; x < spec->BACount; x++) {
270 if ((rc = buildSpec(spec->BASpecs[x],
271 (what & ~RPMBUILD_RMSOURCE) |
272 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
278 if ((what & RPMBUILD_PREP) &&
279 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
282 if ((what & RPMBUILD_BUILD) &&
283 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
286 if ((what & RPMBUILD_INSTALL) &&
287 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
290 if ((what & RPMBUILD_PACKAGESOURCE) &&
291 (rc = processSourceFiles(spec)))
294 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
295 (what & RPMBUILD_FILECHECK)) &&
296 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
299 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
300 (rc = packageSources(spec)))
303 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
304 (rc = packageBinaries(spec)))
307 if ((what & RPMBUILD_CLEAN) &&
308 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
311 if ((what & RPMBUILD_RMBUILD) &&
312 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
316 if (what & RPMBUILD_RMSOURCE)
319 if (what & RPMBUILD_RMSPEC)
320 (void) Unlink(spec->specFile);
323 if (rc && rpmlogGetNrecs() > 0) {
324 rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));