1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: makefile.cc,v 1.23 2005-10-03 14:54:03 wienand Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: lib parsing |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdlib.h> |
---|
10 | #include <sys/stat.h> |
---|
11 | #include <sys/types.h> |
---|
12 | #include <fcntl.h> |
---|
13 | #include <unistd.h> |
---|
14 | |
---|
15 | #include <mod2.h> |
---|
16 | #include <febase.h> |
---|
17 | #include <grammar.h> |
---|
18 | #include <structs.h> |
---|
19 | #include <tok.h> |
---|
20 | #include <ipid.h> |
---|
21 | #include <ipshell.h> |
---|
22 | #include <subexpr.h> |
---|
23 | #include <regex.h> |
---|
24 | |
---|
25 | #include "modgen.h" |
---|
26 | #include "typmap.h" |
---|
27 | #include "pathnames.h" |
---|
28 | |
---|
29 | extern char* inst_dir; |
---|
30 | |
---|
31 | extern void mod_create_makefile(moddefv module); |
---|
32 | extern void build_head_section(FILE *fp, moddefv module); |
---|
33 | extern void build_clean_section(FILE *fp, moddefv module); |
---|
34 | extern void build_install_section(FILE *fp, moddefv module); |
---|
35 | extern void build_compile_section(FILE *fp, moddefv module); |
---|
36 | |
---|
37 | extern int do_create_srcdir; |
---|
38 | |
---|
39 | static char *object_name(char *p); |
---|
40 | /*========================================================================*/ |
---|
41 | /* |
---|
42 | run mod_create_makefile(); |
---|
43 | |
---|
44 | */ |
---|
45 | /*========================================================================*/ |
---|
46 | |
---|
47 | |
---|
48 | /*========================================================================*/ |
---|
49 | void mod_create_makefile(moddefv module) |
---|
50 | { |
---|
51 | FILE *fp; |
---|
52 | |
---|
53 | if(module->targetname==NULL) |
---|
54 | { |
---|
55 | module->targetname = (char *)malloc(strlen(module->name)+1); |
---|
56 | memset(module->targetname, '\0', strlen(module->name)+1); |
---|
57 | memcpy(module->targetname,module->name,strlen(module->name)); |
---|
58 | } |
---|
59 | if(do_create_srcdir) mkdir(module->name, 0755); |
---|
60 | fp = fopen(build_filename(module, "Makefile", 0), "w"); |
---|
61 | cfilesv cf = module->files; |
---|
62 | int i; |
---|
63 | |
---|
64 | if(trace)printf("Creating Makefile ...");fflush(stdout); |
---|
65 | write_header(fp, module->name, "#"); |
---|
66 | build_head_section(fp, module); |
---|
67 | fprintf(fp, "SRCS\t= %s.cc", module->name); |
---|
68 | |
---|
69 | for(i=0; i<module->filecnt; i++) |
---|
70 | fprintf(fp, " %s", cf[i].filename); |
---|
71 | |
---|
72 | fprintf(fp, "\nOBJS\t= %s.o", module->name); |
---|
73 | for(i=0; i<module->filecnt; i++) |
---|
74 | fprintf(fp, " %s", object_name(cf[i].filename)); |
---|
75 | fprintf(fp, "\nDOBJS\t= %s.og", module->name); |
---|
76 | for(i=0; i<module->filecnt; i++) |
---|
77 | fprintf(fp, " %s", object_name(cf[i].filename)); |
---|
78 | |
---|
79 | fprintf(fp, "\n\n"); |
---|
80 | build_compile_section(fp, module); |
---|
81 | build_clean_section(fp, module); |
---|
82 | build_install_section(fp, module); |
---|
83 | |
---|
84 | fprintf(fp, "\n"); |
---|
85 | fprintf(fp, "\n"); |
---|
86 | |
---|
87 | fclose(fp); |
---|
88 | if(trace)printf(" done.\n"); |
---|
89 | } |
---|
90 | |
---|
91 | /*========================================================================*/ |
---|
92 | void build_head_section( |
---|
93 | FILE *fp, |
---|
94 | moddefv module |
---|
95 | ) |
---|
96 | { |
---|
97 | fprintf(fp, "CC\t= gcc\n"); |
---|
98 | fprintf(fp, "CXX\t= gcc\n"); |
---|
99 | #warning "PROBLEM: nice place for include file has to be found" |
---|
100 | fprintf(fp, "CFLAGS\t= -DNDEBUG -DBUILD_MODULE -I. -I%s/kernel -I%s/Singular -I%s/include\n",PREFIX,PREFIX,EXEC_PREFIX); |
---|
101 | fprintf(fp, "DCFLAGS\t= -DBUILD_MODULE -I. -I%s/kernel -I%s/Singular -I%s/include\n",PREFIX,PREFIX,EXEC_PREFIX); |
---|
102 | fprintf(fp, "#LD\t=\n"); |
---|
103 | fprintf(fp, "\n"); |
---|
104 | fprintf(fp, "instdir = %s\n", inst_dir ); |
---|
105 | fprintf(fp, "MKINSTALLDIRS\t\t= %s/modules/mkinstalldirs\n", PREFIX); |
---|
106 | #warning "PROBLEM: do we also install install-sh when installing Singular?" |
---|
107 | fprintf(fp, "INSTALL\t\t= %s/Singular/install-sh -c\n", PREFIX); |
---|
108 | fprintf(fp, "INSTALL_PROGRAM\t= ${INSTALL}\n"); |
---|
109 | fprintf(fp, "INSTALL_DATA\t= ${INSTALL} -m 644\n"); |
---|
110 | } |
---|
111 | |
---|
112 | /*========================================================================*/ |
---|
113 | void build_clean_section( |
---|
114 | FILE *fp, |
---|
115 | moddefv module |
---|
116 | ) |
---|
117 | { |
---|
118 | fprintf(fp, "clean:\n"); |
---|
119 | fprintf(fp, "\trm -f *.o *.og *.lo *.so* *.sl *.la *~ core\n\n"); |
---|
120 | |
---|
121 | fprintf(fp, "distclean: clean\n"); |
---|
122 | fprintf(fp, "\trm -f %s.cc %s.h Makefile *.bin *.pl\n\n", |
---|
123 | module->name, module->name); |
---|
124 | } |
---|
125 | |
---|
126 | /*========================================================================*/ |
---|
127 | void build_install_section( |
---|
128 | FILE *fp, |
---|
129 | moddefv module |
---|
130 | ) |
---|
131 | { |
---|
132 | fprintf(fp, "install bindist:\n"); |
---|
133 | fprintf(fp, "\t${MKINSTALLDIRS} ${instdir}\n"); |
---|
134 | fprintf(fp, "\t${MKINSTALLDIRS} ${instdir}/MOD\n"); |
---|
135 | fprintf(fp, "\t${INSTALL_PROGRAM} %s.so ${instdir}/MOD/%s.so\n", |
---|
136 | module->targetname, module->targetname); |
---|
137 | fprintf(fp, "\t${INSTALL_PROGRAM} %s.bin ${instdir}/MOD/%s.bin\n", |
---|
138 | module->targetname, module->targetname); |
---|
139 | fprintf(fp, "install-bindist:\n"); |
---|
140 | fprintf(fp, "\t${MKINSTALLDIRS} ${install_bindir}\n"); |
---|
141 | fprintf(fp, "\t${INSTALL_PROGRAM} %s.so ${install_bindir}/%s.so\n", |
---|
142 | module->targetname, module->targetname); |
---|
143 | fprintf(fp, "\t${INSTALL_PROGRAM} %s.bin ${install_bindir}/%s.bin\n", |
---|
144 | module->targetname, module->targetname); |
---|
145 | } |
---|
146 | |
---|
147 | /*========================================================================*/ |
---|
148 | static char *object_name(char *p) |
---|
149 | { |
---|
150 | char *q = (char *)strrchr(p, '.'); |
---|
151 | if(q==NULL) return ""; |
---|
152 | *q = '\0'; |
---|
153 | char *r = (char *)malloc(strlen(p)+4); |
---|
154 | sprintf(r, "%s.o", p); |
---|
155 | |
---|
156 | *q = '.'; |
---|
157 | return(r); |
---|
158 | } |
---|
159 | |
---|
160 | /*========================================================================*/ |
---|
161 | /*=== Machine depend Makefile creation ===*/ |
---|
162 | /*========================================================================*/ |
---|
163 | #if defined(ix86_Linux) || defined(ix86_Linux_libc5) || defined(ix86_freebsd)|| defined(x86_64_Linux) || defined(IRIX_6) |
---|
164 | void build_compile_section( |
---|
165 | FILE *fp, |
---|
166 | moddefv module |
---|
167 | ) |
---|
168 | { |
---|
169 | fprintf(fp, "all:\t%s.so %s_g.so \n", module->targetname, module->targetname); |
---|
170 | fprintf(fp, "\n"); |
---|
171 | fprintf(fp, "%%.o: %%.cc Makefile\n"); |
---|
172 | fprintf(fp, "\t${CC} ${CFLAGS} -c -fPIC -DPIC $< -o $*.o\n"); |
---|
173 | fprintf(fp, "\n"); |
---|
174 | fprintf(fp, "%%.og: %%.cc Makefile\n"); |
---|
175 | fprintf(fp, "\t${CC} ${DCFLAGS} -c -fPIC -DPIC $< -o $*.og\n"); |
---|
176 | fprintf(fp, "\n"); |
---|
177 | |
---|
178 | fprintf(fp, "%s.so: ${OBJS}\n", module->targetname); |
---|
179 | fprintf(fp, "\t${CC} ${CFLAGS} -shared -Wl,-soname -Wl,%s.so.%d \\\n", |
---|
180 | module->targetname, module->major); |
---|
181 | fprintf(fp, "\t\t-o %s.so.%d.%d.%d ${OBJS}\n", module->targetname, |
---|
182 | module->major, module->minor, module->level); |
---|
183 | fprintf(fp, "\trm -f %s.so\n", module->targetname); |
---|
184 | fprintf(fp, "\tln -s %s.so.%d.%d.%d %s.so\n", module->targetname, module->major, |
---|
185 | module->minor, module->level, module->targetname); |
---|
186 | fprintf(fp, "\n"); |
---|
187 | |
---|
188 | fprintf(fp, "%s_g.so: ${DOBJS}\n", module->targetname); |
---|
189 | fprintf(fp, "\t${CC} ${DCFLAGS} -shared -Wl,-soname -Wl,%s_g.so.%d \\\n", |
---|
190 | module->targetname, module->major); |
---|
191 | fprintf(fp, "\t\t-o %s_g.so.%d.%d.%d ${DOBJS}\n", module->targetname, |
---|
192 | module->major, module->minor, module->level); |
---|
193 | fprintf(fp, "\trm -f %s_g.so\n", module->targetname); |
---|
194 | fprintf(fp, "\tln -s %s_g.so.%d.%d.%d %s_g.so\n", module->targetname, |
---|
195 | module->major, module->minor, module->level, module->targetname); |
---|
196 | fprintf(fp, "\n"); |
---|
197 | } |
---|
198 | #endif /* ix86_Linux */ |
---|
199 | |
---|
200 | /*========================================================================*/ |
---|
201 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
202 | void build_compile_section( |
---|
203 | FILE *fp, |
---|
204 | moddefv module |
---|
205 | ) |
---|
206 | { |
---|
207 | fprintf(fp, "all:\t%s.sl\n", module->targetname); |
---|
208 | fprintf(fp, "\n"); |
---|
209 | fprintf(fp, "%%.o: %%.cc Makefile\n"); |
---|
210 | fprintf(fp, "\t${CC} ${CFLAGS} -c -fPIC -DPIC $< -o $*.o\n"); |
---|
211 | fprintf(fp, "\n"); |
---|
212 | fprintf(fp, "%s.sl: ${OBJS}\n", module->targetname); |
---|
213 | fprintf(fp, "\t${LD} -b -o %s.sl \\\n", module->targetname); |
---|
214 | fprintf(fp, "\t\t${OBJS}\n"); |
---|
215 | } |
---|
216 | |
---|
217 | #endif /* HPUX_9 or HPUX_10 */ |
---|
218 | |
---|
219 | /*========================================================================*/ |
---|
220 | # ifdef m68k_MPW |
---|
221 | void build_compile_section( |
---|
222 | FILE *fp, |
---|
223 | moddefv module |
---|
224 | ) |
---|
225 | { |
---|
226 | fprintf(fp, "all:\t\n"); |
---|
227 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
228 | } |
---|
229 | # endif /* 68k_MPW */ |
---|
230 | |
---|
231 | /*========================================================================*/ |
---|
232 | # ifdef AIX_4 |
---|
233 | void build_compile_section( |
---|
234 | FILE *fp, |
---|
235 | moddefv module |
---|
236 | ) |
---|
237 | { |
---|
238 | fprintf(fp, "all:\t\n"); |
---|
239 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
240 | } |
---|
241 | # endif /* AIX_4 */ |
---|
242 | |
---|
243 | /*========================================================================*/ |
---|
244 | # ifdef Sun3OS_4 |
---|
245 | void build_compile_section( |
---|
246 | FILE *fp, |
---|
247 | moddefv module |
---|
248 | ) |
---|
249 | { |
---|
250 | fprintf(fp, "all:\t\n"); |
---|
251 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
252 | } |
---|
253 | # endif /* Sun3OS_4 */ |
---|
254 | |
---|
255 | /*========================================================================*/ |
---|
256 | # if defined(SunOS_4) || defined(SunOS_5) |
---|
257 | void build_compile_section( |
---|
258 | FILE *fp, |
---|
259 | moddefv module |
---|
260 | ) |
---|
261 | { |
---|
262 | fprintf(fp, "all:\t\n"); |
---|
263 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
264 | } |
---|
265 | # endif /* SunOS_4 or SunOS_5 */ |
---|
266 | |
---|
267 | /*========================================================================*/ |
---|
268 | # ifdef ix86_Win |
---|
269 | void build_compile_section( |
---|
270 | FILE *fp, |
---|
271 | moddefv module |
---|
272 | ) |
---|
273 | { |
---|
274 | fprintf(fp, "all:\t%s.dll %s_g.dll \n", module->name, module->name); |
---|
275 | fprintf(fp, "\n"); |
---|
276 | fprintf(fp, "%%.o: %%.cc Makefile\n"); |
---|
277 | fprintf(fp, "\t${CC} ${CFLAGS} -c $< -o $*.o\n"); |
---|
278 | fprintf(fp, "\n"); |
---|
279 | fprintf(fp, "%%.og: %%.cc Makefile\n"); |
---|
280 | fprintf(fp, "\t${CC} ${DCFLAGS} -c $< -o $*.og\n"); |
---|
281 | fprintf(fp, "\n"); |
---|
282 | |
---|
283 | fprintf(fp, "%s.dll: ${OBJS}\n", module->name); |
---|
284 | fprintf(fp, "\t${CC} ${CFLAGS} -Wl,--out-implib,lib%s.import.a -shared \\\n", |
---|
285 | module->name); |
---|
286 | fprintf(fp, "\t\t-o %s.dll ${OBJS}\n", module->name); |
---|
287 | fprintf(fp, "\n"); |
---|
288 | |
---|
289 | fprintf(fp, "%s_g.so: ${DOBJS}\n", module->name); |
---|
290 | fprintf(fp, "\t${CC} ${DCFLAGS} -Wl,--out-implib,lib%s_g.import.a -shared \\\n", |
---|
291 | module->name); |
---|
292 | fprintf(fp, "\t\t-o %s_g.dll ${DOBJS}\n", module->name); |
---|
293 | fprintf(fp, "\n"); |
---|
294 | |
---|
295 | fprintf(fp, "all:\t\n"); |
---|
296 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
297 | } |
---|
298 | # endif /* ix86_Win */ |
---|
299 | |
---|
300 | /*========================================================================*/ |
---|
301 | # ifdef ppc_MPW |
---|
302 | void build_compile_section( |
---|
303 | FILE *fp, |
---|
304 | moddefv module |
---|
305 | ) |
---|
306 | { |
---|
307 | fprintf(fp, "all:\t\n"); |
---|
308 | fprintf(fp, "\techo \"don't know how to build library\"\n"); |
---|
309 | } |
---|
310 | # endif /* ppc_MPW */ |
---|
311 | |
---|
312 | /*========================================================================*/ |
---|