File: update_cross_fpc.sh

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (391 lines) | stat: -rwxr-xr-x 12,454 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env bash
# Author: Mattias Gaertner
# License: LGPL
# Abstract: Download, compile binutils and FPC

set -e
set -x

# This is the root for all download and building directories
if [ ! -d "$BuildRoot" ]; then
  BuildRoot=~/freepascal
fi

# the binutils version to download
BinutilsVersion=2.20
BinutilsDownloadPath=http://ftp.gnu.org/gnu/binutils

# the FPC targets
Targets="i386-win32";

# where to install the fpc+binutils executables
# Only used if called with 'install'. This directory must be in $PATH.
InstallBin=~/bin/

# what intel platform do you want/need to build for?
MyIntel=i686

#===============================================================================
# parse command line parameters
DownloadBinutils=no
DownloadFPC=no
BuildBinutils=no
BuildCrossFPC=no
BuildNormalFPC=no
CreateFPCCfg=no
InstallAsDefault=no   # install ~/fpc.cfg, binaries to ~/bin

Params=$@
for p in $Params; do
  case "$p" in
  all)
    DownloadBinutils=yes
    DownloadFPC=yes
    BuildBinutils=yes
    BuildCrossFPC=yes
    BuildNormalFPC=yes
    CreateFPCCfg=yes
    ;;
  downloadbinutils)
    DownloadBinutils=yes
    ;;
  downloadfpc)
    DownloadFPC=yes
    ;;
  buildbinutils)
    BuildBinutils=yes
    ;;
  buildnormalfpc)
    BuildNormalFPC=yes
    ;;
  buildcrossfpc)
    BuildCrossFPC=yes
    ;;
  createfpccfg)
    CreateFPCCfg=yes
    ;;
  install)
    InstallAsDefault=yes
    ;;
  installbin=*)
    InstallBin=$(echo $p | sed -e 's#^installbin=##')
    ;;
  buildroot=*)
    BuildRoot=$(echo $p | sed -e 's#^buildroot=##')
    ;;
  targets=*)
    Targets=$(echo $p | sed -e 's#^targets=##')
    ;;
  *)
    echo "Unknown option: $p"
    echo
    echo "Usage:"
    echo "  $0 [all] [downloadbinutils] [downloadfpc] [buildbinutils] [buildnormalfpc] [buildcrossfpc] [createfpccfg] [install] [installbin=<dir>] [targets=<i386-win32>] [buildroot=<dir default=~/freepascal/>]"
    exit -1
    ;;
  esac
done

#===============================================================================

# expand paths
BuildRoot=$(echo $BuildRoot | sed -e 's#//#/#g' -e 's#/$##')
InstallBin=$(echo $InstallBin | sed -e 's#//#/#g' -e 's#/$##')

# the 'bin' directory - where to put the fpc+binutils executables
# On InstallAsDefault=yes they will be copied to $HomeBin
BinDir=$BuildRoot/bin/

CompilerName="ppc386"

# check install path
if [ $InstallAsDefault = "yes" ]; then
  # check if the $InstallBin is in $PATH
  TrimmedPath=$(echo $InstallBin | sed -e 's#/$##g' -e 's#//#/#')
  if [ -z $(echo $PATH | egrep "(^|:)$TrimmedPath(:|$)") ]; then
    echo "ERROR: $InstallBin not in \$PATH."
    echo "This script installs FPC and binutils executables to a directory"
    echo "that is in your search PATH before your installed FPC."
    echo "Either prepend it to \$PATH or define another with installbin=<path>"
    exit -1
  fi
  # check if $InstallBin is in PATH before the system compiler
  mkdir -p $InstallBin
  NewCompilerName=$InstallBin/$CompilerName
  if [ ! -f $NewCompilerName ]; then
    touch $NewCompilerName
    chmod a+x $NewCompilerName
  fi
  hash -r
  FoundFPC=$(which $CompilerName)
  if [ ! $FoundFPC = $NewCompilerName ]; then
    echo "ERROR: $InstallBin not early enough in \$PATH."
    echo "This script installs FPC and binutils executables to a directory"
    echo "that is in your search PATH before your installed FPC."
    echo "Either prepend it to \$PATH or define another with installbin=<path>"
    exit -1
  fi

  BinDir=$InstallBin
fi

#===============================================================================
# calculate targets

TARGETS_WIN=""        # "cygwin mingw32 msdosdjgpp"
TARGETS_I386=""       # "freebsd netbsd openbsd linux solaris darwin"
TARGETS_POWERPC=""    # "freebsd netbsd openbsd linux darwin"
TARGETS_SPARC=""      # "freebsd netbsd openbsd linux solaris"
TARGETS_M68k=

for Target in $Targets; do
  if [ $Target = 'i386-win32' ]; then
    TARGETS_WIN="$TARGETS_WIN mingw32"
  else
    TargetCPU=$(echo $Target | sed -e 's#^\(.*\)-.*$#\1#')
    TargetOS=$(echo $Target | sed -e 's#^.*-\(.*\)$#\1#')
    if [ $TargetCPU = 'i386' ]; then
      TARGETS_I386="$TARGETS_I386 $TargetOS"
    fi
    if [ $TargetCPU = 'powerpc' ]; then
      TARGETS_I386="$TARGETS_POWERPC $TargetOS"
    fi
    if [ $TargetCPU = 'sparc' ]; then
      TARGETS_I386="$TARGETS_SPARC $TargetOS"
    fi
    if [ $TargetCPU = 'm86k' ]; then
      TARGETS_I386="$TARGETS_M68k $TargetOS"
    fi
  fi
done
TARGETS_WIN=$(echo $TARGETS_WIN | sed -e 's#^ +##g' -e 's# +$##g')
TARGETS_I386=$(echo $TARGETS_I386 | sed -e 's#^ +##g' -e 's# +$##g')
TARGETS_POWERPC=$(echo $TARGETS_POWERPC | sed -e 's#^ +##g' -e 's# +$##g')
TARGETS_SPARC=$(echo $TARGETS_SPARC | sed -e 's#^ +##g' -e 's# +$##g')
TARGETS_M68k=$(echo $TARGETS_M68k | sed -e 's#^ +##g' -e 's# +$##g')

#===============================================================================
# Download binutils

DownloadDir=$BuildRoot/download
BinutilsFilename=binutils-$BinutilsVersion.tar.gz
BinutilsTGZ=$DownloadDir/$BinutilsFilename
if [ $DownloadBinutils = "yes" ]; then
  if [ ! -f $BinutilsTGZ ]; then
    mkdir -p $DownloadDir
    cd $DownloadDir
    echo "Downloading $BinutilsDownloadPath/$BinutilsFilename ..."
    wget $BinutilsDownloadPath/$BinutilsFilename
  fi
fi

#===============================================================================
# Download FPC - update existing or download stable tree

if [ $DownloadFPC = "yes" ]; then
  cd $BuildRoot
  if [ -d fpc ]; then
    cd fpc
    echo "SVN update for FPC ..."
    svn cleanup
    svn up
    cd -
  else
    echo "SVN checkout for FPC 2.4 ..."
    svn co http://svn.freepascal.org/svn/fpc/branches/fixes_2_4 fpc
  fi
  if [ -d install ]; then
    cd install
    echo "SVN update for FPC install ..."
    svn cleanup
    svn up
    cd -
  else
    echo "SVN checkout for FPC 2.4 install ..."
    svn co http://svn.freepascal.org/svn/fpcbuild/branches/fixes_2_4/install install
  fi
fi

#===============================================================================
# setup some variables
# retrieve the version information
VersionFile="$BuildRoot/fpc/compiler/version.pas"
if [ ! -f $VersionFile ]; then
  echo No FPC sources found. To download them call ./update_cross_fpc.sh DownloadFPC
  exit
fi
CompilerVersion=`cat $VersionFile | grep ' *version_nr *=.*;' | sed -e 's/[^0-9]//g'`
CompilerRelease=`cat $VersionFile | grep ' *release_nr *=.*;' | sed -e 's/[^0-9]//g'`
CompilerPatch=`cat $VersionFile | grep ' *patch_nr *=.*;' | sed -e 's/[^0-9]//g'`
CompilerVersionStr="$CompilerVersion.$CompilerRelease.$CompilerPatch"

#===============================================================================
# build binutils
if [ $BuildBinutils = "yes" ]; then
  # check if cc is there
  cc -v
  # check if bison is there
  bison -V
  
  # create custom version of buildcrossbinutils
  echo "building cross binutils for targets:"
  echo "  WIN=$TARGETS_WIN"
  echo "  I386=$TARGETS_I386"
  echo "  POWERPC=$TARGETS_POWERPC"
  echo "  SPARC=$TARGETS_SPARC"
  echo "  M68K=$TARGETS_M68K"
  cd $BuildRoot/install/cross/
  cat buildcrossbinutils | \
    sed -e 's#^BASE=.*$#BASE='"$BuildRoot"'/binutils#' \
        -e 's#^BINUTILSPATH=.*$#BINUTILSPATH='"$DownloadDir"'/#' \
        -e 's#^BINUTILSBASE=.*$#BINUTILSBASE=binutils#' \
        -e 's#^BINUTILSVERSION=.*$#BINUTILSVERSION='"$BinutilsVersion"'#' \
        -e 's#^BINUTILS_GZIP=.*$#BINUTILS_GZIP=yes#' \
        -e 's#^\(TARGETS_.*=\).*$#\1#' \
        -e 's#^TARGETS_WIN=.*$#TARGETS_WIN='"$TARGETS_WIN"'#' \
        -e 's#^TARGETS_I386=.*$#TARGETS_I386='"$TARGETS_I386"'#' \
        -e 's#^TARGETS_POWERPC=.*$#TARGETS_POWERPC='"$TARGETS_POWERPC"'#' \
        -e 's#^TARGETS_SPARC=.*$#TARGETS_SPARC='"$TARGETS_SPARC"'#' \
        -e 's#^TARGETS_M68k=.*$#TARGETS_M68k='"$TARGETS_M68k"'#' \
        -e 's#^rm ./config.cache$##' \
    > buildcrossbinutils.sh
  #debugging: head -n 50 buildcrossbinutils.sh

  # build the cross binutils
  echo "HINT: when something goes wrong see the log files in $BuildRoot/binutils/logs/"
  rm -rf $BuildRoot/binutils
  Command=$BuildRoot/install/cross/buildcrossbinutils.sh
  echo "calling $Command ..."
  Result=$(sh buildcrossbinutils.sh)
  FailResult=$(echo "$Result" | grep -i failes || true)
  if [ -n "$FailResult" ]; then
    echo $Result
    echo "HINT: see the log files in $BuildRoot/binutils/logs/"
    exit -1
  fi

  # link binaries into the bin directory, so that FPC can find them
  mkdir -p $BinDir
  for Target in $Targets; do
    TargetCPU=$(echo $Target | sed -e 's#^\(.*\)-.*$#\1#')
    TargetOS=$(echo $Target | sed -e 's#^.*-\(.*\)$#\1#')
    BinUtilsCPU=$TargetCPU
    BinUtilsOS=$TargetOS

    if [ $TargetOS = "win32" ]; then
      BinUtilsOS="mingw32"
      BinUtilsCPU=$MyIntel
    fi
    
    BinUtilsDir=$BuildRoot/binutils/cross/bin/
    BinUtilsPrefix="$BinUtilsCPU-$BinUtilsOS-"

    cd ${BinUtilsDir}
    for binutility in $(ls -B ${BinUtilsPrefix}*); do
      link=$(echo $binutility | sed -e "s#$BinUtilsPrefix#${TargetCPU}-${TargetOS}-#")
      ln -sf ${BinUtilsDir}${binutility} ${BinDir}${link}
    done
  done
fi

#===============================================================================
# build cross FPC
if [ $BuildCrossFPC = "yes" ]; then
  echo "building cross FPC ..."
  for Target in $Targets; do
    TargetCPU=$(echo $Target | sed -e 's#^\(.*\)-.*$#\1#')
    TargetOS=$(echo $Target | sed -e 's#^.*-\(.*\)$#\1#')

    # create custom version of buildcrosssnapshot
    cd $BuildRoot/install/cross/
    cat buildcrosssnaphot | \
      sed -e 's#^CROSSTOOLSROOT=.*$#CROSSTOOLSROOT='"$BuildRoot"'/binutils/cross/#' \
          -e 's#^FPCSVN=.*$#FPCSVN='"$BuildRoot"'/fpc#' \
          -e 's#^FPCCVS=.*$#FPCCVS='"$BuildRoot"'/fpc#' \
          -e 's#^TARGETS_OS=.*$#TARGETS_OS='"$TargetOS"'#' \
          -e 's#^TARGETS_CPU=.*$#TARGETS_CPU='"$TargetCPU"'#' \
          -e 's#^MOS=cygwin$#MOS=mingw32#' \
          -e 's/ -n ${FAILURES} / -n "${FAILURES}" /' \
      > buildcrosssnapshot.sh
    #debugging: head -n 40 buildcrosssnapshot.sh

    # build the cross fpc
    echo "building cross fpc for target $Target in $BuildRoot/binutils/cross/destination/"
    echo "see logs in $BuildRoot/binutils/cross/logs/"
    sh buildcrosssnapshot.sh
  done
fi

#===============================================================================
# build normal FPC
# compile the non cross FPC into the same structure, so that the fpc.cfg
# can be very simple.
if [ $BuildNormalFPC = "yes" ]; then
  echo "building normal FPC ..."
  CPU=i386  # `uname -p | tr "[:upper:]" "[:lower:]"`
  OS=`uname -s | tr "[:upper:]" "[:lower:]"`
  echo HOST platform is ${CPU}-${OS}

  MAKE=make

  case "$OS" in
   *bsd*) MAKE=gmake
    ;;
  esac

  CROSSTOOLSROOT=$BuildRoot/binutils/cross/
  DESTDIR=${CROSSTOOLSROOT}destination/
  LOGDIR=${CROSSTOOLSROOT}logs/
  BASELIBDIR=${CROSSTOOLSROOT}crosslibs

  mkdir -p ${DESTDIR}
  mkdir -p ${LOGDIR}

  cd $BuildRoot/fpc
  echo "building non cross fpc for target $CPU-$OS in $DESTDIR"
  echo "see logs in $LOGDIR"
  ${MAKE} clean all > ${LOGDIR}snapbuild-${CPU}-${OS} 2>&1
  ${MAKE} install INSTALL_PREFIX=${DESTDIR} LIBDIR=${BASELIBDIR}/${CPU}-${OS} OPT="-Xd -Xt -gl" > ${LOGDIR}snapinstalllog-${CPU}-${OS} 2>&1
  
  # save samplecfg, ppc386
  mkdir -p $BinDir
  cp ${DESTDIR}lib/fpc/$CompilerVersionStr/samplecfg $BinDir
  cp ${DESTDIR}lib/fpc/$CompilerVersionStr/ppc* $BinDir
fi

#===============================================================================
# build ~/.fpc.cfg
if [ $CreateFPCCfg = "yes" ]; then
  echo "building ~/fpc.cfg ..."

  DestDir=~/freepascal
  $BuildRoot/bin/samplecfg $BuildRoot/binutils/cross/destination/lib/fpc/$CompilerVersionStr/ $DestDir
  FPCCfg=$DestDir/fpc.cfg
  
  # add -FD and -XP entry for cross compiling
  echo "# set binutils paths for crosscompiling" >> $FPCCfg
  echo "#IFDEF FPC_CROSSCOMPILING" >> $FPCCfg
  echo "  -FD${BinDir}" >> $FPCCfg
  echo '  -XP$fpctarget-' >> $FPCCfg
  echo "#ENDIF" >> $FPCCfg
  
  # test the fpc.cfg
  echo "Testing compiling test.pas ..."
  TestPas="$BuildRoot/test.pas"
  echo "program Test;" > $TestPas
  echo "{$mode objfpc}{$H+}" >> $TestPas
  echo "uses Classes, SysUtils;" >> $TestPas
  echo "begin" >> $TestPas
  echo "  writeln('Test');" >> $TestPas
  echo "end." >> $TestPas
  cd $BuildRoot
  $BinDir/$CompilerName test.pas

  if [ $InstallAsDefault = "yes" ]; then
    echo "Installing ~/fpc.cfg ..."
    cp $BuildRoot/fpc.cfg ~/.fpc.cfg
  fi
fi

# end.