Typo (t->num rather than tnum causes segfault while re-reading rpmrc on exit).
[platform/upstream/rpm.git] / build.c
1 #include "system.h"
2
3 #include <rpmbuild.h>
4 #include <rpmurl.h>
5
6 #include "build.h"
7 #include "install.h"
8
9 static int checkSpec(Header h)
10 {
11     char *rootdir = NULL;
12     rpmdb db = NULL;
13     int mode = O_RDONLY;
14     rpmTransactionSet ts;
15     struct rpmDependencyConflict * conflicts;
16     int numConflicts;
17     int rc;
18
19     if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
20         return 0;
21
22     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
23         const char *dn;
24         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
25         rpmMessage(RPMMESS_ERROR, _("cannot open %s/packages.rpm\n"), dn);
26         xfree(dn);
27         exit(EXIT_FAILURE);
28     }
29     ts = rpmtransCreateSet(db, rootdir);
30
31     rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
32
33     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34     if (rc == 0 && conflicts) {
35         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
36         printDepProblems(stderr, conflicts, numConflicts);
37         rpmdepFreeConflicts(conflicts, numConflicts);
38         rc = 1;
39     }
40
41     if (ts)
42         rpmtransFree(ts);
43     if (db)
44         rpmdbClose(db);
45
46     return rc;
47 }
48
49 /*
50  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
51  * angielsku...
52  */
53 /* XXX this is still a dumb test but at least it's i18n aware */
54 static int isSpecFile(const char *specfile)
55 {
56     char buf[256];
57     const char * s;
58     FD_t fd;
59     int count;
60     int checking;
61
62     fd = Fopen(specfile, "r.ufdio");
63     if (fd == NULL || Ferror(fd)) {
64         fprintf(stderr, _("Unable to open spec file %s: %s\n"), specfile, Fstrerror(fd));
65         return 0;
66     }
67     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
68     Fclose(fd);
69
70     checking = 1;
71     for (s = buf; count--; s++) {
72         switch (*s) {
73         case '\r':
74         case '\n':
75             checking = 1;
76             break;
77         case ':':
78             checking = 0;
79             break;
80         default:
81             if (checking && !(isprint(*s) || isspace(*s))) return 0;
82             break;
83         }
84     }
85     return 1;
86 }
87
88 static int buildForTarget(const char *arg, struct rpmBuildArguments *ba,
89         const char *passPhrase, int fromTarball, char *cookie,
90         int force, int nodeps)
91 {
92     int buildAmount = ba->buildAmount;
93     const char *buildRootURL = NULL;
94     int test = ba->noBuild;
95     const char * specFile;
96     const char * specURL;
97     int specut;
98     char buf[BUFSIZ];
99     Spec spec = NULL;
100     int rc;
101
102     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
103
104     if (ba->buildRootOverride)
105         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
106
107     if (fromTarball) {
108         FILE *fp;
109         const char *specDir;
110         const char * tmpSpecFile;
111         char * cmd, *s;
112         int res;
113
114         specDir = rpmGetPath("%{_specdir}", NULL);
115
116         {   char tfn[64];
117             strcpy(tfn, "rpm-spec.XXXXXX");
118             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
119         }
120
121         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
122         sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s",
123                         arg, tmpSpecFile);
124         if (!(fp = popen(cmd, "r"))) {
125             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
126                         strerror(errno));
127             xfree(specDir);
128             xfree(tmpSpecFile);
129             return 1;
130         }
131         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
132             /* Try again */
133             pclose(fp);
134
135             sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
136                     tmpSpecFile);
137             if (!(fp = popen(cmd, "r"))) {
138                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
139                         strerror(errno));
140                 xfree(specDir);
141                 xfree(tmpSpecFile);
142                 return 1;
143             }
144             if (!fgets(buf, sizeof(buf) - 1, fp)) {
145                 /* Give up */
146                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
147                 unlink(tmpSpecFile);
148                 xfree(specDir);
149                 xfree(tmpSpecFile);
150                 return 1;
151             }
152         }
153         pclose(fp);
154
155         cmd = s = buf;
156         while (*cmd) {
157             if (*cmd == '/') s = cmd + 1;
158             cmd++;
159         }
160
161         cmd = s;
162
163         /* remove trailing \n */
164         s = cmd + strlen(cmd) - 1;
165         *s = '\0';
166
167         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
168         sprintf(s, "%s/%s", specDir, cmd);
169         res = rename(tmpSpecFile, s);
170         xfree(specDir);
171         xfree(tmpSpecFile);
172         
173         if (res) {
174             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
175                     tmpSpecFile, s, strerror(errno));
176             unlink(tmpSpecFile);
177             return 1;
178         }
179
180         /* Make the directory which contains the tarball the source 
181            directory for this run */
182
183         if (*arg != '/') {
184             (void)getcwd(buf, BUFSIZ);
185             strcat(buf, "/");
186             strcat(buf, arg);
187         } else 
188             strcpy(buf, arg);
189
190         cmd = buf + strlen(buf) - 1;
191         while (*cmd != '/') cmd--;
192         *cmd = '\0';
193
194         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
195     } else {
196         specURL = arg;
197     }
198
199     specut = urlPath(specURL, &specFile);
200     if (*specFile != '/') {
201         char *s = alloca(BUFSIZ);
202         (void)getcwd(s, BUFSIZ);
203         strcat(s, "/");
204         strcat(s, arg);
205         specURL = s;
206     }
207
208     if (specut != URL_IS_DASH) {
209         struct stat st;
210         Stat(specURL, &st);
211         if (! S_ISREG(st.st_mode)) {
212             fprintf(stderr, _("File is not a regular file: %s\n"), specURL);
213             rc = 1;
214             goto exit;
215         }
216
217         /* Try to verify that the file is actually a specfile */
218         if (!isSpecFile(specURL)) {
219             fprintf(stderr, _("File %s does not appear to be a specfile.\n"),
220                 specURL);
221             rc = 1;
222             goto exit;
223         }
224     }
225     
226     /* Parse the spec file */
227 #define _anyarch(_f)    \
228 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
229     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
230                 cookie, _anyarch(buildAmount), force)) {
231         rc = 1;
232         goto exit;
233     }
234 #undef  _anyarch
235
236     /* Assemble source header from parsed components */
237     initSourceHeader(spec);
238
239     /* Check build prerequisites */
240     if (!nodeps && checkSpec(spec->sourceHeader)) {
241         rc = 1;
242         goto exit;
243     }
244
245     if (buildSpec(spec, buildAmount, test)) {
246         rc = 1;
247         goto exit;
248     }
249     
250     if (fromTarball) Unlink(specURL);
251     rc = 0;
252
253 exit:
254     if (spec)
255         freeSpec(spec);
256     if (buildRootURL)
257         xfree(buildRootURL);
258     return rc;
259 }
260
261 int build(const char * arg, struct rpmBuildArguments * ba,
262         const char * passPhrase, int fromTarball, char * cookie,
263         const char * rcfile, int force, int nodeps)
264 {
265     char *t, *te;
266     int rc = 0;
267     char *targets = ba->targets;
268 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
269     int cleanFlags = ba->buildAmount & buildCleanMask;
270
271     if (targets == NULL) {
272         rc =  buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
273                 force, nodeps);
274         goto exit;
275     }
276
277     /* parse up the build operators */
278
279     printf(_("Building target platforms: %s\n"), targets);
280
281     ba->buildAmount &= ~buildCleanMask;
282     for (t = targets; *t != '\0'; t = te) {
283         char *target;
284         if ((te = strchr(t, ',')) == NULL)
285             te = t + strlen(t);
286         target = alloca(te-t+1);
287         strncpy(target, t, (te-t));
288         target[te-t] = '\0';
289         if (*te)
290             te++;
291         else    /* XXX Perform clean-up after last target build. */
292             ba->buildAmount |= cleanFlags;
293
294         printf(_("Building for target %s\n"), target);
295
296         /* Read in configuration for target. */
297         rpmFreeMacros(NULL);
298         rpmReadConfigFiles(rcfile, target);
299         rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
300                 force, nodeps);
301         if (rc)
302             break;
303     }
304
305 exit:
306     /* Restore original configuration. */
307     rpmFreeMacros(NULL);
308     rpmReadConfigFiles(rcfile, NULL);
309     return rc;
310 }
311
312 #define POPT_USECATALOG         1000
313 #define POPT_NOLANG             1001
314 #define POPT_RMSOURCE           1002
315 #define POPT_RMBUILD            1003
316 #define POPT_BUILDROOT          1004
317 #define POPT_BUILDARCH          1005
318 #define POPT_BUILDOS            1006
319 #define POPT_TARGETPLATFORM     1007
320 #define POPT_NOBUILD            1008
321 #define POPT_SHORTCIRCUIT       1009
322 #define POPT_RMSPEC             1010
323
324 extern int noLang;
325 static int noBuild = 0;
326 static int useCatalog = 0;
327
328 static void buildArgCallback( /*@unused@*/ poptContext con,
329         /*@unused@*/ enum poptCallbackReason reason,
330         const struct poptOption * opt, const char * arg, const void * data)
331 {
332     struct rpmBuildArguments * rba = (struct rpmBuildArguments *) data;
333
334     switch (opt->val) {
335     case POPT_USECATALOG: rba->useCatalog = 1; break;
336     case POPT_NOBUILD: rba->noBuild = 1; break;
337     case POPT_NOLANG: rba->noLang = 1; break;
338     case POPT_SHORTCIRCUIT: rba->shortCircuit = 1; break;
339     case POPT_RMSOURCE: rba->buildAmount |= RPMBUILD_RMSOURCE; break;
340     case POPT_RMSPEC: rba->buildAmount |= RPMBUILD_RMSPEC; break;
341     case POPT_RMBUILD: rba->buildAmount |= RPMBUILD_RMBUILD; break;
342     case POPT_BUILDROOT:
343         if (rba->buildRootOverride) {
344             fprintf(stderr, _("buildroot already specified"));
345             exit(EXIT_FAILURE);
346             /*@notreached@*/
347         }
348         rba->buildRootOverride = xstrdup(arg);
349         break;
350     case POPT_BUILDARCH:
351         fprintf(stderr, _("--buildarch has been obsoleted.  Use the --target option instead.\n")); 
352         exit(EXIT_FAILURE);
353         /*@notreached@*/ break;
354     case POPT_BUILDOS:
355         fprintf(stderr, _("--buildos has been obsoleted.  Use the --target option instead.\n")); 
356         exit(EXIT_FAILURE);
357         /*@notreached@*/ break;
358     case POPT_TARGETPLATFORM:
359         if (rba->targets) {
360             int len = strlen(rba->targets) + 1 + strlen(arg) + 1;
361             rba->targets = xrealloc(rba->targets, len);
362             strcat(rba->targets, ",");
363         } else {
364             rba->targets = xmalloc(strlen(arg) + 1);
365             rba->targets[0] = '\0';
366         }
367         strcat(rba->targets, arg);
368         break;
369     }
370 }
371
372 struct poptOption rpmBuildPoptTable[] = {
373         { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
374                 buildArgCallback, 0, NULL, NULL },
375         { "buildarch", '\0', POPT_ARG_STRING, 0,  POPT_BUILDARCH,
376                 N_("override build architecture"), "ARCH" },
377         { "buildos", '\0', POPT_ARG_STRING, 0,  POPT_BUILDOS,
378                 N_("override build operating system"), "OS" },
379         { "buildroot", '\0', POPT_ARG_STRING, 0,  POPT_BUILDROOT,
380                 N_("override build root"), "DIRECTORY" },
381         { "clean", '\0', 0, 0, POPT_RMBUILD,
382                 N_("remove build tree when done"), NULL},
383         { "nobuild", '\0', 0, &noBuild,  POPT_NOBUILD,
384                 N_("do not execute any stages of the build"), NULL },
385         { "nolang", '\0', 0, &noLang, POPT_NOLANG,
386                 N_("do not accept I18N msgstr's from specfile"), NULL},
387         { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
388                 N_("remove sources when done"), NULL},
389         { "rmspec", '\0', 0, 0, POPT_RMSPEC,
390                 N_("remove specfile when done"), NULL},
391         { "short-circuit", '\0', 0, 0,  POPT_SHORTCIRCUIT,
392                 N_("skip straight to specified stage (only for c,i)"), NULL },
393         { "target", '\0', POPT_ARG_STRING, 0,  POPT_TARGETPLATFORM,
394                 N_("override target platform"), "CPU-VENDOR-OS" },
395         { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
396                 N_("lookup I18N strings in specfile catalog"), NULL},
397         { 0, 0, 0, 0, 0,        NULL, NULL }
398 };