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  if test -z "$BINUTILSPREFIX"
227  then
228    # need up to date samplecfg that chains cross compiler additions
229    SampleCfg=$DebianInstallDir/lib/fpc/$FPCVersion/samplecfg
230    grep 'fpc-cross.cfg' "$SampleCfg" &>/dev/null || \
231      sed -i -e "/^FPCPATH=/aFPCPARENT=\"\`dirname \"\$1\"\`\"
232;/^#ENDIF NEEDCROSSBINUTILS/i#include \$FPCPARENT/fpc-cross.cfg"  "$SampleCfg"
233  else cat > $DebianInstallDir/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX}.cfg <<CROSS
234# Detect $TARGET compiles
235#IF \$fpc-target = $TARGET
236 -XP$BINUTILSPREFIX
237#WRITE Target $TARGET with binutils prefix $BINUTILSPREFIX
238#END
239CROSS
240  fi
241  cd -
242
243  # remove non binaries in /usr/bin
244  for f in $DebianInstallDir/bin/*; do
245    if [ ! -x "$f" ]; then
246      rm $f
247    fi
248  done
249
250  # docs
251  if [ -d "$FPCManDir" ]; then
252    #
253    mkdir -p $FPCBuildDir/usr/share/man/man1
254    for man in $FPCManDir/man1/*.1; do
255      echo "copying man page $man"
256      shortman=$(basename $man)
257      cat $man | gzip -n --best > $FPCBuildDir/usr/share/man/man1/$shortman.gz
258    done
259  else
260    echo "WARNING: man directory not found: $FPCManDir"
261  fi
262fi
263
264#------------------------------------------------------------------------------
265# create rulez and files
266
267# change debian files
268DEPENDS="$BINUTILS"
269
270if test -n "$CROSSINSTALL"
271then
272  DEPENDS="$DEPENDS, fpc (= $FPCVersion)"
273fi
274
275# get installed size in kb
276DebSize=$(du -s $FPCBuildDir | cut -f1)
277
278# create debian control file, which contains the package description
279echo "creating DEBIAN/control file"
280cat $ResourceDir/control \
281  | sed -e "s/FPCVERSION/$FPCVersion/g" -e "s/ARCH/$Arch/g" \
282        -e "s/^Package: .*/Package: $PackageName$TARGET_SUFFIX/" \
283        -e "s/Depends: binutils/Depends: $DEPENDS/" \
284        -e "s/DEBSIZE/$DebSize/" \
285  > $DebianRulezDir/control
286mkdir -p $DebianLintianDir/overrides
287cp $ResourceDir/lintian.overrides $DebianLintianDir/overrides/$PackageName$TARGET_SUFFIX
288
289# identify conf files
290if test -n "$TARGET_SUFFIX"
291then
292echo "/usr/lib/fpc/$FPCVersion/fpc${TARGET_SUFFIX:--cross}.cfg" >> $DebianRulezDir/conffiles
293fi
294
295# create debian changelog file, needed for version
296echo "creating usr/share/doc/fpc/changelog file ..."
297File=$DebianDocDir/changelog
298echo "fpc ($FPCVersion-$FPCRelease) unstable; urgency=low" > $File
299echo '  * Unofficial snapshot build for lazarus' >> $File
300echo " -- Mattias Gaertner <mattias@freepascal.org>  $Date" >> $File
301echo "" >> $File
302cat $ResourceDir/changelog >> $File
303rm -f $File.gz
304gzip -n --best $File
305cp $File.gz $File.Debian.gz
306
307# create postinst if needed
308if [ -f "$ResourceDir/postinst" ]
309then
310  if [ -z "$CROSSINSTALL" ]
311  then
312    echo "creating DEBIAN/postinst file"
313    cat $ResourceDir/postinst \
314      | sed -e "s/FPCVERSION/$FPCVersion/g" -e "s/PPCBIN/$PPPRE$ppcbin/g" \
315      > $DebianRulezDir/postinst
316    cat >> $DebianRulezDir/postinst <<CFG
317#! /bin/sh
318set -e
319touch /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
320sed -i -e "/^#if 2.3.1 /{:eat;s/.*//;N;/#end/d;beat}" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
321cat >> /usr/lib/fpc/$FPCVersion/fpc-cross.cfg << FPCCFG
322#if $FPCVersion = \\\$fpcversion
323#include /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
324#end
325FPCCFG
326CFG
327    chmod a+rx $DebianRulezDir/postinst
328    # un-install
329    cat > $DebianRulezDir/prerm <<CROSS
330#! /bin/sh
331set -e
332rm -f /usr/lib/fpc/$FPCVersion/ppc$ppcbin
333# remove fpc-cross include lines
334if [ -f /usr/lib/fpc/$FPCVersion/fpc-cross.cfg ]; then
335  sed -i -e "/^#if 2.3.1 /{:eat;s/.*//;N;/#end/d;beat}" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
336fi
337CROSS
338    chmod a+rx $DebianRulezDir/prerm
339  else
340    # cross-compilerpostinst
341    cat > $DebianRulezDir/postinst <<CROSS
342#! /bin/sh
343set -e
344ln -sf /usr/lib/fpc/$FPCVersion/$PPPRE$ppcbin /usr/bin/ppc$ppcbin
345grep 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
346CROSS
347    chmod a+rx $DebianRulezDir/postinst
348    # un-install
349    cat > $DebianRulezDir/prerm <<CROSS
350#! /bin/sh
351set -e
352rm -f /usr/lib/fpc/$FPCVersion/$PPPRE$ppcbin
353if [ -f /usr/lib/fpc/$FPCVersion/fpc-cross.cfg ]; then
354  sed -i -e "/#include \/usr\/lib\/fpc\/$FPCVersion\/fpc${TARGET_SUFFIX}.cfg/d" /usr/lib/fpc/$FPCVersion/fpc-cross.cfg
355fi
356CROSS
357    chmod a+rx $DebianRulezDir/prerm
358  fi
359fi
360
361# create changelog.Debian file
362echo "creating changelog.Debian file ..."
363File=$DebianDocDir/changelog.Debian
364cp $ResourceDir/changelog.Debian $File
365rm -f $File.gz
366gzip -n --best $File
367
368# create debian copyright file
369echo "creating copyright file ..."
370cp $ResourceDir/copyright $DebianDocDir/
371
372
373#------------------------------------------------------------------------------
374# fixing permissions
375echo "fixing permissions ..."
376find $FPCBuildDir -type d -print0 | xargs -0 chmod 755  # this is needed, don't ask me why
377find $FPCBuildDir -type f -print0 | xargs -0 chmod a+r  # this is needed, don't ask me why
378find $FPCBuildDir -perm 775 | xargs -d '\n' chmod 755 || true
379find $FPCBuildDir -perm 664 | xargs -d '\n' chmod 644 || true
380
381#------------------------------------------------------------------------------
382# creating deb
383
384cd $TmpDir
385fakeroot dpkg-deb --build $FPCBuildDir
386mv $FPCBuildDir.deb $FPCDeb
387
388echo "The new deb can be found at $FPCDeb"
389echo "You can test it with lintian."
390
391# end.
392
393