GNU Pascal Compiler

© 2009-2014 Kevan Hashemi, Brandeis Univiersity HEP Electronics Shop

Contents

Introduction
Installing Binaries
MacOSWindowsLinux
Compiling from Sources
Stage ZeroStage OneStage TwoStage Three
Notes
2014

Introduction

The GNU Pascal Compiler (GPC) is a member of the GNU Compiler Collection. Its back-end is compatible with the GNU C Compiler (GCC) and all the other members of the collection.

GPC Home Introduction to GPC and the GPC Manual. The links to binary distributions are out of date, as are some of the links to the source code. There are instructions for compiling sources on Unix-like platforms, and we used these ourselves.

There are two ways to install GPC. One is to download the source code for the compiler, compile it yourself with GCC, and install the binaries you create. Another is to download pre-compiled binaries. The former is more arduous but at the same time more reliable. The latter is easier, but the resulting installation may be incompatible with your system, especially if you are working on Linux.

Installing Binaries

We have been installing GPC on MacOS, Windows, and Linux machines since 2003.

MacOS

Binary distributions of GPC for MacOS are available here (Microbizz, Maintained by Adriaan van Os).

Windows

Binary distributions of GPC for Cygwin on Windows are available here (GNU-GPC, Maintained by The African Chief). These are binaries and sources ready to install within the 32-bit Cygwin environment on Windows.

Binary distributions of GPC for MinGW on Windows are available here (GNU-GPC, Maintained by The African Chief). These are binaries and sources ready to install within the 32-bit MinGW environment on Windows.

We provide the following binary distributions for Windows.

gpc-20070904-windows-7-32bit: Compiled from 20070904 GPC sources on a 32-bit Windows 7 machine. Install within a 32-bit MinGW environment. This archive includes gcc 3.4.6.

To install any of these binary distributions, download the archive and place it in the top directory. Extract with the following command.

sudo tar -zxvf gpc-20070904-slc-6.5-32bit.tar.gz

The extraction will install the Pascal compiler in usr/local, using usr/local/bin for the commands, usr/local/lib for libraries. Test the compiler first by entering gpc at the command line.

Linux

There is no universal binary distribution for Linux. Here are some we have compiled.

gpc-20070904-slc-3.0-32bit: Compiled from 20070904 GPC sources on 32-bit, Scientific Linux 3.0 using GCC 3.4.6. This archive includes gcc 3.4.6.

gpc-20070904-slc-6.5-32bit: Compiled from 20070904 GPC sources on 32-bit Scientific Linux 6.5 using GCC 3.4.6. This archive includes gcc 3.4.6.

gpc-20070904-slc-6.5-64bit: Compiled from 20070904 GPC sources on 64-bit Scientific Linux 6.5 using GCC 3.4.6. This archive includes gcc 3.4.6. All libraries built with -fPIC so they can be linked to shared libraries.

To install, download the archive and place it in the top directory. Extract with a command like this:

sudo tar -zxvf archivename.tar.gz

The extraction will install the Pascal compiler in usr/local, using usr/local/bin for the commands, usr/local/lib for libraries. Test the compiler first by entering gpc at the command line. If the command does not exist, fix your default path:

PATH=$PATH:/usr/local/bin

When you run gpc, you should get a page of text describing your installation. You can now test the compiler on some Pascal source code. Your gpc may report that it cannot find certain libraries. If that's the case, our binary distribution is incompatible with your Linux system. You may be able to install GPC directly from your Linux version's home page, using yum, rpm, or up2date. Otherwise, you will have to compile GPC yourself.

Here are some Debian packages compiled by Peter Blackman. We have not tried them, but they may be more convenient to install than our own.

gpc-3.4_20070904-102t: Compiled from 20070904 GPC sources for 32-bit Debian machines.

gpc-3.4_20070904-102t: Compiled from 20070904 GPC sources for 64-bit Debian machines.

Please let us know if you have problems with any of the above distributions.

Compiling from Sources

The GNU Pascal Compiler, GPC, is a program written in C. In theory, we could compile GPC with any C compiler, but in practice, we need a compiler compatible with the GPC back-end. Here we provide instructions for compiling GPC using GCC version 3.4.6. The entire process must, of course, be powered by a pre-existing C compiler, which we refer to as the local C compiler. So far as we know, any GCC C-compiler will do, but we may be wrong about that. Our first step in compiling GPC is to make sure we have a C-compiler installed on our machine.

cc --version

The above command should return the name and version of your local C compiler. We leave it to you to make sure that you have such a compiler installed. If the result is some GCC 3.x or 4.x compiler, you can be confident of success.

You will find generic instructions for compiling GPC source code here. We have not yet compiled GPC from sources on MacOS. But we have done so several times on Linux and Windows. In the following instructions, paragraphs that are specific to one platform begin with the name of the platform in bold. All other paragraphs apply to all platforms.

We build the GPC compiler in three stages. In Stage One, we invoke configure to generate a platform-specific makefile for Stage Two. In Stage Two, we use invoke make to compile the GCC_3.4.6 source code using the local C compiler, and to generate another makefile for Stage Three. In Stage Three, we invoke make to build a GCC_3.4.6 executable, compile the GPC sources with GCC_3.4.6, and install GPC in a directory of our choice. We start, however, with Stage Zero, which is preparing our system for the build.

Stage Zero

Windows: We install the developer version of MinGW, any distribution with version number 3.x, but not a 4.x version. We install the Msys shell program also. MinGW provides a 32-bit Unix-like environment in which to compile GPC. We can install MinGW on 32-bit Windows machines or 64-bit Windows machines. In both cases, it will create a 32-bit version of GPC, which will in turn create 32-bit executables and libraries. A 64-bit native build of GPC would require adapting GCC 3.4.6 to MinGW-64.

The make command that compiles GCC_3.4.6 and GPC uses many utilites. We make sure that we have all of the following utilities installed.

  1. bash
  2. bzip2
  3. GNU sed
  4. GNU awk
  5. GNU m4
  6. bison
  7. flex
  8. autoconf
  9. texinfo
  10. help2man

Linux: We used yum on Scientific Linux 6.5 to install bison, flex, autoconf, textinfo and help2man. The default packages worked fine.

Windows: All but help2man are installed with the developer version of MinGW. We install this utility with:

mingw-get install msys-help2man

Download our GPC installation archive gpc-20070904-build.tar.gz. Unpack the archive with the following command:

tar -zxf gpc-20070904-build.tar.gz

Suppose you have unpacked the archive in directory /x. This directory now contains the following directories and files.

build {directory for building GPC}
gcc-3.4.6 {directory containing GCC_3.4.6 source code}
gcc-3.4.6/gcc/p {directory containing GPC_20070904 source code}
gcc-3.4.4.diff {patch to GCC required by GPC}
gcc-3.4.5-20060117-1.diff {patch to GCC required by Windows/MinGW}
gpc-20070904-dialect-bug.diff {patch to GPC fixes minor bugs}
gpc-20070904-mingw-bugfix.diff {patch to GPC required by Windows/MinGW}
os-hacks.h {a header file for Windows/MinGW}

Windows: Add the os-hacks.h header file to the MinGW include directory. In /x, copy the header file with:

cp os-hacks.h /mingw/include/os-hacks.h

Windows: We received extensive instruction on how to compile GPC in MinGW from Dave Bryan. Our instructions are based upon his.

Windows: Patch the GCC_3.4.6 source code for MinGW. In /x, apply with:

patch -p1 -t -d gcc-3.4.6 -i ../gcc-3.4.5-20060117-1.diff

Patch the GCC_3.4.6 source code for GPC. In /x, apply with:

patch -p1 -t -d gcc-3.4.6/gcc -i ../gcc-3.4.4.diff

Patch GPC source code to fix a couple of minor bugs. In /x, apply with:

patch -p0 -t -d gcc-3.4.6/gcc -i ../gpc-20070904-dialect-bug.diff

Windows: Patch the GPC source code for MinGW. In /x , apply with:

patch -p1 -t -d gcc-3.4.6 -i ../gpc-20070904-mingw-bugfix.diff

The source code is now ready to compile.

Stage One

Go to our build directory, and invoke the GCC_3.4.6 configure script. We pass the script two options. The --enable-languages option enables support for pascal. The --disable-multilib option makes sure we don't try to build 32-bit and 64-bit versions of the compiler at the same time. This is necessary on 64-bit machines, and does no harm on 32-bit machines. In /x/build invoke the script with:

/x/gcc-3.4.6/configure --enable-languages=pascal --disable-multilib

You should see a makefile in the /x/build directory when the configure is done.

Stage Two

The makefile in x/build will invoke the local C compiler to compile the GCC_3.4.6 source code. Depending upon the platform, we will have to pass different options and flags to the build.

Linux 32-bit: In x/build invoke make with:

make

If the build fails with the following error, your bison is incompatible with GCC_3.4.6.

gcc: c-parse.c: No such file or directory
gcc: no input files

We find that bison 2.4.1 works. You may be able to fix this error by applying this patch to gcc-3.4.6/gcc/c-parse.in before you build, as described here.

Windows: For our compile to work in MinGW on Windows, we must tell the makefile where our system header files are and where our system libraries. On Windows7, we must specify that we want the local C compiler to use the MinGW standard string input-output routines, or else the %n format code, which is no longer supported in Window7, will generate gigabyte text files and cause the build to abort.

make MAKEFLAGS=-j1 C_INCLUDE_PATH=/mingw/include LIBRARY_PATH=/mingw/lib CFLAGS='-O2 -g -D__USE_MINGW_ANSI_STDIO' LDFLAGS=-s

When we build a 64-bit Pascal compiler, we must make sure that the Pascal run-time library, libgpc.a, is position-independent so that we can link it into 64-bit shared libraries. The 32-bit Pascal compiler does not need to build position-independent objects because on 32-bit platforms we can include position-dependent code in shared libraries. On a 64-bit platform, however, we must build with the -fPIC option, which forces the local C-compiler and GCC_3.4.6 to generate position-independent code. Similarly, when we compile Pascal code on a 64-bit platform, you pass the -fPIC option to GPC so as to force it to produce position-independent objects, which we can link to our position-independent libgpc.a to produce a shared library.

Linux 64-bit: In x/build invoke make with:

make CFLAGS='-g -O2 -fPIC'

At the end of the build, we should have many object files in /x/build/gcc along with a newly-created makefile, which we will use in Stage Three.

Stage Three

The makefile in x/build/gcc will create a GCC_3.4.6 executable, use this to compile the GPC sources, and install the GPC executable and libraries. We install GPC in /usr/local with the following command. In /x/build/gcc enter:

sudo make pascal.install-with-gcc

We can make our own binary distribution of GPC, including GCC, with with the bindist-with-gcc process.

make pascal.bindist-with-gcc

You can set this binary distribution aside and give it to other users of the same platform. They will be able to extract it directly into their root file structure and run the compiler.

With the compiler installed, you can invoke it with gpc on the command line. See the GNU Pascal Manual

Windows 32-bit: So long as you stay in the c-drive, you will be able to invoke GPC and link to its run-time libraries without specifying any library paths. But if you move to another drive, GPC will be unable to find its run-time libraries in the /mingw/lib path, because GPC does not map this path to the MinGW library folder on the c-drive. Set the LIBRARY_PATH environment variable as below to point GPC to the correct folder.

LIBRARY_PATH=/c/mingw/lib

Notes

2014

When installing and uninstalling various versions of GCC on MacOS, the behavior of the default gcc command changes after re-starting the OS. The behavior after re-starting always makes more sense than before re-starting.

We set out to remove from our MacOS 10.7.5 MacBook Pro all traces of previous Xcode, GCC, and GPC installations. We begin by running the uninstaller provided by Xcode.

sudo /Developer/Library/uninstall-devtools -mode=all

We run uninstall-cltools, a script we found here, which removes the files installed by Xcode4's command line tool installer. With rm -fr we delete /Developer/Pascal, all as directories and files in /usr, and all gcc folders and files except the following, which are present on all MacOS 10.7 and 10.8 machines.

/usr/share/file/magic/gcc
/usr/share/TargetConfigs/bin/gcc

We re-start and confirm that we have no gcc (GNU c-compiler executable), no as (GNU assembler executable), and no gpc (GNU pascal compiler executable). We download an Xcode3.2 installer and run it, asking it to install support for MacOS 10.4 in addition to 10.5 and 10.6. We copy the SDKs directory files somewhere else, then run the uninstall-devtools and uninstall-cltools script. We download an Xcode4.6 installer and copy the Xcode4 application bundle to our Applications folder. We extract from the bundle the MacOS 10.7 and 10.8 SDKs and put them with our other SDKs. We put the Xcode4.6 application bundle in the trash and delete it. We run the Xcode4.6 command-line tools installer. We install GPC from gpc346u4-10.6-Intel, which we downloaded from Microbizz. We re-start our OS.

We have a Hello-World program, "program p; begin writeln('Hello World'); end." to test our GPC installation. We get two linker warnings, one saying we have not specified a minimum OS version and another saying we cannot build position-independent code from objects that are position-dependent. These go away when we specify a minimum OS version 10.4, 10.5, or 10.6 because these operating systems assume position-dependent code. For OS 10.7 and 10.8 we must specify position-dependence of the objects because the default for these versions is position-independence.

Kevan-Laptop:Build kevan$ gpc p.pas
ld: warning: -macosx_version_min not specified, assuming 10.7
ld: warning: PIE disabled. Absolute addressing (perhaps 
-mdynamic-no-pic) not allowed
in code signed PIE, but used in __start from 
/Developer/SDKs/MacOSX10.5.sdk//usr/lib/crt1.o.
To fix this warning, don't compile with -mdynamic-no-pic or link with 
-Wl,-no_pie
Kevan-Laptop:Build kevan$ gpc p.pas -Wl,-macosx_version_min,10.4
Kevan-Laptop:Build kevan$ gpc p.pas -Wl,-macosx_version_min,10.5
Kevan-Laptop:Build kevan$ gpc p.pas -Wl,-macosx_version_min,10.6
Kevan-Laptop:Build kevan$ gpc p.pas -Wl,-macosx_version_min,10.7 -Wl,-no_pie
Kevan-Laptop:Build kevan$ gpc p.pas -Wl,-macosx_version_min,10.8 -Wl,-no_pie
Kevan-Laptop:Build kevan$ ./a.out
Hello World

We now attempt to link opbjects created by GPC, the GPC run-time library (in GPC installation), the MacOS SDK (in /Developer/SDKs), and a TclTk framework (in Frameworks) into a dynamic library (a.dylib).

Kevan-Laptop:Build kevan$ make
gcc -dynamiclib -o a.dylib utils.o -FFrameworks -framework Tcl 
-framework Tk \
	/Developer/Pascal/gpc346u4/lib/gcc/i386-apple-darwin10/3.4.6/libgpc.a \
	-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk/ -mmacosx-version-min=10.4
ld: warning: ignoring file utils.o, file was built for i386 which is not 
the architecture being linked (x86_64):

The linker called by gcc is expecting to see 64-bit code on this 64-bit x686 machine. We must force it to link a 32-bit dynamic library with the "-m32" architecture option.

Kevan-Laptop:Build kevan$ make
gcc -m32 -dynamiclib -o a.dylib utils.o -FFrameworks -framework Tcl 
-framework Tk \
	/Developer/Pascal/gpc346u4/lib/gcc/i386-apple-darwin10/3.4.6/libgpc.a \
	-Wl,-syslibroot,/Developer/SDKs/MacOSX10.4u.sdk/ 
-mmacosx-version-min=10.4

We attempt to cross-compile for PPC (power-pc) and OS 10.4, but the PPC assembler is not installed with the Xcode4 command-line tools. There is a way to transfer the PPC assembler from Xcode3 to Xcode4, but we are unable to get command-line cross-compiling to work with the gcc 4.2 linker. We abandon cross-compiling for PPC. We will compile PPC executables on our remaining PPC machines.