2 const char *__progname;
8 #include <rpm/rpmcli.h>
9 #include <rpm/rpmlib.h> /* RPMSIGTAG, rpmReadPackageFile .. */
10 #include <rpm/rpmbuild.h>
11 #include <rpm/rpmlog.h>
12 #include <rpm/rpmfileutil.h>
13 #include <rpm/rpmdb.h>
14 #include <rpm/rpmps.h>
15 #include <rpm/rpmts.h>
16 #include "lib/signature.h"
22 MODE_BUILD = (1 << 4),
23 MODE_REBUILD = (1 << 5),
24 MODE_RECOMPILE = (1 << 8),
25 MODE_TARBUILD = (1 << 11),
30 /* the structure describing the options we take and the defaults */
31 static struct poptOption optionsTable[] = {
33 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmBuildPoptTable, 0,
34 N_("Build options with [ <specfile> | <tarball> | <source package> ]:"),
37 { "quiet", '\0', POPT_ARGFLAG_DOC_HIDDEN, &quiet, 0, NULL, NULL},
39 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
40 N_("Common options for all rpm modes and executables:"),
48 static int checkSpec(rpmts ts, Header h)
53 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
54 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
57 rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
61 ps = rpmtsProblems(ts);
62 if (rc == 0 && rpmpsNumProblems(ps) > 0) {
63 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
69 /* XXX nuke the added package. */
75 static int isSpecFile(const char * specfile)
83 f = fopen(specfile, "r");
84 if (f == NULL || ferror(f)) {
85 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
86 specfile, strerror(errno));
89 count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
96 for (s = buf; count--; s++) {
107 if (checking && !(isprint(*s) || isspace(*s))) return 0;
110 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
119 * Try to find a spec from a tarball pointed to by arg.
120 * Return absolute path to spec name on success, otherwise NULL.
122 static char * getTarSpec(const char *arg)
124 char *specFile = NULL;
130 int gotspec = 0, res;
131 static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
133 specDir = rpmGetPath("%{_specdir}", NULL);
134 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
136 (void) close(mkstemp(tmpSpecFile));
138 for (try = tryspec; *try != NULL; try++) {
142 cmd = rpmExpand("%{uncompress: ", arg, "} | ",
143 "%{__tar} xOvf - --wildcards ", *try,
144 " 2>&1 > ", tmpSpecFile, NULL);
146 if (!(fp = popen(cmd, "r"))) {
147 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
151 fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
152 /* tar sometimes prints "tar: Record size = 16" messages */
153 if (!fok || strncmp(fok, "tar: ", 5) != 0)
157 gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
166 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
170 specBase = basename(tarbuf);
171 /* remove trailing \n */
172 specBase[strlen(specBase)-1] = '\0';
174 rasprintf(&specFile, "%s/%s", specDir, specBase);
175 res = rename(tmpSpecFile, specFile);
178 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
179 tmpSpecFile, specFile);
183 /* mkstemp() can give unnecessarily strict permissions, fixup */
185 umask(mask = umask(0));
186 (void) chmod(specFile, 0666 & ~mask);
190 (void) unlink(tmpSpecFile);
196 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
198 const char * cookie = ba->cookie;
199 int buildAmount = ba->buildAmount;
200 char * buildRootURL = NULL;
201 char * specFile = NULL;
203 int rc = 1; /* assume failure */
204 int justRm = ((buildAmount & ~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) == 0);
207 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
210 if (ba->buildRootOverride)
211 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
213 /* Create build tree if necessary */
214 const char * buildtree = "%{_topdir}:%{_specdir}:%{_sourcedir}:%{_builddir}:%{_rpmdir}:%{_srcrpmdir}:%{_buildrootdir}";
215 const char * rootdir = rpmtsRootDir(ts);
216 if (rpmMkdirs(!rstreq(rootdir, "/") ? rootdir : NULL , buildtree)) {
220 if (ba->buildMode == 't') {
221 char *srcdir = NULL, *dir;
223 specFile = getTarSpec(arg);
227 /* Make the directory of the tarball %_sourcedir for this run */
228 /* dirname() may modify contents so extra hoops needed. */
231 rstrscat(&dir, "/", arg, NULL);
235 srcdir = dirname(dir);
236 addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
239 specFile = xstrdup(arg);
242 if (*specFile != '/') {
243 char *cwd = rpmGetCwd();
245 rasprintf(&s, "%s/%s", cwd, arg);
252 if (stat(specFile, &st) < 0) {
253 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
256 if (! S_ISREG(st.st_mode)) {
257 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
261 /* Try to verify that the file is actually a specfile */
262 if (!isSpecFile(specFile)) {
264 _("File %s does not appear to be a specfile.\n"), specFile);
268 /* Don't parse spec if only its removal is requested */
269 if (ba->buildAmount == RPMBUILD_RMSPEC) {
270 rc = unlink(specFile);
274 /* Parse the spec file */
275 #define _anyarch(_f) \
276 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
277 if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, NULL,
278 cookie, _anyarch(buildAmount), ba->force))
283 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
287 /* Assemble source header from parsed components */
288 initSourceHeader(spec);
290 /* Check build prerequisites if necessary, unless disabled */
291 if (!justRm && !ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
295 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
299 if (ba->buildMode == 't')
300 (void) unlink(specFile);
310 static int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
314 char * targets = ba->targets;
315 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
316 int cleanFlags = ba->buildAmount & buildCleanMask;
317 rpmVSFlags vsflags, ovsflags;
319 vsflags = rpmExpandNumeric("%{_vsflags_build}");
320 if (ba->qva_flags & VERIFY_DIGEST)
321 vsflags |= _RPMVSF_NODIGESTS;
322 if (ba->qva_flags & VERIFY_SIGNATURE)
323 vsflags |= _RPMVSF_NOSIGNATURES;
324 if (ba->qva_flags & VERIFY_HDRCHK)
325 vsflags |= RPMVSF_NOHDRCHK;
326 ovsflags = rpmtsSetVSFlags(ts, vsflags);
328 if (targets == NULL) {
329 rc = buildForTarget(ts, arg, ba);
333 /* parse up the build operators */
335 printf(_("Building target platforms: %s\n"), targets);
337 ba->buildAmount &= ~buildCleanMask;
338 for (t = targets; *t != '\0'; t = te) {
340 if ((te = strchr(t, ',')) == NULL)
342 target = xmalloc(te-t+1);
343 strncpy(target, t, (te-t));
347 else /* XXX Perform clean-up after last target build. */
348 ba->buildAmount |= cleanFlags;
350 printf(_("Building for target %s\n"), target);
352 /* Read in configuration for target. */
355 (void) rpmReadConfigFiles(rcfile, target);
357 rc = buildForTarget(ts, arg, ba);
363 vsflags = rpmtsSetVSFlags(ts, ovsflags);
364 /* Restore original configuration. */
367 (void) rpmReadConfigFiles(rcfile, NULL);
372 int main(int argc, char *argv[])
375 enum modes bigMode = MODE_BUILD;
376 BTA_t ba = &rpmBTArgs;
378 const char *pkg = NULL;
380 poptContext optCon = rpmcliInit(argc, argv, optionsTable);
382 if (argc <= 1 || poptPeekArg(optCon) == NULL) {
383 printUsage(optCon, stderr, 0);
387 switch (ba->buildMode) {
388 case 'b': bigMode = MODE_BUILD; break;
389 case 't': bigMode = MODE_TARBUILD; break;
390 case 'B': bigMode = MODE_REBUILD; break;
391 case 'C': bigMode = MODE_RECOMPILE; break;
394 if (rpmcliRootDir && rpmcliRootDir[0] != '/') {
395 argerror(_("arguments to --root (-r) must begin with a /"));
398 /* rpmbuild is rather chatty by default */
399 rpmSetVerbosity(quiet ? RPMLOG_WARNING : RPMLOG_INFO);
401 if (rpmcliPipeOutput && initPipe())
405 (void) rpmtsSetRootDir(ts, rpmcliRootDir);
410 RPMBUILD_PREP | RPMBUILD_BUILD | RPMBUILD_INSTALL | RPMBUILD_CHECK;
411 if (bigMode == MODE_REBUILD) {
412 ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
413 ba->buildAmount |= RPMBUILD_RMSOURCE;
414 ba->buildAmount |= RPMBUILD_RMSPEC;
415 ba->buildAmount |= RPMBUILD_CLEAN;
416 ba->buildAmount |= RPMBUILD_RMBUILD;
419 while ((pkg = poptGetArg(optCon))) {
420 char * specFile = NULL;
423 ec = rpmInstallSource(ts, pkg, &specFile, &ba->cookie);
425 ba->rootdir = rpmcliRootDir;
426 ec = build(ts, specFile, ba, rpmcliRcfile);
428 ba->cookie = _free(ba->cookie);
429 specFile = _free(specFile);
437 switch (ba->buildChar) {
439 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
441 ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
442 ba->buildAmount |= RPMBUILD_CLEAN;
443 if ((ba->buildChar == 'b') && ba->shortCircuit)
446 ba->buildAmount |= RPMBUILD_INSTALL;
447 ba->buildAmount |= RPMBUILD_CHECK;
448 if ((ba->buildChar == 'i') && ba->shortCircuit)
451 ba->buildAmount |= RPMBUILD_BUILD;
452 if ((ba->buildChar == 'c') && ba->shortCircuit)
455 ba->buildAmount |= RPMBUILD_PREP;
459 ba->buildAmount |= RPMBUILD_FILECHECK;
462 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
466 while ((pkg = poptGetArg(optCon))) {
467 ba->rootdir = rpmcliRootDir;
469 ec = build(ts, pkg, ba, rpmcliRcfile);
473 (void) rpmReadConfigFiles(rpmcliRcfile, NULL);
481 ba->buildRootOverride = _free(ba->buildRootOverride);
482 ba->targets = _free(ba->targets);