Stub in URL_IS_HTTPS everywhere.
[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, h_errno, 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     if (name == NULL)   /* XXX shouldn't happen */
123         name = "???";
124     /*@=branchstate@*/
125
126     if ((what != RPMBUILD_RMBUILD) && sb == NULL) {
127         rc = 0;
128         goto exit;
129     }
130     
131     if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) {
132         rpmError(RPMERR_SCRIPT, _("Unable to open temp file.\n"));
133         rc = RPMERR_SCRIPT;
134         goto exit;
135     }
136
137 #ifdef HAVE_FCHMOD
138     switch (rootut) {
139     case URL_IS_PATH:
140     case URL_IS_UNKNOWN:
141         (void)fchmod(Fileno(fd), 0600);
142         break;
143     default:
144         break;
145     }
146 #endif
147
148     /*@-branchstate@*/
149     if (fdGetFp(fd) == NULL)
150         xfd = Fdopen(fd, "w.fpio");
151     else
152         xfd = fd;
153     /*@=branchstate@*/
154
155     /*@-type@*/ /* FIX: cast? */
156     if ((fp = fdGetFp(xfd)) == NULL) {
157         rc = RPMERR_SCRIPT;
158         goto exit;
159     }
160     /*@=type@*/
161     
162     (void) urlPath(rootURL, &rootDir);
163     /*@-branchstate@*/
164     if (*rootDir == '\0') rootDir = "/";
165     /*@=branchstate@*/
166
167     (void) urlPath(scriptName, &buildScript);
168
169     buildTemplate = rpmExpand(mTemplate, NULL);
170     buildPost = rpmExpand(mPost, NULL);
171
172     (void) fputs(buildTemplate, fp);
173
174     if (what != RPMBUILD_PREP && what != RPMBUILD_RMBUILD && spec->buildSubdir)
175         fprintf(fp, "cd %s\n", spec->buildSubdir);
176
177     if (what == RPMBUILD_RMBUILD) {
178         if (spec->buildSubdir)
179             fprintf(fp, "rm -rf %s\n", spec->buildSubdir);
180     } else if (sb != NULL)
181         fprintf(fp, "%s", getStringBuf(sb));
182
183     (void) fputs(buildPost, fp);
184     
185     (void) Fclose(xfd);
186
187     if (test) {
188         rc = 0;
189         goto exit;
190     }
191     
192 if (_build_debug)
193 fprintf(stderr, "*** rootURL %s buildDirURL %s\n", rootURL, buildDirURL);
194 /*@-boundsread@*/
195     if (buildDirURL && buildDirURL[0] != '/' &&
196         (urlSplit(buildDirURL, &u) != 0)) {
197         rc = RPMERR_SCRIPT;
198         goto exit;
199     }
200 /*@=boundsread@*/
201     if (u != NULL) {
202         switch (u->urltype) {
203         case URL_IS_HTTPS:
204         case URL_IS_HTTP:
205         case URL_IS_FTP:
206 if (_build_debug)
207 fprintf(stderr, "*** addMacros\n");
208             addMacro(spec->macros, "_remsh", NULL, "%{__remsh}", RMIL_SPEC);
209             addMacro(spec->macros, "_remhost", NULL, u->host, RMIL_SPEC);
210             if (strcmp(rootDir, "/"))
211                 addMacro(spec->macros, "_remroot", NULL, rootDir, RMIL_SPEC);
212             break;
213         default:
214             break;
215         }
216     }
217
218     buildCmd = rpmExpand("%{___build_cmd}", " ", buildScript, NULL);
219     (void) poptParseArgvString(buildCmd, &argc, &argv);
220
221     rpmMessage(RPMMESS_NORMAL, _("Executing(%s): %s\n"), name, buildCmd);
222     if (!(child = fork())) {
223
224         /*@-mods@*/
225         errno = 0;
226         /*@=mods@*/
227 /*@-boundsread@*/
228         (void) execvp(argv[0], (char *const *)argv);
229 /*@=boundsread@*/
230
231         rpmError(RPMERR_SCRIPT, _("Exec of %s failed (%s): %s\n"),
232                 scriptName, name, strerror(errno));
233
234         _exit(-1);
235     }
236
237     rc = waitpid(child, &status, 0);
238
239     if (!WIFEXITED(status) || WEXITSTATUS(status)) {
240         rpmError(RPMERR_SCRIPT, _("Bad exit status from %s (%s)\n"),
241                  scriptName, name);
242         rc = RPMERR_SCRIPT;
243     } else
244         rc = 0;
245     
246 exit:
247     if (scriptName) {
248         if (!rc)
249             (void) Unlink(scriptName);
250         scriptName = _free(scriptName);
251     }
252     if (u != NULL) {
253         switch (u->urltype) {
254         case URL_IS_HTTPS:
255         case URL_IS_HTTP:
256         case URL_IS_FTP:
257 if (_build_debug)
258 fprintf(stderr, "*** delMacros\n");
259             delMacro(spec->macros, "_remsh");
260             delMacro(spec->macros, "_remhost");
261             if (strcmp(rootDir, "/"))
262                 delMacro(spec->macros, "_remroot");
263             break;
264         default:
265             break;
266         }
267     }
268     argv = _free(argv);
269     buildCmd = _free(buildCmd);
270     buildTemplate = _free(buildTemplate);
271     buildPost = _free(buildPost);
272     buildDirURL = _free(buildDirURL);
273
274     return rc;
275 }
276
277 int buildSpec(rpmts ts, Spec spec, int what, int test)
278 {
279     int rc = 0;
280
281     if (!spec->recursing && spec->BACount) {
282         int x;
283         /* When iterating over BANames, do the source    */
284         /* packaging on the first run, and skip RMSOURCE altogether */
285         if (spec->BASpecs != NULL)
286         for (x = 0; x < spec->BACount; x++) {
287 /*@-boundsread@*/
288             if ((rc = buildSpec(ts, spec->BASpecs[x],
289                                 (what & ~RPMBUILD_RMSOURCE) |
290                                 (x ? 0 : (what & RPMBUILD_PACKAGESOURCE)),
291                                 test))) {
292                 goto exit;
293             }
294 /*@=boundsread@*/
295         }
296     } else {
297         if ((what & RPMBUILD_PREP) &&
298             (rc = doScript(spec, RPMBUILD_PREP, NULL, NULL, test)))
299                 goto exit;
300
301         if ((what & RPMBUILD_BUILD) &&
302             (rc = doScript(spec, RPMBUILD_BUILD, NULL, NULL, test)))
303                 goto exit;
304
305         if ((what & RPMBUILD_INSTALL) &&
306             (rc = doScript(spec, RPMBUILD_INSTALL, NULL, NULL, test)))
307                 goto exit;
308
309         if ((what & RPMBUILD_CHECK) &&
310             (rc = doScript(spec, RPMBUILD_CHECK, NULL, NULL, test)))
311                 goto exit;
312
313         if ((what & RPMBUILD_PACKAGESOURCE) &&
314             (rc = processSourceFiles(spec)))
315                 goto exit;
316
317         if (((what & RPMBUILD_INSTALL) || (what & RPMBUILD_PACKAGEBINARY) ||
318             (what & RPMBUILD_FILECHECK)) &&
319             (rc = processBinaryFiles(spec, what & RPMBUILD_INSTALL, test)))
320                 goto exit;
321
322         if (((what & RPMBUILD_PACKAGESOURCE) && !test) &&
323             (rc = packageSources(spec)))
324                 return rc;
325
326         if (((what & RPMBUILD_PACKAGEBINARY) && !test) &&
327             (rc = packageBinaries(spec)))
328                 goto exit;
329         
330         if ((what & RPMBUILD_CLEAN) &&
331             (rc = doScript(spec, RPMBUILD_CLEAN, NULL, NULL, test)))
332                 goto exit;
333
334         if ((what & RPMBUILD_RMBUILD) &&
335             (rc = doScript(spec, RPMBUILD_RMBUILD, NULL, NULL, test)))
336                 goto exit;
337     }
338
339     if (what & RPMBUILD_RMSOURCE)
340         doRmSource(spec);
341
342     if (what & RPMBUILD_RMSPEC)
343         (void) Unlink(spec->specFile);
344
345 exit:
346     if (rc && rpmlogGetNrecs() > 0) {
347         rpmMessage(RPMMESS_NORMAL, _("\n\nRPM build errors:\n"));
348         rpmlogPrint(NULL);
349     }
350
351     return rc;
352 }