xref: /titanic_44/usr/src/lib/libshell/misc/buildksh93.sh (revision 2d6b5ea734bb47d251c82670646fde46af15fd69)
1#!/bin/ksh -p
2# (note we use "/bin/ksh -p" for Linux/pdksh support in this script)
3
4#
5# CDDL HEADER START
6#
7# The contents of this file are subject to the terms of the
8# Common Development and Distribution License (the "License").
9# You may not use this file except in compliance with the License.
10#
11# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12# or http://www.opensolaris.org/os/licensing.
13# See the License for the specific language governing permissions
14# and limitations under the License.
15#
16# When distributing Covered Code, include this CDDL HEADER in each
17# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18# If applicable, add the following below this CDDL HEADER, with the
19# fields enclosed by brackets "[]" replaced with your own identifying
20# information: Portions Copyright [yyyy] [name of copyright owner]
21#
22# CDDL HEADER END
23#
24
25#
26# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29
30#
31# buildksh93.ksh - ast-ksh standalone build script for the
32# OpenSolaris ksh93-integration project
33#
34
35# ksh93t sources can be downloaded like this from the AT&T site:
36# wget --http-user="I accept www.opensource.org/licenses/cpl" --http-passwd="." 'http://www.research.att.com/~gsf/download/tgz/INIT.2008-11-04.tgz'
37# wget --http-user="I accept www.opensource.org/licenses/cpl" --http-passwd="." 'http://www.research.att.com/~gsf/download/tgz/ast-ksh.2008-11-04.tgz'
38
39function fatal_error
40{
41    print -u2 "${progname}: $*"
42    exit 1
43}
44
45set -o errexit
46set -o xtrace
47
48typeset progname="$(basename "${0}")"
49typeset buildmode="$1"
50
51if [[ "${buildmode}" == "" ]] ; then
52    fatal_error "buildmode required."
53fi
54
55# Make sure we use the C locale during building to avoid any unintended
56# side-effects
57export LANG=C
58export LC_ALL=$LANG LC_MONETARY=$LANG LC_NUMERIC=$LANG LC_MESSAGES=$LANG LC_COLLATE=$LANG LC_CTYPE=$LANG
59# Make sure the POSIX/XPG6 tools are in front of /usr/bin (/bin is needed for Linux after /usr/bin)
60export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/bin:/opt/SUNWspro/bin
61
62# Make sure the POSIX/XPG6 packages are installed (mandatory for building
63# our version of ksh93 correctly).
64if [[ "$(uname -s)" == "SunOS" ]] ; then
65    if [[ ! -x "/usr/xpg6/bin/tr" ]] ; then
66        fatal_error "XPG6/4 packages (SUNWxcu6,SUNWxcu4) not installed."
67    fi
68fi
69
70function print_solaris_builtin_header
71{
72# Make sure to use \\ instead of \ for continuations
73cat <<ENDOFTEXT
74/*
75 * CDDL HEADER START
76 *
77 * The contents of this file are subject to the terms of the
78 * Common Development and Distribution License (the "License").
79 * You may not use this file except in compliance with the License.
80 *
81 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
82 * or http://www.opensolaris.org/os/licensing.
83 * See the License for the specific language governing permissions
84 * and limitations under the License.
85 *
86 * When distributing Covered Code, include this CDDL HEADER in each
87 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
88 * If applicable, add the following below this CDDL HEADER, with the
89 * fields enclosed by brackets "[]" replaced with your own identifying
90 * information: Portions Copyright [yyyy] [name of copyright owner]
91 *
92 * CDDL HEADER END
93 */
94
95/*
96 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
97 * Use is subject to license terms.
98 */
99
100#ifndef _SOLARIS_KSH_CMDLIST_H
101#define	_SOLARIS_KSH_CMDLIST_H
102
103#ifdef	__cplusplus
104extern "C" {
105#endif
106
107/*
108 * List builtins for Solaris.
109 * The list here is partially autogenerated and partially hand-picked
110 * based on compatibility with the native Solaris versions of these
111 * tools
112 */
113
114/* POSIX compatible commands */
115#ifdef _NOT_YET
116#define	XPG6CMDLIST(f)	\\
117	{ "/usr/xpg6/bin/" #f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
118#define	XPG4CMDLIST(f)	\\
119	{ "/usr/xpg4/bin/" #f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
120#else
121#define	XPG6CMDLIST(f)
122#define	XPG4CMDLIST(f)
123#endif /* NOT_YET */
124/*
125 * Commands which are 100% compatible with native Solaris versions (/bin is
126 * a softlink to ./usr/bin, ksh93 takes care about the lookup)
127 */
128#define	BINCMDLIST(f)	\\
129	{ "/bin/"	#f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
130#define	USRBINCMDLIST(f)	\\
131	{ "/usr/bin/"	#f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
132#define	SBINCMDLIST(f)	\\
133	{ "/sbin/"	#f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
134#define	SUSRBINCMDLIST(f)	\\
135	{ "/usr/sbin/"	#f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
136/*
137 * Make all ksh93 builtins accessible when /usr/ast/bin was added to
138 * /usr/xpg6/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/bin:/opt/SUNWspro/bin
139 */
140#define	ASTCMDLIST(f)	\\
141	{ "/usr/ast/bin/" #f, NV_BLTIN|NV_BLTINOPT|NV_NOFREE, bltin(f) },
142
143/* undo ast_map.h #defines to avoid collision */
144#undef basename
145#undef dirname
146
147/* Generated data, do not edit. */
148XPG4CMDLIST(basename)
149ASTCMDLIST(basename)
150BINCMDLIST(cat)
151ASTCMDLIST(cat)
152XPG4CMDLIST(chgrp)
153ASTCMDLIST(chgrp)
154ASTCMDLIST(chmod)
155XPG4CMDLIST(chown)
156BINCMDLIST(chown)
157ASTCMDLIST(chown)
158BINCMDLIST(cksum)
159ASTCMDLIST(cksum)
160BINCMDLIST(cmp)
161ASTCMDLIST(cmp)
162ASTCMDLIST(comm)
163XPG4CMDLIST(cp)
164ASTCMDLIST(cp)
165BINCMDLIST(cut)
166ASTCMDLIST(cut)
167XPG4CMDLIST(date)
168ASTCMDLIST(date)
169ASTCMDLIST(dirname)
170XPG4CMDLIST(expr)
171ASTCMDLIST(expr)
172ASTCMDLIST(fds)
173ASTCMDLIST(fmt)
174ASTCMDLIST(fold)
175BINCMDLIST(head)
176ASTCMDLIST(head)
177XPG4CMDLIST(id)
178ASTCMDLIST(id)
179ASTCMDLIST(join)
180XPG4CMDLIST(ln)
181ASTCMDLIST(ln)
182BINCMDLIST(logname)
183ASTCMDLIST(logname)
184BINCMDLIST(mkdir)
185ASTCMDLIST(mkdir)
186BINCMDLIST(mkfifo)
187ASTCMDLIST(mkfifo)
188XPG4CMDLIST(mv)
189ASTCMDLIST(mv)
190BINCMDLIST(paste)
191ASTCMDLIST(paste)
192ASTCMDLIST(pathchk)
193BINCMDLIST(rev)
194ASTCMDLIST(rev)
195XPG4CMDLIST(rm)
196ASTCMDLIST(rm)
197BINCMDLIST(rmdir)
198ASTCMDLIST(rmdir)
199XPG4CMDLIST(stty)
200ASTCMDLIST(stty)
201BINCMDLIST(sum)
202ASTCMDLIST(sum)
203SUSRBINCMDLIST(sync)
204SBINCMDLIST(sync)
205BINCMDLIST(sync)
206ASTCMDLIST(sync)
207XPG4CMDLIST(tail)
208ASTCMDLIST(tail)
209BINCMDLIST(tee)
210ASTCMDLIST(tee)
211BINCMDLIST(tty)
212ASTCMDLIST(tty)
213ASTCMDLIST(uname)
214BINCMDLIST(uniq)
215ASTCMDLIST(uniq)
216BINCMDLIST(wc)
217ASTCMDLIST(wc)
218
219/* Mandatory for ksh93 test suite and AST scripts */
220BINCMDLIST(getconf)
221
222#ifdef	__cplusplus
223}
224#endif
225
226#endif /* !_SOLARIS_KSH_CMDLIST_H */
227ENDOFTEXT
228}
229
230function build_shell
231{
232    set -o errexit
233    set -o xtrace
234
235    # OS.cputype.XXbit.compiler
236    case "${buildmode}" in
237        *.linux.*)
238            # ksh93+AST config flags
239            bast_flags="-DSHOPT_CMDLIB_BLTIN=0 -DSH_CMDLIB_DIR=\\\"/usr/ast/bin\\\" -DSHOPT_SYSRC -D_map_libc=1"
240
241            # gcc flags
242            bgcc99="gcc -std=gnu99 "
243            bgcc_ccflags="${bon_flags} ${bast_flags} -g"
244
245            case "${buildmode}" in
246                # Linux i386
247                *.i386.32bit.gcc*)  HOSTTYPE="linux.i386" CC="${bgcc99} -fPIC" cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"
248                    ;;
249                *)
250                    fatal_error "build_shell: Illegal Linux type/compiler build mode \"${buildmode}\"."
251                    ;;
252            esac
253            ;;
254        *.solaris.*)
255            # Notes:
256            # 1. Do not remove/modify these flags or their order before either
257            # asking the project leads at
258            # http://www.opensolaris.org/os/project/ksh93-integration/
259            # These flags all have a purpose, even if they look
260            # weird/redundant/etc. at the first look.
261            #
262            # 2. We use -KPIC here since -Kpic is too small on 64bit sparc and
263            # on 32bit it's close to the barrier so we use it for both 32bit and
264            # 64bit to avoid later suprises when people update libast in the
265            # future
266            #
267            # 3. "-D_map_libc=1" is needed to force map.c to add a "_ast_" prefix to all
268            # AST symbol names which may otherwise collide with Solaris/Linux libc
269            #
270            # 4. "-DSHOPT_SYSRC" enables /etc/ksh.kshrc support (AST default is currently
271            # to enable it if /etc/ksh.kshrc or /etc/bash.bashrc are available on the
272            # build machine).
273            #
274            # 5. -D_lib_socket=1 -lsocket -lnsl" was added to make sure ksh93 is compiled
275            # with networking support enabled, the current AST build infratructure has
276            # problems with detecting networking support in Solaris.
277            #
278            # 6. "-xc99=%all -D_XOPEN_SOURCE=600 -D__EXTENSIONS__=1" is used to force
279            # the compiler into C99 mode. Otherwise ksh93 will be much slower and lacks
280            # lots of arithmethic functions.
281            #
282            # 7. "-D_TS_ERRNO -D_REENTRANT" are flags taken from the default OS/Net
283            # build system.
284            #
285            # 8. "-xpagesize_stack=64K is used on SPARC to enhance the performace
286            #
287            # 9. -DSHOPT_CMDLIB_BLTIN=0 -DSH_CMDLIB_DIR=\\\"/usr/ast/bin\\\" -DSHOPT_CMDLIB_HDR=\\\"/home/test001/ksh93/ast_ksh_20070322/solaris_cmdlist.h\\\"
288            # is used to bind all ksh93 builtins to a "virtual" directory
289            # called "/usr/ast/bin/" and to adjust the list of builtins
290            # enabled by default to those defined by PSARC 2006/550
291
292            solaris_builtin_header="${PWD}/tmp_solaris_builtin_header.h"
293            print_solaris_builtin_header >"${solaris_builtin_header}"
294
295            # OS/Net build flags
296            bon_flags="-D_TS_ERRNO -D_REENTRANT"
297
298            # ksh93+AST config flags
299            bast_flags="-DSHOPT_CMDLIB_BLTIN=0 -DSH_CMDLIB_DIR=\\\"/usr/ast/bin\\\" -DSHOPT_CMDLIB_HDR=\\\"${solaris_builtin_header}\\\" -DSHOPT_SYSRC -D_map_libc=1"
300
301            # Sun Studio flags
302            bsunc99="/opt/SUNWspro/bin/cc -xc99=%all -D_XOPEN_SOURCE=600 -D__EXTENSIONS__=1"
303            bsuncc_app_ccflags_sparc="-xpagesize_stack=64K" # use bsuncc_app_ccflags_sparc only for final executables
304            bsuncc_ccflags="${bon_flags} -KPIC -g -xs -xspace -Xa -xstrconst -z combreloc -xildoff -xcsi -errtags=yes ${bast_flags} -D_lib_socket=1 -lsocket -lnsl"
305
306            # gcc flags
307            bgcc99="/usr/sfw/bin/gcc -std=gnu99 -D_XOPEN_SOURCE=600 -D__EXTENSIONS__=1"
308            bgcc_ccflags="${bon_flags} ${bast_flags} -D_lib_socket=1 -lsocket -lnsl"
309
310
311            case "${buildmode}" in
312                *.i386.32bit.suncc*)  HOSTTYPE="sol11.i386" CC="${bsunc99}"                            cc_sharedlib="-G" CCFLAGS="${bsuncc_ccflags}"  ;;
313                *.i386.64bit.suncc*)  HOSTTYPE="sol11.i386" CC="${bsunc99} -xarch=amd64 -KPIC"         cc_sharedlib="-G" CCFLAGS="${bsuncc_ccflags}"  ;;
314                *.sparc.32bit.suncc*) HOSTTYPE="sol11.sun4" CC="${bsunc99}"                            cc_sharedlib="-G" CCFLAGS="${bsuncc_ccflags}" bsuncc_app_ccflags="${bsuncc_app_ccflags_sparc}" ;;
315                *.sparc.64bit.suncc*) HOSTTYPE="sol11.sun4" CC="${bsunc99} -xarch=v9 -dalign -KPIC"    cc_sharedlib="-G" CCFLAGS="${bsuncc_ccflags}" bsuncc_app_ccflags="${bsuncc_app_ccflags_sparc}" ;;
316
317                *.i386.32bit.gcc*)  HOSTTYPE="sol11.i386" CC="${bgcc99} -fPIC"                                            cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
318                *.i386.64bit.gcc*)  HOSTTYPE="sol11.i386" CC="${bgcc99} -m64 -mtune=opteron -Ui386 -U__i386 -fPIC"        cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
319                *.sparc.32bit.gcc*) HOSTTYPE="sol11.sun4" CC="${bgcc99} -m32 -mcpu=v8 -fPIC"                              cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
320                *.sparc.64bit.gcc*) HOSTTYPE="sol11.sun4" CC="${bgcc99} -m64 -mcpu=v9 -fPIC"                              cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
321                *.s390.32bit.gcc*)  HOSTTYPE="sol11.s390" CC="${bgcc99} -m32          -fPIC"				  cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
322                *.s390.64bit.gcc*)  HOSTTYPE="sol11.s390" CC="${bgcc99} -m64          -fPIC"				  cc_sharedlib="-shared" CCFLAGS="${bgcc_ccflags}"  ;;
323
324                *)
325                    fatal_error "build_shell: Illegal Solaris type/compiler build mode \"${buildmode}\"."
326                    ;;
327            esac
328            ;;
329        *)
330            fatal_error "Illegal OS build mode \"${buildmode}\"."
331            ;;
332    esac
333
334    # some prechecks
335    [[ -z "${CCFLAGS}"  ]] && fatal_error "build_shell: CCFLAGS is empty."
336    [[ -z "${CC}"       ]] && fatal_error "build_shell: CC is empty."
337    [[ -z "${HOSTTYPE}" ]] && fatal_error "build_shell: HOSTTYPE is empty."
338    [[ ! -f "bin/package" ]] && fatal_error "build_shell: bin/package missing."
339    [[ ! -x "bin/package" ]] && fatal_error "build_shell: bin/package not executable."
340
341    export CCFLAGS CC HOSTTYPE
342
343    # build ksh93
344    bin/package make CCFLAGS="${CCFLAGS}" CC="${CC}" HOSTTYPE="${HOSTTYPE}"
345
346    root="${PWD}/arch/${HOSTTYPE}"
347    [[ -d "$root" ]] || fatal_error "build_shell: directory ${root} not found."
348    log="${root}/lib/package/gen/make.out"
349
350    [[ -s $log ]] || fatal_error "build_shell: no make.out log found."
351
352    if [[ "${buildmode}" != *.staticshell* ]] ; then
353        # libcmd causes some trouble since there is a squatter in solaris
354        # This has been fixed in Solaris 11/B48 but may require adjustments
355        # for older Solaris releases
356        for lib in libast libdll libsum libcmd libshell ; do
357            (( $? == 0 )) || exit 1
358            case "$lib" in
359            libshell)
360                base="lib/"
361                vers=1
362                link="-L${root}/lib/ -lcmd -lsum -ldll -last -lm"
363                ;;
364            libdll)
365                base="src/lib/${lib}"
366                vers=1
367                link="-ldl"
368                ;;
369            libast)
370                base="src/lib/${lib}"
371                vers=1
372                link="-lm"
373                ;;
374            *)
375                base="src/lib/${lib}"
376                vers=1
377                link="-L${root}/lib/ -last -lm"
378                ;;
379            esac
380
381            (
382            cd "${root}/${base}"
383            if [[ "${buildmode}" == *solaris* ]] ; then
384                ${CC} ${cc_sharedlib} ${CCFLAGS} -Bdirect -Wl,-zallextract -Wl,-zmuldefs -o "${root}/lib/${lib}.so.${vers}" "${lib}.a"  $link
385            else
386                ${CC} ${cc_sharedlib} ${CCFLAGS} -Wl,--whole-archive -Wl,-zmuldefs "${lib}.a" -Wl,--no-whole-archive -o "${root}/lib/${lib}.so.${vers}" $link
387            fi
388
389            #rm ${lib}.a
390            mv "${lib}.a" "disabled_${lib}.a_"
391
392            cd "${root}/lib"
393            ln -sf "${lib}.so.${vers}" "${lib}.so"
394            )
395        done
396
397        (
398          base=src/cmd/ksh93
399          cd "${root}/${base}"
400          rm -f \
401	      "${root}/lib/libshell.a" \
402              "${root}/lib/libsum.a" \
403              "${root}/lib/libdll.a" \
404              "${root}/lib/libast.a"
405
406          if [[ "${buildmode}" == *solaris* ]] ; then
407              ${CC} ${CCFLAGS} ${bsuncc_app_ccflags} -L${root}/lib/ -Bdirect -o ksh pmain.o -lshell -Bstatic -lcmd -Bdynamic -lsum -ldll -last -lm -lmd -lsecdb
408          else
409              ${CC} ${CCFLAGS} ${bsuncc_app_ccflags} -L${root}/lib/ -o ksh pmain.o -lshell -lcmd -lsum -ldll -last -lm
410          fi
411
412	  file ksh
413	  file shcomp
414
415	  export LD_LIBRARY_PATH="${root}/lib:${LD_LIBRARY_PATH}"
416	  export LD_LIBRARY_PATH_32="${root}/lib:${LD_LIBRARY_PATH_32}"
417	  export LD_LIBRARY_PATH_64="${root}/lib:${LD_LIBRARY_PATH_64}"
418	  ldd ksh
419        )
420    fi
421}
422
423function test_builtin_getconf
424{
425(
426    print "# testing getconf builtin..."
427    set +o errexit
428    export PATH=/bin:/usr/bin
429    for lang in ${TEST_LANG} ; do
430        (
431	    printf "## testing LANG=%s\n" "${lang}"
432            export LC_ALL="${lang}" LANG="${lang}"
433            ${SHELL} -c '/usr/bin/getconf -a |
434                         while read i ; do
435                         t="${i%:*}" ; a="$(getconf "$t" 2>/dev/null)" ;
436                         b="$(/usr/bin/getconf "$t" 2>/dev/null)" ; [ "$a" != "$b" ] && print "# |$t|:|$a| != |$b|" ;
437                         done'
438        )
439    done
440    print "# testing getconf done."
441)
442}
443
444function test_shell
445{
446    set -o errexit
447    set -o xtrace
448
449    export SHELL="$(ls -1 $PWD/arch/*/src/cmd/ksh93/ksh)"
450    export LD_LIBRARY_PATH="$(ls -1ad $PWD/arch/*/lib):${LD_LIBRARY_PATH}"
451    export LD_LIBRARY_PATH_32="$(ls -1ad $PWD/arch/*/lib):${LD_LIBRARY_PATH_32}"
452    export LD_LIBRARY_PATH_64="$(ls -1ad $PWD/arch/*/lib):${LD_LIBRARY_PATH_64}"
453    printf "## SHELL is |%s|\n" "${SHELL}"
454    printf "## LD_LIBRARY_PATH is |%s|\n" "${LD_LIBRARY_PATH}"
455
456    [[ ! -f "${SHELL}" ]] && fatal_error "test_shell: |${SHELL}| is not a file."
457    [[ ! -x "${SHELL}" ]] && fatal_error "test_shell: |${SHELL}| is not executable."
458
459    [[ "${TEST_LANG}" == "" ]] && TEST_LANG="C"
460
461    case "${buildmode}" in
462            testshell.bcheck*)
463                for lang in ${TEST_LANG} ; do
464                    (
465                        export LC_ALL="${lang}" LANG="${lang}"
466                        for i in ./src/cmd/ksh93/tests/*.sh ; do
467                            bc_logfile="$(basename "$i").$$.bcheck"
468                            rm -f "${bc_logfile}"
469                            /opt/SUNWspro/bin/bcheck -q -access -o "${bc_logfile}" ${SHELL} ./src/cmd/ksh93/tests/shtests \
470                                LD_LIBRARY_PATH_64="$LD_LIBRARY_PATH_64" \
471                                LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
472                                LD_LIBRARY_PATH_32="$LD_LIBRARY_PATH_32"\
473                                LC_ALL="${lang}" LANG="${lang}" \
474                                VMDEBUG=a \
475                                "$i"
476                            cat "${bc_logfile}"
477                        done
478                    )
479                done
480                ;;
481            testshell.builtin.getconf)
482                test_builtin_getconf
483                ;;
484            testshell)
485                for lang in ${TEST_LANG} ; do
486                    (
487                        export LC_ALL="${lang}" LANG="${lang}"
488                        for i in ./src/cmd/ksh93/tests/*.sh ; do
489                            ${SHELL} ./src/cmd/ksh93/tests/shtests -a \
490                                LD_LIBRARY_PATH_64="$LD_LIBRARY_PATH_64" \
491                                LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
492                                LD_LIBRARY_PATH_32="$LD_LIBRARY_PATH_32" \
493                                LC_ALL="${lang}" LANG="${lang}" \
494                                VMDEBUG=a \
495				SHCOMP=$PWD/arch/*/bin/shcomp \
496                                "$i"
497                        done
498                    )
499                done
500                test_builtin_getconf
501                ;;
502    esac
503}
504
505# main
506case "${buildmode}" in
507        build.*) build_shell ;;
508        testshell*)  test_shell  ;;
509        *) fatal_error "Illegal build mode \"${buildmode}\"." ;;
510esac
511# EOF.
512