Don't check package BuildRequires when doing --rmsource (rhbz#452477)
[platform/upstream/rpm.git] / build.c
1 /** \ingroup rpmcli
2  * Parse spec file and build package.
3  */
4
5 #include "system.h"
6
7 #include <libgen.h>
8
9 #include <rpm/rpmcli.h>
10 #include <rpm/rpmtag.h>
11 #include <rpm/rpmlib.h>         /* rpmrc, MACHTABLE .. */
12 #include <rpm/rpmbuild.h>
13
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>
19 #include <lib/misc.h>
20
21 #include "build.h"
22 #include "debug.h"
23
24 /**
25  */
26 static int checkSpec(rpmts ts, Header h)
27 {
28     rpmps ps;
29     int rc;
30
31     if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
32      && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
33         return 0;
34
35     rc = rpmtsAddInstallElement(ts, h, NULL, 0, NULL);
36
37     rc = rpmtsCheck(ts);
38
39     ps = rpmtsProblems(ts);
40     if (rc == 0 && rpmpsNumProblems(ps) > 0) {
41         rpmlog(RPMLOG_ERR, _("Failed build dependencies:\n"));
42         rpmpsPrint(NULL, ps);
43         rc = 1;
44     }
45     ps = rpmpsFree(ps);
46
47     /* XXX nuke the added package. */
48     rpmtsClean(ts);
49
50     return rc;
51 }
52
53 /**
54  */
55 static int isSpecFile(const char * specfile)
56 {
57     char buf[256];
58     const char * s;
59     FILE * f;
60     int count;
61     int checking;
62
63     f = fopen(specfile, "r");
64     if (f == NULL || ferror(f)) {
65         rpmlog(RPMLOG_ERR, _("Unable to open spec file %s: %s\n"),
66                 specfile, strerror(errno));
67         return 0;
68     }
69     count = fread(buf, sizeof(buf[0]), sizeof(buf), f);
70     (void) fclose(f);
71
72     if (count == 0)
73         return 0;
74
75     checking = 1;
76     for (s = buf; count--; s++) {
77         switch (*s) {
78         case '\r':
79         case '\n':
80             checking = 1;
81             break;
82         case ':':
83             checking = 0;
84             break;
85         default:
86 #if 0
87             if (checking && !(isprint(*s) || isspace(*s))) return 0;
88             break;
89 #else
90             if (checking && !(isprint(*s) || isspace(*s)) && *(unsigned char *)s < 32) return 0;
91             break;
92 #endif
93         }
94     }
95     return 1;
96 }
97
98 /* 
99  * Try to find a spec from a tarball pointed to by arg. 
100  * Return absolute path to spec name on success, otherwise NULL.
101  */
102 static char * getTarSpec(const char *arg)
103 {
104     char *specFile = NULL;
105     char *specDir;
106     char *specBase;
107     char *tmpSpecFile;
108     const char **try;
109     char tarbuf[BUFSIZ];
110     int gotspec = 0, res;
111     static const char *tryspec[] = { "Specfile", "\\*.spec", NULL };
112
113     specDir = rpmGetPath("%{_specdir}", NULL);
114     tmpSpecFile = rpmGetPath("%{_specdir}/", "rpm-spec.XXXXXX", NULL);
115
116     (void) close(mkstemp(tmpSpecFile));
117
118     for (try = tryspec; *try != NULL; try++) {
119         FILE *fp;
120         char *cmd;
121
122         cmd = rpmExpand("%{uncompress: ", arg, "} | ",
123                         "%{__tar} xOvf - --wildcards ", *try,
124                         " 2>&1 > ", tmpSpecFile, NULL);
125
126         if (!(fp = popen(cmd, "r"))) {
127             rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
128         } else {
129             char *fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
130             pclose(fp);
131             gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
132         }
133
134         if (!gotspec) 
135             unlink(tmpSpecFile);
136         free(cmd);
137     }
138
139     if (!gotspec) {
140         rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
141         goto exit;
142     }
143
144     specBase = basename(tarbuf);
145     /* remove trailing \n */
146     specBase[strlen(specBase)-1] = '\0';
147
148     rasprintf(&specFile, "%s/%s", specDir, specBase);
149     res = rename(tmpSpecFile, specFile);
150
151     if (res) {
152         rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
153                 tmpSpecFile, specFile);
154         free(specFile);
155         specFile = NULL;
156     } else {
157         /* mkstemp() can give unnecessarily strict permissions, fixup */
158         mode_t mask;
159         umask(mask = umask(0));
160         (void) chmod(specFile, 0666 & ~mask);
161     }
162
163 exit:
164     (void) unlink(tmpSpecFile);
165     free(tmpSpecFile);
166     free(specDir);
167     return specFile;
168 }
169
170 /**
171  */
172 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
173 {
174     const char * passPhrase = ba->passPhrase;
175     const char * cookie = ba->cookie;
176     int buildAmount = ba->buildAmount;
177     char * buildRootURL = NULL;
178     char * specFile = NULL;
179     rpmSpec spec = NULL;
180     int rc = 1; /* assume failure */
181
182 #ifndef DYING
183     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
184 #endif
185
186     if (ba->buildRootOverride)
187         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
188
189     /* Create build tree if necessary */
190     const char * buildtree = "%{_topdir}:%{_specdir}:%{_sourcedir}:%{_builddir}:%{_rpmdir}:%{_srcrpmdir}:%{_buildrootdir}";
191     const char * rootdir = rpmtsRootDir(ts);
192     if (rpmMkdirs(strcmp(rootdir, "/") ? rootdir : NULL , buildtree)) {
193         goto exit;
194     }
195
196     if (ba->buildMode == 't') {
197         char *srcdir = NULL, *dir;
198
199         specFile = getTarSpec(arg);
200         if (!specFile)
201             goto exit;
202
203         /* Make the directory of the tarball %_sourcedir for this run */
204         /* dirname() may modify contents so extra hoops needed. */
205         if (*arg != '/') {
206             dir = rpmGetCwd();
207             rstrscat(&dir, "/", arg, NULL);
208         } else {
209             dir = xstrdup(arg);
210         }
211         srcdir = dirname(dir);
212         addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
213         free(dir);
214     } else {
215         specFile = xstrdup(arg);
216     }
217
218     if (*specFile != '/') {
219         char *cwd = rpmGetCwd();
220         char *s = NULL;
221         rasprintf(&s, "%s/%s", cwd, arg);
222         free(specFile);
223         specFile = s;
224     }
225
226     struct stat st;
227     if (stat(specFile, &st) < 0) {
228         rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
229         goto exit;
230     }
231     if (! S_ISREG(st.st_mode)) {
232         rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
233         goto exit;
234     }
235
236     /* Try to verify that the file is actually a specfile */
237     if (!isSpecFile(specFile)) {
238         rpmlog(RPMLOG_ERR,
239                 _("File %s does not appear to be a specfile.\n"), specFile);
240         goto exit;
241     }
242     
243     /* Don't parse spec if only its removal is requested */
244     if (ba->buildAmount == RPMBUILD_RMSPEC) {
245         rc = unlink(specFile);
246         goto exit;
247     }
248
249     /* Parse the spec file */
250 #define _anyarch(_f)    \
251 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
252     if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, passPhrase,
253                 cookie, _anyarch(buildAmount), ba->force))
254     {
255         goto exit;
256     }
257 #undef  _anyarch
258     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
259         goto exit;
260     }
261
262     if ( ba->buildAmount&RPMBUILD_RMSOURCE && !(ba->buildAmount&~(RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)) ) {
263         rc = doRmSource(spec);
264         if ( rc == RPMRC_OK && ba->buildAmount&RPMBUILD_RMSPEC )
265             rc = unlink(specFile);
266         goto exit;
267     }
268
269     /* Assemble source header from parsed components */
270     initSourceHeader(spec);
271
272     /* Check build prerequisites */
273     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
274         goto exit;
275     }
276
277     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
278         goto exit;
279     }
280     
281     if (ba->buildMode == 't')
282         (void) unlink(specFile);
283     rc = 0;
284
285 exit:
286     free(specFile);
287     freeSpec(spec);
288     free(buildRootURL);
289     return rc;
290 }
291
292 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
293 {
294     char *t, *te;
295     int rc = 0;
296     char * targets = ba->targets;
297 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
298     int cleanFlags = ba->buildAmount & buildCleanMask;
299     rpmVSFlags vsflags, ovsflags;
300
301     vsflags = rpmExpandNumeric("%{_vsflags_build}");
302     if (ba->qva_flags & VERIFY_DIGEST)
303         vsflags |= _RPMVSF_NODIGESTS;
304     if (ba->qva_flags & VERIFY_SIGNATURE)
305         vsflags |= _RPMVSF_NOSIGNATURES;
306     if (ba->qva_flags & VERIFY_HDRCHK)
307         vsflags |= RPMVSF_NOHDRCHK;
308     ovsflags = rpmtsSetVSFlags(ts, vsflags);
309
310     if (targets == NULL) {
311         rc =  buildForTarget(ts, arg, ba);
312         goto exit;
313     }
314
315     /* parse up the build operators */
316
317     printf(_("Building target platforms: %s\n"), targets);
318
319     ba->buildAmount &= ~buildCleanMask;
320     for (t = targets; *t != '\0'; t = te) {
321         char *target;
322         if ((te = strchr(t, ',')) == NULL)
323             te = t + strlen(t);
324         target = xmalloc(te-t+1);
325         strncpy(target, t, (te-t));
326         target[te-t] = '\0';
327         if (*te != '\0')
328             te++;
329         else    /* XXX Perform clean-up after last target build. */
330             ba->buildAmount |= cleanFlags;
331
332         printf(_("Building for target %s\n"), target);
333
334         /* Read in configuration for target. */
335         rpmFreeMacros(NULL);
336         rpmFreeRpmrc();
337         (void) rpmReadConfigFiles(rcfile, target);
338         free(target);
339         rc = buildForTarget(ts, arg, ba);
340         if (rc)
341             break;
342     }
343
344 exit:
345     vsflags = rpmtsSetVSFlags(ts, ovsflags);
346     /* Restore original configuration. */
347     rpmFreeMacros(NULL);
348     rpmFreeRpmrc();
349     (void) rpmReadConfigFiles(rcfile, NULL);
350
351     return rc;
352 }