7 #include <rpm/rpmcli.h>
8 #include <rpm/rpmlib.h> /* RPMSIGTAG, rpmReadPackageFile .. */
9 #include <rpm/rpmbuild.h>
10 #include <rpm/rpmlog.h>
11 #include <rpm/rpmfileutil.h>
12 #include <rpm/rpmdb.h>
13 #include <rpm/rpmps.h>
14 #include <rpm/rpmts.h>
15 #include "lib/signature.h"
20 static struct rpmBuildArguments_s rpmBTArgs;
22 #define POPT_NOLANG -1012
23 #define POPT_RMSOURCE -1013
24 #define POPT_RMBUILD -1014
25 #define POPT_BUILDROOT -1015
26 #define POPT_TARGETPLATFORM -1016
27 #define POPT_NOBUILD -1017
28 #define POPT_RMSPEC -1019
29 #define POPT_NODIRTOKENS -1020
30 #define POPT_BUILDINPLACE -1021
32 #define POPT_REBUILD 0x4262 /* Bb */
33 #define POPT_RECOMPILE 0x4369 /* Ci */
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_RA 0x4261
42 #define POPT_RB 0x4262
43 #define POPT_RC 0x4263
44 #define POPT_RI 0x4269
45 #define POPT_RL 0x426c
46 #define POPT_RP 0x4270
47 #define POPT_RS 0x4273
48 #define POPT_TA 0x7461
49 #define POPT_TB 0x7462
50 #define POPT_TC 0x7463
51 #define POPT_TI 0x7469
52 #define POPT_TL 0x746c
53 #define POPT_TP 0x7470
54 #define POPT_TS 0x7473
56 extern int _fsm_debug;
58 static rpmSpecFlags spec_flags = 0; /*!< Bit(s) to control spec parsing. */
59 static int noDeps = 0; /*!< from --nodeps */
60 static int shortCircuit = 0; /*!< from --short-circuit */
61 static char buildMode = 0; /*!< Build mode (one of "btBC") */
62 static char buildChar = 0; /*!< Build stage (one of "abcilps ") */
63 static rpmBuildFlags nobuildAmount = 0; /*!< Build stage disablers */
64 static ARGV_t build_targets = NULL; /*!< Target platform(s) */
65 static int buildInPlace = 0; /*!< from --build-in-place */
67 static void buildArgCallback( poptContext con,
68 enum poptCallbackReason reason,
69 const struct poptOption * opt, const char * arg,
72 BTA_t rba = &rpmBTArgs;
85 /* case POPT_RB: same value as POPT_REBUILD */
98 if (opt->val == POPT_BS || opt->val == POPT_TS)
100 if (buildMode == '\0' && buildChar == '\0') {
101 buildMode = (((unsigned)opt->val) >> 8) & 0xff;
102 buildChar = (opt->val ) & 0xff;
106 case POPT_NODIRTOKENS: rba->pkgFlags |= RPMBUILD_PKG_NODIRTOKENS; break;
107 case POPT_NOBUILD: rba->buildAmount |= RPMBUILD_NOBUILD; break;
108 case POPT_NOLANG: spec_flags |= RPMSPEC_NOLANG; break;
109 case POPT_RMSOURCE: rba->buildAmount |= RPMBUILD_RMSOURCE; break;
110 case POPT_RMSPEC: rba->buildAmount |= RPMBUILD_RMSPEC; break;
111 case POPT_RMBUILD: rba->buildAmount |= RPMBUILD_RMBUILD; break;
113 if (rba->buildRootOverride) {
114 rpmlog(RPMLOG_ERR, _("buildroot already specified, ignoring %s\n"), arg);
117 rba->buildRootOverride = xstrdup(arg);
119 case POPT_TARGETPLATFORM:
120 argvSplit(&build_targets, arg, ",");
123 case RPMCLI_POPT_FORCE:
124 spec_flags |= RPMSPEC_FORCE;
127 case POPT_BUILDINPLACE:
128 rpmDefineMacro(NULL, "_build_in_place 1", 0);
134 static struct poptOption rpmBuildPoptTable[] = {
135 { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
136 buildArgCallback, 0, NULL, NULL },
138 { "bp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BP,
139 N_("build through %prep (unpack sources and apply patches) from <specfile>"),
141 { "bc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BC,
142 N_("build through %build (%prep, then compile) from <specfile>"),
144 { "bi", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BI,
145 N_("build through %install (%prep, %build, then install) from <specfile>"),
147 { "bl", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BL,
148 N_("verify %files section from <specfile>"),
150 { "ba", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BA,
151 N_("build source and binary packages from <specfile>"),
153 { "bb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BB,
154 N_("build binary package only from <specfile>"),
156 { "bs", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BS,
157 N_("build source package only from <specfile>"),
160 { "rp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RP,
161 N_("build through %prep (unpack sources and apply patches) from <source package>"),
162 N_("<source package>") },
163 { "rc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RC,
164 N_("build through %build (%prep, then compile) from <source package>"),
165 N_("<source package>") },
166 { "ri", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RI,
167 N_("build through %install (%prep, %build, then install) from <source package>"),
168 N_("<source package>") },
169 { "rl", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RL,
170 N_("verify %files section from <source package>"),
171 N_("<source package>") },
172 { "ra", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RA,
173 N_("build source and binary packages from <source package>"),
174 N_("<source package>") },
175 { "rb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RB,
176 N_("build binary package only from <source package>"),
177 N_("<source package>") },
178 { "rs", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_RS,
179 N_("build source package only from <source package>"),
180 N_("<source package>") },
182 { "tp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TP,
183 N_("build through %prep (unpack sources and apply patches) from <tarball>"),
185 { "tc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TC,
186 N_("build through %build (%prep, then compile) from <tarball>"),
188 { "ti", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TI,
189 N_("build through %install (%prep, %build, then install) from <tarball>"),
191 { "tl", 0, POPT_ARGFLAG_ONEDASH|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_TL,
192 N_("verify %files section from <tarball>"),
194 { "ta", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TA,
195 N_("build source and binary packages from <tarball>"),
197 { "tb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TB,
198 N_("build binary package only from <tarball>"),
200 { "ts", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TS,
201 N_("build source package only from <tarball>"),
204 { "rebuild", '\0', 0, 0, POPT_REBUILD,
205 N_("build binary package from <source package>"),
206 N_("<source package>") },
207 { "recompile", '\0', 0, 0, POPT_RECOMPILE,
208 N_("build through %install (%prep, %build, then install) from <source package>"),
209 N_("<source package>") },
211 { "buildroot", '\0', POPT_ARG_STRING, 0, POPT_BUILDROOT,
212 N_("override build root"), "DIRECTORY" },
213 { "build-in-place", '\0', 0, 0, POPT_BUILDINPLACE,
214 N_("run build in current directory"), NULL },
215 { "clean", '\0', 0, 0, POPT_RMBUILD,
216 N_("remove build tree when done"), NULL},
217 { "force", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, RPMCLI_POPT_FORCE,
218 N_("ignore ExcludeArch: directives from spec file"), NULL},
219 { "fsmdebug", '\0', (POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN), &_fsm_debug, -1,
220 N_("debug file state machine"), NULL},
221 { "nobuild", '\0', 0, 0, POPT_NOBUILD,
222 N_("do not execute any stages of the build"), NULL },
223 { "nodeps", '\0', POPT_ARG_VAL, &noDeps, 1,
224 N_("do not verify build dependencies"), NULL },
225 { "nodirtokens", '\0', 0, 0, POPT_NODIRTOKENS,
226 N_("generate package header(s) compatible with (legacy) rpm v3 packaging"),
229 { "noclean", '\0', POPT_BIT_SET, &nobuildAmount, RPMBUILD_CLEAN,
230 N_("do not execute %clean stage of the build"), NULL },
231 { "noprep", '\0', POPT_BIT_SET, &nobuildAmount, RPMBUILD_PREP,
232 N_("do not execute %prep stage of the build"), NULL },
233 { "nocheck", '\0', POPT_BIT_SET, &nobuildAmount, RPMBUILD_CHECK,
234 N_("do not execute %check stage of the build"), NULL },
236 { "nolang", '\0', POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_NOLANG,
237 N_("do not accept i18N msgstr's from specfile"), NULL},
238 { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
239 N_("remove sources when done"), NULL},
240 { "rmspec", '\0', 0, 0, POPT_RMSPEC,
241 N_("remove specfile when done"), NULL},
242 { "short-circuit", '\0', POPT_ARG_VAL, &shortCircuit, 1,
243 N_("skip straight to specified stage (only for c,i)"), NULL },
244 { "target", '\0', POPT_ARG_STRING, 0, POPT_TARGETPLATFORM,
245 N_("override target platform"), "CPU-VENDOR-OS" },
250 MODE_BUILD = (1 << 4),
251 MODE_REBUILD = (1 << 5),
252 MODE_RECOMPILE = (1 << 8),
253 MODE_TARBUILD = (1 << 11),
258 /* the structure describing the options we take and the defaults */
259 static struct poptOption optionsTable[] = {
261 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmBuildPoptTable, 0,
262 N_("Build options with [ <specfile> | <tarball> | <source package> ]:"),
265 { "quiet", '\0', POPT_ARGFLAG_DOC_HIDDEN, &quiet, 0, NULL, NULL},
267 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
268 N_("Common options for all rpm modes and executables:"),
276 static int checkSpec(rpmts ts, rpmSpec spec)
279 rpmps ps = rpmSpecCheckDeps(ts, spec);
282 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
283 rpmpsPrint(NULL, ps);
290 static int isSpecFile(const char * specfile)
298 f = fopen(specfile, "r");
300 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
301 specfile, strerror(errno));
304 count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
311 for (s = buf; count--; s++) {
322 if (checking && !(isprint(*s) || isspace(*s))) return 0;
325 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
334 * Try to find a spec from a tarball pointed to by arg.
335 * Return absolute path to spec name on success, otherwise NULL.
337 static char * getTarSpec(const char *arg)
339 char *specFile = NULL;
345 int gotspec = 0, res;
346 static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
348 specDir = rpmGetPath("%{_specdir}", NULL);
349 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
351 (void) close(mkstemp(tmpSpecFile));
353 for (spec = tryspec; *spec != NULL; spec++) {
358 cmd = rpmExpand("%{uncompress: ", arg, "} | ",
359 "%{__tar} xOvof - --wildcards ", *spec,
360 " 2>&1 > ", tmpSpecFile, NULL);
362 if (!(fp = popen(cmd, "r"))) {
363 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
367 fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
369 /* tar sometimes prints "tar: Record size = 16" messages */
370 if (strstr(fok, "tar: ")) {
376 gotspec = (specfiles == 1) && isSpecFile(tmpSpecFile);
378 rpmlog(RPMLOG_ERR, _("Found more than one spec file in %s\n"), arg);
389 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
393 specBase = basename(tarbuf);
394 /* remove trailing \n */
395 specBase[strlen(specBase)-1] = '\0';
397 rasprintf(&specFile, "%s/%s", specDir, specBase);
398 res = rename(tmpSpecFile, specFile);
401 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
402 tmpSpecFile, specFile);
406 /* mkstemp() can give unnecessarily strict permissions, fixup */
408 umask(mask = umask(0));
409 (void) chmod(specFile, 0666 & ~mask);
413 (void) unlink(tmpSpecFile);
419 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
421 int buildAmount = ba->buildAmount;
422 char * buildRootURL = NULL;
423 char * specFile = NULL;
425 int rc = 1; /* assume failure */
426 int justRm = ((buildAmount & ~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) == 0);
427 rpmSpecFlags specFlags = spec_flags;
429 /* Override default BUILD value for _builddir */
431 char *cwd = rpmGetCwd();
432 rpmPushMacro(NULL, "_builddir", NULL, cwd, 0);
436 if (ba->buildRootOverride)
437 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
439 /* Create build tree if necessary */
440 const char * buildtree = "%{_topdir}:%{_specdir}:%{_sourcedir}:%{_builddir}:%{_rpmdir}:%{_srcrpmdir}:%{_buildrootdir}";
441 const char * rootdir = rpmtsRootDir(ts);
442 if (rpmMkdirs(!rstreq(rootdir, "/") ? rootdir : NULL , buildtree)) {
446 if (buildMode == 't') {
447 char *srcdir = NULL, *dir;
449 specFile = getTarSpec(arg);
453 /* Make the directory of the tarball %_sourcedir for this run */
454 /* dirname() may modify contents so extra hoops needed. */
457 rstrscat(&dir, "/", arg, NULL);
461 srcdir = dirname(dir);
462 rpmPushMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
465 specFile = xstrdup(arg);
468 if (*specFile != '/') {
469 char *cwd = rpmGetCwd();
471 rasprintf(&s, "%s/%s", cwd, specFile);
478 if (stat(specFile, &st) < 0) {
479 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
482 if (! S_ISREG(st.st_mode)) {
483 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
487 /* Try to verify that the file is actually a specfile */
488 if (!isSpecFile(specFile)) {
490 _("File %s does not appear to be a specfile.\n"), specFile);
494 /* Don't parse spec if only its removal is requested */
495 if (ba->buildAmount == RPMBUILD_RMSPEC) {
496 rc = unlink(specFile);
500 /* Parse the spec file */
501 #define _anyarch(_f) \
502 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
503 if (_anyarch(buildAmount))
504 specFlags |= RPMSPEC_ANYARCH;
507 spec = rpmSpecParse(specFile, specFlags, buildRootURL);
512 /* Check build prerequisites if necessary, unless disabled */
513 if (!justRm && !noDeps && checkSpec(ts, spec)) {
517 if (rpmSpecBuild(spec, ba)) {
521 if (buildMode == 't')
522 (void) unlink(specFile);
532 static int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
535 char * targets = argvJoin(build_targets, ",");
536 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
537 int cleanFlags = ba->buildAmount & buildCleanMask;
538 rpmVSFlags vsflags, ovsflags;
540 vsflags = rpmExpandNumeric("%{_vsflags_build}");
541 if (rpmcliQueryFlags & VERIFY_DIGEST)
542 vsflags |= _RPMVSF_NODIGESTS;
543 if (rpmcliQueryFlags & VERIFY_SIGNATURE)
544 vsflags |= _RPMVSF_NOSIGNATURES;
545 if (rpmcliQueryFlags & VERIFY_HDRCHK)
546 vsflags |= RPMVSF_NOHDRCHK;
547 ovsflags = rpmtsSetVSFlags(ts, vsflags);
549 if (build_targets == NULL) {
550 rc = buildForTarget(ts, arg, ba);
554 /* parse up the build operators */
556 printf(_("Building target platforms: %s\n"), targets);
558 ba->buildAmount &= ~buildCleanMask;
559 for (ARGV_const_t target = build_targets; target && *target; target++) {
560 /* Perform clean-up after last target build. */
561 if (*(target + 1) == NULL)
562 ba->buildAmount |= cleanFlags;
564 printf(_("Building for target %s\n"), *target);
566 /* Read in configuration for target. */
569 /* Need to redefine this after freeing all the macros */
570 rpmDefineMacro(NULL, "_build_in_place 1", 0);
573 (void) rpmReadConfigFiles(rcfile, *target);
574 rc = buildForTarget(ts, arg, ba);
580 rpmtsSetVSFlags(ts, ovsflags);
581 /* Restore original configuration. */
584 (void) rpmReadConfigFiles(rcfile, NULL);
590 int main(int argc, char *argv[])
593 enum modes bigMode = MODE_BUILD;
594 BTA_t ba = &rpmBTArgs;
596 const char *pkg = NULL;
599 poptContext optCon = NULL;
601 xsetprogname(argv[0]); /* Portability call -- see system.h */
603 optCon = rpmcliInit(argc, argv, optionsTable);
605 /* Args required only when building, let lone --eval etc through */
606 if (ba->buildAmount && poptPeekArg(optCon) == NULL) {
607 printUsage(optCon, stderr, 0);
612 case 'b': bigMode = MODE_BUILD; break;
613 case 't': bigMode = MODE_TARBUILD; break;
614 case 'B': bigMode = MODE_REBUILD; break;
615 case 'C': bigMode = MODE_RECOMPILE; break;
618 if (rpmcliRootDir && rpmcliRootDir[0] != '/') {
619 argerror(_("arguments to --root (-r) must begin with a /"));
622 /* rpmbuild runs in verbose mode by default */
623 if (rpmlogSetMask(0) < RPMLOG_MASK(RPMLOG_INFO))
624 rpmSetVerbosity(RPMLOG_INFO);
627 rpmSetVerbosity(RPMLOG_WARNING);
629 if (rpmcliPipeOutput && initPipe())
633 (void) rpmtsSetRootDir(ts, rpmcliRootDir);
634 rpmtsSetFlags(ts, rpmtsFlags(ts) | RPMTRANS_FLAG_NOPLUGINS);
638 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
640 ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
641 ba->buildAmount |= RPMBUILD_CLEAN;
642 if ((buildChar == 'b') && shortCircuit)
645 ba->buildAmount |= RPMBUILD_INSTALL;
646 ba->buildAmount |= RPMBUILD_CHECK;
647 if ((buildChar == 'i') && shortCircuit)
650 ba->buildAmount |= RPMBUILD_BUILD;
651 if ((buildChar == 'c') && shortCircuit)
654 ba->buildAmount |= RPMBUILD_PREP;
657 ba->buildAmount |= RPMBUILD_FILECHECK;
660 ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
663 ba->buildAmount &= ~(nobuildAmount);
668 if (bigMode == MODE_REBUILD &&
673 ba->buildAmount |= RPMBUILD_RMSOURCE;
674 ba->buildAmount |= RPMBUILD_RMSPEC;
675 ba->buildAmount |= RPMBUILD_RMBUILD;
677 ba->buildAmount &= ~(nobuildAmount);
679 while ((pkg = poptGetArg(optCon))) {
680 char * specFile = NULL;
683 ec = rpmInstallSource(ts, pkg, &specFile, &ba->cookie);
685 ba->rootdir = rpmcliRootDir;
686 ec = build(ts, specFile, ba, rpmcliRcfile);
688 ba->cookie = _free(ba->cookie);
689 specFile = _free(specFile);
698 while ((pkg = poptGetArg(optCon))) {
699 ba->rootdir = rpmcliRootDir;
701 ec = build(ts, pkg, ba, rpmcliRcfile);
705 (void) rpmReadConfigFiles(rpmcliRcfile, NULL);
713 free(ba->buildRootOverride);
714 argvFree(build_targets);