1#!/bin/sh
2#
3#       ZLIB compilation script for the OS/400.
4#
5#
6#       This is a shell script since make is not a standard component of OS/400.
7
8
9################################################################################
10#
11#                       Tunable configuration parameters.
12#
13################################################################################
14
15TARGETLIB='ZLIB'                # Target OS/400 program library
16STATBNDDIR='ZLIB_A'             # Static binding directory.
17DYNBNDDIR='ZLIB'                # Dynamic binding directory.
18SRVPGM="ZLIB"                   # Service program.
19IFSDIR='/zlib'                  # IFS support base directory.
20TGTCCSID='500'                  # Target CCSID of objects
21DEBUG='*NONE'                   # Debug level
22OPTIMIZE='40'                   # Optimisation level
23OUTPUT='*NONE'                  # Compilation output option.
24TGTRLS='V6R1M0'                 # Target OS release
25
26export TARGETLIB STATBNDDIR DYNBNDDIR SRVPGM IFSDIR
27export TGTCCSID DEBUG OPTIMIZE OUTPUT TGTRLS
28
29
30################################################################################
31#
32#                       OS/400 specific definitions.
33#
34################################################################################
35
36LIBIFSNAME="/QSYS.LIB/${TARGETLIB}.LIB"
37
38
39################################################################################
40#
41#                               Procedures.
42#
43################################################################################
44
45#       action_needed dest [src]
46#
47#       dest is an object to build
48#       if specified, src is an object on which dest depends.
49#
50#       exit 0 (succeeds) if some action has to be taken, else 1.
51
52action_needed()
53
54{
55        [ ! -e "${1}" ] && return 0
56        [ "${2}" ] || return 1
57        [ "${1}" -ot "${2}" ] && return 0
58        return 1
59}
60
61
62#       make_module module_name source_name [additional_definitions]
63#
64#       Compile source name into module if needed.
65#       As side effect, append the module name to variable MODULES.
66#       Set LINK to "YES" if the module has been compiled.
67
68make_module()
69
70{
71    MODULES="${MODULES} ${1}"
72    MODIFSNAME="${LIBIFSNAME}/${1}.MODULE"
73    CSRC="`basename \"${2}\"`"
74
75    if action_needed "${MODIFSNAME}" "${2}"
76    then    :
77    elif [ ! "`sed -e \"/<source name=\\\"${CSRC}\\\">/,/<\\\\/source>/!d\" \
78      -e '/<depend /!d'                                                 \
79      -e 's/.* name=\"\\([^\"]*\\)\".*/\\1/' < \"${TOPDIR}/treebuild.xml\" |
80        while read HDR
81        do      if action_needed \"${MODIFSNAME}\" \"${IFSDIR}/include/${HDR}\"
82                then    echo recompile
83                        break
84                fi
85        done`" ]
86    then    return 0
87    fi
88
89    CMD="CRTCMOD MODULE(${TARGETLIB}/${1}) SRCSTMF('${2}')"
90    CMD="${CMD} SYSIFCOPT(*IFS64IO) OPTION(*INCDIRFIRST)"
91    CMD="${CMD} LOCALETYPE(*LOCALE) FLAG(10)"
92    CMD="${CMD} INCDIR('${IFSDIR}/include' ${INCLUDES})"
93    CMD="${CMD} TGTCCSID(${TGTCCSID}) TGTRLS(${TGTRLS})"
94    CMD="${CMD} OUTPUT(${OUTPUT})"
95    CMD="${CMD} OPTIMIZE(${OPTIMIZE})"
96    CMD="${CMD} DBGVIEW(${DEBUG})"
97    system "${CMD}"
98    LINK=YES
99}
100
101
102#       Determine DB2 object name from IFS name.
103
104db2_name()
105
106{
107        basename "${1}"                                                 |
108        tr 'a-z-' 'A-Z_'                                                |
109        sed -e 's/\..*//'                                               \
110            -e 's/^\(.\).*\(.........\)$/\1\2/'
111}
112
113
114#       Force enumeration types to be the same size as integers.
115
116copy_hfile()
117
118{
119        sed -e '1i\
120#pragma enum(int)\
121' "${@}" -e '$a\
122#pragma enum(pop)\
123'
124}
125
126
127################################################################################
128#
129#                             Script initialization.
130#
131################################################################################
132
133SCRIPTDIR=`dirname "${0}"`
134
135case "${SCRIPTDIR}" in
136/*)     ;;
137*)      SCRIPTDIR="`pwd`/${SCRIPTDIR}"
138esac
139
140while true
141do      case "${SCRIPTDIR}" in
142        */.)    SCRIPTDIR="${SCRIPTDIR%/.}";;
143        *)      break;;
144        esac
145done
146
147#  The script directory is supposed to be in ${TOPDIR}/os400.
148
149TOPDIR=`dirname "${SCRIPTDIR}"`
150export SCRIPTDIR TOPDIR
151cd "${TOPDIR}"
152
153
154#  Extract the version from the master compilation XML file.
155
156VERSION=`sed -e '/^<package /!d'                                        \
157            -e 's/^.* version="\([0-9.]*\)".*$/\1/' -e 'q'              \
158            < treebuild.xml`
159export VERSION
160
161################################################################################
162
163
164#       Create the OS/400 library if it does not exist.
165
166if action_needed "${LIBIFSNAME}"
167then    CMD="CRTLIB LIB(${TARGETLIB}) TEXT('ZLIB: Data compression API')"
168        system "${CMD}"
169fi
170
171
172#       Create the DOCS source file if it does not exist.
173
174if action_needed "${LIBIFSNAME}/DOCS.FILE"
175then    CMD="CRTSRCPF FILE(${TARGETLIB}/DOCS) RCDLEN(112)"
176        CMD="${CMD} CCSID(${TGTCCSID}) TEXT('Documentation texts')"
177        system "${CMD}"
178fi
179
180#       Copy some documentation files if needed.
181
182for TEXT in "${TOPDIR}/ChangeLog" "${TOPDIR}/FAQ"                       \
183    "${TOPDIR}/README" "${SCRIPTDIR}/README400"
184do      MEMBER="${LIBIFSNAME}/DOCS.FILE/`db2_name \"${TEXT}\"`.MBR"
185
186        if action_needed "${MEMBER}" "${TEXT}"
187        then    CMD="CPY OBJ('${TEXT}') TOOBJ('${MEMBER}') TOCCSID(${TGTCCSID})"
188                CMD="${CMD} DTAFMT(*TEXT) REPLACE(*YES)"
189                system "${CMD}"
190        fi
191done
192
193
194#       Create the OS/400 source program file for the C header files.
195
196SRCPF="${LIBIFSNAME}/H.FILE"
197
198if action_needed "${SRCPF}"
199then    CMD="CRTSRCPF FILE(${TARGETLIB}/H) RCDLEN(112)"
200        CMD="${CMD} CCSID(${TGTCCSID}) TEXT('ZLIB: C/C++ header files')"
201        system "${CMD}"
202fi
203
204
205#       Create the IFS directory for the C header files.
206
207if action_needed "${IFSDIR}/include"
208then    mkdir -p "${IFSDIR}/include"
209fi
210
211#       Copy the header files to DB2 library. Link from IFS include directory.
212
213for HFILE in "${TOPDIR}/"*.h
214do      DEST="${SRCPF}/`db2_name \"${HFILE}\"`.MBR"
215
216        if action_needed "${DEST}" "${HFILE}"
217        then    copy_hfile < "${HFILE}" > tmphdrfile
218
219                #       Need to translate to target CCSID.
220
221                CMD="CPY OBJ('`pwd`/tmphdrfile') TOOBJ('${DEST}')"
222                CMD="${CMD} TOCCSID(${TGTCCSID}) DTAFMT(*TEXT) REPLACE(*YES)"
223                system "${CMD}"
224                # touch -r "${HFILE}" "${DEST}"
225                rm -f tmphdrfile
226        fi
227
228        IFSFILE="${IFSDIR}/include/`basename \"${HFILE}\"`"
229
230        if action_needed "${IFSFILE}" "${DEST}"
231        then    rm -f "${IFSFILE}"
232                ln -s "${DEST}" "${IFSFILE}"
233        fi
234done
235
236
237#       Install the ILE/RPG header file.
238
239
240HFILE="${SCRIPTDIR}/zlib.inc"
241DEST="${SRCPF}/ZLIB.INC.MBR"
242
243if action_needed "${DEST}" "${HFILE}"
244then    CMD="CPY OBJ('${HFILE}') TOOBJ('${DEST}')"
245        CMD="${CMD} TOCCSID(${TGTCCSID}) DTAFMT(*TEXT) REPLACE(*YES)"
246        system "${CMD}"
247        # touch -r "${HFILE}" "${DEST}"
248fi
249
250IFSFILE="${IFSDIR}/include/`basename \"${HFILE}\"`"
251
252if action_needed "${IFSFILE}" "${DEST}"
253then    rm -f "${IFSFILE}"
254        ln -s "${DEST}" "${IFSFILE}"
255fi
256
257
258#      Create and compile the identification source file.
259
260echo '#pragma comment(user, "ZLIB version '"${VERSION}"'")' > os400.c
261echo '#pragma comment(user, __DATE__)' >> os400.c
262echo '#pragma comment(user, __TIME__)' >> os400.c
263echo '#pragma comment(copyright, "Copyright (C) 1995-2017 Jean-Loup Gailly, Mark Adler. OS/400 version by P. Monnerat.")' >> os400.c
264make_module     OS400           os400.c
265LINK=                           # No need to rebuild service program yet.
266MODULES=
267
268
269#       Get source list.
270
271CSOURCES=`sed -e '/<source name="/!d'                                   \
272    -e 's/.* name="\([^"]*\)".*/\1/' < treebuild.xml`
273
274#       Compile the sources into modules.
275
276for SRC in ${CSOURCES}
277do      MODULE=`db2_name "${SRC}"`
278        make_module "${MODULE}" "${SRC}"
279done
280
281
282#       If needed, (re)create the static binding directory.
283
284if action_needed "${LIBIFSNAME}/${STATBNDDIR}.BNDDIR"
285then    LINK=YES
286fi
287
288if [ "${LINK}" ]
289then    rm -rf "${LIBIFSNAME}/${STATBNDDIR}.BNDDIR"
290        CMD="CRTBNDDIR BNDDIR(${TARGETLIB}/${STATBNDDIR})"
291        CMD="${CMD} TEXT('ZLIB static binding directory')"
292        system "${CMD}"
293
294        for MODULE in ${MODULES}
295        do      CMD="ADDBNDDIRE BNDDIR(${TARGETLIB}/${STATBNDDIR})"
296                CMD="${CMD} OBJ((${TARGETLIB}/${MODULE} *MODULE))"
297                system "${CMD}"
298        done
299fi
300
301
302#       The exportation file for service program creation must be in a DB2
303#               source file, so make sure it exists.
304
305if action_needed "${LIBIFSNAME}/TOOLS.FILE"
306then    CMD="CRTSRCPF FILE(${TARGETLIB}/TOOLS) RCDLEN(112)"
307        CMD="${CMD} CCSID(${TGTCCSID}) TEXT('ZLIB: build tools')"
308        system "${CMD}"
309fi
310
311
312DEST="${LIBIFSNAME}/TOOLS.FILE/BNDSRC.MBR"
313
314if action_needed "${SCRIPTDIR}/bndsrc" "${DEST}"
315then    CMD="CPY OBJ('${SCRIPTDIR}/bndsrc') TOOBJ('${DEST}')"
316        CMD="${CMD} TOCCSID(${TGTCCSID}) DTAFMT(*TEXT) REPLACE(*YES)"
317        system "${CMD}"
318        # touch -r "${SCRIPTDIR}/bndsrc" "${DEST}"
319        LINK=YES
320fi
321
322
323#       Build the service program if needed.
324
325if action_needed "${LIBIFSNAME}/${SRVPGM}.SRVPGM"
326then    LINK=YES
327fi
328
329if [ "${LINK}" ]
330then    CMD="CRTSRVPGM SRVPGM(${TARGETLIB}/${SRVPGM})"
331        CMD="${CMD} SRCFILE(${TARGETLIB}/TOOLS) SRCMBR(BNDSRC)"
332        CMD="${CMD} MODULE(${TARGETLIB}/OS400)"
333        CMD="${CMD} BNDDIR(${TARGETLIB}/${STATBNDDIR})"
334        CMD="${CMD} TEXT('ZLIB ${VERSION} dynamic library')"
335        CMD="${CMD} TGTRLS(${TGTRLS})"
336        system "${CMD}"
337        LINK=YES
338
339        #       Duplicate the service program for a versioned backup.
340
341        BACKUP=`echo "${SRVPGM}${VERSION}"                              |
342                sed -e 's/.*\(..........\)$/\1/' -e 's/\./_/g'`
343        BACKUP="`db2_name \"${BACKUP}\"`"
344        BKUPIFSNAME="${LIBIFSNAME}/${BACKUP}.SRVPGM"
345        rm -f "${BKUPIFSNAME}"
346        CMD="CRTDUPOBJ OBJ(${SRVPGM}) FROMLIB(${TARGETLIB})"
347        CMD="${CMD} OBJTYPE(*SRVPGM) NEWOBJ(${BACKUP})"
348        system "${CMD}"
349fi
350
351
352#       If needed, (re)create the dynamic binding directory.
353
354if action_needed "${LIBIFSNAME}/${DYNBNDDIR}.BNDDIR"
355then    LINK=YES
356fi
357
358if [ "${LINK}" ]
359then    rm -rf "${LIBIFSNAME}/${DYNBNDDIR}.BNDDIR"
360        CMD="CRTBNDDIR BNDDIR(${TARGETLIB}/${DYNBNDDIR})"
361        CMD="${CMD} TEXT('ZLIB dynamic binding directory')"
362        system "${CMD}"
363        CMD="ADDBNDDIRE BNDDIR(${TARGETLIB}/${DYNBNDDIR})"
364        CMD="${CMD} OBJ((*LIBL/${SRVPGM} *SRVPGM))"
365        system "${CMD}"
366fi
367