The death of lib/ftp.c, merged into lib/rpmio.c.
[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 *buildRoot = ba->buildRootOverride;
94     int test = ba->noBuild;
95     FILE *f;
96     const char * specfile;
97     struct stat statbuf;
98     char buf[BUFSIZ];
99     Spec spec = NULL;
100
101     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
102
103     if (fromTarball) {
104         const char *specDir;
105         const char * tmpSpecFile;
106         char * cmd, *s;
107         int res;
108
109         specDir = rpmGetPath("%{_specdir}", NULL);
110
111         {   char tfn[64];
112             strcpy(tfn, "rpm-spec.XXXXXX");
113             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
114         }
115
116         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
117         sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s",
118                         arg, tmpSpecFile);
119         if (!(f = popen(cmd, "r"))) {
120             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
121                         strerror(errno));
122             xfree(specDir);
123             xfree(tmpSpecFile);
124             return 1;
125         }
126         if ((!fgets(buf, sizeof(buf) - 1, f)) || !strchr(buf, '/')) {
127             /* Try again */
128             pclose(f);
129
130             sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
131                     tmpSpecFile);
132             if (!(f = popen(cmd, "r"))) {
133                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
134                         strerror(errno));
135                 xfree(specDir);
136                 xfree(tmpSpecFile);
137                 return 1;
138             }
139             if (!fgets(buf, sizeof(buf) - 1, f)) {
140                 /* Give up */
141                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
142                 unlink(tmpSpecFile);
143                 xfree(specDir);
144                 xfree(tmpSpecFile);
145                 return 1;
146             }
147         }
148         pclose(f);
149
150         cmd = s = buf;
151         while (*cmd) {
152             if (*cmd == '/') s = cmd + 1;
153             cmd++;
154         }
155
156         cmd = s;
157
158         /* remove trailing \n */
159         s = cmd + strlen(cmd) - 1;
160         *s = '\0';
161
162         specfile = s = alloca(strlen(specDir) + strlen(cmd) + 5);
163         sprintf(s, "%s/%s", specDir, cmd);
164         res = rename(tmpSpecFile, s);
165         xfree(specDir);
166         xfree(tmpSpecFile);
167         
168         if (res) {
169             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
170                     tmpSpecFile, s, strerror(errno));
171             unlink(tmpSpecFile);
172             return 1;
173         }
174
175         /* Make the directory which contains the tarball the source 
176            directory for this run */
177
178         if (*arg != '/') {
179             (void)getcwd(buf, BUFSIZ);
180             strcat(buf, "/");
181             strcat(buf, arg);
182         } else 
183             strcpy(buf, arg);
184
185         cmd = buf + strlen(buf) - 1;
186         while (*cmd != '/') cmd--;
187         *cmd = '\0';
188
189         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
190     } else {
191         specfile = arg;
192     }
193
194     switch (urlIsURL(specfile)) {
195     case URL_IS_UNKNOWN:
196         if (arg[0] == '/') {
197             specfile = arg;
198         } else {
199             char *s = alloca(BUFSIZ);
200             (void)getcwd(s, BUFSIZ);
201             strcat(s, "/");
202             strcat(s, arg);
203             specfile = s;
204         }
205         stat(specfile, &statbuf);
206         if (! S_ISREG(statbuf.st_mode)) {
207             fprintf(stderr, _("File is not a regular file: %s\n"), specfile);
208             return 1;
209         }
210
211         /* Try to verify that the file is actually a specfile */
212         if (!isSpecFile(specfile)) {
213             fprintf(stderr, _("File %s does not appear to be a specfile.\n"),
214                 specfile);
215             return 1;
216         }
217         break;
218     default:
219         break;
220     }
221     
222     /* Parse the spec file */
223 #define _anyarch(_f)    \
224 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
225     if (parseSpec(&spec, specfile, ba->rootdir, buildRoot, 0, passPhrase,
226         cookie, _anyarch(buildAmount), force)) {
227             return 1;
228     }
229 #undef  _anyarch
230
231     /* Assemble source header from parsed components */
232     initSourceHeader(spec);
233
234     /* Check build prerequisites */
235     if (!nodeps && checkSpec(spec->sourceHeader)) {
236         freeSpec(spec);
237         return 1;
238     }
239
240     if (buildSpec(spec, buildAmount, test)) {
241         freeSpec(spec);
242         return 1;
243     }
244     
245     if (fromTarball) unlink(specfile);
246
247     freeSpec(spec);
248     
249     return 0;
250 }
251
252 int build(const char *arg, struct rpmBuildArguments *ba, const char *passPhrase,
253           int fromTarball, char *cookie, const char * rcfile, int force,
254           int nodeps)
255 {
256     char *t, *te;
257     int rc = 0;
258     char *targets = ba->targets;
259 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
260     int cleanFlags = ba->buildAmount & buildCleanMask;
261
262     if (targets == NULL) {
263         rc =  buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
264                 force, nodeps);
265         goto exit;
266     }
267
268     /* parse up the build operators */
269
270     printf(_("Building target platforms: %s\n"), targets);
271
272     ba->buildAmount &= ~buildCleanMask;
273     for (t = targets; *t != '\0'; t = te) {
274         char *target;
275         if ((te = strchr(t, ',')) == NULL)
276             te = t + strlen(t);
277         target = alloca(te-t+1);
278         strncpy(target, t, (te-t));
279         target[te-t] = '\0';
280         if (*te)
281             te++;
282         else    /* XXX Perform clean-up after last target build. */
283             ba->buildAmount |= cleanFlags;
284
285         printf(_("Building for target %s\n"), target);
286
287         /* Read in configuration for target. */
288         freeMacros(NULL);       /* XXX macros from CLI are destroyed too */
289         rpmReadConfigFiles(rcfile, target);
290         rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
291                 force, nodeps);
292         if (rc)
293             break;
294     }
295
296 exit:
297     /* Restore original configuration. */
298     freeMacros(NULL);   /* XXX macros from CLI are destroyed too */
299     rpmReadConfigFiles(rcfile, NULL);
300     return rc;
301 }
302
303 #define POPT_USECATALOG         1000
304 #define POPT_NOLANG             1001
305 #define POPT_RMSOURCE           1002
306 #define POPT_RMBUILD            1003
307 #define POPT_BUILDROOT          1004
308 #define POPT_BUILDARCH          1005
309 #define POPT_BUILDOS            1006
310 #define POPT_TARGETPLATFORM     1007
311 #define POPT_NOBUILD            1008
312 #define POPT_SHORTCIRCUIT       1009
313 #define POPT_RMSPEC             1010
314
315 extern int noLang;
316 static int noBuild = 0;
317 static int useCatalog = 0;
318
319 static void buildArgCallback( /*@unused@*/ poptContext con,
320         /*@unused@*/ enum poptCallbackReason reason,
321         const struct poptOption * opt, const char * arg, const void * data)
322 {
323     struct rpmBuildArguments * rba = (struct rpmBuildArguments *) data;
324
325     switch (opt->val) {
326     case POPT_USECATALOG: rba->useCatalog = 1; break;
327     case POPT_NOBUILD: rba->noBuild = 1; break;
328     case POPT_NOLANG: rba->noLang = 1; break;
329     case POPT_SHORTCIRCUIT: rba->shortCircuit = 1; break;
330     case POPT_RMSOURCE: rba->buildAmount |= RPMBUILD_RMSOURCE; break;
331     case POPT_RMSPEC: rba->buildAmount |= RPMBUILD_RMSPEC; break;
332     case POPT_RMBUILD: rba->buildAmount |= RPMBUILD_RMBUILD; break;
333     case POPT_BUILDROOT:
334         if (rba->buildRootOverride) {
335             fprintf(stderr, _("buildroot already specified"));
336             exit(EXIT_FAILURE);
337             /*@notreached@*/
338         }
339         rba->buildRootOverride = xstrdup(arg);
340         break;
341     case POPT_BUILDARCH:
342         fprintf(stderr, _("--buildarch has been obsoleted.  Use the --target option instead.\n")); 
343         exit(EXIT_FAILURE);
344         /*@notreached@*/ break;
345     case POPT_BUILDOS:
346         fprintf(stderr, _("--buildos has been obsoleted.  Use the --target option instead.\n")); 
347         exit(EXIT_FAILURE);
348         /*@notreached@*/ break;
349     case POPT_TARGETPLATFORM:
350         if (rba->targets) {
351             int len = strlen(rba->targets) + 1 + strlen(arg) + 1;
352             rba->targets = xrealloc(rba->targets, len);
353             strcat(rba->targets, ",");
354         } else {
355             rba->targets = xmalloc(strlen(arg) + 1);
356             rba->targets[0] = '\0';
357         }
358         strcat(rba->targets, arg);
359         break;
360     }
361 }
362
363 struct poptOption rpmBuildPoptTable[] = {
364         { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
365                 buildArgCallback, 0, NULL, NULL },
366         { "buildarch", '\0', POPT_ARG_STRING, 0,  POPT_BUILDARCH,
367                 N_("override build architecture"), "ARCH" },
368         { "buildos", '\0', POPT_ARG_STRING, 0,  POPT_BUILDOS,
369                 N_("override build operating system"), "OS" },
370         { "buildroot", '\0', POPT_ARG_STRING, 0,  POPT_BUILDROOT,
371                 N_("override build root"), "DIRECTORY" },
372         { "clean", '\0', 0, 0, POPT_RMBUILD,
373                 N_("remove build tree when done"), NULL},
374         { "nobuild", '\0', 0, &noBuild,  POPT_NOBUILD,
375                 N_("do not execute any stages of the build"), NULL },
376         { "nolang", '\0', 0, &noLang, POPT_NOLANG,
377                 N_("do not accept I18N msgstr's from specfile"), NULL},
378         { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
379                 N_("remove sources when done"), NULL},
380         { "rmspec", '\0', 0, 0, POPT_RMSPEC,
381                 N_("remove specfile when done"), NULL},
382         { "short-circuit", '\0', 0, 0,  POPT_SHORTCIRCUIT,
383                 N_("skip straight to specified stage (only for c,i)"), NULL },
384         { "target", '\0', POPT_ARG_STRING, 0,  POPT_TARGETPLATFORM,
385                 N_("override target platform"), "CPU-VENDOR-OS" },
386         { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
387                 N_("lookup I18N strings in specfile catalog"), NULL},
388         { 0, 0, 0, 0, 0,        NULL, NULL }
389 };