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