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"
21 static struct rpmBuildArguments_s rpmBTArgs;
23 #define POPT_NOLANG -1012
24 #define POPT_RMSOURCE -1013
25 #define POPT_RMBUILD -1014
26 #define POPT_BUILDROOT -1015
27 #define POPT_TARGETPLATFORM -1016
28 #define POPT_NOBUILD -1017
29 #define POPT_RMSPEC -1019
30 #define POPT_NODIRTOKENS -1020
32 #define POPT_REBUILD 0x4220
33 #define POPT_RECOMPILE 0x4320
34 #define POPT_BA 0x6261
35 #define POPT_BB 0x6262
36 #define POPT_BC 0x6263
37 #define POPT_BI 0x6269
38 #define POPT_BL 0x626c
39 #define POPT_BP 0x6270
40 #define POPT_BS 0x6273
41 #define POPT_TA 0x7461
42 #define POPT_TB 0x7462
43 #define POPT_TC 0x7463
44 #define POPT_TI 0x7469
45 #define POPT_TL 0x746c
46 #define POPT_TP 0x7470
47 #define POPT_TS 0x7473
49 extern int _fsm_debug;
51 static rpmSpecFlags spec_flags = 0; /*!< Bit(s) to control spec parsing. */
52 static int noDeps = 0; /*!< from --nodeps */
53 static int shortCircuit = 0; /*!< from --short-circuit */
54 static char buildMode = 0; /*!< Build mode (one of "btBC") */
55 static char buildChar = 0; /*!< Build stage (one of "abcilps ") */
56 static rpmBuildFlags nobuildAmount = 0; /*!< Build stage disablers */
57 static ARGV_t build_targets = NULL; /*!< Target platform(s) */
59 static void buildArgCallback( poptContext con,
60 enum poptCallbackReason reason,
61 const struct poptOption * opt, const char * arg,
64 BTA_t rba = &rpmBTArgs;
83 if (opt->val == POPT_BS || opt->val == POPT_TS)
85 if (buildMode == '\0' && buildChar == '\0') {
86 buildMode = (((unsigned)opt->val) >> 8) & 0xff;
87 buildChar = (opt->val ) & 0xff;
91 case POPT_NODIRTOKENS: rba->pkgFlags |= RPMBUILD_PKG_NODIRTOKENS; break;
92 case POPT_NOBUILD: rba->buildAmount |= RPMBUILD_NOBUILD; break;
93 case POPT_NOLANG: spec_flags |= RPMSPEC_NOLANG; break;
94 case POPT_RMSOURCE: rba->buildAmount |= RPMBUILD_RMSOURCE; break;
95 case POPT_RMSPEC: rba->buildAmount |= RPMBUILD_RMSPEC; break;
96 case POPT_RMBUILD: rba->buildAmount |= RPMBUILD_RMBUILD; break;
98 if (rba->buildRootOverride) {
99 rpmlog(RPMLOG_ERR, _("buildroot already specified, ignoring %s\n"), arg);
102 rba->buildRootOverride = xstrdup(arg);
104 case POPT_TARGETPLATFORM:
105 argvSplit(&build_targets, arg, ",");
108 case RPMCLI_POPT_FORCE:
109 spec_flags |= RPMSPEC_FORCE;
115 static struct poptOption rpmBuildPoptTable[] = {
116 { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
117 buildArgCallback, 0, NULL, NULL },
119 { "bp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BP,
120 N_("build through %prep (unpack sources and apply patches) from <specfile>"),
122 { "bc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BC,
123 N_("build through %build (%prep, then compile) from <specfile>"),
125 { "bi", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BI,
126 N_("build through %install (%prep, %build, then install) from <specfile>"),
128 { "bl", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BL,
129 N_("verify %files section from <specfile>"),
131 { "ba", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BA,
132 N_("build source and binary packages from <specfile>"),
134 { "bb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BB,
135 N_("build binary package only from <specfile>"),
137 { "bs", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BS,
138 N_("build source package only from <specfile>"),
141 { "tp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TP,
142 N_("build through %prep (unpack sources and apply patches) from <tarball>"),
144 { "tc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TC,
145 N_("build through %build (%prep, then compile) from <tarball>"),
147 { "ti", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TI,
148 N_("build through %install (%prep, %build, then install) from <tarball>"),
150 { "tl", 0, POPT_ARGFLAG_ONEDASH|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_TL,
151 N_("verify %files section from <tarball>"),
153 { "ta", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TA,
154 N_("build source and binary packages from <tarball>"),
156 { "tb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TB,
157 N_("build binary package only from <tarball>"),
159 { "ts", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TS,
160 N_("build source package only from <tarball>"),
163 { "rebuild", '\0', 0, 0, POPT_REBUILD,
164 N_("build binary package from <source package>"),
165 N_("<source package>") },
166 { "recompile", '\0', 0, 0, POPT_RECOMPILE,
167 N_("build through %install (%prep, %build, then install) from <source package>"),
168 N_("<source package>") },
170 { "buildroot", '\0', POPT_ARG_STRING, 0, POPT_BUILDROOT,
171 N_("override build root"), "DIRECTORY" },
172 { "clean", '\0', 0, 0, POPT_RMBUILD,
173 N_("remove build tree when done"), NULL},
174 { "force", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_FORCE,
175 N_("ignore ExcludeArch: directives from spec file"), NULL},
176 { "fsmdebug", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN), &_fsm_debug, -1,
177 N_("debug file state machine"), NULL},
178 { "nobuild", '\0', 0, 0, POPT_NOBUILD,
179 N_("do not execute any stages of the build"), NULL },
180 { "nodeps", '\0', POPT_ARG_VAL, &noDeps, 1,
181 N_("do not verify build dependencies"), NULL },
182 { "nodirtokens", '\0', 0, 0, POPT_NODIRTOKENS,
183 N_("generate package header(s) compatible with (legacy) rpm v3 packaging"),
186 { "noclean", '\0', POPT_BIT_SET, &nobuildAmount, RPMBUILD_CLEAN,
187 N_("do not execute %clean stage of the build"), NULL },
188 { "nocheck", '\0', POPT_BIT_SET, &nobuildAmount, RPMBUILD_CHECK,
189 N_("do not execute %check stage of the build"), NULL },
191 { "nolang", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_NOLANG,
192 N_("do not accept i18N msgstr's from specfile"), NULL},
193 { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
194 N_("remove sources when done"), NULL},
195 { "rmspec", '\0', 0, 0, POPT_RMSPEC,
196 N_("remove specfile when done"), NULL},
197 { "short-circuit", '\0', POPT_ARG_VAL, &shortCircuit, 1,
198 N_("skip straight to specified stage (only for c,i)"), NULL },
199 { "target", '\0', POPT_ARG_STRING, 0, POPT_TARGETPLATFORM,
200 N_("override target platform"), "CPU-VENDOR-OS" },
205 MODE_BUILD = (1 << 4),
206 MODE_REBUILD = (1 << 5),
207 MODE_RECOMPILE = (1 << 8),
208 MODE_TARBUILD = (1 << 11),
213 /* the structure describing the options we take and the defaults */
214 static struct poptOption optionsTable[] = {
216 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmBuildPoptTable, 0,
217 N_("Build options with [ <specfile> | <tarball> | <source package> ]:"),
220 { "quiet", '\0', POPT_ARGFLAG_DOC_HIDDEN, &quiet, 0, NULL, NULL},
222 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
223 N_("Common options for all rpm modes and executables:"),
231 static int checkSpec(rpmts ts, rpmSpec spec)
234 rpmps ps = rpmSpecCheckDeps(ts, spec);
237 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
238 rpmpsPrint(NULL, ps);
245 static int isSpecFile(const char * specfile)
253 f = fopen(specfile, "r");
255 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
256 specfile, strerror(errno));
259 count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
266 for (s = buf; count--; s++) {
277 if (checking && !(isprint(*s) || isspace(*s))) return 0;
280 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
289 * Try to find a spec from a tarball pointed to by arg.
290 * Return absolute path to spec name on success, otherwise NULL.
292 static char * getTarSpec(const char *arg)
294 char *specFile = NULL;
300 int gotspec = 0, res;
301 static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
303 specDir = rpmGetPath("%{_specdir}", NULL);
304 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
306 (void) close(mkstemp(tmpSpecFile));
308 for (spec = tryspec; *spec != NULL; spec++) {
312 cmd = rpmExpand("%{uncompress: ", arg, "} | ",
313 "%{__tar} xOvf - --wildcards ", *spec,
314 " 2>&1 > ", tmpSpecFile, NULL);
316 if (!(fp = popen(cmd, "r"))) {
317 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
321 fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
322 /* tar sometimes prints "tar: Record size = 16" messages */
323 if (!fok || strncmp(fok, "tar: ", 5) != 0)
327 gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
336 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
340 specBase = basename(tarbuf);
341 /* remove trailing \n */
342 specBase[strlen(specBase)-1] = '\0';
344 rasprintf(&specFile, "%s/%s", specDir, specBase);
345 res = rename(tmpSpecFile, specFile);
348 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
349 tmpSpecFile, specFile);
353 /* mkstemp() can give unnecessarily strict permissions, fixup */
355 umask(mask = umask(0));
356 (void) chmod(specFile, 0666 & ~mask);
360 (void) unlink(tmpSpecFile);
366 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
368 int buildAmount = ba->buildAmount;
369 char * buildRootURL = NULL;
370 char * specFile = NULL;
372 int rc = 1; /* assume failure */
373 int justRm = ((buildAmount & ~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) == 0);
374 rpmSpecFlags specFlags = spec_flags;
376 if (ba->buildRootOverride)
377 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
379 /* Create build tree if necessary */
380 const char * buildtree = "%{_topdir}:%{_specdir}:%{_sourcedir}:%{_builddir}:%{_rpmdir}:%{_srcrpmdir}:%{_buildrootdir}";
381 const char * rootdir = rpmtsRootDir(ts);
382 if (rpmMkdirs(!rstreq(rootdir, "/") ? rootdir : NULL , buildtree)) {
386 if (buildMode == 't') {
387 char *srcdir = NULL, *dir;
389 specFile = getTarSpec(arg);
393 /* Make the directory of the tarball %_sourcedir for this run */
394 /* dirname() may modify contents so extra hoops needed. */
397 rstrscat(&dir, "/", arg, NULL);
401 srcdir = dirname(dir);
402 addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
405 specFile = xstrdup(arg);
408 if (*specFile != '/') {
409 char *cwd = rpmGetCwd();
411 rasprintf(&s, "%s/%s", cwd, specFile);
418 if (stat(specFile, &st) < 0) {
419 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
422 if (! S_ISREG(st.st_mode)) {
423 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
427 /* Try to verify that the file is actually a specfile */
428 if (!isSpecFile(specFile)) {
430 _("File %s does not appear to be a specfile.\n"), specFile);
434 /* Don't parse spec if only its removal is requested */
435 if (ba->buildAmount == RPMBUILD_RMSPEC) {
436 rc = unlink(specFile);
440 /* Parse the spec file */
441 #define _anyarch(_f) \
442 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
443 if (_anyarch(buildAmount))
444 specFlags |= RPMSPEC_ANYARCH;
447 spec = rpmSpecParse(specFile, specFlags, buildRootURL);
452 /* Check build prerequisites if necessary, unless disabled */
453 if (!justRm && !noDeps && checkSpec(ts, spec)) {
457 if (rpmSpecBuild(spec, ba)) {
461 if (buildMode == 't')
462 (void) unlink(specFile);
472 static int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
475 char * targets = argvJoin(build_targets, ",");
476 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
477 int cleanFlags = ba->buildAmount & buildCleanMask;
478 rpmVSFlags vsflags, ovsflags;
480 vsflags = rpmExpandNumeric("%{_vsflags_build}");
481 if (rpmcliQueryFlags & VERIFY_DIGEST)
482 vsflags |= _RPMVSF_NODIGESTS;
483 if (rpmcliQueryFlags & VERIFY_SIGNATURE)
484 vsflags |= _RPMVSF_NOSIGNATURES;
485 if (rpmcliQueryFlags & VERIFY_HDRCHK)
486 vsflags |= RPMVSF_NOHDRCHK;
487 ovsflags = rpmtsSetVSFlags(ts, vsflags);
489 if (build_targets == NULL) {
490 rc = buildForTarget(ts, arg, ba);
494 /* parse up the build operators */
496 printf(_("Building target platforms: %s\n"), targets);
498 ba->buildAmount &= ~buildCleanMask;
499 for (ARGV_const_t target = build_targets; target && *target; target++) {
500 /* Perform clean-up after last target build. */
501 if (*(target + 1) == NULL)
502 ba->buildAmount |= cleanFlags;
504 printf(_("Building for target %s\n"), *target);
506 /* Read in configuration for target. */
509 (void) rpmReadConfigFiles(rcfile, *target);
510 rc = buildForTarget(ts, arg, ba);
516 rpmtsSetVSFlags(ts, ovsflags);
517 /* Restore original configuration. */
520 (void) rpmReadConfigFiles(rcfile, NULL);
526 int main(int argc, char *argv[])
529 enum modes bigMode = MODE_BUILD;
530 BTA_t ba = &rpmBTArgs;
532 const char *pkg = NULL;
534 poptContext optCon = rpmcliInit(argc, argv, optionsTable);
536 if (argc <= 1 || poptPeekArg(optCon) == NULL) {
537 printUsage(optCon, stderr, 0);
542 case 'b': bigMode = MODE_BUILD; break;
543 case 't': bigMode = MODE_TARBUILD; break;
544 case 'B': bigMode = MODE_REBUILD; break;
545 case 'C': bigMode = MODE_RECOMPILE; break;
548 if (rpmcliRootDir && rpmcliRootDir[0] != '/') {
549 argerror(_("arguments to --root (-r) must begin with a /"));
552 /* rpmbuild is rather chatty by default */
553 rpmSetVerbosity(quiet ? RPMLOG_WARNING : RPMLOG_INFO);
555 if (rpmcliPipeOutput && initPipe())
559 (void) rpmtsSetRootDir(ts, rpmcliRootDir);
564 RPMBUILD_PREP | RPMBUILD_BUILD | RPMBUILD_INSTALL | RPMBUILD_CHECK;
565 if (bigMode == MODE_REBUILD) {
566 ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
567 ba->buildAmount |= RPMBUILD_RMSOURCE;
568 ba->buildAmount |= RPMBUILD_RMSPEC;
569 ba->buildAmount |= RPMBUILD_CLEAN;
570 ba->buildAmount |= RPMBUILD_RMBUILD;
572 ba->buildAmount &= ~(nobuildAmount);
574 while ((pkg = poptGetArg(optCon))) {
575 char * specFile = NULL;
578 ec = rpmInstallSource(ts, pkg, &specFile, &ba->cookie);
580 ba->rootdir = rpmcliRootDir;
581 ec = build(ts, specFile, ba, rpmcliRcfile);
583 ba->cookie = _free(ba->cookie);
584 specFile = _free(specFile);
594 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
596 ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
597 ba->buildAmount |= RPMBUILD_CLEAN;
598 if ((buildChar == 'b') && shortCircuit)
601 ba->buildAmount |= RPMBUILD_INSTALL;
602 ba->buildAmount |= RPMBUILD_CHECK;
603 if ((buildChar == 'i') && shortCircuit)
606 ba->buildAmount |= RPMBUILD_BUILD;
607 if ((buildChar == 'c') && shortCircuit)
610 ba->buildAmount |= RPMBUILD_PREP;
614 ba->buildAmount |= RPMBUILD_FILECHECK;
617 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
620 ba->buildAmount &= ~(nobuildAmount);
622 while ((pkg = poptGetArg(optCon))) {
623 ba->rootdir = rpmcliRootDir;
625 ec = build(ts, pkg, ba, rpmcliRcfile);
629 (void) rpmReadConfigFiles(rpmcliRcfile, NULL);
637 free(ba->buildRootOverride);
638 argvFree(build_targets);