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(rpmTransactionSet ts, Header h)
20 /*@globals fileSystem @*/
21 /*@modifies ts, h, fileSystem @*/
23 rpmDependencyConflict conflicts;
27 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
28 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
31 rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
33 rc = rpmdepCheck(ts, &conflicts, &numConflicts);
35 if (rc == 0 && conflicts) {
36 rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
37 printDepProblems(stderr, conflicts, numConflicts);
38 conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
47 * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
50 /* XXX this is still a dumb test but at least it's i18n aware */
53 static int isSpecFile(const char * specfile)
54 /*@globals fileSystem @*/
55 /*@modifies fileSystem @*/
63 fd = Fopen(specfile, "r.ufdio");
64 if (fd == NULL || Ferror(fd)) {
65 rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
66 specfile, Fstrerror(fd));
69 count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
73 for (s = buf; count--; s++) {
78 /*@switchbreak@*/ break;
81 /*@switchbreak@*/ break;
83 if (checking && !(isprint(*s) || isspace(*s))) return 0;
84 /*@switchbreak@*/ break;
92 static int buildForTarget(rpmTransactionSet ts, const char * arg, BTA_t ba)
93 /*@globals rpmGlobalMacroContext,
94 fileSystem, internalState @*/
95 /*@modifies ts, rpmGlobalMacroContext, fileSystem, internalState @*/
97 const char * passPhrase = ba->passPhrase;
98 const char * cookie = ba->cookie;
99 int buildAmount = ba->buildAmount;
100 const char * buildRootURL = NULL;
101 const char * specFile;
102 const char * specURL;
109 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
113 if (ba->buildRootOverride)
114 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
117 if (ba->buildMode == 't') {
119 const char * specDir;
120 const char * tmpSpecFile;
122 rpmCompressedMagic res = COMPRESSED_OTHER;
123 /*@observer@*/ static const char *zcmds[] =
124 { "cat", "gunzip", "bunzip2", "cat" };
126 specDir = rpmGetPath("%{_specdir}", NULL);
128 /* XXX Using mkstemp is difficult here. */
129 /* XXX FWIW, default %{_specdir} is root.root 0755 */
131 strcpy(tfn, "rpm-spec.XXXXXX");
133 tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
137 (void) isCompressed(arg, &res);
139 cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
140 sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
141 zcmds[res & 0x3], arg, tmpSpecFile);
142 if (!(fp = popen(cmd, "r"))) {
143 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
144 specDir = _free(specDir);
145 tmpSpecFile = _free(tmpSpecFile);
148 if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
152 sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
153 zcmds[res & 0x3], arg, tmpSpecFile);
154 if (!(fp = popen(cmd, "r"))) {
155 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
156 specDir = _free(specDir);
157 tmpSpecFile = _free(tmpSpecFile);
160 if (!fgets(buf, sizeof(buf) - 1, fp)) {
162 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
164 (void) unlink(tmpSpecFile);
165 specDir = _free(specDir);
166 tmpSpecFile = _free(tmpSpecFile);
173 while (*cmd != '\0') {
174 if (*cmd == '/') s = cmd + 1;
180 /* remove trailing \n */
181 s = cmd + strlen(cmd) - 1;
184 specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
185 sprintf(s, "%s/%s", specDir, cmd);
186 res = rename(tmpSpecFile, s);
187 specDir = _free(specDir);
190 rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
192 (void) unlink(tmpSpecFile);
193 tmpSpecFile = _free(tmpSpecFile);
196 tmpSpecFile = _free(tmpSpecFile);
198 /* Make the directory which contains the tarball the source
199 directory for this run */
202 (void)getcwd(buf, BUFSIZ);
208 cmd = buf + strlen(buf) - 1;
209 while (*cmd != '/') cmd--;
212 addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
217 specut = urlPath(specURL, &specFile);
218 if (*specFile != '/') {
219 char *s = alloca(BUFSIZ);
220 (void)getcwd(s, BUFSIZ);
226 if (specut != URL_IS_DASH) {
228 if (Stat(specURL, &st) < 0) {
229 rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
233 if (! S_ISREG(st.st_mode)) {
234 rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
240 /* Try to verify that the file is actually a specfile */
241 if (!isSpecFile(specURL)) {
242 rpmError(RPMERR_BADSPEC,
243 _("File %s does not appear to be a specfile.\n"), specURL);
249 /* Parse the spec file */
250 #define _anyarch(_f) \
251 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
252 if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
253 cookie, _anyarch(buildAmount), ba->force)) {
259 /* Assemble source header from parsed components */
260 initSourceHeader(spec);
262 /* Check build prerequisites */
263 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
268 if (buildSpec(spec, buildAmount, ba->noBuild)) {
273 if (ba->buildMode == 't')
274 (void) Unlink(specURL);
278 spec = freeSpec(spec);
279 buildRootURL = _free(buildRootURL);
283 int build(const char * arg, BTA_t ba, const char * rcfile)
285 rpmTransactionSet ts;
288 char * targets = ba->targets;
289 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
290 int cleanFlags = ba->buildAmount & buildCleanMask;
292 ts = rpmtransCreateSet(NULL, NULL);
294 if (targets == NULL) {
295 rc = buildForTarget(ts, arg, ba);
299 /* parse up the build operators */
301 printf(_("Building target platforms: %s\n"), targets);
303 ba->buildAmount &= ~buildCleanMask;
304 for (t = targets; *t != '\0'; t = te) {
306 if ((te = strchr(t, ',')) == NULL)
308 target = alloca(te-t+1);
309 strncpy(target, t, (te-t));
313 else /* XXX Perform clean-up after last target build. */
314 ba->buildAmount |= cleanFlags;
316 printf(_("Building for target %s\n"), target);
318 /* Read in configuration for target. */
320 (void) rpmReadConfigFiles(rcfile, target);
321 rc = buildForTarget(ts, arg, ba);
327 /* Restore original configuration. */
329 (void) rpmReadConfigFiles(rcfile, NULL);
331 ts = rpmtransFree(ts);