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 rpmRC doRmSource(rpmSpec spec)
24 for (p = spec->sources; p != NULL; p = p->next) {
25 if (! (p->flags & RPMBUILD_ISNO)) {
26 char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
33 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
34 for (p = pkg->icon; p != NULL; p = p->next) {
35 if (! (p->flags & RPMBUILD_ISNO)) {
36 char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
44 return !rc ? RPMRC_OK : RPMRC_FAIL;
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 fd = rpmMkTempFile(rootDir, &scriptName);
130 if (fd == NULL || Ferror(fd)) {
131 rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n"));
136 if (fdGetFILE(fd) == NULL)
137 xfd = Fdopen(fd, "w.fpio");
141 if ((fp = fdGetFILE(xfd)) == NULL) {
146 if (*rootDir == '\0') rootDir = "/";
148 buildTemplate = rpmExpand(mTemplate, NULL);
149 buildPost = rpmExpand(mPost, NULL);
151 (void) fputs(buildTemplate, fp);
153 if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
154 fprintf(fp, "cd '%s'\n", spec->buildSubdir);
156 if (what == RPMBUILD_RMBUILD) {
157 if (spec->buildSubdir)
158 fprintf(fp, "rm -rf '%s'\n", spec->buildSubdir);
159 } else if (sb != NULL)
160 fprintf(fp, "%s", getStringBuf(sb));
162 (void) fputs(buildPost, fp);
172 fprintf(stderr, "*** rootDir %s buildDir %s\n", rootDir, buildDir);
173 if (buildDir && buildDir[0] != '/') {
178 buildCmd = rpmExpand(mCmd, " ", scriptName, NULL);
179 (void) poptParseArgvString(buildCmd, &argc, &argv);
181 rpmlog(RPMLOG_NOTICE, _("Executing(%s): %s\n"), name, buildCmd);
182 if (!(child = fork())) {
185 (void) execvp(argv[0], (char *const *)argv);
187 rpmlog(RPMLOG_ERR, _("Exec of %s failed (%s): %s\n"),
188 scriptName, name, strerror(errno));
190 _exit(127); /* exit 127 for compatibility with bash(1) */
193 pid = waitpid(child, &status, 0);
195 if (!WIFEXITED(status) || WEXITSTATUS(status)) {
196 rpmlog(RPMLOG_ERR, _("Bad exit status from %s (%s)\n"),
205 (void) unlink(scriptName);
206 scriptName = _free(scriptName);
209 buildCmd = _free(buildCmd);
210 buildTemplate = _free(buildTemplate);
211 buildPost = _free(buildPost);
212 buildDir = _free(buildDir);
217 rpmRC buildSpec(rpmts ts, rpmSpec spec, int what, int test)
221 if (!spec->recursing && spec->BACount) {
223 /* When iterating over BANames, do the source */
224 /* packaging on the first run, and skip RMSOURCE altogether */
225 if (spec->BASpecs != NULL)
226 for (x = 0; x < spec->BACount; x++) {
227 if ((rc = buildSpec(ts, spec->BASpecs[x],
228 (what & ~RPMBUILD_RMSOURCE) |
229 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
235 if ((what & RPMBUILD_PREP) &&
236 (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
239 if ((what & RPMBUILD_BUILD) &&
240 (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
243 if ((what & RPMBUILD_INSTALL) &&
244 (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
247 if ((what & RPMBUILD_CHECK) &&
248 (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
251 if ((what & RPMBUILD_PACKAGESOURCE) &&
252 (rc = processSourceFiles(spec)))
255 if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
256 (what & RPMBUILD_FILECHECK)) &&
257 (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
260 if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
261 (rc = packageSources(spec)))
264 if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
265 (rc = packageBinaries(spec)))
268 if ((what & RPMBUILD_CLEAN) &&
269 (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
272 if ((what & RPMBUILD_RMBUILD) &&
273 (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
277 if (what & RPMBUILD_RMSOURCE)
280 if (what & RPMBUILD_RMSPEC)
281 (void) unlink(spec->specFile);
284 if (rc != RPMRC_OK && rpmlogGetNrecs() > 0) {
285 rpmlog(RPMLOG_NOTICE, _("\n\nRPM build errors:\n"));