3 * Top-level build dispatcher.
8 #include <rpm/rpmbuild.h>
9 #include <rpm/rpmlog.h>
10 #include <rpm/rpmfileutil.h>
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 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 char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
48 * @todo Single use by %%doc in files.c prevents static.
50 rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name, StringBuf sb, int test)
52 const char * rootDir = spec->rootDir;
53 char *scriptName = NULL;
54 char * buildDir = rpmGenPath(rootDir, "%{_builddir}", "");
55 char * buildCmd = NULL;
56 char * buildTemplate = NULL;
57 char * buildPost = NULL;
58 const char * mTemplate = NULL;
59 const char * mCmd = NULL;
60 const char * mPost = NULL;
62 const char **argv = NULL;
76 mTemplate = "%{__spec_prep_template}";
77 mPost = "%{__spec_prep_post}";
78 mCmd = "%{__spec_prep_cmd}";
83 mTemplate = "%{__spec_build_template}";
84 mPost = "%{__spec_build_post}";
85 mCmd = "%{__spec_build_cmd}";
87 case RPMBUILD_INSTALL:
90 mTemplate = "%{__spec_install_template}";
91 mPost = "%{__spec_install_post}";
92 mCmd = "%{__spec_install_cmd}";
97 mTemplate = "%{__spec_check_template}";
98 mPost = "%{__spec_check_post}";
99 mCmd = "%{__spec_check_cmd}";
104 mTemplate = "%{__spec_clean_template}";
105 mPost = "%{__spec_clean_post}";
106 mCmd = "%{__spec_clean_cmd}";
108 case RPMBUILD_RMBUILD:
110 mTemplate = "%{__spec_clean_template}";
111 mPost = "%{__spec_clean_post}";
112 mCmd = "%{__spec_clean_cmd}";
114 case RPMBUILD_STRINGBUF:
116 mTemplate = "%{___build_template}";
117 mPost = "%{___build_post}";
118 mCmd = "%{___build_cmd}";
121 if (name == NULL) /* XXX shouldn't happen */
124 if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
129 if (rpmMkTempFile(rootDir, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
130 rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n"));
135 if (fdGetFILE(fd) == NULL)
136 xfd = Fdopen(fd, "w.fpio");
140 if ((fp = fdGetFILE(xfd)) == NULL) {
145 if (*rootDir == '\0') rootDir = "/";
147 buildTemplate = rpmExpand(mTemplate, NULL);
148 buildPost = rpmExpand(mPost, NULL);
150 (void) fputs(buildTemplate, fp);
152 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
153 fprintf(fp, "cd '%s'\n", spec->buildSubdir);
155 if (what == RPMBUILD_RMBUILD) {
156 if (spec->buildSubdir)
157 fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
158 } else if (sb != NULL)
159 fprintf(fp, "%s", getStringBuf(sb));
161 (void) fputs(buildPost, fp);
171 fprintf(stderr, "*** rootDir %s buildDir %s\n", rootDir, buildDir);
172 if (buildDir && buildDir[0] != '/') {
177 buildCmd = rpmExpand(mCmd, " ", scriptName, NULL);
178 (void) poptParseArgvString(buildCmd, &argc, &argv);
180 rpmlog(RPMLOG_NOTICE, _("Executing(%s): %s\n"), name, buildCmd);
181 if (!(child = fork())) {
184 (void) execvp(argv[0], (char *const *)argv);
186 rpmlog(RPMLOG_ERR, _("Exec of %s failed (%s): %s\n"),
187 scriptName, name, strerror(errno));
192 pid = waitpid(child, &status, 0);
194 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
195 rpmlog(RPMLOG_ERR, _("Bad exit status from %s (%s)\n"),
204 (void) unlink(scriptName);
205 scriptName = _free(scriptName);
208 buildCmd = _free(buildCmd);
209 buildTemplate = _free(buildTemplate);
210 buildPost = _free(buildPost);
211 buildDir = _free(buildDir);
216 rpmRC buildSpec(rpmts ts, rpmSpec spec, int what, int test)
220 if (!spec->recursing && spec->BACount) {
222 /* When iterating over BANames, do the source */
223 /* packaging on the first run, and skip RMSOURCE altogether */
224 if (spec->BASpecs != NULL)
225 for (x = 0; x < spec->BACount; x++) {
226 if ((rc = buildSpec(ts, spec->BASpecs[x],
227 (what & ~RPMBUILD_RMSOURCE) |
228 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
234 if ((what & RPMBUILD_PREP) &&
235 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
238 if ((what & RPMBUILD_BUILD) &&
239 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
242 if ((what & RPMBUILD_INSTALL) &&
243 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
246 if ((what & RPMBUILD_CHECK) &&
247 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
250 if ((what & RPMBUILD_PACKAGESOURCE) &&
251 (rc = processSourceFiles(spec)))
254 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
255 (what & RPMBUILD_FILECHECK)) &&
256 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
259 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
260 (rc = packageSources(spec)))
263 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
264 (rc = packageBinaries(spec)))
267 if ((what & RPMBUILD_CLEAN) &&
268 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
271 if ((what & RPMBUILD_RMBUILD) &&
272 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
276 if (what & RPMBUILD_RMSOURCE)
279 if (what & RPMBUILD_RMSPEC)
280 (void) unlink(spec->specFile);
283 if (rc != RPMRC_OK && rpmlogGetNrecs() > 0) {
284 rpmlog(RPMLOG_NOTICE, _("\n\nRPM build errors:\n"));