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)
20 /*@globals rpmGlobalMacroContext, fileSystem @*/
21 /*@modifies h, rpmGlobalMacroContext, fileSystem @*/
23 const char * rootdir = NULL;
27 rpmDependencyConflict conflicts;
31 if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
34 if (rpmdbOpen(rootdir, &db, mode, 0644)) {
36 dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
37 rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
41 ts = rpmtransCreateSet(db, rootdir);
43 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
45 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
47 if (rc == 0 && conflicts) {
48 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
49 printDepProblems(stderr, conflicts, numConflicts);
50 conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
55 ts = rpmtransFree(ts);
57 (void) rpmdbClose(db);
63 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
66 /* XXX this is still a dumb test but at least it's i18n aware */
69 static int isSpecFile(const char * specfile)
70 /*@globals fileSystem @*/
71 /*@modifies fileSystem @*/
79 fd = Fopen(specfile, "r.ufdio");
80 if (fd == NULL || Ferror(fd)) {
81 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
82 specfile, Fstrerror(fd));
85 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
89 for (s = buf; count--; s++) {
94 /*@switchbreak@*/ break;
97 /*@switchbreak@*/ break;
99 if (checking && !(isprint(*s) || isspace(*s))) return 0;
100 /*@switchbreak@*/ break;
108 static int buildForTarget(const char * arg, BTA_t ba)
109 /*@globals rpmGlobalMacroContext,
110 fileSystem, internalState @*/
111 /*@modifies rpmGlobalMacroContext, fileSystem, internalState @*/
113 const char * passPhrase = ba->passPhrase;
114 char * cookie = ba->cookie;
115 int buildAmount = ba->buildAmount;
116 const char * buildRootURL = NULL;
117 const char * specFile;
118 const char * specURL;
125 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
129 if (ba->buildRootOverride)
130 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
133 if (ba->buildMode == 't') {
135 const char * specDir;
136 const char * tmpSpecFile;
138 rpmCompressedMagic res = COMPRESSED_OTHER;
139 /*@observer@*/ static const char *zcmds[] =
140 { "cat", "gunzip", "bunzip2", "cat" };
142 specDir = rpmGetPath("%{_specdir}", NULL);
144 /* XXX Using mkstemp is difficult here. */
145 /* XXX FWIW, default %{_specdir} is root.root 0755 */
147 strcpy(tfn, "rpm-spec.XXXXXX");
149 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
153 (void) isCompressed(arg, &res);
155 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
156 sprintf(cmd, "%s < %s | tar xOvf - Specfile 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)) || !strchr(buf, '/')) {
168 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
169 zcmds[res & 0x3], arg, tmpSpecFile);
170 if (!(fp = popen(cmd, "r"))) {
171 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
172 specDir = _free(specDir);
173 tmpSpecFile = _free(tmpSpecFile);
176 if (!fgets(buf, sizeof(buf) - 1, fp)) {
178 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
180 (void) unlink(tmpSpecFile);
181 specDir = _free(specDir);
182 tmpSpecFile = _free(tmpSpecFile);
189 while (*cmd != '\0') {
190 if (*cmd == '/') s = cmd + 1;
196 /* remove trailing \n */
197 s = cmd + strlen(cmd) - 1;
200 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
201 sprintf(s, "%s/%s", specDir, cmd);
202 res = rename(tmpSpecFile, s);
203 specDir = _free(specDir);
206 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
208 (void) unlink(tmpSpecFile);
209 tmpSpecFile = _free(tmpSpecFile);
212 tmpSpecFile = _free(tmpSpecFile);
214 /* Make the directory which contains the tarball the source
215 directory for this run */
218 (void)getcwd(buf, BUFSIZ);
224 cmd = buf + strlen(buf) - 1;
225 while (*cmd != '/') cmd--;
228 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
233 specut = urlPath(specURL, &specFile);
234 if (*specFile != '/') {
235 char *s = alloca(BUFSIZ);
236 (void)getcwd(s, BUFSIZ);
242 if (specut != URL_IS_DASH) {
244 if (Stat(specURL, &st) < 0) {
245 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
249 if (! S_ISREG(st.st_mode)) {
250 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
256 /* Try to verify that the file is actually a specfile */
257 if (!isSpecFile(specURL)) {
258 rpmError(RPMERR_BADSPEC,
259 _("File %s does not appear to be a specfile.\n"), specURL);
265 /* Parse the spec file */
266 #define _anyarch(_f) \
267 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
268 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
269 cookie, _anyarch(buildAmount), ba->force)) {
275 /* Assemble source header from parsed components */
276 initSourceHeader(spec);
278 /* Check build prerequisites */
279 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
284 if (buildSpec(spec, buildAmount, ba->noBuild)) {
289 if (ba->buildMode == 't')
290 (void) Unlink(specURL);
294 spec = freeSpec(spec);
295 buildRootURL = _free(buildRootURL);
299 int build(const char * arg, BTA_t ba, const char * rcfile)
303 char * targets = ba->targets;
304 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
305 int cleanFlags = ba->buildAmount & buildCleanMask;
307 if (targets == NULL) {
308 rc = buildForTarget(arg, ba);
312 /* parse up the build operators */
314 printf(_("Building target platforms: %s\n"), targets);
316 ba->buildAmount &= ~buildCleanMask;
317 for (t = targets; *t != '\0'; t = te) {
319 if ((te = strchr(t, ',')) == NULL)
321 target = alloca(te-t+1);
322 strncpy(target, t, (te-t));
326 else /* XXX Perform clean-up after last target build. */
327 ba->buildAmount |= cleanFlags;
329 printf(_("Building for target %s\n"), target);
331 /* Read in configuration for target. */
333 (void) rpmReadConfigFiles(rcfile, target);
334 rc = buildForTarget(arg, ba);
340 /* Restore original configuration. */
342 (void) rpmReadConfigFiles(rcfile, NULL);