2 * Parse spec file and build package.
9 #include <rpm/rpmcli.h>
10 #include <rpm/rpmtag.h>
11 #include <rpm/rpmlib.h> /* rpmrc, MACHTABLE .. */
12 #include <rpm/rpmbuild.h>
14 #include <rpm/rpmps.h>
15 #include <rpm/rpmte.h>
16 #include <rpm/rpmts.h>
17 #include <rpm/rpmfileutil.h>
18 #include <rpm/rpmlog.h>
25 static int checkSpec(rpmts ts, Header h)
30 if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
31 && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
34 rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
38 ps = rpmtsProblems(ts);
39 if (rc == 0 && rpmpsNumProblems(ps) > 0) {
40 rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
46 /* XXX nuke the added package. */
54 static int isSpecFile(const char * specfile)
62 f = fopen(specfile, "r");
63 if (f == NULL || ferror(f)) {
64 rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
65 specfile, strerror(errno));
68 count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
75 for (s = buf; count--; s++) {
86 if (checking && !(isprint(*s) || isspace(*s))) return 0;
89 if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
98 * Try to find a spec from a tarball pointed to by arg.
99 * Return absolute path to spec name on success, otherwise NULL.
101 static char * getTarSpec(const char *arg)
103 char *specFile = NULL;
109 int gotspec = 0, res;
110 static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
112 specDir = rpmGetPath("%{_specdir}", NULL);
113 tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
115 (void) close(mkstemp(tmpSpecFile));
117 for (try = tryspec; *try != NULL; try++) {
121 cmd = rpmExpand("%{uncompress: ", arg, "} | ",
122 "%{__tar} xOvf - --wildcards ", *try,
123 " 2>&1 > ", tmpSpecFile, NULL);
125 if (!(fp = popen(cmd, "r"))) {
126 rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
128 char *fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
130 gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
139 rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
143 specBase = basename(tarbuf);
144 /* remove trailing \n */
145 specBase[strlen(specBase)-1] = '\0';
147 rasprintf(&specFile, "%s/%s", specDir, specBase);
148 res = rename(tmpSpecFile, specFile);
151 rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
152 tmpSpecFile, specFile);
156 /* mkstemp() can give unnecessarily strict permissions, fixup */
158 umask(mask = umask(0));
159 (void) chmod(specFile, 0666 & ~mask);
163 (void) unlink(tmpSpecFile);
171 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
173 const char * passPhrase = ba->passPhrase;
174 const char * cookie = ba->cookie;
175 int buildAmount = ba->buildAmount;
176 char * buildRootURL = NULL;
177 char * specFile = NULL;
179 int rc = 1; /* assume failure */
182 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
185 if (ba->buildRootOverride)
186 buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
188 if (ba->buildMode == 't') {
189 char *srcdir = NULL, *dir;
191 specFile = getTarSpec(arg);
195 /* Make the directory of the tarball %_sourcedir for this run */
196 /* dirname() may modify contents so extra hoops needed. */
199 rstrscat(&dir, "/", arg, NULL);
203 srcdir = dirname(dir);
204 addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
207 specFile = xstrdup(arg);
210 if (*specFile != '/') {
211 char *cwd = rpmGetCwd();
213 rasprintf(&s, "%s/%s", cwd, arg);
219 if (stat(specFile, &st) < 0) {
220 rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
223 if (! S_ISREG(st.st_mode)) {
224 rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
228 /* Try to verify that the file is actually a specfile */
229 if (!isSpecFile(specFile)) {
231 _("File %s does not appear to be a specfile.\n"), specFile);
235 /* Parse the spec file */
236 #define _anyarch(_f) \
237 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
238 if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, passPhrase,
239 cookie, _anyarch(buildAmount), ba->force))
244 if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
248 /* Assemble source header from parsed components */
249 initSourceHeader(spec);
251 /* Check build prerequisites */
252 if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
256 if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
260 if (ba->buildMode == 't')
261 (void) unlink(specFile);
271 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
275 char * targets = ba->targets;
276 #define buildCleanMask (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
277 int cleanFlags = ba->buildAmount & buildCleanMask;
278 rpmVSFlags vsflags, ovsflags;
280 vsflags = rpmExpandNumeric("%{_vsflags_build}");
281 if (ba->qva_flags & VERIFY_DIGEST)
282 vsflags |= _RPMVSF_NODIGESTS;
283 if (ba->qva_flags & VERIFY_SIGNATURE)
284 vsflags |= _RPMVSF_NOSIGNATURES;
285 if (ba->qva_flags & VERIFY_HDRCHK)
286 vsflags |= RPMVSF_NOHDRCHK;
287 ovsflags = rpmtsSetVSFlags(ts, vsflags);
289 if (targets == NULL) {
290 rc = buildForTarget(ts, arg, ba);
294 /* parse up the build operators */
296 printf(_("Building target platforms: %s\n"), targets);
298 ba->buildAmount &= ~buildCleanMask;
299 for (t = targets; *t != '\0'; t = te) {
301 if ((te = strchr(t, ',')) == NULL)
303 target = xmalloc(te-t+1);
304 strncpy(target, t, (te-t));
308 else /* XXX Perform clean-up after last target build. */
309 ba->buildAmount |= cleanFlags;
311 printf(_("Building for target %s\n"), target);
313 /* Read in configuration for target. */
316 (void) rpmReadConfigFiles(rcfile, target);
318 rc = buildForTarget(ts, arg, ba);
324 vsflags = rpmtsSetVSFlags(ts, ovsflags);
325 /* Restore original configuration. */
328 (void) rpmReadConfigFiles(rcfile, NULL);