Bring header reggions mods back to top of stack.
[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         rpmError(RPMERR_OPEN, _("cannot open rpm database in %s\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         rpmError(RPMERR_OPEN, _("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 #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 (fromTarball) {
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"));
131             xfree(specDir);
132             xfree(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"));
143                 xfree(specDir);
144                 xfree(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"), 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         
175         if (res) {
176             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m"),
177                         tmpSpecFile, s);
178             unlink(tmpSpecFile);
179             xfree(tmpSpecFile);
180             return 1;
181         }
182         xfree(tmpSpecFile);
183
184         /* Make the directory which contains the tarball the source 
185            directory for this run */
186
187         if (*arg != '/') {
188             (void)getcwd(buf, BUFSIZ);
189             strcat(buf, "/");
190             strcat(buf, arg);
191         } else 
192             strcpy(buf, arg);
193
194         cmd = buf + strlen(buf) - 1;
195         while (*cmd != '/') cmd--;
196         *cmd = '\0';
197
198         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
199     } else {
200         specURL = arg;
201     }
202
203     specut = urlPath(specURL, &specFile);
204     if (*specFile != '/') {
205         char *s = alloca(BUFSIZ);
206         (void)getcwd(s, BUFSIZ);
207         strcat(s, "/");
208         strcat(s, arg);
209         specURL = s;
210     }
211
212     if (specut != URL_IS_DASH) {
213         struct stat st;
214         if (Stat(specURL, &st) < 0) {
215             rpmError(RPMERR_STAT, _("failed to stat %s: %m"), specURL);
216             rc = 1;
217             goto exit;
218         }
219         if (! S_ISREG(st.st_mode)) {
220             rpmError(RPMERR_NOTREG, _("File %s is not a regular file."),
221                 specURL);
222             rc = 1;
223             goto exit;
224         }
225
226         /* Try to verify that the file is actually a specfile */
227         if (!isSpecFile(specURL)) {
228             rpmError(RPMERR_BADSPEC, _("File %s does not appear to be a specfile."),
229                 specURL);
230             rc = 1;
231             goto exit;
232         }
233     }
234     
235     /* Parse the spec file */
236 #define _anyarch(_f)    \
237 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
238     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
239                 cookie, _anyarch(buildAmount), force)) {
240         rc = 1;
241         goto exit;
242     }
243 #undef  _anyarch
244
245     /* Assemble source header from parsed components */
246     initSourceHeader(spec);
247
248     /* Check build prerequisites */
249     if (!nodeps && checkSpec(spec->sourceHeader)) {
250         rc = 1;
251         goto exit;
252     }
253
254     if (buildSpec(spec, buildAmount, test)) {
255         rc = 1;
256         goto exit;
257     }
258     
259     if (fromTarball) Unlink(specURL);
260     rc = 0;
261
262 exit:
263     if (spec)
264         freeSpec(spec);
265     if (buildRootURL)
266         xfree(buildRootURL);
267     return rc;
268 }
269
270 /** */
271 int build(const char * arg, struct rpmBuildArguments * ba,
272         const char * passPhrase, int fromTarball, char * cookie,
273         const char * rcfile, int force, int nodeps)
274 {
275     char *t, *te;
276     int rc = 0;
277     char *targets = ba->targets;
278 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
279     int cleanFlags = ba->buildAmount & buildCleanMask;
280
281     if (targets == NULL) {
282         rc =  buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
283                 force, nodeps);
284         goto exit;
285     }
286
287     /* parse up the build operators */
288
289     printf(_("Building target platforms: %s\n"), targets);
290
291     ba->buildAmount &= ~buildCleanMask;
292     for (t = targets; *t != '\0'; t = te) {
293         char *target;
294         if ((te = strchr(t, ',')) == NULL)
295             te = t + strlen(t);
296         target = alloca(te-t+1);
297         strncpy(target, t, (te-t));
298         target[te-t] = '\0';
299         if (*te)
300             te++;
301         else    /* XXX Perform clean-up after last target build. */
302             ba->buildAmount |= cleanFlags;
303
304         printf(_("Building for target %s\n"), target);
305
306         /* Read in configuration for target. */
307         rpmFreeMacros(NULL);
308         rpmReadConfigFiles(rcfile, target);
309         rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
310                 force, nodeps);
311         if (rc)
312             break;
313     }
314
315 exit:
316     /* Restore original configuration. */
317     rpmFreeMacros(NULL);
318     rpmReadConfigFiles(rcfile, NULL);
319     return rc;
320 }