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 /*@modifies h, fileSystem @*/
22 const char * rootdir = NULL;
26 rpmDependencyConflict conflicts;
30 if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
33 if (rpmdbOpen(rootdir, &db, mode, 0644)) {
35 dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
36 rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
40 ts = rpmtransCreateSet(db, rootdir);
42 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
44 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
45 if (rc == 0 && conflicts) {
46 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
47 printDepProblems(stderr, conflicts, numConflicts);
48 conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
52 ts = rpmtransFree(ts);
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)
67 /*@modifies fileSystem @*/
75 fd = Fopen(specfile, "r.ufdio");
76 if (fd == NULL || Ferror(fd)) {
77 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
78 specfile, Fstrerror(fd));
81 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
85 for (s = buf; count--; s++) {
90 /*@switchbreak@*/ break;
93 /*@switchbreak@*/ break;
95 if (checking && !(isprint(*s) || isspace(*s))) return 0;
96 /*@switchbreak@*/ break;
104 static int buildForTarget(const char * arg, BTA_t ba,
105 const char * passPhrase, char * cookie)
106 /*@modifies fileSystem @*/
108 int buildAmount = ba->buildAmount;
109 const char * buildRootURL = NULL;
110 const char * specFile;
111 const char * specURL;
118 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
121 if (ba->buildRootOverride)
122 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
124 if (ba->buildMode == 't') {
126 const char * specDir;
127 const char * tmpSpecFile;
129 rpmCompressedMagic res = COMPRESSED_OTHER;
130 /*@observer@*/ static const char *zcmds[] =
131 { "cat", "gunzip", "bunzip2", "cat" };
133 specDir = rpmGetPath("%{_specdir}", NULL);
135 /* XXX Using mkstemp is difficult here. */
136 /* XXX FWIW, default %{_specdir} is root.root 0755 */
138 strcpy(tfn, "rpm-spec.XXXXXX");
140 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
144 (void) isCompressed(arg, &res);
146 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
147 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
148 zcmds[res & 0x3], arg, tmpSpecFile);
149 if (!(fp = popen(cmd, "r"))) {
150 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
151 specDir = _free(specDir);
152 tmpSpecFile = _free(tmpSpecFile);
155 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
159 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
160 zcmds[res & 0x3], arg, tmpSpecFile);
161 if (!(fp = popen(cmd, "r"))) {
162 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
163 specDir = _free(specDir);
164 tmpSpecFile = _free(tmpSpecFile);
167 if (!fgets(buf, sizeof(buf) - 1, fp)) {
169 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
171 (void) unlink(tmpSpecFile);
172 specDir = _free(specDir);
173 tmpSpecFile = _free(tmpSpecFile);
180 while (*cmd != '\0') {
181 if (*cmd == '/') s = cmd + 1;
187 /* remove trailing \n */
188 s = cmd + strlen(cmd) - 1;
191 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
192 sprintf(s, "%s/%s", specDir, cmd);
193 res = rename(tmpSpecFile, s);
194 specDir = _free(specDir);
197 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
199 (void) unlink(tmpSpecFile);
200 tmpSpecFile = _free(tmpSpecFile);
203 tmpSpecFile = _free(tmpSpecFile);
205 /* Make the directory which contains the tarball the source
206 directory for this run */
209 (void)getcwd(buf, BUFSIZ);
215 cmd = buf + strlen(buf) - 1;
216 while (*cmd != '/') cmd--;
219 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
224 specut = urlPath(specURL, &specFile);
225 if (*specFile != '/') {
226 char *s = alloca(BUFSIZ);
227 (void)getcwd(s, BUFSIZ);
233 if (specut != URL_IS_DASH) {
235 if (Stat(specURL, &st) < 0) {
236 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
240 if (! S_ISREG(st.st_mode)) {
241 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
247 /* Try to verify that the file is actually a specfile */
248 if (!isSpecFile(specURL)) {
249 rpmError(RPMERR_BADSPEC,
250 _("File %s does not appear to be a specfile.\n"), specURL);
256 /* Parse the spec file */
257 #define _anyarch(_f) \
258 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
259 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
260 cookie, _anyarch(buildAmount), ba->force)) {
266 /* Assemble source header from parsed components */
267 initSourceHeader(spec);
269 /* Check build prerequisites */
270 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
275 if (buildSpec(spec, buildAmount, ba->noBuild)) {
280 if (ba->buildMode == 't')
281 (void) Unlink(specURL);
285 spec = freeSpec(spec);
286 buildRootURL = _free(buildRootURL);
290 int build(const char * arg, BTA_t ba,
291 const char * passPhrase, char * cookie, const char * rcfile)
295 char * targets = ba->targets;
296 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
297 int cleanFlags = ba->buildAmount & buildCleanMask;
299 if (targets == NULL) {
300 rc = buildForTarget(arg, ba, passPhrase, cookie);
304 /* parse up the build operators */
306 printf(_("Building target platforms: %s\n"), targets);
308 ba->buildAmount &= ~buildCleanMask;
309 for (t = targets; *t != '\0'; t = te) {
311 if ((te = strchr(t, ',')) == NULL)
313 target = alloca(te-t+1);
314 strncpy(target, t, (te-t));
318 else /* XXX Perform clean-up after last target build. */
319 ba->buildAmount |= cleanFlags;
321 printf(_("Building for target %s\n"), target);
323 /* Read in configuration for target. */
325 (void) rpmReadConfigFiles(rcfile, target);
326 rc = buildForTarget(arg, ba, passPhrase, cookie);
332 /* Restore original configuration. */
334 (void) rpmReadConfigFiles(rcfile, NULL);