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