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