You are here

Build scripts for installing FreePascal and Lazarus from source

Submitted by devblog on Sun, 03/10/2010 - 08:55

07/10/2010 - After some rereading and some observations made in the mailing list, I have made some corrections, with notes, and added some notes to help those new to FreePascal (like myself) and those new to some of the Linux tools.

After some struggles to install Lazarus and FreePascal from source I created some scripts to set them up properly and they are working well. I think I am 90% there, enough to get me focused on programming than installing.

It is all based on Frank Church's number 1 principle of programming: "Never use a constant or a literal where a variable will do!".

The FreePascal version is not as clean and tidy as it should be, and contains a few bugs.

A point I am concerned with is how the newly installed compiler is used to recompile itself, I am not sure if that is the cleanest approach.

Any tips for improvement are welcome, with fpc optimizations and with bash.

It is setup on Ubuntu 8.04 LTS and should work on even older distrubtions if any dependent libraries are updated.
Conversion to Win32 shouldn't be that hard.

The Show more code link is used to expand the source.

FreePascal installation script.

#!/bin/sh
#set -x 
### Lazarus Build on lzubuntu01 20100925
### command history with variable replacement
 
FPCVER=2.5.1
FPC_VERSION_STRING=251
FPC_REVISION=16073
PPCNAME=ppc386
FPC_BUILD_NAME=fpc_${FPC_VERSION_STRING}_${FPC_REVISION} 
 
HOMEDIR=${HOME}
FPC_BUILD_HOME=${HOMEDIR}/LazarusBuilds
FPC_BUILD_DIR=${FPC_BUILD_HOME}/${FPC_BUILD_NAME} #where files to be installed will be downloaded
FPC_INSTALL_DIR=${HOMEDIR}/.programs/fpc/${FPCVER}/${FPC_BUILD_NAME} #where fpc will be installed
FPC_SOURCE_INSTALL_DIR=${HOMEDIR}/.programs/fpcsrc/${FPCVER}/${FPC_BUILD_NAME} #where fpc sources will be installed 
 
# SYSTEM_FPC_COMPILER - this is initially /usr/bin/fpc, from repository (FPC 2.2.4)
 
# the first iteration was used to compile fpc 2.4.0 fixes, but I didn't want to
#replace it, so I just changed to the build in my user account to move up to 2.5.1
 
SYSTEM_FPC_COMPILER=/home/username/.programs/fpc_240_fixes/lib/fpc/2.4.0/ppc386
 
FPC_REPOSITORY=http://svn.freepascal.org/svn/fpc/trunk/ #to download from svn repo
 
# FPC_DOWNLOAD_URL
 
#FPC_DOWNLOAD_URL=http://svn.freepascal.org/svn/fpc/trunk/
 
# the above line is wrong, it should be a website where you have stored
# a zipped up download of the revision, I think bzip does a better job of compression
# but I am yet to learn it. The zip is stored as
 
# fpc_${FPC_VERSION_STRING}_${FPC_REVISION}.tar.gz = ${FPC_BUILD_NAME}.tar.gz
 
# originally from an $FPC_BUILD_NAME directory, so extracting it the FPC_BUILD_HOME
# automatically creates the tip of the FPC_BUILD_DIR
#correction below
 
FPC_DOWNLOAD_URL=http://lotsofspace.dreamhost.com/fpclazarchives/
 
# start of script execution
 
mkdir -p ${FPC_BUILD_DIR}
 
mkdir -p ${FPC_SOURCE_INSTALL_DIR}
 
mkdir -p ${FPC_INSTALL_DIR}
 
mkdir -p ${FPC_INSTALL_DIR}_backup # probably superfluous
 
 
<<comment
 
 skipped for time consuming aspect, should be unzipped into place already
 either svn or download archive and unzip
 svn command can be run to see if files are present
 download from repository or other location if not present
 
 comment around download commands below removed for clarity
 use wget or svn but not both
 
comment
 
 cd ${FPC_BUILD_HOME} 
 
 # download from svn or tar.gz file on some web page
 
 # correction made here, execute svn checkout in ${FPC_BUILD_HOME}
 # to create tip of FPC_BUILD_DIR
 # svn checkout -r ${FPC_REVISION} ${FPC_REPOSITORY}
 
 svn checkout -r ${FPC_REVISION} ${FPC_REPOSITORY} ${FPC_BUILD_NAME}
 
  #  or
 
 # similar to above
 # untar ${FPC_BUILD_NAME}.tar.gz in ${FPC_BUILD_HOME}
 # to create tip of FPC_BUILD_DIR
 
 wget ${FPC_DOWNLOAD_URL}/${FPC_BUILD_NAME}.tar.gz
 tar zxf ${FPC_BUILD_NAME}.tar.gz
 
 cd ${FPC_BUILD_NAME}
 #following command does the same with non relative path
 
 #compile from build directory, source files should be in place
 
cd ${FPC_BUILD_DIR}
 
# notes of fpc.cfg
# each build of fpc needs its own fpc.cfg file, which has to be stored as
# .fpc.cfg in the $HOME folder. I believe it can't be passed as an option
# to the make command, so if you are using different compilers then you
# have to copy it your home folder as .fpc.cfg . It is somewhat limiting
# because you probably will not be able to have 2 compiler versions running
# simultaneously under a single use account. FPC experts correct me here if
# I am wrong.
 
# This script is configured to create it in a /settings directory
# in the $FPC_INSTALL_DIR after installation, hence the strange command below
# to copy it into $HOME
 
# the proper command to create is fpcmkcfg, but I used samplecfg because it is
# what I came across and learned first.
 
# you will see it created a few times for the new compiler to rebuild itself
# To be absolutely correct I should have restored the fpc.cfg for the SYSTEM_FPC_COMPILER
# but as I am switching to fpc 2.5.1 I didn't bother
 
# put fpc.cfg for system compiler.
cp `dirname $SYSTEM_FPC_COMPILER`/../../../settings/fpc.cfg ${HOME}/.fpc.cfg
make all PP=${SYSTEM_FPC_COMPILER}
 
#ensure target directory is cleaned out
rm -rf ${FPC_INSTALL_DIR}/*
 
#install ensuring that directory prefix is correct by using newly built compiler
cd ${FPC_BUILD_DIR}
make install PP=${FPC_BUILD_DIR}/compiler/${PPCNAME} INSTALL_PREFIX=${FPC_INSTALL_DIR}
 
#generate fpc.cfg for new compiler copy to .fpc.cfg for use by new compiler
mkdir -p ${FPC_INSTALL_DIR}/settings
${FPC_INSTALL_DIR}/lib/fpc/${FPCVER}/samplecfg ${FPC_INSTALL_DIR}/lib/fpc/${FPCVER} ${FPC_INSTALL_DIR}/settings
cp ${FPC_INSTALL_DIR}/settings/fpc.cfg ${HOMEDIR}/.fpc.cfg
 
#make again, using new build, rather than using SYSTEM_FPC_COMPILER
make all PP=${FPC_INSTALL_DIR}/lib/fpc/${FPCVER}/${PPCNAME}
 
# some the commands relating to backing up the new build before compiling and
# reinstalling with itself are superfluous, but after some bad experiences
# I felt you can't be too defensive with Lazarus, especially on slow systems
 
# move current install out of the way
mv ${FPC_INSTALL_DIR} ${FPC_INSTALL_DIR}_backup
# mv ${FPC_INSTALL_DIR}/lib/fpc/${FPCVER} ${FPC_INSTALL_DIR}/lib/fpc/${FPCVER}_backup
 
#install using current compiler
make install PP=${FPC_BUILD_DIR}/compiler/${PPCNAME} INSTALL_PREFIX=${FPC_INSTALL_DIR}
 
# regenerate fpc.cfg for new compiler, copy to .fpc.cfg for use by new compiler
mkdir -p ${FPC_INSTALL_DIR}/settings
${FPC_INSTALL_DIR}/lib/fpc/${FPCVER}/samplecfg ${FPC_INSTALL_DIR}/lib/fpc/${FPCVER} ${FPC_INSTALL_DIR}/settings
cp ${FPC_INSTALL_DIR}/settings/fpc.cfg ${HOMEDIR}/.fpc.cfg
 
# clean out build dir for sources
cd ${FPC_BUILD_DIR}
make clean #
 
#move contents to sources directory
mv ${FPC_BUILD_DIR}/* ${FPC_SOURCE_INSTALL_DIR}/.
 
 
echo "If you use this compiler in a Lazarus installation, copy this line "
echo "to the Compiler path (e.g. fpc) in Environment | IDE Options dialog."
echo ${FPC_INSTALL_DIR}/lib/fpc/${FPCVER}/${PPCNAME}
 
echo "If you use this compiler in a Lazarus installation, copy this line "
echo "to the FPC source directory in Environment | IDE Options dialog."
echo ${FPC_SOURCE_INSTALL_DIR}
echo ""
echo ""
echo ""
echo "Build Complete"
echo ""
echo ""
echo ""

You will find the odd literal or constant in the code, but it is only to help those new to Lazarus on *nix with their bearings.

As a side note I prefer to create programming tools in a user account. I'd rather symlink from the system files to the build in a user account, rather than mess with the system originals.
It is a hassle more often than not.

Bash script for compiling Lazarus from source

#!/bin/sh
# set -x 
 
### to do list
###	1.	add support for generating install location related values which will be required
### 	by the Lazarus IDE configuration
 
### 2.	make a list of them and either create and save them in some location or
###		display them at the end of the compilation
###		eg. SYSTEM_FPC_COMPILER is important for .fpc.cfg and fpc sources in Lazarus IDE
 
 
SYSTEM_FPC_COMPILER=/home/username/Programs/fpc/2.5.1/fpc_251_16073/lib/fpc/2.5.1/ppc386
 
LAZARUS_VERSION=0.9.29
LAZARUS_VERSION_STRING=0929
LAZARUS_REVISION=27548
LAZARUS_BUILD_NAME=lazarus_${LAZARUS_VERSION_STRING}_${LAZARUS_REVISION}
 
HOMEDIR=${HOME} 
LAZARUS_BUILD_HOME=${HOMEDIR}/LazarusBuilds
LAZARUS_BUILD_DIR=${LAZARUS_BUILD_HOME}/${LAZARUS_BUILD_NAME} #location of source
LAZARUS_INSTALL_DIR=${HOMEDIR}/.programs/lazarus/${LAZARUS_VERSION}/${LAZARUS_BUILD_NAME} #where lazarus will be installed
LAZARUS_SETTINGS_DIR=${LAZARUS_INSTALL_DIR}/settings
LAZARUS_SETTINGS=${LAZARUS_SETTINGS_DIR}/settings.txt
LAZARUS_RUN=${LAZARUS_SETTINGS_DIR}/run_${LAZARUS_BUILD_NAME}
 
LAZARUS_REPOSITORY=http://svn.freepascal.org/svn/lazarus/trunk
 
mkdir -p ${LAZARUS_BUILD_DIR}
mkdir -p ${LAZARUS_INSTALL_DIR}
 
 
#skipped for time consuming aspect, shoud be unzipped into place already
# either svn or download archive and unzip
# svn command can be run to see if files are present
# download from repository or other location if not present
#
# cd ${LAZARUS_BUILD_HOME} 
#
# svn checkout -r ${LAZARUS_REVISION} ${LAZARUS_REPOSITORY} ${LAZARUS_BUILD_NAME}
# cd ${LAZARUS_BUILD_NAME}
#
#  or
#
# cd ${LAZARUS_BUILD_HOME} 
#
# wget http://download_url/${LAZARUS_BUILD_NAME}.tar.gz
# tar zxf ${LAZARUS_BUILD_NAME}.tar.gz
 
#compile from build directory, source code should be in place
 
cd ${LAZARUS_BUILD_DIR}
make all PP=${SYSTEM_FPC_COMPILER}
 
#move current install out of the way, if it exists
mv ${LAZARUS_INSTALL_DIR} ${LAZARUS_INSTALL_DIR}_backup/.
 
#install
cd ${LAZARUS_BUILD_DIR}
 
make install INSTALL_PREFIX=${LAZARUS_INSTALL_DIR}
 
make clean
 
mkdir -p ${LAZARUS_SETTINGS_DIR}
 
touch ${LAZARUS_SETTINGS}
touch ${LAZARUS_RUN} 
 
echo "When you run this Lazarus build, copy the following line " >>  ${LAZARUS_SETTINGS}
echo "to the Lazarus directory (default for all projects) in Environment | IDE Options dialog." >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
echo "${LAZARUS_INSTALL_DIR}/share/lazarus"  >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
 
echo "When you run this Lazarus build, copy the following line" >>  ${LAZARUS_SETTINGS}
echo "to the Compiler path (e.g. fpc) in Environment | IDE Options dialog." >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
echo "${SYSTEM_FPC_COMPILER}" >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
 
echo "If you use this compiler in a Lazarus installation, copy this line " >>  ${LAZARUS_SETTINGS}
echo "to the FPC source directory in Environment | IDE Options dialog." >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
echo "${FPC_SOURCE_INSTALL_DIR}"  >>  ${LAZARUS_SETTINGS}
echo "" >>  ${LAZARUS_SETTINGS}
 
touch ${LAZARUS_RUN}
 
echo "#!/bin/sh" >> ${LAZARUS_RUN}
echo "${LAZARUS_INSTALL_DIR}/bin/lazarus-ide --primary-config-path=~/.lazarusConfigs/${LAZARUS_BUILD_NAME}" >> ${LAZARUS_RUN}
 
chmod u+x ${LAZARUS_RUN}
 
echo ""
echo ""
echo ""
echo ""
echo "Build Complete"
echo ""
echo ""
echo ""
echo ""

A nice touch would be to add a link to ${LAZARUS_RUN} to the destkop menu

PS. I learnt something new about bash today, the use of HEREDOC style multiline comments http://www.cyberciti.biz/faq/bash-comment-out-multiple-line-code/.

It will be useful for cleanly commenting out the successful parts of the build, rather than the messy single line comments.

Sysadmin Docs: