1# $Id: mk-1st.awk,v 1.78 2007/03/24 22:10:55 tom Exp $
2##############################################################################
3# Copyright (c) 1998-2006,2007 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#   TermlibRoot   ("tinfo" or other root for libterm.so)
44#   TermlibSuffix (".so" or other suffix for libterm.so)
45#	ReLink		  ("yes", or "no", flag to rebuild shared libs on install)
46#	DoLinks		  ("yes", "reverse" or "no", flag to add symbolic links)
47#	rmSoLocs	  ("yes" or "no", flag to add extra clean target)
48#	ldconfig	  (path for this tool, if used)
49#	overwrite	  ("yes" or "no", flag to add link to libcurses.a
50#	depend		  (optional dependencies for all objects, e.g, ncurses_cfg.h)
51#	host		  (cross-compile host, if any)
52#
53# Notes:
54#	CLIXs nawk does not like underscores in command-line variable names.
55#	Mixed-case variable names are ok.
56#	HP/UX requires shared libraries to have executable permissions.
57#
58function is_ticlib() {
59		return ( subset ~ /^ticlib$/ );
60	}
61function is_termlib() {
62		return ( subset ~ /^(ticlib\+)?termlib(\+ext_tinfo)?$/ );
63	}
64# see lib_name
65function lib_name_of(a_name) {
66		return sprintf("%s%s%s", prefix, a_name, suffix)
67	}
68# see imp_name
69function imp_name_of(a_name) {
70		if (ShlibVerInfix == "cygdll") {
71			result = sprintf("%s%s%s.a", prefix, a_name, suffix);
72		} else {
73			result = "";
74		}
75		return result;
76	}
77# see abi_name
78function abi_name_of(a_name) {
79		if (ShlibVerInfix == "cygdll") {
80			result = sprintf("%s%s$(ABI_VERSION)%s", "cyg", a_name, suffix);
81		} else if (ShlibVerInfix == "yes") {
82			result = sprintf("%s%s.$(ABI_VERSION)%s", prefix, a_name, suffix);
83		} else {
84			result = sprintf("%s.$(ABI_VERSION)", lib_name_of(a_name));
85		}
86		return result;
87	}
88# see rel_name
89function rel_name_of(a_name) {
90		if (ShlibVerInfix == "cygdll") {
91			result = sprintf("%s%s$(REL_VERSION)%s", "cyg", a_name, suffix);
92		} else if (ShlibVerInfix == "yes") {
93			result = sprintf("%s%s.$(REL_VERSION)%s", prefix, a_name, suffix);
94		} else {
95			result = sprintf("%s.$(REL_VERSION)", lib_name_of(a_name));
96		}
97		return result;
98	}
99# see end_name
100function end_name_of(a_name) {
101		if ( MODEL != "SHARED" ) {
102			result = lib_name_of(a_name);
103		} else if ( DoLinks == "reverse") {
104			result = lib_name_of(a_name);
105		} else {
106			if ( ShlibVer == "rel" ) {
107				result = rel_name_of(a_name);
108			} else if ( ShlibVer == "abi" || ShlibVer == "cygdll" ) {
109				result = abi_name_of(a_name);
110			} else {
111				result = lib_name_of(a_name);
112			}
113		}
114		return result
115	}
116function symlink(src,dst) {
117		if ( src != dst ) {
118			printf "rm -f %s; ", dst
119			printf "$(LN_S) %s %s; ", src, dst
120		}
121	}
122function rmlink(directory, dst) {
123		printf "\t-rm -f %s/%s\n", directory, dst
124	}
125function removelinks(directory) {
126		rmlink(directory, end_name);
127		if ( DoLinks == "reverse" ) {
128			if ( ShlibVer == "rel" ) {
129				rmlink(directory, abi_name);
130				rmlink(directory, rel_name);
131			} else if ( ShlibVer == "abi" ) {
132				rmlink(directory, abi_name);
133			}
134		} else {
135			if ( ShlibVer == "rel" ) {
136				rmlink(directory, abi_name);
137				rmlink(directory, lib_name);
138			} else if ( ShlibVer == "abi" ) {
139				rmlink(directory, lib_name);
140			}
141		}
142	}
143function make_shlib(objs, shlib_list) {
144		printf "\t$(MK_SHARED_LIB) $(%s_OBJS) $(%s) $(LDFLAGS)\n", objs, shlib_list
145	}
146function sharedlinks(directory) {
147		if ( ShlibVer != "auto" && ShlibVer != "cygdll" ) {
148			printf "\tcd %s && (", directory
149			if ( DoLinks == "reverse" ) {
150				if ( ShlibVer == "rel" ) {
151					symlink(lib_name, abi_name);
152					symlink(abi_name, rel_name);
153				} else if ( ShlibVer == "abi" ) {
154					symlink(lib_name, abi_name);
155				}
156			} else {
157				if ( ShlibVer == "rel" ) {
158					symlink(rel_name, abi_name);
159					symlink(abi_name, lib_name);
160				} else if ( ShlibVer == "abi" ) {
161					symlink(abi_name, lib_name);
162				}
163			}
164			printf ")\n"
165		}
166	}
167# termlib may be named explicitly via "--with-termlib=XXX", which overrides
168# any suffix.  Temporarily override "suffix" to account for this.
169function termlib_end_of() {
170	termlib_save_suffix = suffix;
171	suffix = TermlibSuffix;
172	termlib_temp_result = end_name_of(TermlibRoot);
173	suffix = termlib_save_suffix;
174	return termlib_temp_result;
175}
176function shlib_build(directory) {
177		dst_libs = sprintf("%s/%s", directory, end_name);
178		printf "%s : \\\n", dst_libs
179		printf "\t\t%s \\\n", directory
180		if (subset ~ /^base/ || subset == "ticlib" ) {
181			save_suffix = suffix
182			sub(/^[^.]\./,".",suffix)
183			if (directory != "../lib") {
184				printf "\t\t%s/%s \\\n", "../lib", termlib_end_of();
185			}
186			printf "\t\t%s/%s \\\n", directory, termlib_end_of();
187			suffix = save_suffix
188		}
189		printf "\t\t$(%s_OBJS)\n", OBJS
190		printf "\t@echo linking $@\n"
191		if ( is_ticlib() ) {
192			make_shlib(OBJS, "TICS_LIST")
193		} else if ( is_termlib() ) {
194			make_shlib(OBJS, "TINFO_LIST")
195		} else {
196			make_shlib(OBJS, "SHLIB_LIST")
197		}
198		sharedlinks(directory)
199	}
200function shlib_install(directory) {
201		src_lib1 = sprintf("../lib/%s", end_name);
202		dst_lib1 = sprintf("%s/%s", directory, end_name);
203		printf "%s : \\\n", dst_lib1
204		printf "\t\t%s \\\n", directory
205		printf "\t\t%s\n", src_lib1
206		printf "\t@echo installing $@\n"
207		printf "\t$(INSTALL_LIB) %s %s\n", src_lib1, dst_lib1;
208		sharedlinks(directory)
209	}
210function install_dll(directory,filename) {
211		src_name = sprintf("../lib/%s", filename);
212		dst_name = sprintf("$(DESTDIR)%s/%s", directory, filename);
213		printf "\t@echo installing %s as %s\n", src_name, dst_name
214		if ( directory == "$(bindir)" ) {
215			program = "$(INSTALL) -m 755";
216		} else {
217			program = "$(INSTALL_LIB)";
218		}
219		printf "\t%s %s %s\n", program, src_name, dst_name
220	}
221BEGIN	{
222		found = 0
223		using = 0
224	}
225	/^@/ {
226		using = 0
227		if (subset == "none") {
228			using = 1
229		} else if (index(subset,$2) > 0) {
230			if (using == 0) {
231				if (found == 0) {
232					print  ""
233					printf "# generated by mk-1st.awk (subset=%s)\n", subset
234					printf "#  name:          %s\n", name 
235					printf "#  traces:        %s\n", traces 
236					printf "#  MODEL:         %s\n", MODEL 
237					printf "#  model:         %s\n", model 
238					printf "#  prefix:        %s\n", prefix 
239					printf "#  suffix:        %s\n", suffix 
240					printf "#  subset:        %s\n", subset 
241					printf "#  ShlibVer:      %s\n", ShlibVer 
242					printf "#  ShlibVerInfix: %s\n", ShlibVerInfix 
243					printf "#  TermlibRoot:   %s\n", TermlibRoot 
244					printf "#  TermlibSuffix: %s\n", TermlibSuffix 
245					printf "#  ReLink:        %s\n", ReLink 
246					printf "#  DoLinks:       %s\n", DoLinks 
247					printf "#  rmSoLocs:      %s\n", rmSoLocs 
248					printf "#  ldconfig:      %s\n", ldconfig 
249					printf "#  overwrite:     %s\n", overwrite 
250					printf "#  depend:        %s\n", depend 
251					printf "#  host:          %s\n", host 
252					print  ""
253				}
254				using = 1
255			}
256			if ( is_ticlib() ) {
257				OBJS  = MODEL "_P"
258			} else if ( is_termlib() ) {
259				OBJS  = MODEL "_T"
260			} else {
261				OBJS  = MODEL
262			}
263		}
264	}
265	/^[@#]/ {
266		next
267	}
268	$1 ~ /trace/ {
269		if (traces != "all" && traces != MODEL && $1 != "lib_trace")
270			next
271	}
272	{
273		if (using \
274		 && ( $1 != "link_test" ) \
275		 && ( $2 == "lib" \
276		   || $2 == "progs" \
277		   || $2 == "c++" \
278		   || $2 == "tack" ))
279		{
280			if ( found == 0 )
281			{
282				printf "%s_OBJS =", OBJS
283				if ( $2 == "lib" )
284					found = 1
285				else
286					found = 2
287			}
288			printf " \\\n\t../%s/%s$o", model, $1
289		}
290	}
291END	{
292		print  ""
293		if ( found != 0 )
294		{
295			printf "\n$(%s_OBJS) : %s\n", OBJS, depend
296		}
297		if ( found == 1 )
298		{
299			print  ""
300			lib_name = lib_name_of(name);
301			if ( MODEL == "SHARED" )
302			{
303				abi_name = abi_name_of(name);
304				rel_name = rel_name_of(name);
305				imp_name = imp_name_of(name);
306				end_name = end_name_of(name);
307
308				shlib_build("../lib")
309
310				print  ""
311				print  "install \\"
312				print  "install.libs \\"
313
314				if ( ShlibVer == "cygdll" ) {
315
316					dst_dirs = "$(DESTDIR)$(bindir) $(DESTDIR)$(libdir)";
317					printf "install.%s :: %s $(LIBRARIES)\n", name, dst_dirs
318					install_dll("$(bindir)",end_name);
319					install_dll("$(libdir)",imp_name);
320
321				} else {
322
323					lib_dir = "$(DESTDIR)$(libdir)";
324					printf "install.%s :: %s/%s\n", name, lib_dir, end_name
325					print ""
326					if ( ReLink == "yes" ) {
327						shlib_build(lib_dir)
328					} else {
329						shlib_install(lib_dir)
330					}
331				}
332
333				if ( overwrite == "yes" && name == "ncurses" )
334				{
335					if ( ShlibVer == "cygdll" ) {
336						ovr_name = sprintf("libcurses%s.a", suffix)
337						printf "\t@echo linking %s to %s\n", imp_name, ovr_name
338						printf "\tcd $(DESTDIR)$(libdir) && (rm -f %s; $(LN_S) %s %s; )\n", ovr_name, imp_name, ovr_name
339					} else {
340						ovr_name = sprintf("libcurses%s", suffix)
341						printf "\t@echo linking %s to %s\n", end_name, ovr_name
342						printf "\tcd $(DESTDIR)$(libdir) && (rm -f %s; $(LN_S) %s %s; )\n", ovr_name, end_name, ovr_name
343					}
344				}
345				if ( ldconfig != "" && ldconfig != ":" ) {
346					printf "\t- test -z \"$(DESTDIR)\" && %s\n", ldconfig
347				}
348				print  ""
349				print  "uninstall \\"
350				print  "uninstall.libs \\"
351				printf "uninstall.%s ::\n", name
352				if ( ShlibVer == "cygdll" ) {
353
354					printf "\t@echo uninstalling $(DESTDIR)$(bindir)/%s\n", end_name
355					printf "\t-@rm -f $(DESTDIR)$(bindir)/%s\n", end_name
356
357					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", imp_name
358					printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", imp_name
359
360				} else {
361					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", end_name
362					removelinks("$(DESTDIR)$(libdir)")
363					if ( overwrite == "yes" && name == "ncurses" )
364					{
365						if ( ShlibVer == "cygdll" ) {
366							ovr_name = sprintf("libcurses%s.a", suffix)
367						} else {
368							ovr_name = sprintf("libcurses%s", suffix)
369						}
370						printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", ovr_name
371					}
372				}
373				if ( rmSoLocs == "yes" ) {
374					print  ""
375					print  "mostlyclean \\"
376					print  "clean ::"
377					printf "\t-@rm -f so_locations\n"
378				}
379			}
380			else if ( MODEL == "LIBTOOL" )
381			{
382				if ( $2 == "c++" ) {
383					compile="CXX"
384				} else {
385					compile="CC"
386				}
387				end_name = lib_name;
388				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
389				printf "\tcd ../lib && $(LIBTOOL_LINK) $(%s) -o %s $(%s_OBJS:$o=.lo) -rpath $(DESTDIR)$(libdir) -version-info $(NCURSES_MAJOR):$(NCURSES_MINOR) $(SHLIB_LIST)\n", compile, lib_name, OBJS
390				print  ""
391				print  "install \\"
392				print  "install.libs \\"
393				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
394				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
395				printf "\tcd ../lib; $(LIBTOOL_INSTALL) $(INSTALL) %s $(DESTDIR)$(libdir)\n", lib_name
396				print  ""
397				print  "uninstall \\"
398				print  "uninstall.libs \\"
399				printf "uninstall.%s ::\n", name
400				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
401				printf "\t-@$(LIBTOOL_UNINSTALL) rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
402			}
403			else
404			{
405				end_name = lib_name;
406				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
407				printf "\t$(AR) $(AR_OPTS) $@ $?\n"
408				printf "\t$(RANLIB) $@\n"
409				if ( host == "vxworks" )
410				{
411					printf "\t$(LD) $(LD_OPTS) $? -o $(@:.a=$o)\n"
412				}
413				print  ""
414				print  "install \\"
415				print  "install.libs \\"
416				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
417				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
418				printf "\t$(INSTALL_DATA) ../lib/%s $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
419				if ( overwrite == "yes" && lib_name == "libncurses.a" )
420				{
421					printf "\t@echo linking libcurses.a to libncurses.a\n"
422					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
423					printf "\t(cd $(DESTDIR)$(libdir) && $(LN_S) libncurses.a libcurses.a)\n"
424				}
425				printf "\t$(RANLIB) $(DESTDIR)$(libdir)/%s\n", lib_name
426				if ( host == "vxworks" )
427				{
428					printf "\t@echo installing ../lib/lib%s$o as $(DESTDIR)$(libdir)/lib%s$o\n", name, name
429					printf "\t$(INSTALL_DATA) ../lib/lib%s$o $(DESTDIR)$(libdir)/lib%s$o\n", name, name
430				}
431				print  ""
432				print  "uninstall \\"
433				print  "uninstall.libs \\"
434				printf "uninstall.%s ::\n", name
435				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
436				printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
437				if ( overwrite == "yes" && lib_name == "libncurses.a" )
438				{
439					printf "\t@echo linking libcurses.a to libncurses.a\n"
440					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
441				}
442				if ( host == "vxworks" )
443				{
444					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/lib%s$o\n", name
445					printf "\t-@rm -f $(DESTDIR)$(libdir)/lib%s$o\n", name
446				}
447			}
448			print ""
449			print "clean ::"
450			removelinks("../lib");
451			print ""
452			print "mostlyclean::"
453			printf "\t-rm -f $(%s_OBJS)\n", OBJS
454			if ( MODEL == "LIBTOOL" ) {
455				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
456			}
457		}
458		else if ( found == 2 )
459		{
460			print ""
461			print "mostlyclean::"
462			printf "\t-rm -f $(%s_OBJS)\n", OBJS
463			if ( MODEL == "LIBTOOL" ) {
464				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
465			}
466			print ""
467			print "clean ::"
468			printf "\t-rm -f $(%s_OBJS)\n", OBJS
469			if ( MODEL == "LIBTOOL" ) {
470				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
471			}
472		}
473	}
474# vile:ts=4 sw=4
475