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