- tsort prefers presentation order.
[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 #include "debug.h"
9
10 static int checkSpec(Header h)
11 {
12     char *rootdir = NULL;
13     rpmdb db = NULL;
14     int mode = O_RDONLY;
15     rpmTransactionSet ts;
16     struct rpmDependencyConflict * conflicts;
17     int numConflicts;
18     int rc;
19
20     if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
21         return 0;
22
23     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
24         const char *dn;
25         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
26         rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\n"), dn);
27         free((void *)dn);
28         exit(EXIT_FAILURE);
29     }
30     ts = rpmtransCreateSet(db, rootdir);
31
32     rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
33
34     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
35     if (rc == 0 && conflicts) {
36         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
37         printDepProblems(stderr, conflicts, numConflicts);
38         rpmdepFreeConflicts(conflicts, numConflicts);
39         rc = 1;
40     }
41
42     if (ts)
43         rpmtransFree(ts);
44     if (db)
45         rpmdbClose(db);
46
47     return rc;
48 }
49
50 /*
51  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
52  * angielsku...
53  */
54 /* XXX this is still a dumb test but at least it's i18n aware */
55 static int isSpecFile(const char *specfile)
56 {
57     char buf[256];
58     const char * s;
59     FD_t fd;
60     int count;
61     int checking;
62
63     fd = Fopen(specfile, "r.ufdio");
64     if (fd == NULL || Ferror(fd)) {
65         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
66                 specfile, Fstrerror(fd));
67         return 0;
68     }
69     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
70     Fclose(fd);
71
72     checking = 1;
73     for (s = buf; count--; s++) {
74         switch (*s) {
75         case '\r':
76         case '\n':
77             checking = 1;
78             break;
79         case ':':
80             checking = 0;
81             break;
82         default:
83             if (checking && !(isprint(*s) || isspace(*s))) return 0;
84             break;
85         }
86     }
87     return 1;
88 }
89
90 static int buildForTarget(const char *arg, struct rpmBuildArguments *ba,
91         const char *passPhrase, char *cookie)
92 {
93     int buildAmount = ba->buildAmount;
94     const char *buildRootURL = NULL;
95     const char * specFile;
96     const char * specURL;
97     int specut;
98     char buf[BUFSIZ];
99     Spec spec = NULL;
100     int rc;
101
102 #ifndef DYING
103     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
104 #endif
105
106     if (ba->buildRootOverride)
107         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
108
109     if (ba->buildMode == 't') {
110         FILE *fp;
111         const char *specDir;
112         const char * tmpSpecFile;
113         char * cmd, *s;
114         rpmCompressedMagic res = COMPRESSED_OTHER;
115         static const char *zcmds[] = { "cat", "gunzip", "bunzip2", "cat" };
116
117         specDir = rpmGetPath("%{_specdir}", NULL);
118
119         {   char tfn[64];
120             strcpy(tfn, "rpm-spec.XXXXXX");
121             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
122         }
123
124         isCompressed(arg, &res);
125
126         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
127         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
128                         zcmds[res & 0x3], arg, tmpSpecFile);
129         if (!(fp = popen(cmd, "r"))) {
130             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
131             free((void *)specDir);
132             free((void *)tmpSpecFile);
133             return 1;
134         }
135         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
136             /* Try again */
137             pclose(fp);
138
139             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
140                     zcmds[res & 0x3], arg, tmpSpecFile);
141             if (!(fp = popen(cmd, "r"))) {
142                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
143                 free((void *)specDir);
144                 free((void *)tmpSpecFile);
145                 return 1;
146             }
147             if (!fgets(buf, sizeof(buf) - 1, fp)) {
148                 /* Give up */
149                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
150                         arg);
151                 unlink(tmpSpecFile);
152                 free((void *)specDir);
153                 free((void *)tmpSpecFile);
154                 return 1;
155             }
156         }
157         pclose(fp);
158
159         cmd = s = buf;
160         while (*cmd) {
161             if (*cmd == '/') s = cmd + 1;
162             cmd++;
163         }
164
165         cmd = s;
166
167         /* remove trailing \n */
168         s = cmd + strlen(cmd) - 1;
169         *s = '\0';
170
171         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
172         sprintf(s, "%s/%s", specDir, cmd);
173         res = rename(tmpSpecFile, s);
174         free((void *)specDir);
175         
176         if (res) {
177             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
178                         tmpSpecFile, s);
179             unlink(tmpSpecFile);
180             free((void *)tmpSpecFile);
181             return 1;
182         }
183         free((void *)tmpSpecFile);
184
185         /* Make the directory which contains the tarball the source 
186            directory for this run */
187
188         if (*arg != '/') {
189             (void)getcwd(buf, BUFSIZ);
190             strcat(buf, "/");
191             strcat(buf, arg);
192         } else 
193             strcpy(buf, arg);
194
195         cmd = buf + strlen(buf) - 1;
196         while (*cmd != '/') cmd--;
197         *cmd = '\0';
198
199         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
200     } else {
201         specURL = arg;
202     }
203
204     specut = urlPath(specURL, &specFile);
205     if (*specFile != '/') {
206         char *s = alloca(BUFSIZ);
207         (void)getcwd(s, BUFSIZ);
208         strcat(s, "/");
209         strcat(s, arg);
210         specURL = s;
211     }
212
213     if (specut != URL_IS_DASH) {
214         struct stat st;
215         if (Stat(specURL, &st) < 0) {
216             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
217             rc = 1;
218             goto exit;
219         }
220         if (! S_ISREG(st.st_mode)) {
221             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
222                 specURL);
223             rc = 1;
224             goto exit;
225         }
226
227         /* Try to verify that the file is actually a specfile */
228         if (!isSpecFile(specURL)) {
229             rpmError(RPMERR_BADSPEC,
230                 _("File %s does not appear to be a specfile.\n"), specURL);
231             rc = 1;
232             goto exit;
233         }
234     }
235     
236     /* Parse the spec file */
237 #define _anyarch(_f)    \
238 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
239     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
240                 cookie, _anyarch(buildAmount), ba->force)) {
241         rc = 1;
242         goto exit;
243     }
244 #undef  _anyarch
245
246     /* Assemble source header from parsed components */
247     initSourceHeader(spec);
248
249     /* Check build prerequisites */
250     if (!ba->noDeps && checkSpec(spec->sourceHeader)) {
251         rc = 1;
252         goto exit;
253     }
254
255     if (buildSpec(spec, buildAmount, ba->noBuild)) {
256         rc = 1;
257         goto exit;
258     }
259     
260     if (ba->buildMode == 't') Unlink(specURL);
261     rc = 0;
262
263 exit:
264     if (spec)
265         freeSpec(spec);
266     if (buildRootURL)
267         free((void *)buildRootURL);
268     return rc;
269 }
270
271 int build(const char * arg, struct rpmBuildArguments * ba,
272         const char * passPhrase, char * cookie, const char * rcfile)
273 {
274     char *t, *te;
275     int rc = 0;
276     char *targets = ba->targets;
277 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
278     int cleanFlags = ba->buildAmount & buildCleanMask;
279
280     if (targets == NULL) {
281         rc =  buildForTarget(arg, ba, passPhrase, cookie);
282         goto exit;
283     }
284
285     /* parse up the build operators */
286
287     printf(_("Building target platforms: %s\n"), targets);
288
289     ba->buildAmount &= ~buildCleanMask;
290     for (t = targets; *t != '\0'; t = te) {
291         char *target;
292         if ((te = strchr(t, ',')) == NULL)
293             te = t + strlen(t);
294         target = alloca(te-t+1);
295         strncpy(target, t, (te-t));
296         target[te-t] = '\0';
297         if (*te)
298             te++;
299         else    /* XXX Perform clean-up after last target build. */
300             ba->buildAmount |= cleanFlags;
301
302         printf(_("Building for target %s\n"), target);
303
304         /* Read in configuration for target. */
305         rpmFreeMacros(NULL);
306         rpmReadConfigFiles(rcfile, target);
307         rc = buildForTarget(arg, ba, passPhrase, cookie);
308         if (rc)
309             break;
310     }
311
312 exit:
313     /* Restore original configuration. */
314     rpmFreeMacros(NULL);
315     rpmReadConfigFiles(rcfile, NULL);
316     return rc;
317 }