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