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, 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 const char * passPhrase, char * cookie)
110 /*@globals rpmGlobalMacroContext,
111 fileSystem, internalState @*/
112 /*@modifies fileSystem, internalState @*/
114 int buildAmount = ba->buildAmount;
115 const char * buildRootURL = NULL;
116 const char * specFile;
117 const char * specURL;
124 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
128 if (ba->buildRootOverride)
129 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
132 if (ba->buildMode == 't') {
134 const char * specDir;
135 const char * tmpSpecFile;
137 rpmCompressedMagic res = COMPRESSED_OTHER;
138 /*@observer@*/ static const char *zcmds[] =
139 { "cat", "gunzip", "bunzip2", "cat" };
141 specDir = rpmGetPath("%{_specdir}", NULL);
143 /* XXX Using mkstemp is difficult here. */
144 /* XXX FWIW, default %{_specdir} is root.root 0755 */
146 strcpy(tfn, "rpm-spec.XXXXXX");
148 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
152 (void) isCompressed(arg, &res);
154 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
155 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
156 zcmds[res & 0x3], arg, tmpSpecFile);
157 if (!(fp = popen(cmd, "r"))) {
158 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
159 specDir = _free(specDir);
160 tmpSpecFile = _free(tmpSpecFile);
163 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
167 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
168 zcmds[res & 0x3], arg, tmpSpecFile);
169 if (!(fp = popen(cmd, "r"))) {
170 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
171 specDir = _free(specDir);
172 tmpSpecFile = _free(tmpSpecFile);
175 if (!fgets(buf, sizeof(buf) - 1, fp)) {
177 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
179 (void) unlink(tmpSpecFile);
180 specDir = _free(specDir);
181 tmpSpecFile = _free(tmpSpecFile);
188 while (*cmd != '\0') {
189 if (*cmd == '/') s = cmd + 1;
195 /* remove trailing \n */
196 s = cmd + strlen(cmd) - 1;
199 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
200 sprintf(s, "%s/%s", specDir, cmd);
201 res = rename(tmpSpecFile, s);
202 specDir = _free(specDir);
205 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
207 (void) unlink(tmpSpecFile);
208 tmpSpecFile = _free(tmpSpecFile);
211 tmpSpecFile = _free(tmpSpecFile);
213 /* Make the directory which contains the tarball the source
214 directory for this run */
217 (void)getcwd(buf, BUFSIZ);
223 cmd = buf + strlen(buf) - 1;
224 while (*cmd != '/') cmd--;
227 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
232 specut = urlPath(specURL, &specFile);
233 if (*specFile != '/') {
234 char *s = alloca(BUFSIZ);
235 (void)getcwd(s, BUFSIZ);
241 if (specut != URL_IS_DASH) {
243 if (Stat(specURL, &st) < 0) {
244 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
248 if (! S_ISREG(st.st_mode)) {
249 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
255 /* Try to verify that the file is actually a specfile */
256 if (!isSpecFile(specURL)) {
257 rpmError(RPMERR_BADSPEC,
258 _("File %s does not appear to be a specfile.\n"), specURL);
264 /* Parse the spec file */
265 #define _anyarch(_f) \
266 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
267 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
268 cookie, _anyarch(buildAmount), ba->force)) {
274 /* Assemble source header from parsed components */
275 initSourceHeader(spec);
277 /* Check build prerequisites */
278 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
283 if (buildSpec(spec, buildAmount, ba->noBuild)) {
288 if (ba->buildMode == 't')
289 (void) Unlink(specURL);
293 spec = freeSpec(spec);
294 buildRootURL = _free(buildRootURL);
298 int build(const char * arg, BTA_t ba,
299 const char * passPhrase, char * cookie, 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, passPhrase, cookie);
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, passPhrase, cookie);
340 /* Restore original configuration. */
342 (void) rpmReadConfigFiles(rcfile, NULL);