Add config.sub update to %configure macro
[platform/upstream/rpm.git] / rpmbuild.c
1 #include "system.h"
2
3 #include <errno.h>
4 #include <libgen.h>
5 #include <ctype.h>
6
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"
16 #include "cliutils.h"
17
18 #include "debug.h"
19
20 static struct rpmBuildArguments_s rpmBTArgs;
21
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
31
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
55
56 extern int _fsm_debug;
57
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 */
66
67 static void buildArgCallback( poptContext con,
68         enum poptCallbackReason reason,
69         const struct poptOption * opt, const char * arg,
70         const void * data)
71 {
72     BTA_t rba = &rpmBTArgs;
73
74     switch (opt->val) {
75     case POPT_REBUILD:
76     case POPT_RECOMPILE:
77     case POPT_BA:
78     case POPT_BB:
79     case POPT_BC:
80     case POPT_BI:
81     case POPT_BL:
82     case POPT_BP:
83     case POPT_BS:
84     case POPT_RA:
85     /* case POPT_RB: same value as POPT_REBUILD */
86     case POPT_RC:
87     case POPT_RI:
88     case POPT_RL:
89     case POPT_RP:
90     case POPT_RS:
91     case POPT_TA:
92     case POPT_TB:
93     case POPT_TC:
94     case POPT_TI:
95     case POPT_TL:
96     case POPT_TP:
97     case POPT_TS:
98         if (opt->val == POPT_BS || opt->val == POPT_TS)
99             noDeps = 1;
100         if (buildMode == '\0' && buildChar == '\0') {
101             buildMode = (((unsigned)opt->val) >> 8) & 0xff;
102             buildChar = (opt->val     ) & 0xff;
103         }
104         break;
105
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;
112     case POPT_BUILDROOT:
113         if (rba->buildRootOverride) {
114             rpmlog(RPMLOG_ERR, _("buildroot already specified, ignoring %s\n"), arg);
115             break;
116         }
117         rba->buildRootOverride = xstrdup(arg);
118         break;
119     case POPT_TARGETPLATFORM:
120         argvSplit(&build_targets, arg, ",");
121         break;
122
123     case RPMCLI_POPT_FORCE:
124         spec_flags |= RPMSPEC_FORCE;
125         break;
126
127     case POPT_BUILDINPLACE:
128         rpmDefineMacro(NULL, "_build_in_place 1", 0);
129         buildInPlace = 1;
130         break;
131     }
132 }
133
134 static struct poptOption rpmBuildPoptTable[] = {
135  { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA | POPT_CBFLAG_CONTINUE,
136         buildArgCallback, 0, NULL, NULL },
137
138  { "bp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BP,
139         N_("build through %prep (unpack sources and apply patches) from <specfile>"),
140         N_("<specfile>") },
141  { "bc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BC,
142         N_("build through %build (%prep, then compile) from <specfile>"),
143         N_("<specfile>") },
144  { "bi", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BI,
145         N_("build through %install (%prep, %build, then install) from <specfile>"),
146         N_("<specfile>") },
147  { "bl", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BL,
148         N_("verify %files section from <specfile>"),
149         N_("<specfile>") },
150  { "ba", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BA,
151         N_("build source and binary packages from <specfile>"),
152         N_("<specfile>") },
153  { "bb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BB,
154         N_("build binary package only from <specfile>"),
155         N_("<specfile>") },
156  { "bs", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_BS,
157         N_("build source package only from <specfile>"),
158         N_("<specfile>") },
159
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>") },
181
182  { "tp", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TP,
183         N_("build through %prep (unpack sources and apply patches) from <tarball>"),
184         N_("<tarball>") },
185  { "tc", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TC,
186         N_("build through %build (%prep, then compile) from <tarball>"),
187         N_("<tarball>") },
188  { "ti", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TI,
189         N_("build through %install (%prep, %build, then install) from <tarball>"),
190         N_("<tarball>") },
191  { "tl", 0, POPT_ARGFLAG_ONEDASH|POPT_ARGFLAG_DOC_HIDDEN, 0, POPT_TL,
192         N_("verify %files section from <tarball>"),
193         N_("<tarball>") },
194  { "ta", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TA,
195         N_("build source and binary packages from <tarball>"),
196         N_("<tarball>") },
197  { "tb", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TB,
198         N_("build binary package only from <tarball>"),
199         N_("<tarball>") },
200  { "ts", 0, POPT_ARGFLAG_ONEDASH, 0, POPT_TS,
201         N_("build source package only from <tarball>"),
202         N_("<tarball>") },
203
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>") },
210
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"),
227         NULL},
228
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 },
235
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" },
246    POPT_TABLEEND
247 };
248
249 enum modes {
250     MODE_BUILD          = (1 <<  4),
251     MODE_REBUILD        = (1 <<  5),
252     MODE_RECOMPILE      = (1 <<  8),
253     MODE_TARBUILD       = (1 << 11),
254 };
255
256 static int quiet;
257
258 /* the structure describing the options we take and the defaults */
259 static struct poptOption optionsTable[] = {
260
261  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmBuildPoptTable, 0,
262         N_("Build options with [ <specfile> | <tarball> | <source package> ]:"),
263         NULL },
264
265  { "quiet", '\0', POPT_ARGFLAG_DOC_HIDDEN, &quiet, 0, NULL, NULL},
266
267  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
268         N_("Common options for all rpm modes and executables:"),
269         NULL },
270
271    POPT_AUTOALIAS
272    POPT_AUTOHELP
273    POPT_TABLEEND
274 };
275
276 static int checkSpec(rpmts ts, rpmSpec spec)
277 {
278     int rc;
279     rpmps ps = rpmSpecCheckDeps(ts, spec);
280
281     if (ps) {
282         rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
283         rpmpsPrint(NULL, ps);
284     }
285     rc = (ps != NULL);
286     rpmpsFree(ps);
287     return rc;
288 }
289
290 static int isSpecFile(const char * specfile)
291 {
292     char buf[256];
293     const char * s;
294     FILE * f;
295     int count;
296     int checking;
297
298     f = fopen(specfile, "r");
299     if (f == NULL) {
300         rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
301                 specfile, strerror(errno));
302         return 0;
303     }
304     count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
305     (void) fclose(f);
306
307     if (count == 0)
308         return 0;
309
310     checking = 1;
311     for (s = buf; count--; s++) {
312         switch (*s) {
313         case '\r':
314         case '\n':
315             checking = 1;
316             break;
317         case ':':
318             checking = 0;
319             break;
320         default:
321 #if 0
322             if (checking && !(isprint(*s) || isspace(*s))) return 0;
323             break;
324 #else
325             if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
326             break;
327 #endif
328         }
329     }
330     return 1;
331 }
332
333 /* 
334  * Try to find a spec from a tarball pointed to by arg. 
335  * Return absolute path to spec name on success, otherwise NULL.
336  */
337 static char * getTarSpec(const char *arg)
338 {
339     char *specFile = NULL;
340     char *specDir;
341     char *specBase;
342     char *tmpSpecFile;
343     const char **spec;
344     char tarbuf[BUFSIZ];
345     int gotspec = 0, res;
346     static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
347
348     specDir = rpmGetPath("%{_specdir}", NULL);
349     tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
350
351     (void) close(mkstemp(tmpSpecFile));
352
353     for (spec = tryspec; *spec != NULL; spec++) {
354         FILE *fp;
355         char *cmd;
356         int specfiles = 0;
357
358         cmd = rpmExpand("%{uncompress: ", arg, "} | ",
359                         "%{__tar} xOvof - --wildcards ", *spec,
360                         " 2>&1 > ", tmpSpecFile, NULL);
361
362         if (!(fp = popen(cmd, "r"))) {
363             rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
364         } else {
365             char *fok;
366             for (;;) {
367                 fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
368                 if (!fok) break;
369                 /* tar sometimes prints "tar: Record size = 16" messages */
370                 if (strstr(fok, "tar: ")) {
371                     continue;
372                 }
373                 specfiles++;
374             }
375             pclose(fp);
376             gotspec = (specfiles == 1) && isSpecFile(tmpSpecFile);
377             if (specfiles > 1) {
378                 rpmlog(RPMLOG_ERR, _("Found more than one spec file in %s\n"), arg);
379                 goto exit;
380             }
381         }
382
383         if (!gotspec) 
384             unlink(tmpSpecFile);
385         free(cmd);
386     }
387
388     if (!gotspec) {
389         rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
390         goto exit;
391     }
392
393     specBase = basename(tarbuf);
394     /* remove trailing \n */
395     specBase[strlen(specBase)-1] = '\0';
396
397     rasprintf(&specFile, "%s/%s", specDir, specBase);
398     res = rename(tmpSpecFile, specFile);
399
400     if (res) {
401         rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
402                 tmpSpecFile, specFile);
403         free(specFile);
404         specFile = NULL;
405     } else {
406         /* mkstemp() can give unnecessarily strict permissions, fixup */
407         mode_t mask;
408         umask(mask = umask(0));
409         (void) chmod(specFile, 0666 & ~mask);
410     }
411
412 exit:
413     (void) unlink(tmpSpecFile);
414     free(tmpSpecFile);
415     free(specDir);
416     return specFile;
417 }
418
419 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
420 {
421     int buildAmount = ba->buildAmount;
422     char * buildRootURL = NULL;
423     char * specFile = NULL;
424     rpmSpec spec = NULL;
425     int rc = 1; /* assume failure */
426     int justRm = ((buildAmount & ~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) == 0);
427     rpmSpecFlags specFlags = spec_flags;
428
429     /* Override default BUILD value for _builddir */
430     if (buildInPlace) {
431         char *cwd = rpmGetCwd();
432         rpmPushMacro(NULL, "_builddir", NULL, cwd, 0);
433         free(cwd);
434     }
435
436     if (ba->buildRootOverride)
437         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
438
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)) {
443         goto exit;
444     }
445
446     if (buildMode == 't') {
447         char *srcdir = NULL, *dir;
448
449         specFile = getTarSpec(arg);
450         if (!specFile)
451             goto exit;
452
453         /* Make the directory of the tarball %_sourcedir for this run */
454         /* dirname() may modify contents so extra hoops needed. */
455         if (*arg != '/') {
456             dir = rpmGetCwd();
457             rstrscat(&dir, "/", arg, NULL);
458         } else {
459             dir = xstrdup(arg);
460         }
461         srcdir = dirname(dir);
462         rpmPushMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
463         free(dir);
464     } else {
465         specFile = xstrdup(arg);
466     }
467
468     if (*specFile != '/') {
469         char *cwd = rpmGetCwd();
470         char *s = NULL;
471         rasprintf(&s, "%s/%s", cwd, specFile);
472         free(cwd);
473         free(specFile);
474         specFile = s;
475     }
476
477     struct stat st;
478     if (stat(specFile, &st) < 0) {
479         rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
480         goto exit;
481     }
482     if (! S_ISREG(st.st_mode)) {
483         rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
484         goto exit;
485     }
486
487     /* Try to verify that the file is actually a specfile */
488     if (!isSpecFile(specFile)) {
489         rpmlog(RPMLOG_ERR,
490                 _("File %s does not appear to be a specfile.\n"), specFile);
491         goto exit;
492     }
493     
494     /* Don't parse spec if only its removal is requested */
495     if (ba->buildAmount == RPMBUILD_RMSPEC) {
496         rc = unlink(specFile);
497         goto exit;
498     }
499
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;
505 #undef  _anyarch
506     
507     spec = rpmSpecParse(specFile, specFlags, buildRootURL);
508     if (spec == NULL) {
509         goto exit;
510     }
511
512     /* Check build prerequisites if necessary, unless disabled */
513     if (!justRm && !noDeps && checkSpec(ts, spec)) {
514         goto exit;
515     }
516
517     if (rpmSpecBuild(spec, ba)) {
518         goto exit;
519     }
520     
521     if (buildMode == 't')
522         (void) unlink(specFile);
523     rc = 0;
524
525 exit:
526     free(specFile);
527     rpmSpecFree(spec);
528     free(buildRootURL);
529     return rc;
530 }
531
532 static int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
533 {
534     int rc = 0;
535     char * targets = argvJoin(build_targets, ",");
536 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
537     int cleanFlags = ba->buildAmount & buildCleanMask;
538     rpmVSFlags vsflags, ovsflags;
539
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);
548
549     if (build_targets == NULL) {
550         rc =  buildForTarget(ts, arg, ba);
551         goto exit;
552     }
553
554     /* parse up the build operators */
555
556     printf(_("Building target platforms: %s\n"), targets);
557
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;
563
564         printf(_("Building for target %s\n"), *target);
565
566         /* Read in configuration for target. */
567         rpmFreeMacros(NULL);
568         if (buildInPlace) {
569                 /* Need to redefine this after freeing all the macros */
570                 rpmDefineMacro(NULL, "_build_in_place 1", 0);
571         }
572         rpmFreeRpmrc();
573         (void) rpmReadConfigFiles(rcfile, *target);
574         rc = buildForTarget(ts, arg, ba);
575         if (rc)
576             break;
577     }
578
579 exit:
580     rpmtsSetVSFlags(ts, ovsflags);
581     /* Restore original configuration. */
582     rpmFreeMacros(NULL);
583     rpmFreeRpmrc();
584     (void) rpmReadConfigFiles(rcfile, NULL);
585     free(targets);
586
587     return rc;
588 }
589
590 int main(int argc, char *argv[])
591 {
592     rpmts ts = NULL;
593     enum modes bigMode = MODE_BUILD;
594     BTA_t ba = &rpmBTArgs;
595
596     const char *pkg = NULL;
597     int ec = 0;
598
599     poptContext optCon = NULL;
600
601     xsetprogname(argv[0]); /* Portability call -- see system.h */
602
603     optCon = rpmcliInit(argc, argv, optionsTable);
604
605     /* Args required only when building, let lone --eval etc through */
606     if (ba->buildAmount && poptPeekArg(optCon) == NULL) {
607         printUsage(optCon, stderr, 0);
608         exit(EXIT_FAILURE);
609     }
610
611     switch (buildMode) {
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;
616     }
617
618     if (rpmcliRootDir && rpmcliRootDir[0] != '/') {
619         argerror(_("arguments to --root (-r) must begin with a /"));
620     }
621
622     /* rpmbuild runs in verbose mode by default */
623     if (rpmlogSetMask(0) < RPMLOG_MASK(RPMLOG_INFO))
624         rpmSetVerbosity(RPMLOG_INFO);
625
626     if (quiet)
627         rpmSetVerbosity(RPMLOG_WARNING);
628
629     if (rpmcliPipeOutput && initPipe())
630         exit(EXIT_FAILURE);
631         
632     ts = rpmtsCreate();
633     (void) rpmtsSetRootDir(ts, rpmcliRootDir);
634     rpmtsSetFlags(ts, rpmtsFlags(ts) | RPMTRANS_FLAG_NOPLUGINS);
635
636     switch (buildChar) {
637     case 'a':
638         ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
639     case 'b':
640         ba->buildAmount |= RPMBUILD_PACKAGEBINARY;
641         ba->buildAmount |= RPMBUILD_CLEAN;
642         if ((buildChar == 'b') && shortCircuit)
643             break;
644     case 'i':
645         ba->buildAmount |= RPMBUILD_INSTALL;
646         ba->buildAmount |= RPMBUILD_CHECK;
647         if ((buildChar == 'i') && shortCircuit)
648             break;
649     case 'c':
650         ba->buildAmount |= RPMBUILD_BUILD;
651         if ((buildChar == 'c') && shortCircuit)
652             break;
653     case 'p':
654         ba->buildAmount |= RPMBUILD_PREP;
655         break;
656     case 'l':
657         ba->buildAmount |= RPMBUILD_FILECHECK;
658         break;
659     case 's':
660         ba->buildAmount |= RPMBUILD_PACKAGESOURCE;
661         break;
662     }
663     ba->buildAmount &= ~(nobuildAmount);
664
665     switch (bigMode) {
666     case MODE_REBUILD:
667     case MODE_RECOMPILE:
668         if (bigMode == MODE_REBUILD &&
669             buildChar != 'p' &&
670             buildChar != 'c' &&
671             buildChar != 'i' &&
672             buildChar != 'l') {
673             ba->buildAmount |= RPMBUILD_RMSOURCE;
674             ba->buildAmount |= RPMBUILD_RMSPEC;
675             ba->buildAmount |= RPMBUILD_RMBUILD;
676         }
677         ba->buildAmount &= ~(nobuildAmount);
678
679         while ((pkg = poptGetArg(optCon))) {
680             char * specFile = NULL;
681
682             ba->cookie = NULL;
683             ec = rpmInstallSource(ts, pkg, &specFile, &ba->cookie);
684             if (ec == 0) {
685                 ba->rootdir = rpmcliRootDir;
686                 ec = build(ts, specFile, ba, rpmcliRcfile);
687             }
688             ba->cookie = _free(ba->cookie);
689             specFile = _free(specFile);
690
691             if (ec)
692                 break;
693         }
694         break;
695     case MODE_BUILD:
696     case MODE_TARBUILD:
697
698         while ((pkg = poptGetArg(optCon))) {
699             ba->rootdir = rpmcliRootDir;
700             ba->cookie = NULL;
701             ec = build(ts, pkg, ba, rpmcliRcfile);
702             if (ec)
703                 break;
704             rpmFreeMacros(NULL);
705             (void) rpmReadConfigFiles(rpmcliRcfile, NULL);
706         }
707         break;
708     }
709
710     rpmtsFree(ts);
711     if (finishPipe())
712         ec = EXIT_FAILURE;
713     free(ba->buildRootOverride);
714     argvFree(build_targets);
715
716     rpmcliFini(optCon);
717
718     return RETVAL(ec);
719 }