2 * Parse spec file and build package.
14 /*@access rpmTransactionSet @*/ /* XXX compared with NULL @*/
15 /*@access rpmdb @*/ /* XXX compared with NULL @*/
16 /*@access FD_t @*/ /* XXX compared with NULL @*/
20 static int checkSpec(Header h)
21 /*@modifies h, 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);
46 if (rc == 0 && conflicts) {
47 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
48 printDepProblems(stderr, conflicts, numConflicts);
49 conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
56 (void) rpmdbClose(db);
62 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
65 /* XXX this is still a dumb test but at least it's i18n aware */
68 static int isSpecFile(const char * specfile)
69 /*@modifies fileSystem @*/
77 fd = Fopen(specfile, "r.ufdio");
78 if (fd == NULL || Ferror(fd)) {
79 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
80 specfile, Fstrerror(fd));
83 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
87 for (s = buf; count--; s++) {
97 if (checking && !(isprint(*s) || isspace(*s))) return 0;
106 static int buildForTarget(const char * arg, BTA_t ba,
107 const char * passPhrase, char * cookie)
108 /*@modifies fileSystem @*/
110 int buildAmount = ba->buildAmount;
111 const char * buildRootURL = NULL;
112 const char * specFile;
113 const char * specURL;
120 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
123 if (ba->buildRootOverride)
124 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
126 if (ba->buildMode == 't') {
128 const char * specDir;
129 const char * tmpSpecFile;
131 rpmCompressedMagic res = COMPRESSED_OTHER;
132 /*@observer@*/ static const char *zcmds[] =
133 { "cat", "gunzip", "bunzip2", "cat" };
135 specDir = rpmGetPath("%{_specdir}", NULL);
137 /* XXX Using mkstemp is difficult here. */
138 /* XXX FWIW, default %{_specdir} is root.root 0755 */
140 strcpy(tfn, "rpm-spec.XXXXXX");
142 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
146 (void) isCompressed(arg, &res);
148 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
149 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
150 zcmds[res & 0x3], arg, tmpSpecFile);
151 if (!(fp = popen(cmd, "r"))) {
152 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
153 specDir = _free(specDir);
154 tmpSpecFile = _free(tmpSpecFile);
157 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
161 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
162 zcmds[res & 0x3], arg, tmpSpecFile);
163 if (!(fp = popen(cmd, "r"))) {
164 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
165 specDir = _free(specDir);
166 tmpSpecFile = _free(tmpSpecFile);
169 if (!fgets(buf, sizeof(buf) - 1, fp)) {
171 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
173 (void) unlink(tmpSpecFile);
174 specDir = _free(specDir);
175 tmpSpecFile = _free(tmpSpecFile);
182 while (*cmd != '\0') {
183 if (*cmd == '/') s = cmd + 1;
189 /* remove trailing \n */
190 s = cmd + strlen(cmd) - 1;
193 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
194 sprintf(s, "%s/%s", specDir, cmd);
195 res = rename(tmpSpecFile, s);
196 specDir = _free(specDir);
199 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
201 (void) unlink(tmpSpecFile);
202 tmpSpecFile = _free(tmpSpecFile);
205 tmpSpecFile = _free(tmpSpecFile);
207 /* Make the directory which contains the tarball the source
208 directory for this run */
211 (void)getcwd(buf, BUFSIZ);
217 cmd = buf + strlen(buf) - 1;
218 while (*cmd != '/') cmd--;
221 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
226 specut = urlPath(specURL, &specFile);
227 if (*specFile != '/') {
228 char *s = alloca(BUFSIZ);
229 (void)getcwd(s, BUFSIZ);
235 if (specut != URL_IS_DASH) {
237 if (Stat(specURL, &st) < 0) {
238 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
242 if (! S_ISREG(st.st_mode)) {
243 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
249 /* Try to verify that the file is actually a specfile */
250 if (!isSpecFile(specURL)) {
251 rpmError(RPMERR_BADSPEC,
252 _("File %s does not appear to be a specfile.\n"), specURL);
258 /* Parse the spec file */
259 #define _anyarch(_f) \
260 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
261 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
262 cookie, _anyarch(buildAmount), ba->force)) {
268 /* Assemble source header from parsed components */
269 initSourceHeader(spec);
271 /* Check build prerequisites */
272 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
277 if (buildSpec(spec, buildAmount, ba->noBuild)) {
282 if (ba->buildMode == 't')
283 (void) Unlink(specURL);
287 spec = freeSpec(spec);
288 buildRootURL = _free(buildRootURL);
292 int build(const char * arg, BTA_t ba,
293 const char * passPhrase, char * cookie, const char * rcfile)
297 char * targets = ba->targets;
298 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
299 int cleanFlags = ba->buildAmount & buildCleanMask;
301 if (targets == NULL) {
302 rc = buildForTarget(arg, ba, passPhrase, cookie);
306 /* parse up the build operators */
308 printf(_("Building target platforms: %s\n"), targets);
310 ba->buildAmount &= ~buildCleanMask;
311 for (t = targets; *t != '\0'; t = te) {
313 if ((te = strchr(t, ',')) == NULL)
315 target = alloca(te-t+1);
316 strncpy(target, t, (te-t));
320 else /* XXX Perform clean-up after last target build. */
321 ba->buildAmount |= cleanFlags;
323 printf(_("Building for target %s\n"), target);
325 /* Read in configuration for target. */
327 (void) rpmReadConfigFiles(rcfile, target);
328 rc = buildForTarget(arg, ba, passPhrase, cookie);
334 /* Restore original configuration. */
336 (void) rpmReadConfigFiles(rcfile, NULL);