1#!/bin/bash
2
3set -x
4set -e
5
6#------------------------------------------------------------------------------
7# parse parameters
8#------------------------------------------------------------------------------
9Usage="Usage: [OS_TARGET=xxx] [CPU_TARGET=xxx] [BINUTILSPREFIX=xxx] $0 fpc|fpc-src [notemp] <FPCSrcDir> [release]"
10
11TmpDir=$TEMP
12if [ -z "$TmpDir" ]; then
13  TmpDir=~/tmp
14fi
15TmpDir=$TmpDir/fpc_patchdir
16
17# what package should be built ...
18PackageName=""
19if [ "$1" = fpc ]; then
20    PackageName=fpc-laz
21fi
22if [ "$1" = fpc-src ]; then
23    PackageName=$1
24fi
25if [ "x$PackageName" = "x" ]; then
26  echo $Usage
27  exit -1
28fi
29shift
30
31WithTempDir=yes
32if [ "x$1" = "xnotemp" ]; then
33  WithTempDir=no
34  shift
35fi
36
37FPCSrcDir=$1
38if [ "x$FPCSrcDir" = "x" ]; then
39  echo $Usage
40  exit -1
41fi
42FPCSrcDir=$(echo $FPCSrcDir)
43shift
44
45FPCRelease=$1
46if [ "x$FPCRelease" = "x" ]; then
47  FPCRelease=$(date +%y%m%d)
48else
49  shift
50fi
51
52if [ ! -d $FPCSrcDir/compiler ]; then
53  echo "The directory $FPCSrcDir does not look like a fpc source directory (fpc/)"
54  exit -1
55fi
56
57
58#------------------------------------------------------------------------------
59# quick tests
60
61./check_fpc_dependencies.sh
62
63fakeroot -v
64
65getBINUTILSPREFIX() {
66  _IFS="$IFS"
67  IFS=":"
68  set $PATH
69  IFS="$_IFS"
70  for p in "$@"
71  do
72    set `echo $p/${TARGET_PREFIX}*as`
73    as="$1"
74    if test -x "$as"
75    then
76      TARGET_PREFIX="${as%%as}"
77      break
78    fi
79  done
80  if test -x "${TARGET_PREFIX}as"
81  then echo "${TARGET_PREFIX}"
82  fi
83}
84
85#------------------------------------------------------------------------------
86# retrieve the version information
87
88echo -n "getting FPC version from local svn ..."
89VersionFile="$FPCSrcDir/compiler/version.pas"
90CompilerVersion=$(cat $VersionFile | grep ' *version_nr *=.*;' | sed -e 's/[^0-9]//g')
91CompilerRelease=$(cat $VersionFile | grep ' *release_nr *=.*;' | sed -e 's/[^0-9]//g')
92CompilerPatch=$(cat $VersionFile | grep ' *patch_nr *=.*;' | sed -e 's/[^0-9]//g')
93CompilerVersionStr="$CompilerVersion.$CompilerRelease.$CompilerPatch"
94FPCVersion="$CompilerVersion.$CompilerRelease.$CompilerPatch"
95echo " $CompilerVersionStr-$FPCRelease"
96
97#------------------------------------------------------------------------------
98# architecture dependent stuff
99
100Arch=`dpkg --print-architecture`
101
102CPU_TARGET="${CPU_TARGET:-$Arch}"
103
104case "$CPU_TARGET" in
105  i386)    ppcbin=386;   FPCArch=i386;;
106  amd64)   ppcbin=x64;   FPCArch=x86_64;;
107  powerpc) ppcbin=ppc;   FPCArch=powerpc;;
108  sparc)   ppcbin=sparc; FPCArch=sparc;;
109  arm)     ppcbin=arm;   FPCArch=arm;;
110  *)    echo "$CPU_TARGET is not supported."
111        exit -1;;
112esac
113
114if [ "$CPU_TARGET" != "$Arch" ]
115then TARGET_SUFFIX="-${CPU_TARGET}"
116     TARGET_PREFIX="${CPU_TARGET}-"
117     CROSSINSTALL=1
118     PPPRE=ppcross
119else
120     PPPRE=ppc
121fi
122
123if test -n "$OS_TARGET"
124then
125    TARGET_SUFFIX="${TARGET_SUFFIX}-${OS_TARGET}"
126    TARGET_RPPEFIX="${TARGET_PREFIX}${OS_TARGET}-"
127    TARGET="${CPU_TARGET}-${OS_TARGET}"
128    CROSSINSTALL=1
129fi
130
131if test -z "$FPC"
132then
133    FPC="`fpc -P$FPCArch -PB`"
134fi
135
136BINUTILS=binutils
137# detect any finalprefix elements
138if test -n "$TARGET_PREFIX" -a -z "$BINUTILSPREFIX"
139then
140  BINUTILSPREFIX="`getBINUTILSPREFIX $BINUTILSPREFIX`"
141
142  if test -n "$BINUTILSPREFIX"
143  then echo "BINUTILSPREFIX=$BINUTILSPREFIX"
144     BINUTILS=`dpkg -S "${BINUTILSPREFIX}as" | sed "s/:.*//"`
145  else echo "Can't find cross binutils automatically, consider setting BINUTILSPREFIX"
146     exit 1
147  fi
148fi
149
150#------------------------------------------------------------------------------
151# download/export fpc svn if needed
152
153SrcTGZ=$(pwd)/fpc-$FPCVersion-$FPCRelease.tar.gz
154
155if [ ! -f $SrcTGZ ]; then
156  ./create_fpc_export_tgz.sh $FPCSrcDir $SrcTGZ
157fi
158
159# optional: svn/fpcbuild/trunk/install under ../install
160FPCManDir=$FPCSrcDir/../install/man
161
162#------------------------------------------------------------------------------
163# create a temporary copy of the fpc sources to patch it
164
165if [ "$WithTempDir" = "yes" ]; then
166  if [ -d $TmpDir ]; then
167    rm -rf $TmpDir
168  fi
169  mkdir -p $TmpDir
170
171  cd $TmpDir
172  echo "unpacking $SrcTGZ to "$(pwd)" ..."
173  tar xzf $SrcTGZ
174  cd -
175  FPCSrcDir=$TmpDir/fpc
176else
177  TmpDir=$FPCSrcDir
178fi
179
180#------------------------------------------------------------------------------
181# setup variables
182
183CurDir=`pwd`
184FPCBuildDir=$TmpDir/fpc_build
185FPCDeb=$CurDir/${PackageName}${TARGET_SUFFIX}_$FPCVersion-${FPCRelease}_$Arch.deb
186ResourceDir=$CurDir/debian_$PackageName
187DebianInstallDir=$FPCBuildDir/usr
188DebianRulezDir=$FPCBuildDir/DEBIAN/
189DebianDocDir=$FPCBuildDir/usr/share/doc/$PackageName${TARGET_SUFFIX}
190DebianLintianDir=$FPCBuildDir/usr/share/lintian
191DebianSourceDir=$FPCBuildDir/usr/share/fpcsrc/$FPCVersion
192Date=`date --rfc-822`
193
194#------------------------------------------------------------------------------
195# patch sources
196
197ReplaceScript=replace_in_files.pl
198
199# set version numbers in all Makefiles
200echo "set version numbers in all Makefiles ..."
201perl replace_in_files.pl -sR -f 'version=\d.\d.\d' -r version=$CompilerVersionStr -m 'Makefile(.fpc)?' $FPCSrcDir/*
202
203
204#------------------------------------------------------------------------------
205
206mkdir -p $DebianDocDir
207chmod 755 $DebianDocDir
208mkdir -p $DebianRulezDir
209chmod 755 $DebianRulezDir
210mkdir -p $DebianLintianDir
211chmod 755 $DebianLintianDir
212
213if [ "$PackageName" = "fpc-src" ]; then
214    # copy fpc sources
215    mkdir -p $DebianSourceDir
216    cp -a $FPCSrcDir/* $DebianSourceDir/
217fi
218
219if [ "$PackageName" = "fpc-laz" ]; then
220  # build fpc
221  mkdir -p $FPCBuildDir/etc
222  cd $FPCSrcDir
223  make clean all ${FPCArch:+FPCArch=$FPCArch} ${OS_TARGET:+OS_TARGET=$OS_TARGET} ${FPC:+FPC=$FPC} ${BINUTILSPREFIX:+BINUTILSPREFIX=$BINUTILSPREFIX} ${CROSSINSTALL:+CROSSINSTALL=$CROSSINSTALL}
224  mkdir -p $DebianInstallDir
225  make install INSTALL_PREFIX=$DebianInstallDir ${FPCArch:+FPCArch=$FPCArch} ${OS_TARGET:+OS_TARGET=$OS_TARGET} ${FPC:+FPC=$FPC} ${BINUTILSPREFIX:+BINUTILSPREFIX=$BINUTILSPREFIX} ${CROSSINSTALL:+CROSSINSTALL=$CROSSINSTALL}
226
227  # remove pas2jslib.so as debian require the version in its filename
228  rm -f $DebianInstallDir/lib/libpas2jslib.so
229
230  if test -z "$BINUTILSPREFIX"
231  then
232    # need up to date samplecfg that chains cross compiler additions
233    SampleCfg=$DebianInstallDir/lib/fpc/$FPCVersion/samplecfg
234    grep 'fpc-cross.cfg' "$SampleCfg" &>/dev/null || \
235      sed -i -e "/^FPCPATH=/aFPCPARENT=\"\`dirname \"\$1\"\`\"
236;/^#ENDIF NEEDCROSSBINUTILS/i#include \$FPCPARENT/fpc-cross.cfg"  "$SampleCfg"
237  else cat > $DebianInstallDir/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX}.cfg <<CROSS
238# Detect $TARGET compiles
239#IF \$fpc-target = $TARGET
240 -XP$BINUTILSPREFIX
241#WRITE Target $TARGET with binutils prefix $BINUTILSPREFIX
242#END
243CROSS
244  fi
245  cd -
246
247  # remove non binaries in /usr/bin
248  for f in $DebianInstallDir/bin/*; do
249    if [ ! -x "$f" ]; then
250      rm $f
251    fi
252  done
253
254  # docs
255  if [ -d "$FPCManDir" ]; then
256    #
257    mkdir -p $FPCBuildDir/usr/share/man/man1
258    for man in $FPCManDir/man1/*.1; do
259      echo "copying man page $man"
260      shortman=$(basename $man)
261      cat $man | gzip -n --best > $FPCBuildDir/usr/share/man/man1/$shortman.gz
262    done
263  else
264    echo "WARNING: man directory not found: $FPCManDir"
265  fi
266fi
267
268#------------------------------------------------------------------------------
269# create rulez and files
270
271# change debian files
272DEPENDS="$BINUTILS"
273
274if test -n "$CROSSINSTALL"
275then
276  DEPENDS="$DEPENDS, fpc (= $FPCVersion)"
277fi
278
279# get installed size in kb
280DebSize=$(du -s $FPCBuildDir | cut -f1)
281
282# create debian control file, which contains the package description
283echo "creating DEBIAN/control file"
284cat $ResourceDir/control \
285  | sed -e "s/FPCVERSION/$FPCVersion/g" -e "s/ARCH/$Arch/g" \
286        -e "s/^Package: .*/Package: $PackageName$TARGET_SUFFIX/" \
287        -e "s/Depends: binutils/Depends: $DEPENDS/" \
288        -e "s/DEBSIZE/$DebSize/" \
289  > $DebianRulezDir/control
290mkdir -p $DebianLintianDir/overrides
291cp $ResourceDir/lintian.overrides $DebianLintianDir/overrides/$PackageName$TARGET_SUFFIX
292
293# identify conf files
294if test -n "$TARGET_SUFFIX"
295then
296echo "/usr/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX:--cross}.cfg" >> $DebianRulezDir/conffiles
297fi
298
299# create debian changelog file, needed for version
300echo "creating usr/share/doc/fpc/changelog file ..."
301File=$DebianDocDir/changelog
302echo "fpc ($FPCVersion-$FPCRelease) unstable; urgency=low" > $File
303echo '  * Unofficial snapshot build for lazarus' >> $File
304echo " -- Mattias Gaertner <mattias@freepascal.org>  $Date" >> $File
305echo "" >> $File
306cat $ResourceDir/changelog >> $File
307rm -f $File.gz
308gzip -n --best $File
309cp $File.gz $File.Debian.gz
310
311# create postinst if needed
312if [ -f "$ResourceDir/postinst" ]
313then
314  if [ -z "$CROSSINSTALL" ]
315  then
316    echo "creating DEBIAN/postinst file"
317    cat $ResourceDir/postinst \
318      | sed -e "s/FPCVERSION/$FPCVersion/g" -e "s/PPCBIN/$PPPRE$ppcbin/g" \
319      > $DebianRulezDir/postinst
320    cat >> $DebianRulezDir/postinst <<CFG
321#! /bin/sh
322set -e
323touch /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
324sed -i -e "/^#if 2.3.1 /{:eat;s/.*//;N;/#end/d;beat}" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
325cat >> /usr/lib/fpc/$FPCVersion/fpc-cross.cfg << FPCCFG
326#if $FPCVersion = \\\$fpcversion
327#include /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
328#end
329FPCCFG
330CFG
331    chmod a+rx $DebianRulezDir/postinst
332    # un-install
333    cat > $DebianRulezDir/prerm <<CROSS
334#! /bin/sh
335set -e
336rm -f /usr/lib/fpc/$FPCVersion/ppc$ppcbin
337# remove fpc-cross include lines
338if [ -f /usr/lib/fpc/$FPCVersion/fpc-cross.cfg ]; then
339  sed -i -e "/^#if 2.3.1 /{:eat;s/.*//;N;/#end/d;beat}" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
340fi
341CROSS
342    chmod a+rx $DebianRulezDir/prerm
343  else
344    # cross-compilerpostinst
345    cat > $DebianRulezDir/postinst <<CROSS
346#! /bin/sh
347set -e
348ln -sf /usr/lib/fpc/$FPCVersion/$PPPRE$ppcbin /usr/bin/ppc$ppcbin
349grep 2>/dev/null '#include /usr/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX}.cfg' /usr/lib/fpc/$FPCVersion/fpc-cross.cfg || echo '#include /usr/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX}.cfg' >> /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
350CROSS
351    chmod a+rx $DebianRulezDir/postinst
352    # un-install
353    cat > $DebianRulezDir/prerm <<CROSS
354#! /bin/sh
355set -e
356rm -f /usr/lib/fpc/$FPCVersion/$PPPRE$ppcbin
357if [ -f /usr/lib/fpc/$FPCVersion/fpc-cross.cfg ]; then
358  sed -i -e "/#include \/usr\/lib\/fpc\/$FPCVersion\/fpc${TARGET_SUFFIX}.cfg/d" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
359fi
360CROSS
361    chmod a+rx $DebianRulezDir/prerm
362  fi
363fi
364
365# create changelog.Debian file
366echo "creating changelog.Debian file ..."
367File=$DebianDocDir/changelog.Debian
368cp $ResourceDir/changelog.Debian $File
369rm -f $File.gz
370gzip -n --best $File
371
372# create debian copyright file
373echo "creating copyright file ..."
374cp $ResourceDir/copyright $DebianDocDir/
375
376
377#------------------------------------------------------------------------------
378# fixing permissions
379echo "fixing permissions ..."
380find $FPCBuildDir -type d -print0 | xargs -0 chmod 755  # this is needed, don't ask me why
381find $FPCBuildDir -type f -print0 | xargs -0 chmod a+r  # this is needed, don't ask me why
382find $FPCBuildDir -perm 775 | xargs -d '\n' chmod 755 || true
383find $FPCBuildDir -perm 664 | xargs -d '\n' chmod 644 || true
384
385#------------------------------------------------------------------------------
386# creating deb
387
388cd $TmpDir
389fakeroot dpkg-deb --build $FPCBuildDir
390mv $FPCBuildDir.deb $FPCDeb
391
392echo "The new deb can be found at $FPCDeb"
393echo "You can test it with lintian."
394
395# end.
396
397