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