Add support for --buildplatform, and support for the setenv: tag in rpmrc files....
[platform/upstream/rpm.git] / build.c
1 #include "system.h"
2 #include "build/rpmbuild.h"
3 #include "build.h"
4 #include "intl.h"
5
6 int buildplatform(char *arg, int buildAmount, char *passPhrase,
7                  char *buildRoot, int fromTarball, int test, char *cookie);
8
9 int build(char *arg, int buildAmount, char *passPhrase,
10                  char *buildRoot, int fromTarball, int test, char *cookie,
11                  char * rcfile, char * arch, char * os, 
12                  char *buildplatforms) {
13
14   char * platform;
15
16   /* parse up the build operators */
17
18   printf("building these platforms: %s\n",buildplatforms);
19
20   if (buildplatforms) {
21     while(platform = strsep(&buildplatforms,",")) {
22        printf("building %s\n",platform);
23
24        rpmSetVar(RPMVAR_BUILDPLATFORM,platform);
25        rpmReadConfigFiles(rcfile, arch, os, 1,platform);
26        buildplatform(arg,buildAmount,passPhrase,buildRoot,
27            fromTarball,test,cookie);
28     }
29   } else {
30        buildplatform(arg,buildAmount,passPhrase,buildRoot,
31            fromTarball,test,cookie);
32   }
33 }
34
35
36 int buildplatform(char *arg, int buildAmount, char *passPhrase,
37                  char *buildRoot, int fromTarball, int test, char *cookie) {
38
39     FILE *f;
40     char * specfile;
41     int res = 0;
42     struct stat statbuf;
43     char * specDir;
44     char * tmpSpecFile;
45     char * cmd;
46     char * s;
47     int count, fd;
48     char buf[BUFSIZ];
49     Spec spec = NULL;
50
51     if (fromTarball) {
52         specDir = rpmGetVar(RPMVAR_SPECDIR);
53         tmpSpecFile = alloca(BUFSIZ);
54         sprintf(tmpSpecFile, "%s/rpm-spec-file-%d", specDir, (int) getpid());
55
56         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
57         sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s", arg,
58                         tmpSpecFile);
59         if (!(f = popen(cmd, "r"))) {
60             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
61                         strerror(errno));
62             return 1;
63         }
64         if ((!fgets(buf, sizeof(buf) - 1, f)) || !strchr(buf, '/')) {
65             /* Try again */
66             pclose(f);
67
68             sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
69                     tmpSpecFile);
70             if (!(f = popen(cmd, "r"))) {
71                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
72                         strerror(errno));
73                 return 1;
74             }
75             if (!fgets(buf, sizeof(buf) - 1, f)) {
76                 /* Give up */
77                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
78                 unlink(tmpSpecFile);
79                 return 1;
80             }
81         }
82         pclose(f);
83
84         cmd = specfile = buf;
85         while (*cmd) {
86             if (*cmd == '/') specfile = cmd + 1;
87             cmd++;
88         }
89
90         cmd = specfile;
91
92         /* remove trailing \n */
93         specfile = cmd + strlen(cmd) - 1;
94         *specfile = '\0';
95
96         specfile = alloca(strlen(specDir) + strlen(cmd) + 5);
97         sprintf(specfile, "%s/%s", specDir, cmd);
98         
99         if (rename(tmpSpecFile, specfile)) {
100             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
101                     tmpSpecFile, specfile, strerror(errno));
102             unlink(tmpSpecFile);
103             return 1;
104         }
105
106         /* Make the directory which contains the tarball the source 
107            directory for this run */
108
109         if (*arg != '/') {
110             getcwd(buf, BUFSIZ);
111             strcat(buf, "/");
112             strcat(buf, arg);
113         } else 
114             strcpy(buf, arg);
115
116         cmd = buf + strlen(buf) - 1;
117         while (*cmd != '/') cmd--;
118         *cmd = '\0';
119
120         rpmSetVar(RPMVAR_SOURCEDIR, buf);
121     } else if (arg[0] == '/') {
122         specfile = arg;
123     } else {
124         specfile = alloca(BUFSIZ);
125         getcwd(specfile, BUFSIZ);
126         strcat(specfile, "/");
127         strcat(specfile, arg);
128     }
129
130     stat(specfile, &statbuf);
131     if (! S_ISREG(statbuf.st_mode)) {
132         fprintf(stderr, _("File is not a regular file: %s\n"), specfile);
133         return 1;
134     }
135     
136     if ((fd = open(specfile, O_RDONLY)) < 0) {
137         fprintf(stderr, _("Unable to open spec file: %s\n"), specfile);
138         return 1;
139     }
140     count = read(fd, buf, sizeof(buf) < 128 ? sizeof(buf) : 128);
141     close(fd);
142     s = buf;
143     while(count--) {
144         if (! (isprint(*s) || isspace(*s))) {
145             fprintf(stderr, _("File contains non-printable characters(%c): %s\n"), *s,
146                     specfile);
147             return 1;
148         }
149         s++;
150     }
151
152     if (parseSpec(&spec, specfile, buildRoot, 0, passPhrase, cookie)) {
153         return 1;
154     }
155
156     if (buildSpec(spec, buildAmount, test)) {
157         freeSpec(spec);
158         return 1;
159     }
160     
161     if (fromTarball) unlink(specfile);
162
163     freeSpec(spec);
164     
165     return res;
166 }