2 * Parse spec file and build package.
13 /*@access rpmTransactionSet @*/ /* XXX compared with NULL @*/
14 /*@access rpmdb @*/ /* XXX compared with NULL @*/
15 /*@access FD_t @*/ /* XXX compared with NULL @*/
19 static int checkSpec(Header h)
21 const char * rootdir = NULL;
25 struct rpmDependencyConflict * conflicts;
29 if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
32 if (rpmdbOpen(rootdir, &db, mode, 0644)) {
34 dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
35 rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
39 ts = rpmtransCreateSet(db, rootdir);
41 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
43 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
44 if (rc == 0 && conflicts) {
45 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
46 printDepProblems(stderr, conflicts, numConflicts);
47 rpmdepFreeConflicts(conflicts, numConflicts);
54 (void) rpmdbClose(db);
60 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
63 /* XXX this is still a dumb test but at least it's i18n aware */
66 static int isSpecFile(const char * specfile)
74 fd = Fopen(specfile, "r.ufdio");
75 if (fd == NULL || Ferror(fd)) {
76 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
77 specfile, Fstrerror(fd));
80 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
84 for (s = buf; count--; s++) {
94 if (checking && !(isprint(*s) || isspace(*s))) return 0;
103 static int buildForTarget(const char * arg, BTA_t ba,
104 const char * passPhrase, char * cookie)
106 int buildAmount = ba->buildAmount;
107 const char *buildRootURL = NULL;
108 const char * specFile;
109 const char * specURL;
116 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
119 if (ba->buildRootOverride)
120 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
122 if (ba->buildMode == 't') {
125 const char * tmpSpecFile;
127 rpmCompressedMagic res = COMPRESSED_OTHER;
128 static const char *zcmds[] = { "cat", "gunzip", "bunzip2", "cat" };
130 specDir = rpmGetPath("%{_specdir}", NULL);
132 /* XXX Using mkstemp is difficult here. */
133 /* XXX FWIW, default %{_specdir} is root.root 0755 */
135 strcpy(tfn, "rpm-spec.XXXXXX");
137 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
141 (void) isCompressed(arg, &res);
143 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
144 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
145 zcmds[res & 0x3], arg, tmpSpecFile);
146 if (!(fp = popen(cmd, "r"))) {
147 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
148 specDir = _free(specDir);
149 tmpSpecFile = _free(tmpSpecFile);
152 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
156 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
157 zcmds[res & 0x3], arg, tmpSpecFile);
158 if (!(fp = popen(cmd, "r"))) {
159 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
160 specDir = _free(specDir);
161 tmpSpecFile = _free(tmpSpecFile);
164 if (!fgets(buf, sizeof(buf) - 1, fp)) {
166 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
168 (void) unlink(tmpSpecFile);
169 specDir = _free(specDir);
170 tmpSpecFile = _free(tmpSpecFile);
177 while (*cmd != '\0') {
178 if (*cmd == '/') s = cmd + 1;
184 /* remove trailing \n */
185 s = cmd + strlen(cmd) - 1;
188 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
189 sprintf(s, "%s/%s", specDir, cmd);
190 res = rename(tmpSpecFile, s);
191 specDir = _free(specDir);
194 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
196 (void) unlink(tmpSpecFile);
197 tmpSpecFile = _free(tmpSpecFile);
200 tmpSpecFile = _free(tmpSpecFile);
202 /* Make the directory which contains the tarball the source
203 directory for this run */
206 (void)getcwd(buf, BUFSIZ);
212 cmd = buf + strlen(buf) - 1;
213 while (*cmd != '/') cmd--;
216 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
221 specut = urlPath(specURL, &specFile);
222 if (*specFile != '/') {
223 char *s = alloca(BUFSIZ);
224 (void)getcwd(s, BUFSIZ);
230 if (specut != URL_IS_DASH) {
232 if (Stat(specURL, &st) < 0) {
233 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
237 if (! S_ISREG(st.st_mode)) {
238 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
244 /* Try to verify that the file is actually a specfile */
245 if (!isSpecFile(specURL)) {
246 rpmError(RPMERR_BADSPEC,
247 _("File %s does not appear to be a specfile.\n"), specURL);
253 /* Parse the spec file */
254 #define _anyarch(_f) \
255 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
256 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
257 cookie, _anyarch(buildAmount), ba->force)) {
263 /* Assemble source header from parsed components */
264 initSourceHeader(spec);
266 /* Check build prerequisites */
267 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
272 if (buildSpec(spec, buildAmount, ba->noBuild)) {
277 if (ba->buildMode == 't')
278 (void) Unlink(specURL);
282 spec = freeSpec(spec);
283 buildRootURL = _free(buildRootURL);
287 int build(const char * arg, BTA_t ba,
288 const char * passPhrase, char * cookie, const char * rcfile)
292 char * targets = ba->targets;
293 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
294 int cleanFlags = ba->buildAmount & buildCleanMask;
296 if (targets == NULL) {
297 rc = buildForTarget(arg, ba, passPhrase, cookie);
301 /* parse up the build operators */
303 printf(_("Building target platforms: %s\n"), targets);
305 ba->buildAmount &= ~buildCleanMask;
306 for (t = targets; *t != '\0'; t = te) {
308 if ((te = strchr(t, ',')) == NULL)
310 target = alloca(te-t+1);
311 strncpy(target, t, (te-t));
315 else /* XXX Perform clean-up after last target build. */
316 ba->buildAmount |= cleanFlags;
318 printf(_("Building for target %s\n"), target);
320 /* Read in configuration for target. */
322 (void) rpmReadConfigFiles(rcfile, target);
323 rc = buildForTarget(arg, ba, passPhrase, cookie);
329 /* Restore original configuration. */
331 (void) rpmReadConfigFiles(rcfile, NULL);