2 * Parse spec file and build package.
9 #include <rpm/rpmcli.h>
10 #include <rpm/rpmtag.h>
11 #include <rpm/rpmlib.h> /* rpmrc, MACHTABLE .. */
12 #include <rpm/rpmbuild.h>
14 #include <rpm/rpmps.h>
15 #include <rpm/rpmte.h>
16 #include <rpm/rpmts.h>
17 #include <rpm/rpmfileutil.h>
18 #include <rpm/rpmlog.h>
25 static int checkSpec(rpmts ts, Header h)
30 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
31 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
34 rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
38 ps = rpmtsProblems(ts);
39 if (rc == 0 && rpmpsNumProblems(ps) > 0) {
40 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
46 /* XXX nuke the added package. */
54 static int isSpecFile(const char * specfile)
62 f = fopen(specfile, "r");
63 if (f == NULL || ferror(f)) {
64 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
65 specfile, strerror(errno));
68 count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
75 for (s = buf; count--; s++) {
86 if (checking && !(isprint(*s) || isspace(*s))) return 0;
89 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
98 * Try to find a spec from a tarball pointed to by arg.
99 * Return absolute path to spec name on success, otherwise NULL.
101 static char * getTarSpec(const char *arg)
103 char *specFile = NULL;
109 int gotspec = 0, res;
110 static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
112 specDir = rpmGetPath("%{_specdir}", NULL);
113 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
115 #if defined(HAVE_MKSTEMP)
116 (void) close(mkstemp(tmpSpecFile));
118 (void) mktemp(tmpSpecFile);
121 for (try = tryspec; *try != NULL; try++) {
125 cmd = rpmExpand("%{uncompress: ", arg, "} | ",
126 "%{__tar} xOvf - --wildcards ", *try,
127 " 2>&1 > ", tmpSpecFile, NULL);
129 if (!(fp = popen(cmd, "r"))) {
130 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
132 char *fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
134 gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
143 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
147 specBase = basename(tarbuf);
148 /* remove trailing \n */
149 specBase[strlen(specBase)-1] = '\0';
151 rasprintf(&specFile, "%s/%s", specDir, specBase);
152 res = rename(tmpSpecFile, specFile);
155 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
156 tmpSpecFile, specFile);
162 (void) unlink(tmpSpecFile);
170 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
172 const char * passPhrase = ba->passPhrase;
173 const char * cookie = ba->cookie;
174 int buildAmount = ba->buildAmount;
175 char * buildRootURL = NULL;
176 char * specFile = NULL;
178 int rc = 1; /* assume failure */
181 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
184 if (ba->buildRootOverride)
185 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
187 if (ba->buildMode == 't') {
188 char *srcdir = NULL, *dir;
190 specFile = getTarSpec(arg);
194 /* Make the directory of the tarball %_sourcedir for this run */
195 /* dirname() may modify contents so extra hoops needed. */
197 srcdir = dir = rpmGetCwd();
200 srcdir = dirname(dir);
202 addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
205 specFile = xstrdup(arg);
208 if (*specFile != '/') {
209 char *cwd = rpmGetCwd();
211 rasprintf(&s, "%s/%s", cwd, arg);
217 if (stat(specFile, &st) < 0) {
218 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
221 if (! S_ISREG(st.st_mode)) {
222 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
226 /* Try to verify that the file is actually a specfile */
227 if (!isSpecFile(specFile)) {
229 _("File %s does not appear to be a specfile.\n"), specFile);
233 /* Parse the spec file */
234 #define _anyarch(_f) \
235 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
236 if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, passPhrase,
237 cookie, _anyarch(buildAmount), ba->force))
242 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
246 /* Assemble source header from parsed components */
247 initSourceHeader(spec);
249 /* Check build prerequisites */
250 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
254 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
258 if (ba->buildMode == 't')
259 (void) unlink(specFile);
269 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
273 char * targets = ba->targets;
274 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
275 int cleanFlags = ba->buildAmount & buildCleanMask;
276 rpmVSFlags vsflags, ovsflags;
278 vsflags = rpmExpandNumeric("%{_vsflags_build}");
279 if (ba->qva_flags & VERIFY_DIGEST)
280 vsflags |= _RPMVSF_NODIGESTS;
281 if (ba->qva_flags & VERIFY_SIGNATURE)
282 vsflags |= _RPMVSF_NOSIGNATURES;
283 if (ba->qva_flags & VERIFY_HDRCHK)
284 vsflags |= RPMVSF_NOHDRCHK;
285 ovsflags = rpmtsSetVSFlags(ts, vsflags);
287 if (targets == NULL) {
288 rc = buildForTarget(ts, arg, ba);
292 /* parse up the build operators */
294 printf(_("Building target platforms: %s\n"), targets);
296 ba->buildAmount &= ~buildCleanMask;
297 for (t = targets; *t != '\0'; t = te) {
299 if ((te = strchr(t, ',')) == NULL)
301 target = alloca(te-t+1);
302 strncpy(target, t, (te-t));
306 else /* XXX Perform clean-up after last target build. */
307 ba->buildAmount |= cleanFlags;
309 printf(_("Building for target %s\n"), target);
311 /* Read in configuration for target. */
314 (void) rpmReadConfigFiles(rcfile, target);
315 rc = buildForTarget(ts, arg, ba);
321 vsflags = rpmtsSetVSFlags(ts, ovsflags);
322 /* Restore original configuration. */
325 (void) rpmReadConfigFiles(rcfile, NULL);