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);
55 (void) rpmdbClose(db);
61 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
64 /* XXX this is still a dumb test but at least it's i18n aware */
67 static int isSpecFile(const char * specfile)
68 /*@modifies fileSystem @*/
76 fd = Fopen(specfile, "r.ufdio");
77 if (fd == NULL || Ferror(fd)) {
78 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
79 specfile, Fstrerror(fd));
82 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
86 for (s = buf; count--; s++) {
96 if (checking && !(isprint(*s) || isspace(*s))) return 0;
105 static int buildForTarget(const char * arg, BTA_t ba,
106 const char * passPhrase, char * cookie)
107 /*@modifies fileSystem @*/
109 int buildAmount = ba->buildAmount;
110 const char * buildRootURL = NULL;
111 const char * specFile;
112 const char * specURL;
119 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
122 if (ba->buildRootOverride)
123 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
125 if (ba->buildMode == 't') {
127 const char * specDir;
128 const char * tmpSpecFile;
130 rpmCompressedMagic res = COMPRESSED_OTHER;
131 /*@observer@*/ static const char *zcmds[] =
132 { "cat", "gunzip", "bunzip2", "cat" };
134 specDir = rpmGetPath("%{_specdir}", NULL);
136 /* XXX Using mkstemp is difficult here. */
137 /* XXX FWIW, default %{_specdir} is root.root 0755 */
139 strcpy(tfn, "rpm-spec.XXXXXX");
141 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
145 (void) isCompressed(arg, &res);
147 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
148 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
149 zcmds[res & 0x3], arg, tmpSpecFile);
150 if (!(fp = popen(cmd, "r"))) {
151 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
152 specDir = _free(specDir);
153 tmpSpecFile = _free(tmpSpecFile);
156 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
160 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
161 zcmds[res & 0x3], arg, tmpSpecFile);
162 if (!(fp = popen(cmd, "r"))) {
163 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
164 specDir = _free(specDir);
165 tmpSpecFile = _free(tmpSpecFile);
168 if (!fgets(buf, sizeof(buf) - 1, fp)) {
170 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
172 (void) unlink(tmpSpecFile);
173 specDir = _free(specDir);
174 tmpSpecFile = _free(tmpSpecFile);
181 while (*cmd != '\0') {
182 if (*cmd == '/') s = cmd + 1;
188 /* remove trailing \n */
189 s = cmd + strlen(cmd) - 1;
192 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
193 sprintf(s, "%s/%s", specDir, cmd);
194 res = rename(tmpSpecFile, s);
195 specDir = _free(specDir);
198 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
200 (void) unlink(tmpSpecFile);
201 tmpSpecFile = _free(tmpSpecFile);
204 tmpSpecFile = _free(tmpSpecFile);
206 /* Make the directory which contains the tarball the source
207 directory for this run */
210 (void)getcwd(buf, BUFSIZ);
216 cmd = buf + strlen(buf) - 1;
217 while (*cmd != '/') cmd--;
220 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
225 specut = urlPath(specURL, &specFile);
226 if (*specFile != '/') {
227 char *s = alloca(BUFSIZ);
228 (void)getcwd(s, BUFSIZ);
234 if (specut != URL_IS_DASH) {
236 if (Stat(specURL, &st) < 0) {
237 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
241 if (! S_ISREG(st.st_mode)) {
242 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
248 /* Try to verify that the file is actually a specfile */
249 if (!isSpecFile(specURL)) {
250 rpmError(RPMERR_BADSPEC,
251 _("File %s does not appear to be a specfile.\n"), specURL);
257 /* Parse the spec file */
258 #define _anyarch(_f) \
259 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
260 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
261 cookie, _anyarch(buildAmount), ba->force)) {
267 /* Assemble source header from parsed components */
268 initSourceHeader(spec);
270 /* Check build prerequisites */
271 if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
276 if (buildSpec(spec, buildAmount, ba->noBuild)) {
281 if (ba->buildMode == 't')
282 (void) Unlink(specURL);
286 spec = freeSpec(spec);
287 buildRootURL = _free(buildRootURL);
291 int build(const char * arg, BTA_t ba,
292 const char * passPhrase, char * cookie, const char * rcfile)
296 char * targets = ba->targets;
297 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
298 int cleanFlags = ba->buildAmount & buildCleanMask;
300 if (targets == NULL) {
301 rc = buildForTarget(arg, ba, passPhrase, cookie);
305 /* parse up the build operators */
307 printf(_("Building target platforms: %s\n"), targets);
309 ba->buildAmount &= ~buildCleanMask;
310 for (t = targets; *t != '\0'; t = te) {
312 if ((te = strchr(t, ',')) == NULL)
314 target = alloca(te-t+1);
315 strncpy(target, t, (te-t));
319 else /* XXX Perform clean-up after last target build. */
320 ba->buildAmount |= cleanFlags;
322 printf(_("Building for target %s\n"), target);
324 /* Read in configuration for target. */
326 (void) rpmReadConfigFiles(rcfile, target);
327 rc = buildForTarget(arg, ba, passPhrase, cookie);
333 /* Restore original configuration. */
335 (void) rpmReadConfigFiles(rcfile, NULL);