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