3 #include "build/rpmbuild.h"
9 static int checkSpec(Header h)
15 struct rpmDependencyConflict * conflicts;
19 if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
22 if (rpmdbOpen(rootdir, &db, mode, 0644)) {
24 dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
25 rpmMessage(RPMMESS_ERROR, _("cannot open %s/packages.rpm\n"), dn);
29 ts = rpmtransCreateSet(db, rootdir);
31 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
33 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34 if (rc == 0 && conflicts) {
35 rpmMessage(RPMMESS_ERROR, _("failed build prerequisites:\n"));
36 printDepProblems(stderr, conflicts, numConflicts);
37 rpmdepFreeConflicts(conflicts, numConflicts);
49 static int buildForTarget(const char *arg, struct rpmBuildArguments *ba,
50 const char *passPhrase, int fromTarball, char *cookie,
51 int force, int nodeps)
53 int buildAmount = ba->buildAmount;
54 const char *buildRoot = ba->buildRootOverride;
55 int test = ba->noBuild;
58 const char * specfile;
66 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
70 const char * tmpSpecFile;
73 specDir = rpmGetPath("%{_specdir}", NULL);
76 strcpy(tfn, "rpm-spec.XXXXXX");
77 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
80 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
81 sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s",
83 if (!(f = popen(cmd, "r"))) {
84 fprintf(stderr, _("Failed to open tar pipe: %s\n"),
90 if ((!fgets(buf, sizeof(buf) - 1, f)) || !strchr(buf, '/')) {
94 sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
96 if (!(f = popen(cmd, "r"))) {
97 fprintf(stderr, _("Failed to open tar pipe: %s\n"),
103 if (!fgets(buf, sizeof(buf) - 1, f)) {
105 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
116 if (*cmd == '/') s = cmd + 1;
122 /* remove trailing \n */
123 s = cmd + strlen(cmd) - 1;
126 s = alloca(strlen(specDir) + strlen(cmd) + 5);
127 sprintf(s, "%s/%s", specDir, cmd);
129 if (rename(tmpSpecFile, s)) {
130 fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
131 tmpSpecFile, s, strerror(errno));
138 /* Make the directory which contains the tarball the source
139 directory for this run */
142 (void)getcwd(buf, BUFSIZ);
148 cmd = buf + strlen(buf) - 1;
149 while (*cmd != '/') cmd--;
152 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
156 } else if (arg[0] == '/') {
159 char *s = alloca(BUFSIZ);
160 (void)getcwd(s, BUFSIZ);
166 stat(specfile, &statbuf);
167 if (! S_ISREG(statbuf.st_mode)) {
168 fprintf(stderr, _("File is not a regular file: %s\n"), specfile);
172 if ((fd = open(specfile, O_RDONLY)) < 0) {
173 fprintf(stderr, _("Unable to open spec file: %s\n"), specfile);
176 count = read(fd, buf, sizeof(buf) < 128 ? sizeof(buf) : 128);
180 if (! (isprint(*s) || isspace(*s) || (*s == 0x1b))) {
181 fprintf(stderr, _("File contains non-printable characters(%c): %s\n"), *s,
188 /* Parse the spec file */
189 #define _anyarch(_f) \
190 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
191 if (parseSpec(&spec, specfile, buildRoot, 0, passPhrase, cookie,
192 _anyarch(buildAmount), force)) {
197 /* Assemble source header from parsed components */
198 initSourceHeader(spec);
200 /* Check build prerequisites */
201 if (!nodeps && checkSpec(spec->sourceHeader)) {
206 if (buildSpec(spec, buildAmount, test)) {
211 if (fromTarball) unlink(specfile);
218 int build(const char *arg, struct rpmBuildArguments *ba, const char *passPhrase,
219 int fromTarball, char *cookie, const char * rcfile, int force,
224 char *targets = ba->targets;
226 if (targets == NULL) {
227 rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
232 /* parse up the build operators */
234 printf("Building target platforms: %s\n", targets);
236 for (t = targets; *t != '\0'; t = te) {
238 if ((te = strchr(t, ',')) == NULL)
240 target = alloca(te-t+1);
241 strncpy(target, t, (te-t));
243 printf("Building for target %s\n", target);
245 rpmReadConfigFiles(rcfile, target);
246 rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
257 #define POPT_USECATALOG 1000
258 #define POPT_NOLANG 1001
259 #define POPT_RMSOURCE 1002
260 #define POPT_RMBUILD 1003
261 #define POPT_BUILDROOT 1004
262 #define POPT_BUILDARCH 1005
263 #define POPT_BUILDOS 1006
264 #define POPT_TARGETPLATFORM 1007
265 #define POPT_NOBUILD 1008
266 #define POPT_SHORTCIRCUIT 1009
269 static int noBuild = 0;
270 static int useCatalog = 0;
272 static void buildArgCallback(poptContext con, enum poptCallbackReason reason,
273 const struct poptOption * opt, const char * arg,
274 struct rpmBuildArguments * data)
277 case POPT_USECATALOG: data->useCatalog = 1; break;
278 case POPT_NOBUILD: data->noBuild = 1; break;
279 case POPT_NOLANG: data->noLang = 1; break;
280 case POPT_SHORTCIRCUIT: data->shortCircuit = 1; break;
281 case POPT_RMSOURCE: data->buildAmount |= RPMBUILD_RMSOURCE; break;
282 case POPT_RMBUILD: data->buildAmount |= RPMBUILD_RMBUILD; break;
284 if (data->buildRootOverride) {
285 fprintf(stderr, _("buildroot already specified"));
288 data->buildRootOverride = strdup(arg);
291 fprintf(stderr, _("--buildarch has been obsoleted. Use the --target option instead.\n"));
295 fprintf(stderr, _("--buildos has been obsoleted. Use the --target option instead.\n"));
298 case POPT_TARGETPLATFORM:
300 int len = strlen(data->targets) + strlen(arg) + 2;
301 data->targets = realloc(data->targets, len);
302 strcat(data->targets, ",");
304 data->targets = malloc(strlen(arg) + 1);
305 data->targets[0] = '\0';
307 strcat(data->targets, arg);
312 struct poptOption rpmBuildPoptTable[] = {
313 { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
314 buildArgCallback, 0, NULL, NULL },
315 { "buildarch", '\0', POPT_ARG_STRING, 0, POPT_BUILDARCH,
316 N_("override build architecture"), "ARCH" },
317 { "buildos", '\0', POPT_ARG_STRING, 0, POPT_BUILDOS,
318 N_("override build operating system"), "OS" },
319 { "buildroot", '\0', POPT_ARG_STRING, 0, POPT_BUILDROOT,
320 N_("override build root"), "DIRECTORY" },
321 { "clean", '\0', 0, 0, POPT_RMBUILD,
322 N_("remove build tree when done"), NULL},
323 { "nobuild", '\0', 0, &noBuild, POPT_NOBUILD,
324 N_("do not execute any stages of the build"), NULL },
325 { "nolang", '\0', 0, &noLang, POPT_NOLANG,
326 N_("do not accept I18N msgstr's from specfile"), NULL},
327 { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
328 N_("remove sources and specfile when done"), NULL},
329 { "short-circuit", '\0', 0, 0, POPT_SHORTCIRCUIT,
330 N_("skip straight to specified stage (only for c,i)"), NULL },
331 { "target", '\0', POPT_ARG_STRING, 0, POPT_TARGETPLATFORM,
332 N_("override target platform"), "CPU-VENDOR-OS" },
333 { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
334 N_("lookup I18N strings in specfile catalog"), NULL},
335 { 0, 0, 0, 0, 0, NULL, NULL }