- fix: remove rpmfi scareMem so that headers can be reloaded on ia64.
[platform/upstream/rpm.git] / build / build.c
1 /** \ingroup rpmbuild
2  * \file build/build.c
3  *  Top-level build dispatcher.
4  */
5
6 #include "system.h"
7
8 #include <rpmio_internal.h>
9 #include <rpmbuild.h>
10
11 #include "debug.h"
12
13 /*@unchecked@*/
14 static int _build_debug = 0;
15
16 /*@access StringBuf @*/
17 /*@access urlinfo @*/           /* XXX compared with NULL */
18 /*@access FD_t @*/
19
20 /**
21  */
22 static void doRmSource(Spec spec)
23         /*@globals rpmGlobalMacroContext, fileSystem, internalState @*/
24         /*@modifies rpmGlobalMacroContext, fileSystem, internalState  @*/
25 {
26     struct Source *p;
27     Package pkg;
28     int rc;
29     
30 #if 0
31     rc = Unlink(spec->specFile);
32 #endif
33
34     for (p = spec->sources; p != NULL; p = p->next) {
35         if (! (p->flags & RPMBUILD_ISNO)) {
36             const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
37             rc = Unlink(fn);
38             fn = _free(fn);
39         }
40     }
41
42     for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
43         for (p = pkg->icon; p != NULL; p = p->next) {
44             if (! (p->flags & RPMBUILD_ISNO)) {
45                 const char *fn = rpmGetPath("%{_sourcedir}/", p->source, NULL);
46                 rc = Unlink(fn);
47                 fn = _free(fn);
48             }
49         }
50     }
51 }
52
53 /*
54  * @todo Single use by %%doc in files.c prevents static.
55  */
56 int doScript(Spec spec, int what, const char *name, StringBuf sb, int test)
57 {
58     const char * rootURL = spec->rootURL;
59     const char * rootDir;
60     const char *scriptName = NULL;
61     const char * buildDirURL = rpmGenPath(rootURL, "%{_builddir}", "");
62     const char * buildScript;
63     const char * buildCmd = NULL;
64     const char * buildTemplate = NULL;
65     const char * buildPost = NULL;
66     const char * mTemplate = NULL;
67     const char * mPost = NULL;
68     int argc = 0;
69     const char **argv = NULL;
70     FILE * fp = NULL;
71     urlinfo u = NULL;
72
73     FD_t fd;
74     FD_t xfd;
75     int child;
76     int status;
77     int rc;
78     
79     /*@-branchstate@*/
80     switch (what) {
81     case RPMBUILD_PREP:
82         name = "%prep";
83         sb = spec->prep;
84         mTemplate = "%{__spec_prep_template}";
85         mPost = "%{__spec_prep_post}";
86         break;
87     case RPMBUILD_BUILD:
88         name = "%build";
89         sb = spec->build;
90         mTemplate = "%{__spec_build_template}";
91         mPost = "%{__spec_build_post}";
92         break;
93     case RPMBUILD_INSTALL:
94         name = "%install";
95         sb = spec->install;
96         mTemplate = "%{__spec_install_template}";
97         mPost = "%{__spec_install_post}";
98         break;
99     case RPMBUILD_CHECK:
100         name = "%check";
101         sb = spec->check;
102         mTemplate = "%{__spec_check_template}";
103         mPost = "%{__spec_check_post}";
104         break;
105     case RPMBUILD_CLEAN:
106         name = "%clean";
107         sb = spec->clean;
108         mTemplate = "%{__spec_clean_template}";
109         mPost = "%{__spec_clean_post}";
110         break;
111     case RPMBUILD_RMBUILD:
112         name = "--clean";
113         mTemplate = "%{__spec_clean_template}";
114         mPost = "%{__spec_clean_post}";
115         break;
116     case RPMBUILD_STRINGBUF:
117     default:
118         mTemplate = "%{___build_template}";
119         mPost = "%{___build_post}";
120         break;
121     }
122     /*@=branchstate@*/
123
124     if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
125         rc = 0;
126         goto exit;
127     }
128     
129     if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
130         rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
131         rc = RPMERR_SCRIPT;
132         goto exit;
133     }
134
135 #ifdef HAVE_FCHMOD
136     switch (rootut) {
137     case URL_IS_PATH:
138     case URL_IS_UNKNOWN:
139         (void)fchmod(Fileno(fd), 0600);
140         break;
141     default:
142         break;
143     }
144 #endif
145
146     /*@-branchstate@*/
147     if (fdGetFp(fd) == NULL)
148         xfd = Fdopen(fd, "w.fpio");
149     else
150         xfd = fd;
151     /*@=branchstate@*/
152
153     /*@-type@*/ /* FIX: cast? */
154     if ((fp = fdGetFp(xfd)) == NULL) {
155         rc = RPMERR_SCRIPT;
156         goto exit;
157     }
158     /*@=type@*/
159     
160     (void) urlPath(rootURL, &rootDir);
161     /*@-branchstate@*/
162     if (*rootDir == '\0') rootDir = "/";
163     /*@=branchstate@*/
164
165     (void) urlPath(scriptName, &buildScript);
166
167     buildTemplate = rpmExpand(mTemplate, NULL);
168     buildPost = rpmExpand(mPost, NULL);
169
170     (void) fputs(buildTemplate, fp);
171
172     if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
173         fprintf(fp, "cd %s\n", spec->buildSubdir);
174
175     if (what == RPMBUILD_RMBUILD) {
176         if (spec->buildSubdir)
177             fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
178     } else
179         fprintf(fp, "%s", getStringBuf(sb));
180
181     (void) fputs(buildPost, fp);
182     
183     (void) Fclose(xfd);
184
185     if (test) {
186         rc = 0;
187         goto exit;
188     }
189     
190 if (_build_debug)
191 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
192 /*@-boundsread@*/
193     if (buildDirURL && buildDirURL[0] != '/' &&
194         (urlSplit(buildDirURL, &u) != 0)) {
195         rc = RPMERR_SCRIPT;
196         goto exit;
197     }
198 /*@=boundsread@*/
199     if (u != NULL) {
200         switch (u->urltype) {
201         case URL_IS_FTP:
202 if (_build_debug)
203 fprintf(stderr, "*** addMacros\n");
204             addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
205             addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
206             if (strcmp(rootDir, "/"))
207                 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
208             break;
209         case URL_IS_HTTP:
210         default:
211             break;
212         }
213     }
214
215     buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
216     (void) poptParseArgvString(buildCmd, &argc, &argv);
217
218     rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
219     if (!(child = fork())) {
220
221         /*@-mods@*/
222         errno = 0;
223         /*@=mods@*/
224 /*@-boundsread@*/
225         (void) execvp(argv[0], (char *const *)argv);
226 /*@=boundsread@*/
227
228         rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
229                 scriptName, name, strerror(errno));
230
231         _exit(-1);
232     }
233
234     rc = waitpid(child, &status, 0);
235
236     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
237         rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
238                  scriptName, name);
239         rc = RPMERR_SCRIPT;
240     } else
241         rc = 0;
242     
243 exit:
244     if (scriptName) {
245         if (!rc)
246             (void) Unlink(scriptName);
247         scriptName = _free(scriptName);
248     }
249     if (u != NULL) {
250         switch (u->urltype) {
251         case URL_IS_FTP:
252         case URL_IS_HTTP:
253 if (_build_debug)
254 fprintf(stderr, "*** delMacros\n");
255             delMacro(spec->macros, "_remsh");
256             delMacro(spec->macros, "_remhost");
257             if (strcmp(rootDir, "/"))
258                 delMacro(spec->macros, "_remroot");
259             break;
260         default:
261             break;
262         }
263     }
264     argv = _free(argv);
265     buildCmd = _free(buildCmd);
266     buildTemplate = _free(buildTemplate);
267     buildPost = _free(buildPost);
268     buildDirURL = _free(buildDirURL);
269
270     return rc;
271 }
272
273 int buildSpec(rpmts ts, Spec spec, int what, int test)
274 {
275     int rc = 0;
276
277     if (!spec->recursing && spec->BACount) {
278         int x;
279         /* When iterating over BANames, do the source    */
280         /* packaging on the first run, and skip RMSOURCE altogether */
281         if (spec->BASpecs != NULL)
282         for (x = 0; x < spec->BACount; x++) {
283 /*@-boundsread@*/
284             if ((rc = buildSpec(ts, spec->BASpecs[x],
285                                 (what & ~RPMBUILD_RMSOURCE) |
286                                 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
287                                 test))) {
288                 goto exit;
289             }
290 /*@=boundsread@*/
291         }
292     } else {
293         if ((what & RPMBUILD_PREP) &&
294             (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
295                 goto exit;
296
297         if ((what & RPMBUILD_BUILD) &&
298             (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
299                 goto exit;
300
301         if ((what & RPMBUILD_INSTALL) &&
302             (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
303                 goto exit;
304
305         if ((what & RPMBUILD_CHECK) &&
306             (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
307                 goto exit;
308
309         if ((what & RPMBUILD_PACKAGESOURCE) &&
310             (rc = processSourceFiles(spec)))
311                 goto exit;
312
313         if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
314             (what & RPMBUILD_FILECHECK)) &&
315             (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
316                 goto exit;
317
318         if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
319             (rc = packageSources(spec)))
320                 return rc;
321
322         if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
323             (rc = packageBinaries(spec)))
324                 goto exit;
325         
326         if ((what & RPMBUILD_CLEAN) &&
327             (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
328                 goto exit;
329
330         if ((what & RPMBUILD_RMBUILD) &&
331             (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
332                 goto exit;
333     }
334
335     if (what & RPMBUILD_RMSOURCE)
336         doRmSource(spec);
337
338     if (what & RPMBUILD_RMSPEC)
339         (void) Unlink(spec->specFile);
340
341 exit:
342     if (rc && rpmlogGetNrecs() > 0) {
343         rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
344         rpmlogPrint(NULL);
345     }
346
347     return rc;
348 }