2 * Parse spec file and build package.
13 #include <rpmfileutil.h>
21 static int checkSpec(rpmts ts, Header h)
26 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
27 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
30 rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
34 ps = rpmtsProblems(ts);
35 if (rc == 0 && rpmpsNumProblems(ps) > 0) {
36 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
42 /* XXX nuke the added package. */
50 static int isSpecFile(const char * specfile)
58 fd = Fopen(specfile, "r.ufdio");
59 if (fd == NULL || Ferror(fd)) {
60 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
61 specfile, Fstrerror(fd));
64 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
68 for (s = buf; count--; s++) {
79 if (checking && !(isprint(*s) || isspace(*s))) return 0;
82 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
92 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
94 const char * passPhrase = ba->passPhrase;
95 const char * cookie = ba->cookie;
96 int buildAmount = ba->buildAmount;
97 const char * buildRootURL = NULL;
98 const char * specFile;
106 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
109 if (ba->buildRootOverride)
110 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
112 /* FIX: static zcmds heartburn */
113 if (ba->buildMode == 't') {
115 const char * specDir;
118 rpmCompressedMagic res = COMPRESSED_OTHER;
119 static const char *zcmds[] =
120 { "cat", "gunzip", "bunzip2", "cat" };
122 specDir = rpmGetPath("%{_specdir}", NULL);
124 tmpSpecFile = (char *) rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
125 #if defined(HAVE_MKSTEMP)
126 (void) close(mkstemp(tmpSpecFile));
128 (void) mktemp(tmpSpecFile);
131 (void) isCompressed(arg, &res);
133 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
134 sprintf(cmd, "%s < '%s' | tar xOvf - Specfile 2>&1 > '%s'",
135 zcmds[res & 0x3], arg, tmpSpecFile);
136 if (!(fp = popen(cmd, "r"))) {
137 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
138 specDir = _free(specDir);
139 tmpSpecFile = _free(tmpSpecFile);
142 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
146 sprintf(cmd, "%s < '%s' | tar xOvf - --wildcards \\*.spec 2>&1 > '%s'",
147 zcmds[res & 0x3], arg, tmpSpecFile);
148 if (!(fp = popen(cmd, "r"))) {
149 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
150 specDir = _free(specDir);
151 tmpSpecFile = _free(tmpSpecFile);
154 if (!fgets(buf, sizeof(buf) - 1, fp)) {
156 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"),
158 (void) unlink(tmpSpecFile);
159 specDir = _free(specDir);
160 tmpSpecFile = _free(tmpSpecFile);
167 while (*cmd != '\0') {
168 if (*cmd == '/') s = cmd + 1;
174 /* remove trailing \n */
175 s = cmd + strlen(cmd) - 1;
178 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
179 sprintf(s, "%s/%s", specDir, cmd);
180 res = rename(tmpSpecFile, s);
181 specDir = _free(specDir);
184 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
186 (void) unlink(tmpSpecFile);
187 tmpSpecFile = _free(tmpSpecFile);
190 tmpSpecFile = _free(tmpSpecFile);
192 /* Make the directory which contains the tarball the source
193 directory for this run */
196 if (!getcwd(buf, BUFSIZ)) {
197 rpmlog(RPMLOG_ERR, "getcwd failed: %m\n");
205 cmd = buf + strlen(buf) - 1;
206 while (*cmd != '/') cmd--;
209 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
214 specut = urlPath(specURL, &specFile);
215 if (*specFile != '/') {
216 char *s = alloca(BUFSIZ);
217 if (!getcwd(s, BUFSIZ)) {
218 rpmlog(RPMLOG_ERR, "getcwd failed: %m\n");
227 if (specut != URL_IS_DASH) {
229 if (Stat(specURL, &st) < 0) {
230 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specURL);
234 if (! S_ISREG(st.st_mode)) {
235 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"),
241 /* Try to verify that the file is actually a specfile */
242 if (!isSpecFile(specURL)) {
244 _("File %s does not appear to be a specfile.\n"), specURL);
250 /* Parse the spec file */
251 #define _anyarch(_f) \
252 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
253 if (parseSpec(ts, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
254 cookie, _anyarch(buildAmount), ba->force))
260 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
265 /* Assemble source header from parsed components */
266 initSourceHeader(spec);
268 /* Check build prerequisites */
269 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
274 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
279 if (ba->buildMode == 't')
280 (void) Unlink(specURL);
284 spec = freeSpec(spec);
285 buildRootURL = _free(buildRootURL);
289 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
293 char * targets = ba->targets;
294 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
295 int cleanFlags = ba->buildAmount & buildCleanMask;
296 rpmVSFlags vsflags, ovsflags;
298 vsflags = rpmExpandNumeric("%{_vsflags_build}");
299 if (ba->qva_flags & VERIFY_DIGEST)
300 vsflags |= _RPMVSF_NODIGESTS;
301 if (ba->qva_flags & VERIFY_SIGNATURE)
302 vsflags |= _RPMVSF_NOSIGNATURES;
303 if (ba->qva_flags & VERIFY_HDRCHK)
304 vsflags |= RPMVSF_NOHDRCHK;
305 ovsflags = rpmtsSetVSFlags(ts, vsflags);
307 if (targets == NULL) {
308 rc = buildForTarget(ts, arg, ba);
312 /* parse up the build operators */
314 printf(_("Building target platforms: %s\n"), targets);
316 ba->buildAmount &= ~buildCleanMask;
317 for (t = targets; *t != '\0'; t = te) {
319 if ((te = strchr(t, ',')) == NULL)
321 target = alloca(te-t+1);
322 strncpy(target, t, (te-t));
326 else /* XXX Perform clean-up after last target build. */
327 ba->buildAmount |= cleanFlags;
329 printf(_("Building for target %s\n"), target);
331 /* Read in configuration for target. */
334 (void) rpmReadConfigFiles(rcfile, target);
335 rc = buildForTarget(ts, arg, ba);
341 vsflags = rpmtsSetVSFlags(ts, ovsflags);
342 /* Restore original configuration. */
345 (void) rpmReadConfigFiles(rcfile, NULL);