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