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