Oops, no newline at end of filename please...
[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 #if defined(HAVE_MKSTEMP)
116     (void) close(mkstemp(tmpSpecFile));
117 #else
118     (void) mktemp(tmpSpecFile);
119 #endif
120
121     for (try = tryspec; *try != NULL; try++) {
122         FILE *fp;
123         char *cmd;
124
125         cmd = rpmExpand("%{uncompress: ", arg, "} | ",
126                         "%{__tar} xOvf - --wildcards ", *try,
127                         " 2>&1 > ", tmpSpecFile, NULL);
128
129         if (!(fp = popen(cmd, "r"))) {
130             rpmlog(RPMLOG_ERR, _("Failed to open tar pipe: %m\n"));
131         } else {
132             char *fok = fgets(tarbuf, sizeof(tarbuf) - 1, fp);
133             pclose(fp);
134             gotspec = (fok != NULL) && isSpecFile(tmpSpecFile);
135         }
136
137         if (!gotspec) 
138             unlink(tmpSpecFile);
139         free(cmd);
140     }
141
142     if (!gotspec) {
143         rpmlog(RPMLOG_ERR, _("Failed to read spec file from %s\n"), arg);
144         goto exit;
145     }
146
147     specBase = basename(tarbuf);
148     /* remove trailing \n */
149     specBase[strlen(specBase)-1] = '\0';
150
151     rasprintf(&specFile, "%s/%s", specDir, specBase);
152     res = rename(tmpSpecFile, specFile);
153
154     if (res) {
155         rpmlog(RPMLOG_ERR, _("Failed to rename %s to %s: %m\n"),
156                 tmpSpecFile, specFile);
157         free(specFile);
158         specFile = NULL;
159     }
160
161 exit:
162     (void) unlink(tmpSpecFile);
163     free(tmpSpecFile);
164     free(specDir);
165     return specFile;
166 }
167
168 /**
169  */
170 static int buildForTarget(rpmts ts, const char * arg, BTA_t ba)
171 {
172     const char * passPhrase = ba->passPhrase;
173     const char * cookie = ba->cookie;
174     int buildAmount = ba->buildAmount;
175     char * buildRootURL = NULL;
176     char * specFile = NULL;
177     rpmSpec spec = NULL;
178     int rc = 1; /* assume failure */
179
180 #ifndef DYING
181     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
182 #endif
183
184     if (ba->buildRootOverride)
185         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
186
187     if (ba->buildMode == 't') {
188         char *srcdir = NULL, *dir;
189
190         specFile = getTarSpec(arg);
191         if (!specFile)
192             goto exit;
193
194         /* Make the directory of the tarball %_sourcedir for this run */
195         /* dirname() may modify contents so extra hoops needed. */
196         if (*arg != '/') {
197             srcdir = dir = rpmGetCwd();
198         } else {
199             dir = xstrdup(arg);
200             srcdir = dirname(dir);
201         }
202         addMacro(NULL, "_sourcedir", NULL, srcdir, RMIL_TARBALL);
203         free(dir);
204     } else {
205         specFile = xstrdup(arg);
206     }
207
208     if (*specFile != '/') {
209         char *cwd = rpmGetCwd();
210         char *s = NULL;
211         rasprintf(&s, "%s/%s", cwd, arg);
212         free(specFile);
213         specFile = s;
214     }
215
216     struct stat st;
217     if (stat(specFile, &st) < 0) {
218         rpmlog(RPMLOG_ERR, _("failed to stat %s: %m\n"), specFile);
219         goto exit;
220     }
221     if (! S_ISREG(st.st_mode)) {
222         rpmlog(RPMLOG_ERR, _("File %s is not a regular file.\n"), specFile);
223         goto exit;
224     }
225
226     /* Try to verify that the file is actually a specfile */
227     if (!isSpecFile(specFile)) {
228         rpmlog(RPMLOG_ERR,
229                 _("File %s does not appear to be a specfile.\n"), specFile);
230         goto exit;
231     }
232     
233     /* Parse the spec file */
234 #define _anyarch(_f)    \
235 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
236     if (parseSpec(ts, specFile, ba->rootdir, buildRootURL, 0, passPhrase,
237                 cookie, _anyarch(buildAmount), ba->force))
238     {
239         goto exit;
240     }
241 #undef  _anyarch
242     if ((spec = rpmtsSetSpec(ts, NULL)) == NULL) {
243         goto exit;
244     }
245
246     /* Assemble source header from parsed components */
247     initSourceHeader(spec);
248
249     /* Check build prerequisites */
250     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
251         goto exit;
252     }
253
254     if (buildSpec(ts, spec, buildAmount, ba->noBuild)) {
255         goto exit;
256     }
257     
258     if (ba->buildMode == 't')
259         (void) unlink(specFile);
260     rc = 0;
261
262 exit:
263     free(specFile);
264     freeSpec(spec);
265     free(buildRootURL);
266     return rc;
267 }
268
269 int build(rpmts ts, const char * arg, BTA_t ba, const char * rcfile)
270 {
271     char *t, *te;
272     int rc = 0;
273     char * targets = ba->targets;
274 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
275     int cleanFlags = ba->buildAmount & buildCleanMask;
276     rpmVSFlags vsflags, ovsflags;
277
278     vsflags = rpmExpandNumeric("%{_vsflags_build}");
279     if (ba->qva_flags & VERIFY_DIGEST)
280         vsflags |= _RPMVSF_NODIGESTS;
281     if (ba->qva_flags & VERIFY_SIGNATURE)
282         vsflags |= _RPMVSF_NOSIGNATURES;
283     if (ba->qva_flags & VERIFY_HDRCHK)
284         vsflags |= RPMVSF_NOHDRCHK;
285     ovsflags = rpmtsSetVSFlags(ts, vsflags);
286
287     if (targets == NULL) {
288         rc =  buildForTarget(ts, arg, ba);
289         goto exit;
290     }
291
292     /* parse up the build operators */
293
294     printf(_("Building target platforms: %s\n"), targets);
295
296     ba->buildAmount &= ~buildCleanMask;
297     for (t = targets; *t != '\0'; t = te) {
298         char *target;
299         if ((te = strchr(t, ',')) == NULL)
300             te = t + strlen(t);
301         target = alloca(te-t+1);
302         strncpy(target, t, (te-t));
303         target[te-t] = '\0';
304         if (*te != '\0')
305             te++;
306         else    /* XXX Perform clean-up after last target build. */
307             ba->buildAmount |= cleanFlags;
308
309         printf(_("Building for target %s\n"), target);
310
311         /* Read in configuration for target. */
312         rpmFreeMacros(NULL);
313         rpmFreeRpmrc();
314         (void) rpmReadConfigFiles(rcfile, target);
315         rc = buildForTarget(ts, arg, ba);
316         if (rc)
317             break;
318     }
319
320 exit:
321     vsflags = rpmtsSetVSFlags(ts, ovsflags);
322     /* Restore original configuration. */
323     rpmFreeMacros(NULL);
324     rpmFreeRpmrc();
325     (void) rpmReadConfigFiles(rcfile, NULL);
326
327     return rc;
328 }