1# $Id: mk-1st.awk,v 1.65 2005/06/18 19:15:57 tom Exp $
2##############################################################################
3# Copyright (c) 1998-2004,2005 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")
41#	ShlibVer ("rel", "abi" or "auto", to augment DoLinks variable)
42#	ShlibVerInfix ("yes" or "no", determines location of version #)
43#	DoLinks ("yes", "reverse" or "no", flag to add symbolic links)
44#	rmSoLocs ("yes" or "no", flag to add extra clean target)
45#	ldconfig (path for this tool, if used)
46#	overwrite ("yes" or "no", flag to add link to libcurses.a
47#	depend (optional dependencies for all objects, e.g, ncurses_cfg.h)
48#	host (cross-compile host, if any)
49#
50# Notes:
51#	CLIXs nawk does not like underscores in command-line variable names.
52#	Mixed-case is ok.
53#	HP/UX requires shared libraries to have executable permissions.
54#
55function symlink(src,dst) {
56		if ( src != dst ) {
57			printf "rm -f %s; ", dst
58			printf "$(LN_S) %s %s; ", src, dst
59		}
60	}
61function rmlink(directory, dst) {
62		printf "\t-rm -f %s/%s\n", directory, dst
63}
64function removelinks(directory) {
65		rmlink(directory, end_name);
66		if ( DoLinks == "reverse" ) {
67				if ( ShlibVer == "rel" ) {
68					rmlink(directory, abi_name);
69					rmlink(directory, rel_name);
70				} else if ( ShlibVer == "abi" ) {
71					rmlink(directory, abi_name);
72				}
73		} else {
74				if ( ShlibVer == "rel" ) {
75					rmlink(directory, abi_name);
76					rmlink(directory, lib_name);
77				} else if ( ShlibVer == "abi" ) {
78					rmlink(directory, lib_name);
79				}
80		}
81	}
82function sharedlinks(directory) {
83		if ( ShlibVer != "auto" && ShlibVer != "cygdll" ) {
84			printf "\tcd %s && (", directory
85			if ( DoLinks == "reverse" ) {
86				if ( ShlibVer == "rel" ) {
87					symlink(lib_name, abi_name);
88					symlink(abi_name, rel_name);
89				} else if ( ShlibVer == "abi" ) {
90					symlink(lib_name, abi_name);
91				}
92			} else {
93				if ( ShlibVer == "rel" ) {
94					symlink(rel_name, abi_name);
95					symlink(abi_name, lib_name);
96				} else if ( ShlibVer == "abi" ) {
97					symlink(abi_name, lib_name);
98				}
99			}
100			printf ")\n"
101		}
102	}
103BEGIN	{
104		found = 0
105		using = 0
106	}
107	/^@/ {
108		using = 0
109		if (subset == "none") {
110			using = 1
111		} else if (index(subset,$2) > 0) {
112			if (using == 0) {
113				if (found == 0) {
114					print  ""
115					printf "# generated by mk-1st.awk (subset=%s)\n", subset
116					printf "#  name:          %s\n", name 
117					printf "#  traces:        %s\n", traces 
118					printf "#  MODEL:         %s\n", MODEL 
119					printf "#  model:         %s\n", model 
120					printf "#  prefix:        %s\n", prefix 
121					printf "#  suffix:        %s\n", suffix 
122					printf "#  subset:        %s\n", subset 
123					printf "#  ShlibVer:      %s\n", ShlibVer 
124					printf "#  ShlibVerInfix: %s\n", ShlibVerInfix 
125					printf "#  DoLinks:       %s\n", DoLinks 
126					printf "#  rmSoLocs:      %s\n", rmSoLocs 
127					printf "#  ldconfig:      %s\n", ldconfig 
128					printf "#  overwrite:     %s\n", overwrite 
129					printf "#  depend:        %s\n", depend 
130					printf "#  host:          %s\n", host 
131					print  ""
132				}
133				using = 1
134			}
135			if ( subset == "termlib" || subset == "termlib+ext_tinfo" ) {
136				OBJS  = MODEL "_T"
137			} else {
138				OBJS  = MODEL
139			}
140		}
141	}
142	/^[@#]/ {
143		next
144	}
145	$1 ~ /trace/ {
146		if (traces != "all" && traces != MODEL && $1 != "lib_trace")
147			next
148	}
149	{
150		if (using \
151		 && ( $1 != "link_test" ) \
152		 && ( $2 == "lib" \
153		   || $2 == "progs" \
154		   || $2 == "c++" \
155		   || $2 == "tack" ))
156		{
157			if ( found == 0 )
158			{
159				printf "%s_OBJS =", OBJS
160				if ( $2 == "lib" )
161					found = 1
162				else
163					found = 2
164			}
165			printf " \\\n\t../%s/%s$o", model, $1
166		}
167	}
168END	{
169		print  ""
170		if ( found != 0 )
171		{
172			printf "\n$(%s_OBJS) : %s\n", OBJS, depend
173		}
174		if ( found == 1 )
175		{
176			print  ""
177			lib_name = sprintf("%s%s%s", prefix, name, suffix)
178			if ( MODEL == "SHARED" )
179			{
180				if (ShlibVerInfix == "cygdll") {
181					abi_name = sprintf("%s%s$(ABI_VERSION)%s", "cyg", name, suffix);
182					rel_name = sprintf("%s%s$(REL_VERSION)%s", "cyg", name, suffix);
183					imp_name = sprintf("%s%s%s.a", prefix, name, suffix);
184				} else if (ShlibVerInfix == "yes") {
185					abi_name = sprintf("%s%s.$(ABI_VERSION)%s", prefix, name, suffix);
186					rel_name = sprintf("%s%s.$(REL_VERSION)%s", prefix, name, suffix);
187				} else {
188					abi_name = sprintf("%s.$(ABI_VERSION)", lib_name);
189					rel_name = sprintf("%s.$(REL_VERSION)", lib_name);
190				}
191				if ( DoLinks == "reverse") {
192					end_name = lib_name;
193				} else {
194					if ( ShlibVer == "rel" ) {
195						end_name = rel_name;
196					} else if ( ShlibVer == "abi" || ShlibVer == "cygdll" ) {
197						end_name = abi_name;
198					} else {
199						end_name = lib_name;
200					}
201				}
202
203				if ( ShlibVer == "cygdll" ) {
204					dst_dirs = "$(DESTDIR)$(bindir) $(DESTDIR)$(libdir)";
205					printf "$(SHARED_LIB) $(IMPORT_LIB) : $(%s_OBJS)\n", OBJS;
206					print "\t-@rm -f $(SHARED_LIB) $(IMPORT_LIB)";
207				} else {
208					dst_dirs = "$(DESTDIR)$(libdir)";
209					printf "../lib/%s : $(%s_OBJS)\n", end_name, OBJS
210					print "\t-@rm -f $@";
211				}
212				if ( subset == "termlib" || subset == "termlib+ext_tinfo" ) {
213					printf "\t$(MK_SHARED_LIB) $(%s_OBJS) $(TINFO_LIST) $(LDFLAGS)\n", OBJS
214				} else {
215					printf "\t$(MK_SHARED_LIB) $(%s_OBJS) $(SHLIB_LIST) $(LDFLAGS)\n", OBJS
216				}
217				sharedlinks("../lib")
218
219				print  ""
220				print  "install \\"
221				print  "install.libs \\"
222				printf "install.%s :: %s $(LIBRARIES)\n", name, dst_dirs
223
224				if ( ShlibVer == "cygdll" ) {
225
226					src_name = sprintf("../lib/%s", end_name);
227					dst_name = sprintf("$(DESTDIR)$(bindir)/%s", end_name);
228					printf "\t@echo installing %s as %s\n", src_name, dst_name
229					printf "\t-@rm -f %s\n", dst_name
230					printf "\t$(INSTALL_LIB) %s %s\n", src_name, dst_name
231
232					src_name = sprintf("../lib/%s", imp_name);
233					dst_name = sprintf("$(DESTDIR)$(libdir)/%s", imp_name);
234					printf "\t@echo installing %s as %s\n", src_name, dst_name
235					printf "\t-@rm -f %s\n", dst_name
236					printf "\t$(INSTALL_LIB) %s %s\n", src_name, dst_name
237
238				} else {
239
240					src_name = sprintf("../lib/%s", end_name);
241					dst_name = sprintf("$(DESTDIR)$(libdir)/%s", end_name);
242					printf "\t@echo installing %s as %s\n", src_name, dst_name
243					printf "\t-@rm -f %s\n", dst_name
244					printf "\t$(INSTALL_LIB) %s %s\n", src_name, dst_name
245
246					sharedlinks("$(DESTDIR)$(libdir)")
247
248				}
249
250				if ( overwrite == "yes" && name == "ncurses" )
251				{
252					if ( ShlibVer == "cygdll" ) {
253						ovr_name = sprintf("libcurses%s.a", suffix)
254						printf "\t@echo linking %s to %s\n", imp_name, ovr_name
255						printf "\tcd $(DESTDIR)$(libdir) && (rm -f %s; $(LN_S) %s %s; )\n", ovr_name, imp_name, ovr_name
256					} else {
257						ovr_name = sprintf("libcurses%s", suffix)
258						printf "\t@echo linking %s to %s\n", end_name, ovr_name
259						printf "\tcd $(DESTDIR)$(libdir) && (rm -f %s; $(LN_S) %s %s; )\n", ovr_name, end_name, ovr_name
260					}
261				}
262				if ( ldconfig != "" ) {
263					printf "\t- test -z \"$(DESTDIR)\" && %s\n", ldconfig
264				}
265				print  ""
266				print  "uninstall \\"
267				print  "uninstall.libs \\"
268				printf "uninstall.%s ::\n", name
269				if ( ShlibVer == "cygdll" ) {
270
271					printf "\t@echo uninstalling $(DESTDIR)$(bindir)/%s\n", end_name
272					printf "\t-@rm -f $(DESTDIR)$(bindir)/%s\n", end_name
273
274					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", imp_name
275					printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", imp_name
276
277				} else {
278					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", end_name
279					removelinks("$(DESTDIR)$(libdir)")
280					if ( overwrite == "yes" && name == "ncurses" )
281					{
282						if ( ShlibVer == "cygdll" ) {
283							ovr_name = sprintf("libcurses%s.a", suffix)
284						} else {
285							ovr_name = sprintf("libcurses%s", suffix)
286						}
287						printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", ovr_name
288					}
289				}
290				if ( rmSoLocs == "yes" ) {
291					print  ""
292					print  "mostlyclean \\"
293					print  "clean ::"
294					printf "\t-@rm -f so_locations\n"
295				}
296			}
297			else if ( MODEL == "LIBTOOL" )
298			{
299				if ( $2 == "c++" ) {
300					compile="CXX"
301				} else {
302					compile="CC"
303				}
304				end_name = lib_name;
305				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
306				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
307				print  ""
308				print  "install \\"
309				print  "install.libs \\"
310				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
311				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
312				printf "\tcd ../lib; $(LIBTOOL_INSTALL) $(INSTALL) %s $(DESTDIR)$(libdir)\n", lib_name
313				print  ""
314				print  "uninstall \\"
315				print  "uninstall.libs \\"
316				printf "uninstall.%s ::\n", name
317				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
318				printf "\t-@$(LIBTOOL_UNINSTALL) rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
319			}
320			else
321			{
322				end_name = lib_name;
323				printf "../lib/%s : $(%s_OBJS)\n", lib_name, OBJS
324				printf "\t$(AR) $(AR_OPTS) $@ $?\n"
325				printf "\t$(RANLIB) $@\n"
326				if ( host == "vxworks" )
327				{
328					printf "\t$(LD) $(LD_OPTS) $? -o $(@:.a=$o)\n"
329				}
330				print  ""
331				print  "install \\"
332				print  "install.libs \\"
333				printf "install.%s :: $(DESTDIR)$(libdir) ../lib/%s\n", name, lib_name
334				printf "\t@echo installing ../lib/%s as $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
335				printf "\t$(INSTALL_DATA) ../lib/%s $(DESTDIR)$(libdir)/%s\n", lib_name, lib_name
336				if ( overwrite == "yes" && lib_name == "libncurses.a" )
337				{
338					printf "\t@echo linking libcurses.a to libncurses.a\n"
339					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
340					printf "\t(cd $(DESTDIR)$(libdir) && $(LN_S) libncurses.a libcurses.a)\n"
341				}
342				printf "\t$(RANLIB) $(DESTDIR)$(libdir)/%s\n", lib_name
343				if ( host == "vxworks" )
344				{
345					printf "\t@echo installing ../lib/lib%s$o as $(DESTDIR)$(libdir)/lib%s$o\n", name, name
346					printf "\t$(INSTALL_DATA) ../lib/lib%s$o $(DESTDIR)$(libdir)/lib%s$o\n", name, name
347				}
348				print  ""
349				print  "uninstall \\"
350				print  "uninstall.libs \\"
351				printf "uninstall.%s ::\n", name
352				printf "\t@echo uninstalling $(DESTDIR)$(libdir)/%s\n", lib_name
353				printf "\t-@rm -f $(DESTDIR)$(libdir)/%s\n", lib_name
354				if ( overwrite == "yes" && lib_name == "libncurses.a" )
355				{
356					printf "\t@echo linking libcurses.a to libncurses.a\n"
357					printf "\t-@rm -f $(DESTDIR)$(libdir)/libcurses.a\n"
358				}
359				if ( host == "vxworks" )
360				{
361					printf "\t@echo uninstalling $(DESTDIR)$(libdir)/lib%s$o\n", name
362					printf "\t-@rm -f $(DESTDIR)$(libdir)/lib%s$o\n", name
363				}
364			}
365			print ""
366			print "clean ::"
367			removelinks("../lib");
368			print ""
369			print "mostlyclean::"
370			printf "\t-rm -f $(%s_OBJS)\n", OBJS
371			if ( MODEL == "LIBTOOL" ) {
372				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
373			}
374		}
375		else if ( found == 2 )
376		{
377			print ""
378			print "mostlyclean::"
379			printf "\t-rm -f $(%s_OBJS)\n", OBJS
380			if ( MODEL == "LIBTOOL" ) {
381				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
382			}
383			print ""
384			print "clean ::"
385			printf "\t-rm -f $(%s_OBJS)\n", OBJS
386			if ( MODEL == "LIBTOOL" ) {
387				printf "\t-$(LIBTOOL_CLEAN) rm -f $(%s_OBJS:$o=.lo)\n", OBJS
388			}
389		}
390	}
391