mk-1st.awk revision 262629
1# $Id: mk-1st.awk,v 1.85 2010/08/07 20:42:30 Gabriele.Balducci Exp $
2##############################################################################
3# Copyright (c) 1998-2009,2010 Free Software Foundation, Inc.                #
4#                                                                            #
5# Permission is hereby granted, free of charge, to any person obtaining a    #
6# copy of this software and associated documentation files (the "Software"), #
7# to deal in the Software without restriction, including without limitation  #
8# the rights to use, copy, modify, merge, publish, distribute, distribute    #
9# with modifications, sublicense, and/or sell copies of the Software, and to #
10# permit persons to whom the Software is furnished to do so, subject to the  #
11# following conditions:                                                      #
12#                                                                            #
13# The above copyright notice and this permission notice shall be included in #
14# all copies or substantial portions of the Software.                        #
15#                                                                            #
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
19# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
22# DEALINGS IN THE SOFTWARE.                                                  #
23#                                                                            #
24# Except as contained in this notice, the name(s) of the above copyright     #
25# holders shall not be used in advertising or otherwise to promote the sale, #
26# use or other dealings in this Software without prior written               #
27# authorization.                                                             #
28##############################################################################
29#
30# Author: Thomas E. Dickey
31#
32# Generate list of objects for a given model library
33# Variables:
34#	name		  (library name, e.g., "ncurses", "panel", "forms", "menus")
35#	traces		  ("all" or "DEBUG", to control whether tracing is compiled in)
36#	MODEL		  (e.g., "DEBUG", uppercase; toupper is not portable)
37#	model		  (directory into which we compile, e.g., "obj")
38#	prefix		  (e.g., "lib", for Unix-style libraries)
39#	suffix		  (e.g., "_g.a", for debug libraries)
40#	subset		  ("none", "base", "base+ext_funcs" or "termlib", etc.)
41#	ShlibVer	  ("rel", "abi" or "auto", to augment DoLinks variable)
42#	ShlibVerInfix ("yes" or "no", determines location of version #)
43#	SymLink		  ("ln -s", etc)
44#	TermlibRoot	  ("tinfo" or other root for libterm.so)
45#	TermlibSuffix (".so" or other suffix for libterm.so)
46#	ReLink		  ("yes", or "no", flag to rebuild shared libs on install)
47#	DoLinks		  ("yes", "reverse" or "no", flag to add symbolic links)
48#	rmSoLocs	  ("yes" or "no", flag to add extra clean target)
49#	ldconfig	  (path for this tool, if used)
50#	overwrite	  ("yes" or "no", flag to add link to libcurses.a
51#	depend		  (optional dependencies for all objects, e.g, ncurses_cfg.h)
52#	host		  (cross-compile host, if any)
53#	libtool_version (libtool "-version-info" or "-version-number")
54#
55# Notes:
56#	CLIXs nawk does not like underscores in command-line variable names.
57#	Mixed-case variable names are ok.
58#	HP/UX requires shared libraries to have executable permissions.
59#
60function is_ticlib() {
61		return ( subset ~ /^ticlib$/ );
62	}
63function is_termlib() {
64		return ( subset ~ /^(ticlib\+)?termlib((\+[^+ ]+)*\+[a-z_]+_tinfo)?$/ );
65	}
66# see lib_name
67function lib_name_of(a_name) {
68		return sprintf("%s%s%s", prefix, a_name, suffix)
69	}
70# see imp_name
71function imp_name_of(a_name) {
72		if (ShlibVerInfix == "cygdll") {
73			result = sprintf("%s%s%s.a", prefix, a_name, suffix);
74		} else {
75			result = "";
76		}
77		return result;
78	}
79# see abi_name
80function abi_name_of(a_name) {
81		if (ShlibVerInfix == "cygdll") {
82			result = sprintf("%s%s$(ABI_VERSION)%s", "cyg", a_name, suffix);
83		} else if (ShlibVerInfix == "yes") {
84			result = sprintf("%s%s.$(ABI_VERSION)%s", prefix, a_name, suffix);
85		} else {
86			result = sprintf("%s.$(ABI_VERSION)", lib_name_of(a_name));
87		}
88		return result;
89	}
90# see rel_name
91function rel_name_of(a_name) {
92		if (ShlibVerInfix == "cygdll") {
93			result = sprintf("%s%s$(REL_VERSION)%s", "cyg", a_name, suffix);
94		} else if (ShlibVerInfix == "yes") {
95			result = sprintf("%s%s.$(REL_VERSION)%s", prefix, a_name, suffix);
96		} else {
97			result = sprintf("%s.$(REL_VERSION)", lib_name_of(a_name));
98		}
99		return result;
100	}
101# see end_name
102function end_name_of(a_name) {
103		if ( MODEL != "SHARED" ) {
104			result = lib_name_of(a_name);
105		} else if ( DoLinks == "reverse") {
106			result = lib_name_of(a_name);
107		} else {
108			if ( ShlibVer == "rel" ) {
109				result = rel_name_of(a_name);
110			} else if ( ShlibVer == "abi" || ShlibVer == "cygdll" ) {
111				result = abi_name_of(a_name);
112			} else {
113				result = lib_name_of(a_name);
114			}
115		}
116		return result
117	}
118function symlink(src,dst) {
119		if ( src != dst ) {
120			if ( SymLink !~ /.*-f.*/ ) {
121				printf "rm -f %s; ", dst
122			}
123			printf "$(LN_S) %s %s; ", src, dst
124		}
125	}
126function rmlink(directory, dst) {
127		printf "\t-rm -f %s/%s\n", directory, dst
128	}
129function removelinks(directory) {
130		rmlink(directory, end_name);
131		if ( DoLinks == "reverse" ) {
132			if ( ShlibVer == "rel" ) {
133				rmlink(directory, abi_name);
134				rmlink(directory, rel_name);
135			} else if ( ShlibVer == "abi" ) {
136				rmlink(directory, abi_name);
137			}
138		} else {
139			if ( ShlibVer == "rel" ) {
140				rmlink(directory, abi_name);
141				rmlink(directory, lib_name);
142			} else if ( ShlibVer == "abi" ) {
143				rmlink(directory, lib_name);
144			}
145		}
146	}
147function make_shlib(objs, shlib_list) {
148		printf "\t$(MK_SHARED_LIB) $(%s_OBJS) $(%s) $(LDFLAGS)\n", objs, shlib_list
149	}
150function sharedlinks(directory) {
151		if ( ShlibVer != "auto" && ShlibVer != "cygdll" ) {
152			printf "\tcd %s && (", directory
153			if ( DoLinks == "reverse" ) {
154				if ( ShlibVer == "rel" ) {
155					symlink(lib_name, abi_name);
156					symlink(abi_name, rel_name);
157				} else if ( ShlibVer == "abi" ) {
158					symlink(lib_name, abi_name);
159				}
160			} else {
161				if ( ShlibVer == "rel" ) {
162					symlink(rel_name, abi_name);
163					symlink(abi_name, lib_name);
164				} else if ( ShlibVer == "abi" ) {
165					symlink(abi_name, lib_name);
166				}
167			}
168			printf ")\n"
169		}
170	}
171# termlib may be named explicitly via "--with-termlib=XXX", which overrides
172# any suffix.  Temporarily override "suffix" to account for this.
173function termlib_end_of() {
174	termlib_save_suffix = suffix;
175	suffix = TermlibSuffix;
176	termlib_temp_result = end_name_of(TermlibRoot);
177	suffix = termlib_save_suffix;
178	return termlib_temp_result;
179}
180function shlib_build(directory) {
181		dst_libs = sprintf("%s/%s", directory, end_name);
182		printf "%s : \\\n", dst_libs
183		printf "\t\t%s \\\n", directory
184		if (subset ~ /^base/ || subset == "ticlib" ) {
185			save_suffix = suffix
186			sub(/^[^.]\./,".",suffix)
187			if (directory != "../lib") {
188				printf "\t\t%s/%s \\\n", "../lib", termlib_end_of();
189			}
190			printf "\t\t%s/%s \\\n", directory, termlib_end_of();
191			suffix = save_suffix
192		}
193		printf "\t\t$(%s_OBJS)\n", OBJS
194		printf "\t@echo linking $@\n"
195		if ( is_ticlib() ) {
196			make_shlib(OBJS, "TICS_LIST")
197		} else if ( is_termlib() ) {
198			make_shlib(OBJS, "TINFO_LIST")
199		} else {
200			make_shlib(OBJS, "SHLIB_LIST")
201		}
202		sharedlinks(directory)
203	}
204function shlib_install(directory) {
205		src_lib1 = sprintf("../lib/%s", end_name);
206		dst_lib1 = sprintf("%s/%s", directory, end_name);
207		printf "%s : \\\n", dst_lib1
208		printf "\t\t%s \\\n", directory
209		printf "\t\t%s\n", src_lib1
210		printf "\t@echo installing $@\n"
211		printf "\t$(INSTALL_LIB) %s %s\n", src_lib1, dst_lib1;
212		sharedlinks(directory)
213	}
214function install_dll(directory,filename) {
215		src_name = sprintf("../lib/%s", filename);
216		dst_name = sprintf("$(DESTDIR)%s/%s", directory, filename);
217		printf "\t@echo installing %s as %s\n", src_name, dst_name
218		if ( directory == "$(bindir)" ) {
219			program = "$(INSTALL) -m 755";
220		} else {
221			program = "$(INSTALL_LIB)";
222		}
223		printf "\t%s %s %s\n", program, src_name, dst_name
224	}
225BEGIN	{
226		found = 0
227		using = 0
228	}
229	/^@/ {
230		using = 0
231		if (subset == "none") {
232			using = 1
233		} else if (index(subset,$2) > 0) {
234			if (using == 0) {
235				if (found == 0) {
236					print  ""
237					printf "# generated by mk-1st.awk (subset=%s)\n", subset
238					printf "#  name:          %s\n", name 
239					printf "#  traces:        %s\n", traces 
240					printf "#  MODEL:         %s\n", MODEL 
241					printf "#  model:         %s\n", model 
242					printf "#  prefix:        %s\n", prefix 
243					printf "#  suffix:        %s\n", suffix 
244					printf "#  subset:        %s\n", subset 
245					printf "#  ShlibVer:      %s\n", ShlibVer 
246					printf "#  ShlibVerInfix: %s\n", ShlibVerInfix 
247					printf "#  SymLink:       %s\n", SymLink 
248					printf "#  TermlibRoot:   %s\n", TermlibRoot 
249					printf "#  TermlibSuffix: %s\n", TermlibSuffix 
250					printf "#  ReLink:        %s\n", ReLink 
251					printf "#  DoLinks:       %s\n", DoLinks 
252					printf "#  rmSoLocs:      %s\n", rmSoLocs 
253					printf "#  ldconfig:      %s\n", ldconfig 
254					printf "#  overwrite:     %s\n", overwrite 
255					printf "#  depend:        %s\n", depend 
256					printf "#  host:          %s\n", host 
257					print  ""
258				}
259				using = 1
260			}
261			if ( is_ticlib() ) {
262				OBJS  = MODEL "_P"
263			} else if ( is_termlib() ) {
264				OBJS  = MODEL "_T"
265			} else {
266				OBJS  = MODEL
267			}
268		}
269	}
270	/^[@#]/ {
271		next
272	}
273	$1 ~ /trace/ {
274		if (traces != "all" && traces != MODEL && $1 != "lib_trace")
275			next
276	}
277	{
278		if (using \
279		 && ( $1 != "link_test" ) \
280		 && ( $2 == "lib" \
281		   || $2 == "progs" \
282		   || $2 == "c++" \
283		   || $2 == "tack" ))
284		{
285			if ( found == 0 )
286			{
287				printf "%s_OBJS =", OBJS
288				if ( $2 == "lib" )
289					found = 1
290				else
291					found = 2
292			}
293			printf " \\\n\t../%s/%s$o", model, $1
294		}
295	}
296END	{
297		print  ""
298		if ( found != 0 )
299		{
300			printf "\n$(%s_OBJS) : %s\n", OBJS, depend
301		}
302		if ( found == 1 )
303		{
304			print  ""
305			lib_name = lib_name_of(name);
306			if ( MODEL == "SHARED" )
307			{
308				abi_name = abi_name_of(name);
309				rel_name = rel_name_of(name);
310				imp_name = imp_name_of(name);
311				end_name = end_name_of(name);
312
313				shlib_build("../lib")
314
315				print  ""
316				print  "install \\"
317				print  "install.libs \\"
318
319				if ( ShlibVer == "cygdll" ) {
320
321					dst_dirs = "$(DESTDIR)$(bindir) $(DESTDIR)$(libdir)";
322					printf "install.%s :: %s $(LIBRARIES)\n", name, dst_dirs
323					install_dll("$(bindir)",end_name);
324					install_dll("$(libdir)",imp_name);
325
326				} else {
327
328					lib_dir = "$(DESTDIR)$(libdir)";
329					printf "install.%s :: %s/%s\n", name, lib_dir, end_name
330					print ""
331					if ( ReLink == "yes" ) {
332						shlib_build(lib_dir)
333					} else {
334						shlib_install(lib_dir)
335					}
336				}
337
338				if ( overwrite == "yes" && name == "ncurses" )
339				{
340					if ( ShlibVer == "cygdll" ) {
341						ovr_name = sprintf("libcurses%s.a", suffix)
342						printf "\t@echo linking %s to %s\n", imp_name, ovr_name
343						printf "\tcd $(DESTDIR)$(libdir) && ("
344						symlink(imp_name, ovr_name)
345						printf ")\n"
346					} else {
347						ovr_name = sprintf("libcurses%s", suffix)
348						printf "\t@echo linking %s to %s\n", end_name, ovr_name
349						printf "\tcd $(DESTDIR)$(libdir) && ("
350						symlink(end_name, ovr_name)
351						printf ")\n"
352					}
353				}
354				if ( ldconfig != "" && ldconfig != ":" ) {
355					printf "\t- test -z \"$(DESTDIR)\" && %s\n", ldconfig
356				}
357				print  ""
358				print  "uninstall \\"
359				print  "uninstall.libs \\"
360				printf "uninstall.%s ::\n", name
361				if ( ShlibVer == "cygdll" ) {
362
363					printf "\t@echo uninstalling $(DESTDIR)$(bindir)/%s\n", end_name
364					printf "\t-@rm -f $(DESTDIR)$(bindir)/%s\n", end_name
365
366					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", imp_name
367					printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", imp_name
368
369				} else {
370					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", end_name
371					removelinks("$(DESTDIR)$(libdir)")
372					if ( overwrite == "yes" && name == "ncurses" )
373					{
374						if ( ShlibVer == "cygdll" ) {
375							ovr_name = sprintf("libcurses%s.a", suffix)
376						} else {
377							ovr_name = sprintf("libcurses%s", suffix)
378						}
379						printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", ovr_name
380					}
381				}
382				if ( rmSoLocs == "yes" ) {
383					print  ""
384					print  "mostlyclean \\"
385					print  "clean ::"
386					printf "\t-@rm -f so_locations\n"
387				}
388			}
389			else if ( MODEL == "LIBTOOL" )
390			{
391				if ( $2 == "c++" ) {
392					compile="CXX"
393				} else {
394					compile="CC"
395				}
396				end_name = lib_name;
397				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
398				if ( is_ticlib() ) {
399					printf "\tcd ../lib && $(LIBTOOL_LINK) $(%s) -o %s $(%s_OBJS:$o=.lo) -rpath $(DESTDIR)$(libdir) %s $(NCURSES_MAJOR):$(NCURSES_MINOR) $(LT_UNDEF) $(TICS_LIST)\n", compile, lib_name, OBJS, libtool_version
400				} else if ( is_termlib() ) {
401					printf "\tcd ../lib && $(LIBTOOL_LINK) $(%s) -o %s $(%s_OBJS:$o=.lo) -rpath $(DESTDIR)$(libdir) %s $(NCURSES_MAJOR):$(NCURSES_MINOR) $(LT_UNDEF) $(TINFO_LIST)\n", compile, lib_name, OBJS, libtool_version
402				} else {
403					printf "\tcd ../lib && $(LIBTOOL_LINK) $(%s) -o %s $(%s_OBJS:$o=.lo) -rpath $(DESTDIR)$(libdir) %s $(NCURSES_MAJOR):$(NCURSES_MINOR) $(LT_UNDEF) $(SHLIB_LIST)\n", compile, lib_name, OBJS, libtool_version
404				}
405				print  ""
406				print  "install \\"
407				print  "install.libs \\"
408				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
409				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
410				printf "\tcd ../lib; $(LIBTOOL_INSTALL) $(INSTALL) %s $(DESTDIR)$(libdir)\n", lib_name
411				print  ""
412				print  "uninstall \\"
413				print  "uninstall.libs \\"
414				printf "uninstall.%s ::\n", name
415				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
416				printf "\t-@$(LIBTOOL_UNINSTALL) rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
417			}
418			else
419			{
420				end_name = lib_name;
421				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
422				printf "\t$(AR) $(ARFLAGS) $@ $?\n"
423				printf "\t$(RANLIB) $@\n"
424				if ( host == "vxworks" )
425				{
426					printf "\t$(LD) $(LD_OPTS) $? -o $(@:.a=$o)\n"
427				}
428				print  ""
429				print  "install \\"
430				print  "install.libs \\"
431				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
432				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
433				printf "\t$(INSTALL_DATA) ../lib/%s $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
434				if ( overwrite == "yes" && lib_name == "libncurses.a" )
435				{
436					printf "\t@echo linking libcurses.a to libncurses.a\n"
437					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
438					printf "\t(cd $(DESTDIR)$(libdir) && "
439					symlink("libncurses.a", "libcurses.a")
440					printf ")\n"
441				}
442				printf "\t$(RANLIB) $(DESTDIR)$(libdir)/%s\n", lib_name
443				if ( host == "vxworks" )
444				{
445					printf "\t@echo installing ../lib/lib%s$o as $(DESTDIR)$(libdir)/lib%s$o\n", name, name
446					printf "\t$(INSTALL_DATA) ../lib/lib%s$o $(DESTDIR)$(libdir)/lib%s$o\n", name, name
447				}
448				print  ""
449				print  "uninstall \\"
450				print  "uninstall.libs \\"
451				printf "uninstall.%s ::\n", name
452				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
453				printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
454				if ( overwrite == "yes" && lib_name == "libncurses.a" )
455				{
456					printf "\t@echo linking libcurses.a to libncurses.a\n"
457					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
458				}
459				if ( host == "vxworks" )
460				{
461					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/lib%s$o\n", name
462					printf "\t-@rm -f $(DESTDIR)$(libdir)/lib%s$o\n", name
463				}
464			}
465			print ""
466			print "clean ::"
467			removelinks("../lib");
468			print ""
469			print "mostlyclean::"
470			printf "\t-rm -f $(%s_OBJS)\n", OBJS
471			if ( MODEL == "LIBTOOL" ) {
472				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
473			}
474		}
475		else if ( found == 2 )
476		{
477			print ""
478			print "mostlyclean::"
479			printf "\t-rm -f $(%s_OBJS)\n", OBJS
480			if ( MODEL == "LIBTOOL" ) {
481				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
482			}
483			print ""
484			print "clean ::"
485			printf "\t-rm -f $(%s_OBJS)\n", OBJS
486			if ( MODEL == "LIBTOOL" ) {
487				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
488			}
489		}
490	}
491# vile:ts=4 sw=4
492