\input texinfo
@c %**start of header
@setfilename R-admin.info
@settitle R Installation and Administration
@setchapternewpage on
@defcodeindex en
@c %**end of header

@syncodeindex fn vr

@dircategory Programming
@direntry
* R Administration: (R-admin).      R Installation and Administration.
@end direntry

@finalout

@include R-defs.texi
@include version.texi

@ifinfo
This is a guide to installation and administration for R.

@Rcopyright{2001}

@ignore
Permission is granted to process this file through TeX and print the
results, provided the printed document carries a copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

@end ignore

@permission{}
@c ---------- ^- read that
@end ifinfo

@titlepage
@title R Installation and Administration
@subtitle Version @value{VERSION}
@author R Development Core Team
@page
@vskip 0pt plus 1filll
@permission{}

@Rcopyright{2001}

@value{ISBN-admin}
@end titlepage

@c @ifnothtml
@contents
@c @end ifnothtml

@ifnottex
@node Top, Obtaining R, (dir), (dir)
@top R Installation and Administration

This is a guide to installation and administration for R.

The current version of this document is @value{VERSION}.

@value{ISBN-admin}
@end ifnottex

@menu
* Obtaining R::                 
* Installing R under Unix-alikes::  
* Installing R under Windows::  
* Installing R under Mac OS X::  
* Running R::                   
* Add-on packages::             
* Internationalization::        
* Choosing between 32- and 64-bit builds::  
* The standalone Rmath library::  
* Essential and useful other programs under Unix::  
* Configuration on Unix::       
* Platform notes::              
* The Windows toolset::         
* Function and variable index::  
* Concept index::               
* Environment variable index::  
@end menu

@node Obtaining R, Installing R under Unix-alikes, Top, Top
@chapter Obtaining R
@cindex Obtaining R

Sources, binaries and documentation for @R{} can be obtained via
@acronym{CRAN}, the ``Comprehensive R Archive Network'' whose current
members are listed at @uref{http://cran.r-project.org/@/mirrors.html}.

@menu
* Getting and unpacking the sources::  
* Getting patched and development versions::  
@end menu

@node Getting and unpacking the sources, Getting patched and development versions, Obtaining R, Obtaining R
@section Getting and unpacking the sources
@cindex Sources for R

The simplest way is to download the most recent
@file{R-@var{x}.@var{y}.@var{z}.tar.gz} file, and unpack it with

@example
tar xf R-@var{x}.@var{y}.@var{z}.tar.gz
@end example

@noindent
on systems that have a suitable@footnote{e.g.@: @acronym{GNU} @command{tar}
version 1.15 or later, or that from the @samp{libarchive} (as used on
Mac OS 10.6) or `Heirloom Toolchest' distributions.} @command{tar}
installed.  On other systems you need to have the @command{gzip} program
installed, when you can use

@example
gzip -dc R-@var{x}.@var{y}.@var{z}.tar.gz | tar xf -
@end example

The pathname of the directory into which the sources are unpacked should
not contain spaces, as @command{make} (specifically @acronym{GNU}
@command{make} 3.81) does not expect spaces.

If you want the build to be usable by a group of users, set @code{umask}
before unpacking so that the files will be readable by the target group
(e.g.,@: @code{umask 022} to be usable by all users).  (Keep this
setting of @code{umask} whilst building and installing.)


@node Getting patched and development versions,  , Getting and unpacking the sources, Obtaining R
@section Getting patched and development versions

A patched version of the current release, @samp{r-patched}, and the
current development version, @samp{r-devel}, are available as daily
tarballs and via access to the R Subversion repository.  (For the two
weeks prior to the release of a minor (2.x.0) version, @samp{r-patched}
will refer to beta/release candidates of the upcoming release, the
patched version of the current release being available only via
Subversion.)

The tarballs are available from
@uref{ftp://ftp.stat.math.ethz.ch/@/pub/@/Software/R/}.  Download either
@file{R-patched.tar.gz} or @file{R-devel.tar.gz} (or the @file{.tar.bz2}
versions) and unpack as described in the previous section.  They are
built in exactly the same way as distributions of R releases.

@menu
* Using Subversion and rsync::  
@end menu

@node Using Subversion and rsync,  , Getting patched and development versions, Getting patched and development versions
@subsection Using Subversion and rsync
@cindex Subversion

Sources are also available via @uref{https://svn.R-project.org/R/}, the
R Subversion repository.  If you have a Subversion client (see
@uref{http://subversion.apache.org/}), you can check out and update the
current @samp{r-devel} from @uref{https://svn.r-project.org/R/trunk/} and the
current @samp{r-patched} from
@samp{https://svn.r-project.org/R/branches/R-@var{x}-@var{y}-branch/}
(where @var{x} and @var{y} are the major and minor number of the current
released version of R).  E.g., use

@example
svn checkout https://svn.r-project.org/R/trunk/ @var{path}
@end example

@noindent
to check out @samp{r-devel} into directory @var{path}.  The alpha, beta
and RC versions of an upcoming @var{x.y.0} release are available from
@samp{https://svn.r-project.org/R/branches/R-@var{x}-@var{y}-branch/} in
the four-week period prior to the release.

Note that @samp{https:} is required, and that the SSL certificate for
the Subversion server of the R project is

@example
Certificate information:
 - Hostname: svn.r-project.org
 - Valid: from Jul 16 08:10:01 2004 GMT until Jul 14 08:10:01 2014 GMT
 - Issuer: Department of Mathematics, ETH Zurich, Zurich, Switzerland, CH
 - Fingerprint: c9:5d:eb:f9:f2:56:d1:04:ba:44:61:f8:64:6b:d9:33:3f:93:6e:ad
@end example

@noindent
(currently, there is no ``trusted certificate'').  You can accept this
certificate permanently and will not be asked about it anymore.

Note that retrieving the sources by e.g.@: @command{wget -r} or
@command{svn export} from that URL will not work: the Subversion
information is needed to build @R{}.

The Subversion repository does not contain the current sources for the
recommended packages, which can be obtained by @command{rsync} or
downloaded from @acronym{CRAN}.  To use @code{rsync} to install the
appropriate sources for the recommended packages, run
@code{./tools/rsync-recommended} from the top-level of the @R{} sources.

If downloading manually from @acronym{CRAN}, do ensure that you have the
correct versions of the recommended packages: if the number in the file
@file{VERSION} is @samp{@var{x}.@var{y}.@var{z}} you need to download
the contents of @samp{http://CRAN.R-project.org/src/contrib/@var{dir}},
where @var{dir} is @samp{@var{x}.@var{y}.@var{z}/Recommended} for
r-devel or @file{@var{x}.@var{y}-patched/Recommended} for r-patched,
respectively, to directory @file{src/library/Recommended} in the sources
you have unpacked.  After downloading manually you need to execute
@command{tools/link-recommended} from the top level of the sources to
make the requisite links in @file{src/library/Recommended}.  A suitable
incantation from the top level of the R sources using @command{wget}
might be

@example
wget -r -l1 --no-parent -A\*.gz -nd -P src/library/Recommended \
  http://CRAN.R-project.org/src/contrib/@var{dir}
./tools/link-recommended
@end example



@node Installing R under Unix-alikes, Installing R under Windows, Obtaining R, Top
@chapter Installing R under Unix-alikes
@cindex Installing under Unix-alikes

@R{} will configure and build under a number of common Unix and
Unix-alike platforms including @samp{@var{cpu}-*-linux-gnu} for the
@cputype{alpha}, @cputype{arm}, @cputype{hppa}, @cputype{ix86},
@cputype{ia64}, @cputype{m68k}, @cputype{mips}, @cputype{mipsel},
@cputype{powerpc}, @cputype{s390}, @cputype{sparc}, and @cputype{x86_64}
@acronym{CPU}s,
@c (see e.g.@:  @uref{http://buildd.debian.org/build.php?&pkg=r-base}),
@c Actually, see http://packages.debian.org/unstable/math/r-base-core as
@c the build daemon is not used for all platforms; note also that Debian
@c has x86_64 <=> amd, ix86 <=> i386.
@samp{powerpc-@/apple-@/darwin}, @samp{i386-@/apple-@/darwin},
@samp{x86_64-@/apple-@/darwin}, @samp{i386-@/sun-@/solaris},
@samp{sparc-@/sun-@/solaris}, @samp{x86_64-@/*-@/freebsd}, and
@samp{powerpc-@/ibm-@/aix6*} as well as perhaps (it is tested less
frequently on these platforms) @samp{i386-@/*-@/freebsd},
@samp{i386-@/*-@/netbsd} and @samp{i386-@/*-@/openbsd}.

@cindex Linux
@cindex Mac OS X
In addition, binary distributions are available for some common Linux
distributions and for Mac OS X.  See the @acronym{FAQ} for current
details.  These are installed in platform-specific ways, so for the rest
of this chapter we consider only building from the sources.

@menu
* Simple compilation::          
* Help options::                
* Making the manuals::          
* Installation::                
* Uninstallation::              
* Sub-architectures::           
* Testing a Unix Installation::  
@end menu

@node Simple compilation, Help options, Installing R under Unix-alikes, Installing R under Unix-alikes
@section Simple compilation

First review the essential and useful tools and libraries in
@ref{Essential and useful other programs under Unix}, and install those you
@enindex TMPDIR
want or need.  Ensure that the environment variable @env{TMPDIR} is
either unset (and @file{/tmp} exists and can be written in and scripts
can be executed from) or points to a valid temporary directory (one from
which execution of scripts is allowed).

@findex R_HOME
Choose a place to install the @R{} tree (@R{} is not just a binary, but
has additional data sets, help files, font metrics etc).  Let us call
this place @var{R_HOME}.  Untar the source code.  This should create
directories @file{src}, @file{doc}, and several more under a top-level
directory: change to that top-level directory (At this point North
American readers should consult @ref{Setting paper size}.)  Issue the
following commands:

@findex configure

@example
./configure
make
@end example

@noindent
(See @ref{Using make} if your make is not called @samp{make}.)

Then check the built system works correctly by

@example
make check
@end example

@noindent
Failures are not necessarily problems as they might be caused by missing
functionality,@footnote{for example, if you configured @R{} with
@option{--without-recommended}.} but you should look carefully at any
reported discrepancies.  (Some non-fatal errors are expected in locales
that do not support Latin-1, in particular in true @code{C} locales and
non-UTF-8 non-Western-European locales.)  A failure in
@file{tests/ok-errors.R} may indicate inadequate resource limits
(@pxref{Running R}).

More comprehensive testing can be done by

@example
make check-devel
@end example

@noindent
or

@example
make check-all
@end example

@noindent
see file @file{tests/README}.

If the command @command{configure} and @command{make} commands execute
successfully, a shell-script front-end called @file{R} will be created
and copied to @file{@var{R_HOME}/bin}.  You can copy this script to a
place where users can invoke it, for example to @file{/usr/local/bin/R}.
You could also copy the man page @file{R.1} to a place where your
@command{man} reader finds it, such as @file{/usr/local/man/man1}.  If
you want to install the complete @R{} tree to, e.g.,
@file{/usr/local/lib/R}, see @ref{Installation}.  Note: you do not
@emph{need} to install @R{}: you can run it from where it was built.

You do not necessarily have to build @R{} in the top-level source
directory (say, @file{@var{TOP_SRCDIR}}).  To build in
@file{@var{BUILDDIR}}, run

@findex configure

@example
cd @var{BUILDDIR}
@var{TOP_SRCDIR}/configure
make
@end example

@noindent
and so on, as described further below.  This has the advantage of
always keeping your source tree clean and is particularly
recommended when you work with a version of @R{} from Subversion.
(You may need @acronym{GNU} @command{make} to allow this, and the
pathname of the build directory should not contain spaces.)

For those obtaining @R{} @emph{via} Subversion, one additional step is
necessary:
@cindex Vignettes
@cindex Subversion

@example
make vignettes
@end example

@noindent
which makes the @pkg{grid} vignettes (which are contained in the
tarballs): it make take several minutes.

Now @code{rehash} if necessary, type @kbd{R}, and read the @R{} manuals
and the @R{} @acronym{FAQ} (files @file{FAQ} or
@file{doc/manual/R-FAQ.html}, or
@uref{http://CRAN.R-project.org/@/doc/@/FAQ/@/R-FAQ.html} which always
has the version for the latest release of @R{}).

@node Help options, Making the manuals, Simple compilation, Installing R under Unix-alikes
@section Help options

As from @R{} 2.10.0, by default @HTML{} help pages are created when
needed rather than being built at install time.

If you need to disable the server and want HTML help, there is the
option to continue to build @HTML{} pages at when packages are installed
(including those installed with @R{}).  This is enabled by the
@command{configure} option @option{--enable-prebuilt-html}.  Whether
@command{R CMD INSTALL} (and hence @code{install.packages}) pre-builds
@HTML{} pages is determined by looking at the @R{} installation and is
reported by @command{R CMD INSTALL --help}: it can be overridden by
specifying one of the @command{INSTALL} options @option{--html} or
@option{-no-html}.

The server is disabled by setting the environment variable
@enindex R_DISABLE_HTTPD
@env{R_DISABLE_HTTPD} to a non-empty value, either before @R{} is
started or within the @R{} session before @HTML{} help (including
@code{help.start}) is used.  It is also possible that system security
measures will prevent the server from being started, for example if the
loopback interface has been disabled.  See
@code{?tools::startDynamicHelp} for more details.

@node Making the manuals, Installation, Help options, Installing R under Unix-alikes
@section Making the manuals
@cindex Manuals

There is a set of manuals that can be built from the sources,

@table @samp
@item refman
Printed versions of the help pages for the commonly-used packages.
@item fullrefman
Printed versions of all the help pages.
@item R-FAQ
R @acronym{FAQ}
@item R-intro
``An Introduction to R''.
@item R-data
``R Data Import/Export''.
@item R-admin
``R Installation and Administration'', this manual.
@item R-exts
``Writing R Extensions''.
@item R-lang
``The R Language Definition''.
@end table

@noindent
To make these (except @samp{fullrefman}), use

@example
make dvi      @r{to create DVI versions}
make pdf      @r{to create PDF versions}
make info     @r{to create info files (not @samp{refman}).}
@end example

You will not be able to build any of these unless you have
@command{makeinfo} version 4.7 or later installed, and for DVI or PDF
you must have @command{texi2dvi} and @file{texinfo.tex} installed (which
are part of the @acronym{GNU} @pkg{texinfo} distribution but are,
especially @file{texinfo.tex}, often made part of the @TeX{} package in
re-distributions).

The DVI versions can be previewed and printed using standard programs
such as @command{xdvi} and @command{dvips}.  The PDF versions can be
viewed using any recent PDF viewer: they have hyperlinks that can be
followed.  The info files are suitable for reading online with Emacs or
the standalone @acronym{GNU} @command{info} program.  The DVI and PDF
versions will be created using the papersize selected at configuration
(default ISO a4): this can be overridden by setting @env{R_PAPERSIZE}
@enindex R_PAPERSIZE
on the @command{make} command line, or setting @env{R_PAPERSIZE} in the
environment and using @command{make -e}.  (If re-making the manuals for
a different papersize, you should first delete the file
@file{doc/manual/version.texi}.)

There are some issues with making the reference manual, and in
particular with the PDF version @file{refman.pdf}.  The help files
contain both ISO Latin1 characters (e.g.@: in @file{text.Rd}) and
upright quotes, neither of which are contained in the standard @LaTeX{}
Computer Modern fonts.  We have provided four alternatives:

@table @code
@item times
(The default for PDF.) Using standard PostScript fonts.  This works
well both for on-screen viewing and for printing.
The one disadvantage is that the Usage and Examples sections may come
out rather wide.

@item lm
Using the @emph{Latin Modern} fonts.  These are not often installed as
part of a @TeX{} distribution, but can obtained from
@uref{http://www.ctan.org/@/tex-archive/@/fonts/@/ps-type1/@/lm/} and
mirrors.  This uses fonts rather similar to Computer Modern, but is not
so good on-screen as @code{times}.

@item cm-super
Using type-1 versions of the Computer Modern fonts by Vladimir Volovich.
This is a large installation, obtainable from
@uref{http://www.ctan.org/@/tex-archive/@/fonts/@/ps-type1/@/cm-super/}
and its mirrors.  These type-1 fonts have poor hinting and so are
nowhere near so readable on-screen as the other three options.

@item ae
(The default for DVI.)  A package to use composites of Computer Modern
fonts.  This works well most of the time, and its PDF is more readable
on-screen than the previous two options.  There are three fonts for
which it will need to use bitmapped fonts, @file{tctt0900.600pk},
@file{tctt1000.600pk} and @file{tcrm1000.600pk}.  Unfortunately, if
those files are not available, Acrobat Reader will substitute
completely incorrect glyphs so you need to examine the logs carefully.
@end table

The default can be overridden by setting the environment variables
@enindex R_RD4DVI
@enindex R_RD4PDF
@env{R_RD4PDF} and @env{R_RD4DVI}.  (On Unix, these will be picked up
at install time.)  The default value for @env{R_RD4PDF} is
@samp{times,hyper}: omit @samp{hyper} if you do not want hyperlinks,
e.g.@: for printing.  The default for @env{R_RD4DVI} is @samp{ae}.


@node Installation, Uninstallation, Making the manuals, Installing R under Unix-alikes
@section Installation
@cindex Installation

To ensure that the installed tree is usable by the right group of users,
set @code{umask} appropriately (perhaps to @samp{022}) before unpacking
the sources and throughout the build process.

After

@findex configure
@example
./configure
make
make check
@end example

@noindent
(or, when building outside the source,
@code{@var{TOP_SRCDIR}/configure}, etc) have been completed
successfully, you can install the complete @R{} tree to your system by
typing

@example
make install
@end example

@noindent
This will install to the following directories:

@table @asis
@item @file{@var{prefix}/bin} or @file{@var{bindir}}
the front-end shell script and other scripts and executables
@item @file{@var{prefix}/man/man1} or @file{@var{mandir}/man1}
the man page
@item @file{@var{prefix}/@var{LIBnn}/R} or @file{@var{libdir}/R}
all the rest (libraries, on-line help system, @dots{}).  Here
@var{LIBnn} is usually @samp{lib}, but may be @samp{lib64} on some
64-bit Linux systems.  This is known as the @R{} home directory.
@end table

@noindent
where @var{prefix} is determined during configuration (typically
@file{/usr/local}) and can be set by running @command{configure} with
the option @option{--prefix}, as in

@findex configure
@example
./configure --prefix=/where/you/want/R/to/go
@end example

@noindent
This causes @command{make install} to install the @R{} executable to
@file{/where/you/want/R/to/go/bin}, and so on.  The prefix of the
installation directories can be seen in the status message that is
displayed at the end of @command{configure}.  You can install into
another directory tree by using

@example
make prefix=/path/to/here install
@end example

@noindent
at least with @acronym{GNU} @command{make} (but not e.g.@: Solaris 8's
@command{make}).

More precise control is available at configure time via options: see
@command{configure --help} for details.  (However, most of the `Fine
tuning of the installation directories' options are not used by @R{}.)

Configure options @option{--bindir} and @option{--mandir} are supported
and govern where a copy of the @command{R} script and the @command{man}
page are installed.

The configure option @option{--libdir} controls where the main @R{}
files are installed: the default is @samp{@var{eprefix}/@var{LIBnn}},
where @var{eprefix} is the prefix used for installing
architecture-dependent files, defaults to @var{prefix}, and can be set
via the configure option @option{--exec-prefix}.

Each of @code{bindir}, @code{mandir} and @code{libdir} can also be
specified on the @command{make install} command line (at least for
@acronym{GNU} @command{make}).

The @command{configure} or @command{make} variables @code{rdocdir} and
@code{rsharedir} can be used to install the system-independent
@file{doc} and @file{share} directories to somewhere other than
@code{libdir}.  The C header files can be installed to the value of
@code{rincludedir}: note that as the headers are not installed into a
subdirectory you probably want something like
@code{rincludedir=/usr/local/include/R-@value{VERSIONno}}.

If you want the @R{} home to be something other than
@file{@var{libdir}/R}, use @option{rhome}: for example

@example
make install rhome=/usr/local/lib64/R-2.11.0
@end example

@noindent
will use a version-specific @R{} home on a Linux 64-bit system.

If you have made @R{} as a shared/dynamic library you can install it in
your system's library directory by

@example
make prefix=/path/to/here install-libR
@end example

@noindent
where @code{prefix} is optional, and @code{libdir} will give more
precise control.

@example
make install-strip
@end example

@noindent
will install stripped executables, and on platforms where this is
supported, stripped libraries in directories @file{lib} and
@file{modules} and in the standard packages.

@cindex Manuals, installing

To install DVI, info and PDF versions of the manuals, use one or more of

@example
make install-dvi
make install-info
make install-pdf
@end example

@noindent
Once again, it is optional to specify @code{prefix}, @code{libdir} or
@code{rhome} (the DVI and PDF manuals are installed under the @R{} home
directory).  (@command{make install-info} needs Perl installed if there
is no command @command{install-info} on the system.)

More precise control is possible.  For info, the setting used is that of
@code{infodir} (default @file{@var{prefix}/info}, set by configure
option @option{--infodir}).  The DVI and PDF files are installed into
the @R{} @file{doc} tree, set by the @command{make} variable
@code{rdocdir}.

A staged installation is possible, that it is installing @R{} into a
temporary directory in order to move the installed tree to its final
destination.  In this case @code{prefix} (and so on) should reflect the
@enindex DESTDIR
final destination, and @env{DESTDIR} should be used: see
@uref{http://www.gnu.org/@/prep/@/standards/@/html_node/@/DESTDIR.html}.

You can optionally install the run-time tests that are part of
@command{make check-all} by

@example
make install-tests
@end example

@noindent
which populates a @file{tests} directory in the installation.


@node Uninstallation, Sub-architectures, Installation, Installing R under Unix-alikes
@section Uninstallation

You can uninstall @R{} by

@example
make uninstall
@end example

@noindent
optionally specifying @code{prefix} etc in the same way as specified for
installation.

This will also uninstall any installed manuals.  There are specific
targets to uninstall DVI, info and PDF manuals in file
@file{doc/manual/Makefile}.

Target @code{uninstall-tests} will uninstall any installed tests, as
well as removing the directory @file{tests} containing the test results.



@node Sub-architectures, Testing a Unix Installation, Uninstallation, Installing R under Unix-alikes
@section Sub-architectures

Some platforms can support closely related builds of @R{} which can
share all but the executables and dynamic objects.  Examples include
builds under Solaris for different chips (in particular, 32- and 64-bit
builds), 64- and 32- bit builds on @cputype{x86_64} Linux and different
@acronym{CPU}s (e.g.@: @cputype{ppc}, @cputype{i386} and
@cputype{x86_64}) under Mac OS >= 10.4.

@R{} supports the idea of architecture-specific builds, specified by
adding @samp{r_arch=@var{name}} to the @command{configure} line.  Here
@var{name} can be anything non-empty, and is used to name subdirectories
of @file{lib}, @file{etc}, @file{include} and the package @file{libs}
subdirectories.  Example names from other systems are the use of
@file{sparcv9} on Sparc Solaris and @file{32} by @code{gcc} on
@cputype{x86_64} Linux.

If you have two or more such builds you can install them over each other
(and for 32/64-bit builds on one architecture, one build can be done
without @samp{r_arch}).  The space savings can be considerable: on
@cputype{x86_64} Linux a basic install (without debugging symbols) took
63Mb, and adding a 32-bit build added 6Mb.  If you have installed
multiple builds you can select which build to run by

@example
R --arch=@var{name}
@end example

@noindent
and just running @samp{R} will run the last build that was installed.

@code{R CMD INSTALL} will detect if more than one build is installed and
try to install packages with the appropriate library objects for each.
This will not be done if the package has an executable @code{configure}
script or a @file{src/Makefile} file.  In such cases you can install for
extra builds by

@example
R --arch=@var{name} CMD INSTALL --libs-only @var{pkg(s)}
@end example

If you want to mix sub-architectures compiled on different platforms
(for example @cputype{x86_64} Linux and @cputype{i686} Linux), it is
wise to use explicit names for each, and you may also need to set
@option{libdir} to ensure that they install into the same place.

When sub-architectures are used the version of @command{Rscript} in
e.g.@: @file{/usr/bin} will be the last installed, but
architecture-specific versions will be available in e.g.@:
@file{/usr/lib64/R/bin/exec@var{R_ARCH}}.  Normally all installed
architectures will run on the platform so the architecture of
@command{Rscript} does not matter.

When running post-install tests with sub-architectures, use

@example
R --arch=@var{name} CMD make check[-devel|all]
@end example

@noindent
to select a sub-architecture to check.

@menu
* Multilib::                    
@end menu

@node Multilib,  , Sub-architectures, Sub-architectures
@subsection Multilib

On Linux, there is an alternative mechanism for mixing 32-bit and
64-bit libraries known as @emph{multilib}. If a Linux distribution supports
multilib, then parallel builds of @R{} may be installed in the
sub-directories @file{lib} (32-bit) and @file{lib64} (64-bit).  The
build to be run may then be chosen using the @command{setarch}
command. For example, a 32-bit build may be chosen by

@example
setarch i686 R
@end example

The @command{setarch} command is only operational if both 32-bit and
64-bit builds are installed. If there is only one installation of @R{},
then this will always be run regardless of the architecture specified
by the @command{setarch} command.

There can be problems with installing packages on the non-native
architecture.  It is a good idea to run e.g.@: @code{setarch i686 R} for
sessions in which packages are to be installed, even if that is the only
version of @R{} installed (since this tells the package installation
code the architecture needed).

At present there is a potential problem with packages using Java, as
the post-install for a @cputype{i386} RPM on @cputype{x86_64} Linux
reconfigures Java and will find the @cputype{x86_64} Java.  If you know
where a 32-bit Java is installed you may be able to run (as root)

@example
export JAVA_HOME=<path to jre directory of 32-bit Java>
setarch i686 R CMD javareconf
@end example

@noindent
to get a suitable setting.

When this mechanism is used, the version of @command{Rscript} in
e.g.@: @file{/usr/bin} will be the last installed, but an
architecture-specific version will be available in
e.g.@: @file{/usr/lib64/R/bin}.  Normally all installed architectures
will run on the platform so the architecture of @command{Rscript} does
not matter.

@node Testing a Unix Installation,  , Sub-architectures, Installing R under Unix-alikes
@section Testing an Installation

Full testing is possible only if the test files have been installed with

@example
make install-tests
@end example

@noindent
which populates a @file{tests} directory in the installation.

If this has been done, two testing routes are available.
The first is to move to the home directory of the @R{} installation
(as given by @code{R.home()}) and run

@example
cd tests
## followed by one of
../bin/R CMD make check
../bin/R CMD make check-devel
../bin/R CMD make check-all
@end example

@noindent
and other useful targets are @code{test-BasePackages} and
@code{test-Recommended} to the run tests of the standard and
recommended packages (if installed) respectively.

This re-runs all the tests relevant to the installed @R{} (including for
example code in the package vignettes), but not for example the ones
checking the example code in the manuals nor making the standalone Rmath
library.  This can occasionally be useful when the operating environment
has been changed, for example by OS updates or by substituting the BLAS
(@pxref{Shared BLAS}).

Alternatively, the installed @R{} can be run, preferably with
@option{--vanilla}.  Then

@example
library("tools")
testInstalledBasic("both")
testInstalledPackages(scope = "base")
testInstalledPackages(scope = "recommended")
@end example

@noindent
runs the basic tests and then all the tests on the standard and
recommended packages.  These tests can be run from anywhere: the basic
tests write their results in the @file{tests} folder of the @R{} home
directory and run slightly fewer tests than the first approach: in
particular they do not test Internet access.

These tests work best if @command{diff} (in @file{Rtools.exe} for
Windows users) is in the path, and on some systems need the collation
locale set manually (the @R{} code tries to do so but it may not be
possible to reset it): so if needed try setting the environment variable
@env{LC_COLLATE} to @samp{C} before starting @R{}.

It is possible to test the installed packages (but not the
package-specific tests) by @code{testInstalledPackages} even if
@command{make install-tests} was not run.

@node Installing R under Windows, Installing R under Mac OS X, Installing R under Unix-alikes, Top
@chapter Installing R under Windows
@cindex Installing under Windows

The @file{bin/windows} directory of a @acronym{CRAN} site contains
binaries for a base distribution and a large number of add-on packages
from @acronym{CRAN} to run on Windows 2000 or later on ix86
@acronym{CPU}s (including AMD64/Intel64@footnote{formerly EM64T.} chips
and Windows x64).

As from @R{} 2.11.0 there is also a 64-bit version for Windows x64 in
the @file{bin/windows64} directory: this is primarily intended for users
who require large amounts of memory and beginning users of @R{} should
install the standard version even on a 64-bit Windows OS.

Your file system must allow long file names (as is likely except
perhaps for some network-mounted systems).

Installation is @emph{via} the installer
@file{@value{RWVERSION}-win32.exe} (or
@file{@value{RWVERSION}-win64.exe}).  Just double-click on the icon and
follow the instructions.  You can uninstall @R{} from the Control Panel
or from the (optional) @R{} program group on the Start Menu.

Note that you will be asked to choose a language for installation, and
that choice applies to both installation and un-installation but not to
running @R{} itself.

See the @uref{http://CRAN.R-project.org/bin/windows/base/rw-FAQ.html, R
Windows @acronym{FAQ}} for more details on the binary installer.

@menu
* Building from source::        
* Testing a Windows Installation::  
* Building R for 64-bit Windows::  
@end menu

@node Building from source, Testing a Windows Installation, Installing R under Windows, Installing R under Windows
@section Building from source

@menu
* Getting the tools::           
* Getting the source files::    
* Building the core files::     
* Building the bitmap files::   
* Checking the build::          
* Building the manuals::        
* Building the Inno Setup installer::  
* Building the MSI installer::  
* Cross-building on Linux::     
@end menu

@node Getting the tools, Getting the source files, Building from source, Building from source
@subsection Getting the tools

If you want to build @R{} from the sources, you will first need to
collect, install and test an extensive set of tools.  See @ref{The
Windows toolset} (and perhaps updates in
@uref{http://www.murdoch-sutherland.com/@/Rtools/}) for details.

The @file{Rtools.exe} executable installer described in @ref{The Windows
toolset} also includes some source files in addition to the @R{} source
as noted below. You should run it first, to obtain a working @code{tar}
and other necessities.  Choose a ``Full installation'', and install the
extra files into your intended @R{} source directory, e.g.@:
@file{C:/R}. The directory name @emph{should not contain spaces}. We
will call this directory @var{R_HOME} below.

@c To avoid warnings you may need to set the environment variable
@c @env{CYGWIN} to @samp{nodosfilewarning}.

@node Getting the source files, Building the core files, Getting the tools, Building from source
@subsection Getting the source files

You need to collect the following sets of files:
@itemize

@item
Get the @R{} source code @file{R-@value{VERSIONno}.tar.gz} from
@acronym{CRAN}.  Open a command window (or another shell) at 
directory @var{R_HOME}, and run

@example
tar xf R-@value{VERSIONno}.tar.gz
@end example

@noindent
to create the source tree in @var{R_HOME}.  @strong{Beware}: do use
@command{tar} to extract the sources rather than tools such as WinZip
that do not understand symbolic links.

It is also possible to obtain the source code using Subversion; see 
@ref{Obtaining R} for details.


@item
If you are not using a tarball you need to obtain copies of the
recommended packages from @acronym{CRAN}.  Put the @file{.tar.gz} files
in @file{@var{R_HOME}/src/library/Recommended} and run @code{make
link-recommended}.  If you have an Internet
connection, you can do this automatically using

@example
make rsync-recommended
@end example

@item
Optionally, you can install a version of ATLAS
(@uref{http://math-atlas.sourceforge.net/}) tuned to your system for
fast linear algebra routines. Pre-built @file{Rblas.dll} for various
@acronym{CPU}s are available in the @file{windows/contrib/ATLAS} area on
@acronym{CRAN}.  If you are building R from source, there are macros
@code{USE_ATLAS} and @code{ATLAS_PATH} in the file @file{MkRules.dist}.
Copy file @file{MkRules.dist} to @file{MkRules.local} and edit it to set
@code{USE_ATLAS = YES} and @code{ATLAS_PATH} to where the ATLAS
libraries are located.  You will need to make the libraries
yourself@footnote{We do this using the Cygwin compilers, often with some
difficulty.}: none of the binaries we have seen are compiled for the
correct compiler.  Since @R{} has its own @samp{xerbla} it is necessary
to delete that in ATLAS by

@example
ar d /path/to/libf77blas.a xerbla.o
@end example

There used to be support for AMD's AMD Core Math Library (ACML) and
Kazushige Goto's BLAS, but neither is currently available for use with
the compilers used to compile @R{}.

@end itemize

The following additional items are normally installed by
@file{Rtools.exe}.  If instead you choose to do a completely manual
build you will also need

@itemize

@c @item
@c Get @file{iconv.dll} from
@c @uref{http://www.stats.ox.ac.uk/pub/Rtools/iconv.dll} and put it in
@c @file{@var{R_HOME}/@/src/@/gnuwin32/@/unicode}.

@item
The Tcl/Tk support files are in a zip file at
@uref{http://www.stats.ox.ac.uk/@/pub/@/Rtools/}: unzip this in
@var{R_HOME}, and it will add directory tree @file{@var{R_HOME}/Tcl}.

@item
You need @code{libpng} and @code{jpeg} sources (available, e.g., from
@uref{http://www.libpng.org/}, @uref{http://www.ijg.org},
@uref{http://www.libtiff.org/}.@footnote{but the current versions are at
@uref{ftp://ftp.remotesensing.org/pub/libtiff/}: we used
@file{tiff-3.9.2.tar.gz}.}  You will need files
@file{libpng-1.2.18.tar.gz}, @file{jpegsrc.v6b.tar.gz},
@file{tiff-3.8.0.tar.gz} or later.

Working in the directory @file{@var{R_HOME}/src/gnuwin32/bitmap},
install the @code{libpng} and @code{jpeg} sources in sub-directories.
The @code{jpeg} sub-directory for version 8a is named @file{jpeg-8a}; if
you use a different version (e.g.@: @file{jpeg-6b} or @file{jpeg-7}),
copy file @file{src/gnuwin32/MkRules.dist} to
@file{src/gnuwin32/MkRules.local} and edit the definition of
@code{JPEGDIR}: the names of the @file{libpng} and @file{libtiff}
directories can also be set there.

Example:

@example
> tar zxf libpng-1.2.40.tar.gz
> mv libpng-1.2.40 libpng
> tar zxf jpegsrc.v8a.tar.gz
> tar zxf tiff-3.9.1.tar.gz
> mv tiff-3.9.1/libtiff .
> rm -rf tiff-3.9.1
@end example

@end itemize

@node Building the core files, Building the bitmap files, Getting the source files, Building from source
@subsection Building the core files

You may need to compile under a case-honouring file system: we found
that a @command{samba}-mounted file system (which maps all file names to
lower case) did not work.

Open a command window at @file{@var{R_HOME}/src/gnuwin32}.  Look at
@file{MkRules.dist} and if settings need to be altered, copy it to
@file{MkRules.local} and edit the settings there.  Then run

@example
make all recommended
@end example

@noindent
and sit back and wait while the basic compile takes place.

Notes:
@itemize

@item
We have had reports that earlier versions of anti-virus software locking
up the machine, but not for several years.  However, aggressive
anti-virus checking such as the on-access scanning of Sophos can slow
the build down several-fold.

@item
By default Doug Lea's @code{malloc} in the file
@file{@var{R_HOME}/src/gnuwin32/malloc.c} is used for R's internal
memory allocations.  You can opt out of this by setting
@code{LEA_MALLOC=NO} in @file{MkRules.dist}, in which case the @code{malloc}
in @file{msvcrt.dll} is used.  This does impose a considerable
performance penalty and has not been tested recently.

@item
You can run a parallel make by e.g.

@example
make -j4 all
make -j4 recommended
@end example

@noindent
but this is only likely to be worthwhile on a multi-core machine with
ample memory, and is not 100% reliable.

@end itemize

@node Building the bitmap files, Checking the build, Building the core files, Building from source
@subsection Building the bitmap files
@cindex Rbitmap.dll

The file @file{@var{R_HOME}/library/grDevices/libs/Rbitmap.dll} is not built
automatically.  

Running @code{make} in @file{@var{R_HOME}/src/gnuwin32/bitmap} or
@code{make bitmapdll} in @file{@var{R_HOME}/@/src/gnuwin32} should build
@file{Rbitmap.dll} and install it under
@file{@var{R_HOME}/library/grDevices/libs}.

@node Checking the build, Building the manuals, Building the bitmap files, Building from source
@subsection Checking the build

You can test a build by running @code{make check}.  You may need to set
@enindex TMPDIR
@env{TMPDIR} to the absolute path to a suitable temporary directory: the
default is @file{c:/TEMP}.  (Use forward slashes and do not use a path
including spaces.  It will be ignored if not set to a directory.)

The recommended packages can be  checked by

@example
make check-recommended
@end example

@noindent
Other levels of checking are

@example
make check-devel
@end example

@noindent
for a more thorough check of the @R{} functionality, and

@example
make check-all
@end example

@noindent
for @code{check-devel} and @code{check-recommended}.


@node Building the manuals, Building the Inno Setup installer, Checking the build, Building from source
@subsection Building the manuals

The PDF manuals can be made by

@example
make manuals
@end example

@noindent
If you want to make the info versions (not the Reference Manual), use

@example
cd ../../doc/manual
make -f Makefile.win info
@end example

@noindent
To make DVI versions of the manuals use

@example
cd ../../doc/manual
make -f Makefile.win dvi
@end example

@noindent
(all assuming you have @command{pdftex}/@command{pdflatex} or
@command{tex}/@command{latex} installed and in your path).

See the @ref{Making the manuals} section in the Unix section for setting
options such as the paper size.

@node Building the Inno Setup installer, Building the MSI installer, Building the manuals, Building from source
@subsection Building the Inno Setup installer

You need to have the files for a complete R build, including bitmap and
Tcl/Tk support and the manuals, as well as the recommended packages and
Inno Setup (@pxref{The Inno Setup installer}).

Once everything is set up

@example
make distribution
make check-all
@end example

@noindent
will make all the pieces and the installers and put them in the
@file{gnuwin32/cran} subdirectory, then check the build.  This works by
building all the parts in the sequence:

@example
Rpwd.exe @r{(a utility needed in the build)}
rbuild @r{(the executables, the @acronym{FAQ} docs etc.)}
rpackage @r{(the base packages)}
htmldocs @r{(the HTML documentation)}
bitmapdll @r{(the bitmap support files)}
recommended @r{(the recommended packages)}
vignettes @r{(the vignettes in package @strong{grid}:}
	  @r{ only needed if building from an svn checkout)}
manuals @r{(the PDF manuals)}
rinstaller @r{(the install program)}
crandir @r{(the @acronym{CRAN} distribution directory)}
@end example

The parts can be made individually if a full build is not needed, but
earlier parts must be built before later ones.  (The @file{Makefile}
doesn't enforce this dependency---some build targets force a lot of
computation even if all files are up to date.)  The first four targets
are the default build if just @samp{make} is run.

If you want to customize the installation by adding extra packages,
replace @code{make rinstaller} by something like

@example
make rinstaller EXTRA_PKGS='pkg1 pkg2 pkg3'
@end example


An alternative way to customize the installer starting with a binary
distribution is to first make a full installation of R from the standard
installer (that is, select @samp{Full Installation} from the
@samp{Select Components} screen), then add packages and make other
customizations to that installation.  Then in
@file{src/gnuwin32/installer} run

@example
make myR IMAGEDIR=rootdir
@end example

@noindent
where @file{rootdir} is the path to the root of the customized
installation (forward slashes and no spaces, please).  

Both methods create an executable with the standard name,
@file{@value{RWVERSION}-win32.exe}, so please rename it to indicate that
it is customized.  If you intend to @emph{distribute} a customized
installer please do check that license requirements are met -- note that
the installer will state that the contents are distributed under GPL-2
and this has a requirement for @emph{you} to supply the complete sources
(including the R sources even if you started with a binary distribution
of R).


The defaults for the startup parameters may also be customized.  For example

@example
make myR IMAGEDIR=rootdir MDISDI=1
@end example

@noindent
will create an installer that defaults to installing @R{} to run in SDI
mode.  See @file{src/@/gnuwin32/installer/Makefile} for the names and
values that can be set.

@node Building the MSI installer, Cross-building on Linux, Building the Inno Setup installer, Building from source
@subsection Building the MSI installer

It is also possible to build an installer for use with Microsoft
Installer.  This is intended for use by sysadmins doing automated
installs, and is not recommended for casual use.  It is only supported
for 32-bit Windows at present.

It makes use of the Windows Installer XML (WiX) toolkit @emph{version
2.0} available from @uref{http://wix.sourceforge.net/}.  (This needs the
.NET 1.1 framework installed: it ran on a vanilla Windows XP SP2
machine. Unfortunately the file format has been changed within the same
version: currently our code works with releases 2.0.4221.0 and
2.0.5805.0 -- the latter is now said to be `production/stable'.)  Once
WiX is installed, set the path to its home directory in @file{MkRules.local}.

You need to have the files for a complete R build, including bitmap and
Tcl/Tk support and the manuals, as well as the recommended packages.
There is no option in the installer to customize startup options, so
edit @file{etc/Rconsole} and @file{etc/Rprofile.site} to set these as
required.  Then

@example
cd installer
make msi
@end example

@noindent
which will results in a file of about 45Mb with a name like
@file{@value{RWVERSION}-win32.msi}.  This can be double-clicked to be
installed, but those who need it will know what to do with it.

Thanks to David del Campo (Dept of Statistics, University of Oxford)
for suggesting WiX and building a prototype installer.

@node Cross-building on Linux,  , Building the MSI installer, Building from source
@subsection Cross-building on Linux

Support for cross-building was withdrawn at @R{} 2.9.0.

@node Testing a Windows Installation, Building R for 64-bit Windows, Building from source, Installing R under Windows
@section Testing an Installation

The Windows installer contains a set of test files used when building
@R{}.  These are an optional part of the installation, and if it is
desired to run tests on the installation these should be selected as
well as the help source files (perhaps most easily by doing a `Full
Installation').

The @code{Rtools} are not needed to run these tests. but more
comprehensive analysis of errors will be given if @command{diff} is in
the path.

Once this has been done, launch either @code{Rgui} or @code{Rterm},
preferably with @option{--vanilla}.  Then

@example
library("tools")
testInstalledBasic("both")
testInstalledPackages("base")
testInstalledPackages("recommended")
@end example

@noindent
runs the basic tests and then all the tests on the standard and
recommended packages.  These tests can be run from anywhere: they write
their results in the @file{tests} folder of the @R{} home directory (as
given by @code{R.home()}).

@node Building R for 64-bit Windows,  , Testing a Windows Installation, Installing R under Windows
@section Building R for 64-bit Windows

Note that the standard 32-bit build of R runs perfectly well on 64-bit
versions of Windows, with a limit on the total memory allocation (and
address space) of 4GB.  Thus a native 64-bit build will be attractive to
those wanting to work with large R objects, or many medium-sized ones.

To build a 64-bit version of R you need a 64-bit toolchain: the only one
discussed here is based on the work of the MinGW-w64 project
(@url{http://sourceforge.net/projects/mingw-w64/}, a port of
@acronym{GNU} @code{binutils} and @code{gcc}), but commercial compilers
such as those from Intel and PGI could be used (and have been by @R{}
redistributors).

Support for MinGW-w64 was developed in the R sources over the period
2008--10 and was first released as part of @R{} 2.11.0.  The binary
version available from @acronym{CRAN} was built with the MinGW-w64
toolchain described in this section, using static linking.

Several versions of the MinGW-w64 toolchain are available: use one of
them to replace the @file{MinGW/bin} directory of @code{Rtools} in your
path (but still have the Rtools @file{bin} directory and Perl in your
path).  Then copy @file{src/gnuwin32/MkRules.dist} to
@file{src/gnuwin32/MkRules.local} and edit it to set @code{WIN=64} and
@code{BINPREF64} appropriate to your toolchain, then make @R{} in the
usual way.

The toolchain we use is technically a cross-compiler: the tools run
under 32-bit Windows but produce code to run under 64-bit
Windows.@footnote{As the @R{} build process itself runs @R{}, it has to
be done under 64-bit Windows.  So does installation of source packages.}
This comes from
@url{http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Automated%20Builds/}
and has a name like @file{mingw-w64-1.0-bin_i686-mingw_20100322.zip}.
(Care is needed: that site also has compilers targeting 32-bit Windows
and running on other host OSes.  You do want the `release' version with
@code{-1.0-} in the name.  There was a native x64 toolchain under
`Personal builds'.)  Some versions of that toolchain by default use
dynamic linking to the compiler runtimes, which causes problems (see
@url{http://www.stats.ox.ac.uk/~ripley/Win64/W64porting.html}), and can
be converted to static linking by removing the files like
@example
.../x86_64-w64-mingw32/lib64/*.dll.a
.../bin/*.dll
@end example

@noindent
where @code{...} is the top-level directory of the toolchain. If do you
use a dynamically-linking toolchain be sure to arrange to distribute the
required run-time DLLs (and note the requirements that their GPL license
imposes on re-distribution).

@c Other toolchains are available: the time of writing native 64-bit
@c compilers from @url{ftp://ftp.equation.com/gcc/} and from
@c @url{http://www.cadforte.com/downloads.html}.  Use at your own risk!

To build package @pkg{tcltk} you need to unpack
@url{http://www.stats.ox.ac.uk/pub/Rtools/R64_Tcl_8-5-8.zip}
(@strong{NB} this is not the same file as for @R{} 2.12.0) at the top of
the source tree: otherwise edit @file{src/library/tcltk/Makefile.win} to
make a stub package.

The default is @code{options(pkgType="win64.binary")}.  Packages using
Gtk+ (@pkg{Cairo}, @pkg{RGtk2}, @pkg{cairoDevice} and those that depend
on them) need the @file{bin} directory of the distribution at
@url{http://www.gtk.org/download-windows-64bit.html} in the path: note
that this conflicts with the directory of DLLs needed for 32-bit Gtk+ so
you need the correct one first in your path if using both.

A package-building service is available at
@url{http://win-builder.r-project.org/}, and some notes on porting
packages can be found at
@url{http://www.stats.ox.ac.uk/~ripley/Win64/W64porting.html}.

The default memory limit will be the amount of installed RAM: it can
be changed by using (preferably) @option{--max-mem-size=} at startup or
@code{memory.limit} in a running R session.

The personal library is (by default) folder
@file{R\win64-library\@var{x.y}} of your home directory.

The assistance of Yu Gong at a crucial step in porting R to MinGW-w64 is
gratefully acknowledged, as well as help from Kai Tietz, the lead
developer of the MinGW-w64 project.

@node Installing R under Mac OS X, Running R, Installing R under Windows, Top
@chapter Installing R under Mac OS X
@cindex Mac OS X

The @file{bin/macosx} directory of a @acronym{CRAN} site contains
binaries for Mac OS X for a base distribution and a large number of
add-on packages from @acronym{CRAN} to run on Mac OS X 10.5/6.

The simplest way is to use @file{R-@value{VERSIONno}.pkg}: just
double-click on the icon.  Note that Tcl/Tk and the Fortran compiler
need to be installed separately if needed (unlike for @R{} 2.9.x and
earlier).

See the @uref{http://CRAN.R-project.org/bin/macosx/RMacOSX-FAQ.html, R
for Mac OS X @acronym{FAQ}} for more details.

@menu
* Building from source on Mac OS X::  
@end menu

@node Building from source on Mac OS X,  , Installing R under Mac OS X, Installing R under Mac OS X
@section Building from source on Mac OS X

If you want to build this port from the sources, you can read the FAQ
for full details.  You will need to collect and install some tools as
explained in the document.  Then you have to expand the @R{} sources and
configure @R{} appropriately, for example

@example
tar zxvf R-@value{VERSIONno}.tar.gz
cd R-@value{VERSIONno}
./configure --with-blas='-framework vecLib' --with-lapack \
  --with-aqua --enable-R-framework
make
@end example

@noindent
and then sit back and wait.  The first two options are the default (and
strongly recommended), and with some toolsets have been essential.

The second line of options is also default on Mac OS X, but needed only
if you want to build @R{} for use with @code{R.app} Console, and imply
@option{--enable-R-shlib} to build @R{} as a shared/dynamic library.
These options configure @R{} to be built and installed as a framework
called @file{R.framework}.  The default path for @file{R.framework} is
@file{/Library/Frameworks} but this can be changed at configure time by
specifying the flag @option{--enable-R-framework[=@var{DIR}]} or at
install time as

@example
make prefix=/where/you/want/R.framework/to/go install
@end example

@noindent
(the @file{R.framework} directory should not be included in the path).

For compatibility with the @acronym{CRAN} distribution you may need to
specify @option{--with-included-gettext} to avoid linking against a
@samp{libintl} dynamic library you may have available, for example in
@file{/usr/local/lib}.

Note that building the @file{R.app} GUI console is a separate project:
see the Mac OS X FAQ for details.


@node Running R, Add-on packages, Installing R under Mac OS X, Top
@chapter Running R

How to start @R{} and what command-line options are available is discussed
in @ref{Invoking R, , Invoking R, R-intro, An Introduction to R}.

You should ensure that the shell has set adequate resource limits: @R{}
expects a stack size of at least 8MB and to be able to open at least 256
file descriptors.  (Any modern OS will have default limits at least as
large as these, but apparently NetBSD does not.)

@R{} makes use of a number of environment variables, the default values
of many of which are set in file @file{@var{R_HOME}/etc/Renviron} (there
are none set by default on Windows and hence no such file).  These are
set at @command{configure} time, and you would not normally want to
@enindex R_PAPERSIZE
change them -- a possible exception is @env{R_PAPERSIZE} (@pxref{Setting
paper size}).  The paper size will be deduced from the @samp{LC_PAPER}
locale category if it exists and @env{R_PAPERSIZE} is unset, and this
will normally produce the right choice from @samp{a4} and @samp{letter}
on modern Unix-alikes (but can always be overridden by setting
@env{R_PAPERSIZE}).

Various environment variables can be set to determine where @R{} creates
its per-session temporary directory.  The environment variables
@enindex TMPDIR
@enindex TMP
@enindex TEMP
@env{TMPDIR}, @env{TMP} and @env{TEMP} are searched in turn and the
first one which is set and points to a writable area is used.  If none
do, the final default is @file{/tmp} on Unix-alikes and the value of
@enindex R_USER
@env{R_USER} on Windows.

Some Unix-alike systems are set up to remove files and directories
periodically from @file{/tmp}, for example by a @command{cron} job
@enindex TMPDIR
running @command{tmpwatch}.  Set @env{TMPDIR} to another directory
before running long-running jobs on such a system.

Note that @env{TMPDIR} will be used to execute @command{configure}
scripts when installing packages, so if @file{/tmp} has been mounted as
@samp{noexec}, @env{TMPDIR} needs to be set to a directory from which
execution is allowed.

@node Add-on packages, Internationalization, Running R, Top
@chapter Add-on packages
@cindex Packages
@cindex Libraries

@menu
* Default packages::            
* Managing libraries::          
* Installing packages::         
* Updating packages::           
* Removing packages::           
* Setting up a package repository::  
@end menu

It is helpful to use the correct terminology.  A @emph{package} is
loaded from a @emph{library} by the function @code{library()}.  Thus a
library is a directory containing installed packages; the main library
is @file{@var{R_HOME}/library}, but others can be used, for example by
@enindex R_LIBS
setting the environment variable @env{R_LIBS} or using the @R{} function
@code{.libPaths()}.

@node Default packages, Managing libraries, Add-on packages, Add-on packages
@section Default packages
@cindex Packages, default

The set of packages loaded on startup is by default

@example
> getOption("defaultPackages")
[1] "datasets"  "utils"     "grDevices" "graphics"  "stats"     "methods"
@end example

@noindent
(plus, of course, @pkg{base}) and this can be changed by setting the
option in startup code (e.g.@: in @file{~/.Rprofile}).  It is initially
@enindex R_DEFAULT_PACKAGES
set to the value of the environment variable @env{R_DEFAULT_PACKAGES} if
set (as a comma-separated list).  Setting @env{R_DEFAULT_PACKAGES=NULL}
ensures that only package @pkg{base} is loaded.

Changing the set of default packages is normally used to reduce the set
for speed when scripting: in particular not using @pkg{methods} will
reduce the start-up time by a factor of up to two (and this is done by
@command{Rscript}).  But it can also be used to customize @R{}, e.g.@:
for class use.


@node Managing libraries, Installing packages, Default packages, Add-on packages
@section Managing libraries
@cindex Libraries, managing

@R{} packages are installed into @emph{libraries}, which are
directories in the file system containing a subdirectory for each
package installed there.

@R{} comes with a single library, @file{@var{R_HOME}/library} which is
the value of the @R{} object @samp{.Library} containing the standard and
recommended@footnote{unless they were excluded in the build.}  packages.
Both sites and users can create others and make use of them (or not) in
an @R{} session.  At the lowest level @samp{.libPaths()} can be used to
add paths to the collection of libraries or to report the current
collection.

@cindex Libraries, site
@cindex Site libraries
@R{} will automatically make use of a site-specific library
@file{@var{R_HOME}/site-library} if this exists (it does not in a
vanilla @R{} installation).  This location can be overridden by
setting@footnote{its binding is locked once that files has been read, so
users cannot easily change it.} @samp{.Library.site} in
@file{@var{R_HOME}/etc/Rprofile.site}, or (not recommended) by setting
the
@enindex R_LIBS_SITE
environment variable @env{R_LIBS_SITE}.  Like @samp{.Library}, the
site libraries are always included by @samp{.libPaths()}.

@cindex Libraries, user
@cindex User libraries
@enindex R_LIBS_USER
Users can have one or more libraries, normally specified by the
environment variable @env{R_LIBS_USER}.  This has a default value (use
@samp{Sys.getenv("R_LIBS_USER")} within an @R{} session to see what it
is), but only is used if the corresponding directory actually exists
(which by default it will not).

Both @env{R_LIBS_USER} and @env{R_LIBS_SITE} can specify multiple
library paths, separated by colons (semicolons on Windows).


@node Installing packages, Updating packages, Managing libraries, Add-on packages
@section Installing packages
@cindex Packages, installing

@menu
* Windows packages::            
* Mac OS X packages::           
* Customizing package compilation::  
@end menu


Packages may be distributed in source form or compiled binary form.
Installing source packages which contain C/C++/Fortran code requires
that compilers and related tools be installed.  Binary packages are
platform-specific and generally need no special tools to install, but
see the documentation for your platform for details.

Note that you may need to specify implicitly or explicitly the library to
which the package is to be installed.  This is only an issue if you have
more than one library, of course.

If installing packages to be used by other users, ensure that the system
@code{umask} is set to give sufficient permissions (see also
@code{Sys.umask} in @R{}).

For most users it suffices to call
@samp{install.packages(@var{pkgname})} or its GUI equivalent if the
intention is to install a @acronym{CRAN} package and internet access is
available.@footnote{If a proxy needs to be set, see
@command{?download.file}.}  On most systems @samp{install.packages()}
will allow packages to be selected from a list box.

To install packages from source in Unix use

@example
R CMD INSTALL -l /path/to/library @var{pkg1} @var{pkg2} @dots{}
@end example

@noindent
The part @samp{-l /path/to/library} can be omitted, in which case the
@enindex R_LIBS
first library in @env{R_LIBS} is used if set, otherwise the main library
@file{@var{R_HOME}/library} is used.  (@env{R_LIBS} is looked for in the
environment: note that @file{.Renviron} is not read by @command{R CMD}.)
@enindex TMPDIR
Ensure that the environment variable @env{TMPDIR} is either unset (and
@file{/tmp} exists and can be written in and executed from) or points to
a valid temporary directory.

There are a number of options available: use @code{R CMD INSTALL --help}
to see the current list.

@findex install.packages
Alternatively, packages can be downloaded and installed from within
@R{}.  First set the option @code{CRAN} to your nearest @acronym{CRAN}
mirror using @command{chooseCRANmirror()}.  Then download
and install packages @pkg{pkg1} and @pkg{pkg2} by

@example
> install.packages(c("pkg1", "pkg2"))
@end example

@noindent
The essential dependencies of the specified packages will also be fetched.
Unless the library is specified (argument @code{lib}) the first library
in the library search path is used: if this is not writable, @R{} will
ask the user (in an interactive session) if the default user library
should be created, and if allowed to will install the packages there.


If you want to fetch a package and all those it depends on that are
not already installed, use e.g.

@example
> install.packages("Rcmdr", dependencies = TRUE)
@end example

@code{install.packages} can install a source package from a local
@file{.tar.gz} file by setting argument @code{repos} to @code{NULL}:
this will be selected automatically if the name given is a single
@file{.tar.gz} file.

@code{install.packages} can look in several repositories, specified as a
character vector by the argument @code{repos}: these can include a
@acronym{CRAN} mirror, Bioconductor, Omegahat, R-forge, local archives,
local files, @dots{}).  Function @code{setRepositories()} can select
amongst those repositories that the @R{} installation is aware of.

Naive users sometimes forget that as well as installing a package, they
have to use @code{library} to make its functionality available.

@node Windows packages, Mac OS X packages, Installing packages, Installing packages
@subsection Windows

What @code{install.packages} does by default is different on Unix-alikes
(except Mac OS X) and Windows.  On Unix-alikes it consults the list of
available @emph{source} packages on @acronym{CRAN} (or other
repository/ies), downloads the latest version of the package sources,
and installs them (via @code{R CMD INSTALL}).  On Windows it looks (by
default) at the list of @emph{binary} versions of packages available for
your version of @R{} and downloads the latest versions (if any),
although optionally it will also download and install a source package
by setting the @code{type} argument.

On Windows @code{install.packages} can also install a binary package
from a local @file{zip} file by setting argument @code{repos} to
@code{NULL}.   @code{Rgui.exe} has a menu @code{Packages} with a GUI
interface to @code{install.packages}, @code{update.packages} and
@code{library}.

@command{R CMD INSTALL} works in Windows to install source packages if
you have set up the tools needed (see @ref{The Windows toolset}).
@command{R CMD INSTALL --help} will tell you the current options under
Windows (which differ from those on a Unix-alike).

We have seen permission problems after unpacking source packages on some
Vista/Windows 7/Server 2008 systems: these have been circumvented by
setting the environment variable @env{R_INSTALL_TAR} to @samp{tar.exe}.

If you have only a source package that is known to work with current
@R{} and just want a binary (32- or 64-bit) Windows build of it, you
could make use of the building service offered at
@uref{http://win-builder.r-project.org/}.

@node Mac OS X packages, Customizing package compilation, Windows packages, Installing packages
@subsection Mac OS X

On Mac OS X @code{install.packages} works as it does on other Unix-like
systems, but there are additional types @code{mac.binary*} (the default
in the @acronym{CRAN} distribution) that can be passed to
@code{install.packages} in order to download and install binary packages
from a suitable repository, and is the default for @acronym{CRAN} builds
of R.  These Macintosh binary package files have the extension
@samp{tgz}.  The @R{} GUI provides for installation of either binary or
source packages, from @acronym{CRAN} or local files.

Originally there was just package type @code{mac.binary}, and this was
used for `universal' binaries, that is those designed to run on multiple
CPU types (PowerPC and i386 at the time).  Later it became necessary to
use different binary distributions for different versions of Mac OS X,
and we now have @code{mac.binary.universal} (for Mac OS 10.4 aka
`Tiger', last built for @R{} 2.10.1) and @code{mac.binary.leopard} (for
Mac OS 10.5 and 10.6).

@node Customizing package compilation,  , Mac OS X packages, Installing packages
@subsection Customizing package compilation

The R system and package-specific compilation flags can be overridden or
added to by setting the appropriate Make variables in the personal file
@file{@var{HOME}/.R/Makevars-@var{R_PLATFORM}} (but
@file{@var{HOME}/.R/Makevars.win} on Windows), or if that does not
exist, @file{@var{HOME}/.R/Makevars}, where @samp{R_PLATFORM} is the
platform for which R was built, as available in the @code{platform}
component of the R variable @code{R.version}.

Package developers are encouraged to use this mechanism to enable a
reasonable amount of diagnostic messaging (``warnings'') when compiling,
such as e.g.@: @option{-Wall -pedantic} for tools from GCC, the Gnu
Compiler Collection.

Note that this mechanism can also be used when it necessary to change
the optimization level for a particular package.  For example

@example
## @r{for C code}
CFLAGS=-g -O
## @r{for C++ code}
CXXFLAGS=-g -O
## @r{for Fortran code}
FFLAGS=-g -O
## @r{for Fortran 95 code}
FCFLAGS=-g -O
@end example

@node Updating packages, Removing packages, Installing packages, Add-on packages
@section Updating packages
@findex update.packages
@cindex Packages, updating

The command @code{update.packages()} is the simplest way to ensure that
all the packages on your system are up to date.  Set the @code{repos}
argument as in the previous section.  The @code{update.packages()}
downloads the list of available packages and their current versions,
compares it with those installed and offers to fetch and install any
that have later versions on the repositories.

An alternative interface to keeping packages up-to-date is provided by
the command @code{packageStatus()}, which returns an object with
information on all installed packages and packages available at multiple
repositories.  The @code{print} and @code{summary} methods give an
overview of installed and available packages, the @code{upgrade} method
offers to fetch and install the latest versions of outdated packages.


@node Removing packages, Setting up a package repository, Updating packages, Add-on packages
@section Removing packages
@findex remove.packages
@cindex Packages, removing

Packages can be removed in a number of ways.  From a command prompt they
can be removed by

@example
R CMD REMOVE -l /path/to/library @var{pkg1} @var{pkg2} @dots{}
@end example

From a running @R{} process they can be removed by

@example
> remove.packages(c("pkg1", "pkg2"),
                  lib = file.path("path", "to", "library"))
@end example

Finally, in most installations one can just remove the package directory
from the library.


@node Setting up a package repository,  , Removing packages, Add-on packages
@section Setting up a package repository
@cindex Repositories

Utilities such as @code{install.packages} can be pointed at any
@acronym{CRAN}-style repository, and @R{} users may want to set up their
own.  The `base' of a repository is a URL such as
@uref{http://www.omegahat.org/R/}: this must be an URL scheme that
@code{download.packages} supports (which also includes @samp{ftp://} and
@samp{file://}).  Under that base URL there should be directory trees
for one or more of the following types of package distributions:

@itemize
@item 
@code{"source"}: located at @file{src/contrib} and containing
@file{.tar.gz} files.  As from @R{} 2.10.0 other forms of compression
can be used, e.g.@: @file{.tar.bz2} or @file{.tar.xz} files.

@item 
@code{"win.binary"}: located at @file{bin/windows/contrib/@var{x.y}} for
@R{} versions @var{x.y.z} and containing @file{.zip} files for 32-bit Windows

@item 
@code{"win64.binary"}: located at @file{bin/windows64/contrib/@var{x.y}} for
@R{} versions @var{x.y.z} and containing @file{.zip} files for 64-bit Windows.

@item 
@code{"mac.binary"}: located at
@file{bin/macosx/universal/contrib/@var{x.y}} for @R{} versions @var{x.y.z}
and containing @file{.tgz} files.  If the repository contains only
packages for a specific architecture, the package distribution type
can be set to @code{"mac.binary.xxx"} where @var{xxx} specifies the
architecture, replacing @code{universal} by @var{xxx} in the path
above.
@end itemize

Each terminal directory must also contain a @file{PACKAGES} file.  This
can be a concatenation of the @file{DESCRIPTION} files of the packages
separated by blank lines, but only a few of the fields are needed.  The
simplest way to set up such a file is to use function
@code{write_PACKAGES} in the @pkg{tools} package, and its help explains
which fields are needed.  Optionally there can also be a
@file{PACKAGES.gz} file, a @command{gzip}-compressed version of
@file{PACKAGES}---as this will be downloaded in preference to
@file{PACKAGES} it should be included for large repositories.

To add your repository to the list offered by @code{setRepositories()},
see the help file for that function.

A repository can contain subdirectories, when the descriptions in the
@file{PACKAGES} file of packages in subdirectories must include a line
of the form

@example
Path: @var{path/to/subdirectory}
@end example

@noindent
---once again @code{write_PACKAGES} is the simplest way to set this up.


@node Internationalization, Choosing between 32- and 64-bit builds, Add-on packages, Top
@chapter Internationalization and Localization
@cindex Internationalization
@cindex Localization

@emph{Internationalization} refers to the process of enabling support
for many human languages, and @emph{localization} to adapting to a
specific country and language.

Historically @R{} worked in the ISO Latin-1 8-bit character set and so
covered English and most Western European languages (if not necessarily
their currency symbols).  Since @R{} 2.1.0 it has supported (optionally
before @R{} 2.10.0) multi-byte character sets such as UTF-8 and others
used for Chinese, Japanese and Korean.

Current builds of @R{} support all the character sets that the
underlying OS can handle.  These are interpreted according to the
@cindex Locale
current @code{locale}, a sufficiently complicated topic to merit a
separate section.  Note though that @R{} has no built-in support for
right-to-left languages and bidirectional output, relying on the OS
services.  For example, how character vectors in UTF-8 containing both
English digits and Hebrew characters are printed is OS-dependent (and
perhaps locale-dependent).

The other aspect of the internationalization is support for the
translation of messages.  This is enabled in almost all builds of @R{}.

@menu
* Locales::                     
* Localization of messages::    
@end menu

@node Locales, Localization of messages, Internationalization, Internationalization
@section Locales
@cindex Locale

A @emph{locale} is a description of the local environment of the user,
including the preferred language, the encoding of characters, the
currency used and its conventions, and so on.  Aspects of the locale are
accessed by the @R{} functions @code{Sys.getlocale} and
@code{Sys.localeconv}.

The system of naming locales is OS-specific.  There is quite wide
agreement on schemes, but not on the details of their implementation.  A
locale needs to specify
@itemize
@item
A human language.  These are generally specified by a lower-case
two-character abbreviation following ISO 639 (see e.g.@:
@url{http://en.wikipedia.org/wiki/ISO_639-1}).

@item
A `territory', used mainly to specify the currency. These are generally
specified by an upper-case two-character abbreviation following ISO 3166
(see e.g.@: @url{http://@/en.wikipedia.org/wiki/ISO_3166}).  Sometimes
the combination of language and territory is used to specify the
encoding, for example to distinguish between traditional and simplified
Chinese.

@item
A charset encoding, which determines both how a byte stream should be
divided into characters, and which characters the subsequences of bytes
represent.

@item
Optionally, a modifier, for example to indicate that Austria is to be
considered pre- or post-Euro.
@end itemize

@R{} is principally concerned with the first (for translations) and
third.  Note that the charset may be deducible from the language, as
some OSes offer only one charset per language, and most OSes have only
one charset each for many languages.  Note too the remark above about
Chinese.

@menu
* Locales under Linux::         
* Locales under Windows::       
* Locales under Mac OS X::      
@end menu

@node Locales under Linux, Locales under Windows, Locales, Locales
@subsection Locales under Linux

Modern Linux uses the XPG locale specifications which have the form
@samp{en_GB}, @samp{en_GB.utf8}, @samp{aa_ER.utf8@@saaho},
@samp{de_AT.iso885915@@euro}, the components being in the order listed
above.  (See @command{man locale} and @command{locale -a} for more
details.)  Similar schemes (but often in different cases) are used by
most Unix-alikes: some use @samp{.UTF-8} rather than @samp{.utf8}.

@node Locales under Windows, Locales under Mac OS X, Locales under Linux, Locales
@subsection Locales under Windows

Windows also uses locales, but specified in a rather less concise way.
Most users will encounter locales only via drop-down menus, but more
information and lists can be found at
@uref{http://msdn.microsoft.com/@/library/@/default.asp?@/url=@//library/@/en-us/@/vccore98/@/html/@/_crt_language_and_country_strings.asp}.

It offers only one encoding per language.

@node Locales under Mac OS X,  , Locales under Windows, Locales
@subsection Locales under Mac OS X

Mac OS X supports locales in its own particular way, but the R GUI tries
to make this easier for users. See
@uref{http://developer.apple.com/@/documentation/@/MacOSX/@/Conceptual/@/BPInternational/}
for how users can set their locales.  As with Windows, end users will
generally only see lists of languages/territories.  Users of @R{} in a
terminal may need to set the locale to something like @samp{en_GB.UTF-8}
if it defaults to @samp{C}.

Internally Mac OS X uses a form similar to Linux but without specifying
the encoding (which is @code{UTF-8}).  It is based on ICU locales
(@uref{http://userguide.icu-project.org/locale}) and not POSIX ones.


@node Localization of messages,  , Locales, Internationalization
@section Localization of messages

The preferred language for messages is by default taken from the locale.
This can be overridden first by the setting of the environment variable
@enindex LANGUAGE
@enindex LC_ALL
@enindex LC_MESSAGES
@enindex LANG
@env{LANGUAGE} and then@footnote{On some systems setting
@env{LC_ALL} or @env{LC_MESSAGES} to @samp{C} disables @env{LANGUAGE}.}
by the environment variables @env{LC_ALL}, @env{LC_MESSAGES} and
@env{LANG}. (The last three are normally used to set the locale and so
should not be needed, but the first is only used to select the language
for messages.)  The code tries hard to map locales to languages, but on
some systems (notably Windows) the locale names needed for the
environment variable @env{LC_ALL} do not all correspond to XPG language
names and so @env{LANGUAGE} may need to be set.  (One example is
@samp{LC_ALL=es} on Windows which sets the locale to Estonian and the
language to Spanish.)

It is usually possible to change the language once @R{} is running
@emph{via} (not Windows) @code{Sys.setlocale("LC_MESSAGES",
"new_locale")}, or by setting an environment variable such as
@env{LANGUAGE}, @emph{provided}@footnote{If you try changing from French
to Russian except in a UTF-8 locale, you will find messages change to
English.} the language you are changing to can be output in the current
character set.

Messages are divided into @emph{domains}, and translations may be
available for some or all messages in a domain.  @R{} makes use of the
following domains.

@itemize
@item
Domain @code{R} for basic C-level error messages.

@item
Domain @code{R-@var{pkg}} for the R @code{stop}, @code{warning} and
@code{message} messages in each package, including @code{R-base} for the
@pkg{base} package.

@item
Domain @code{@var{pkg}} for the C-level messages in each package.

@item
Domain @code{RGui} for the menus etc of the R for Windows GUI front-end.

@end itemize

Dividing up the messages in this way allows @R{} to be extensible: as
packages are loaded, their message translation catalogues can be loaded
too.

Translations are looked for by domain according to the currently
specified language, as specifically as possible, so for example an
Austrian (@samp{de_AT}) translation catalogue will be used in preference
to a generic German one (@samp{de}) for an Austrian user.  However, if a
specific translation catalogue exists but does not contain a
translation, the less specific catalogues are consulted.  For example,
@R{} has catalogues for @samp{en_GB} that translate the Americanisms
(e.g., @samp{gray}) in the standard messages into English.  Two other
examples: there are catalogues for @samp{es}, which is Spanish as
written in Spain and these will by default also be used in
Spanish-speaking Latin American countries, and also for @samp{pt_BR},
which are used for Brazilian locales but not for locales specifying
Portugal.

Translations in the right language but the wrong charset be made use of
@enindex LANGUAGE
by on-the-fly re-encoding.  The @env{LANGUAGE} variable (only) can be a
colon-separated list, for example @samp{se:de}, giving a set of
languages in decreasing order of preference.  One special value is
@samp{en@@quot}, which can be used in a UTF-8 locale to have American
error messages with pairs of quotes translated to Unicode directional
quotes.

If no suitable translation catalogue is found or a particular message is
not translated in any suitable catalogue, `English'@footnote{with
Americanisms.} is used.

See @uref{http://developer.r-project.org/@/Translations.html} for how to
prepare and install translation catalogues.

@node Choosing between 32- and 64-bit builds, The standalone Rmath library, Internationalization, Top
@chapter Choosing between 32- and 64-bit builds

@menu
* Windows::                     
@end menu

Many current @acronym{CPU}s have both 32- and 64-bit sets of
instructions: this has long been true for UltraSparc and more recently
for MIPS, PPC and @cputype{x86_64} (sometimes known as @cputype{amd64}
and @cputype{Intel64}: all but a few low-power current @cputype{ix86}
CPUs support this chipset).  Many OSes running on such @acronym{CPU}s
offer the choice of building a 32-bit or a 64-bit version of R (and
details are given below under specific OSes).  For most a 32-bit version
is the default, but for some (e.g., @cputype{x86_64} Linux and Mac OS
10.6) 64-bit is.

All current versions of @R{} use 32-bit integers and
@acronym{IEC}@tie{}60559@footnote{also known as @acronym{IEC}@tie{}559
and @acronym{IEEE}@tie{}754} double-precision reals, and so compute to
the same precision@footnote{at least when storing quantities: the on-FPU
precision is allowed to vary} and with the same limits on the sizes of
numerical quantities.  The principal difference is in the size of the
pointers.

64-bit builds have both advantages and disadvantages:
@itemize
@item
The total virtual memory space made available to a 32-bit process is
limited by the pointer size to 4GB, and on most OSes to 3GB (or even
2GB).  The limits for 64-bit processes are much larger (e.g.@:
8--128TB).

@R{} allocates memory for large objects as needed, and removes any
unused ones at garbage collection.  When the sizes of objects become an
appreciable fraction of the address limit, fragmentation of the address
space becomes an issue and there may be no hole available that is the
size requested.  This can cause more frequent garbage collection or the
inability to allocate large objects.  As a guide, this will become an
issue with objects more than 10% of the size of the address space
(around 300Mb) or when the total size of objects in use is around one
third (around 1Gb).

@item
32-bit OSes by default limit file sizes to 2GB (and this may also apply
to 32-bit builds on 64-bit OSes).  This can often be worked around: and
@command{configure} selects suitable defines if this is possible.  (We
have also largely worked around that limit on 32-bit Windows.)  64-bit
builds have much larger limits.

@item
Because the pointers are larger, @R{}'s basic structures are larger.
This means that @R{} objects take more space and (usually) more time to
manipulate.  So 64-bit builds of @R{} will, all other things being
equal, run slower than 32-bit builds.  (On Sparc Solaris the difference
was 15-20%.)

@item
However, `other things' may not be equal.  In the specific case of
@cputype{x86_64} @emph{vs} @cputype{ix86}, the 64-bit CPU has features
(such as SSE2 instructions) which are guaranteed to be present but are
optional on the 32-bit CPU, and also has more general-purpose registers.
This means that on chips like Intel Core 2 Duo the vanilla 64-bit
version of @R{} is around 10% faster on both Linux and Mac OS X --- this
can be reduced by tuning the compilation to the chip.
@end itemize

So, for speed you may want to use a 32-bit build, but to handle large
datasets (and perhaps large files) a 64-bit build.  You can often build
both and install them in the same place: @xref{Sub-architectures}.

Even on 64-bit builds of @R{} there are limits on the size of @R{}
objects (see @code{help("Memory-limits")}, some of which stem from the
use of 32-bit integers (especially in FORTRAN code).  On all builds of
@R{}, the maximum length (number of elements) of a vector is
@math{2^{31}-1}, about 2 billion, and on 64-bit builds the size of a
block of memory allocated is limited to @math{2^{34}-1} bytes (8GB).  It
is anticipated these will be raised eventually but routine use of 8GB
objects is (when this was written in 2005) several years off.

@node Windows,  , Choosing between 32- and 64-bit builds, Choosing between 32- and 64-bit builds
@section Windows

Currently the main Windows build of @R{} from @acronym{CRAN} is a 32-bit
executable.  This runs happily on Windows x64, but is limited to an
address space of 4GB (see the `R for Windows FAQ' for details).  As from
@R{} 2.11.0 a 64-bit build is also available from @acronym{CRAN}, and a
different build has been available from commercial @R{} redistributors
since 2009.

@node The standalone Rmath library, Essential and useful other programs under Unix, Choosing between 32- and 64-bit builds, Top
@chapter The standalone Rmath library

The routines supporting the distribution and
special@footnote{e.g.@: Bessel, beta and gamma functions} functions in @R{}
and a few others are declared in C header file @file{Rmath.h}.  These
can be compiled into a standalone library for linking to other
applications.  (Note that they are not a separate library when @R{} is
built, and the standalone version differs in several ways.)

The makefiles and other sources needed are in directory
@file{src/nmath/standalone}, so the following instructions assume that
is the current working directory (in the build directory tree on Unix if
that is separate from the sources).

@file{Rmath.h} contains @samp{R_VERSION_STRING}, which is a character
string containing the current @R{} version, for example @code{"2.11.0"}.

There is full access to @R{}'s handling of @code{NaN}s, @code{Inf} and
@code{-Inf} via special versions of the macros and functions

@example
    ISNAN, R_FINITE, R_log, R_pow and R_pow_di
@end example

@noindent
and (extern) constants @code{R_PosInf}, @code{R_NegInf} and @code{NA_REAL}.

There is no support for @R{}'s notion of missing values, in particular
not for @code{NA_INTEGER} nor the distinction between @code{NA} and
@code{NaN} for doubles.

A little care is needed to use the random-number routines. You will
need to supply the uniform random number generator 

@example
    double unif_rand(void)
@end example

@noindent
or use the one supplied (and with a shared library or DLL you will
have to use the one supplied, which is the Marsaglia-multicarry with
an entry point

@example
    set_seed(unsigned int, unsigned int)
@end example

@noindent
to set its seeds).

The facilities to change the normal random number generator are
available through the constant N01_kind. This takes values
from the enumeration type

@example
typedef enum @{
    BUGGY_KINDERMAN_RAMAGE,
    AHRENS_DIETER,
    BOX_MULLER,
    USER_NORM,
    INVERSION,
    KINDERMAN_RAMAGE
@} N01type;
@end example

@noindent
(and @samp{USER_NORM} is not available).


@menu
* Unix standalone::             
* Windows standalone::          
@end menu

@node Unix standalone, Windows standalone, The standalone Rmath library, The standalone Rmath library
@section Unix

If @R{} has not already be made in the directory tree,
@command{configure} must be run as described in the main build
instructions.

Then

@example
make
@end example

@noindent
will make standalone libraries @file{libRmath.a} and @file{libRmath.so}:
@samp{make static} and @code{make shared} will create just one of them.

@strong{NB:} certain compilers are unable to do compile-time IEEE-754
arithmetic and so cannot compile @file{mlutils.c} and several other
files.  The known example is old versions of Sun's @code{cc} (e.g.@:
Forte 6 and 7).


To use the routines in your own C or C++ programs, include

@example
#define MATHLIB_STANDALONE
#include <Rmath.h>
@end example

@noindent
and link against @samp{-lRmath} (and @samp{-lm} if needed on your OS).
The example file @file{test.c} does nothing useful, but is provided to
test the process (via @command{make test}).  Note that you will probably
not be able to run it unless you add the directory containing
@enindex LD_LIBRARY_PATH
@file{libRmath.so} to the @env{LD_LIBRARY_PATH} environment variable.

The targets

@example
make install
make uninstall
@end example

@noindent
will (un)install the header @file{Rmath.h} and shared and static
@enindex DESTDIR
libraries (if built).  Both @code{prefix=} and @env{DESTDIR} are
supported, together with more precise control as described for the main
build.

@samp{make install} installs a file for @command{pkg-config} to use by
e.g.

@example
$(CC) `pkg-config --cflags libRmath` -c test.c
$(CC) `pkg-config --libs libRmath` test.o -o test
@end example

On some systems @samp{make install-strip} will install a stripped shared
library.



@node Windows standalone,  , Unix standalone, The standalone Rmath library
@section Windows

You need to set up almost all the tools to make @R{} and then run (in a
Unix-like shell)

@example
(cd ../../include; make -f Makefile.win config.h Rconfig.h Rmath.h)
make -f Makefile.win
@end example

@noindent
For @file{cmd.exe} use

@example
cd ../../include
make -f Makefile.win config.h Rconfig.h Rmath.h
cd ../nmath/standalone
make -f Makefile.win
@end example


This creates a static library @file{libRmath.a} and a DLL
@file{Rmath.dll}.  If you want an import library @file{libRmath.dll.a}
(you don't need one), use

@example
make -f Makefile.win shared implib
@end example

To use the routines in your own C or C++ programs using MinGW, include

@example
#define MATHLIB_STANDALONE
#include <Rmath.h>
@end example

@noindent
and link against @samp{-lRmath}.  This will use the first found of
@file{libRmath.dll.a}, @file{libRmath.a} and @file{Rmath.dll} in that
order, so the result depends on which files are present.  You should be
able to force static or dynamic linking  @emph{via}

@example
-Wl,-Bstatic -lRmath -Wl,dynamic
-Wl,-Bdynamic -lRmath
@end example

@noindent
or by linking to explicit files (as in the @samp{test} target in
@file{Makefile.win}: this makes two executables, @file{test.exe} which
is dynamically linked, and @code{test-static}, which is statically
linked).

It is possible to link to @file{Rmath.dll} using other compilers, either
directly or via an import library: if you make a MinGW import library as
above, you will create a file @file{Rmath.def} which can be used
(possibly after editing) to create an import library for other systems
such as Visual C++.

If you make use of dynamic linking you should use

@example
#define MATHLIB_STANDALONE
#define RMATH_DLL
#include <Rmath.h>
@end example

@noindent
to ensure that the constants like @code{NA_REAL} are linked correctly.
(Auto-import will probably work with MinGW, but it is better to be
sure. This is likely to also work with VC++, Borland and similar
compilers.)


@node Essential and useful other programs under Unix, Configuration on Unix, The standalone Rmath library, Top
@appendix Essential and useful other programs under Unix

This appendix gives details of programs you will need to build @R{} on
Unix-like platforms, or which will be used by @R{} if found by
@command{configure}.

Remember that some package management systems (such as @acronym{RPM} and
deb) make a distinction between the user version of a package and the
development version.  The latter usually has the same name but with the
extension @samp{-devel} or @samp{-dev}: you need both versions
installed.

@menu
* Essential programs and libraries::  
* Useful libraries and programs::  
* Linear algebra::              
@end menu

@node Essential programs and libraries, Useful libraries and programs, Essential and useful other programs under Unix, Essential and useful other programs under Unix
@section Essential programs and libraries

You need a means of compiling C and FORTRAN 77 (see @ref{Using
FORTRAN}).  Some add-on packages also need a C++ compiler.  Your C
compiler should be @acronym{IEC}@tie{}60059@footnote{also known as
@acronym{IEEE}@tie{}754}, POSIX 1003.1 and C99-compliant if at all
possible.  @R{} tries to choose suitable flags for the C compilers it
knows about, but you may have to set @code{CC} or @code{CFLAGS}
suitably.  For recent versions of @command{gcc} with @code{glibc} this
means including @option{-std=gnu99}@footnote{@option{-std=c99} excludes
POSIX functionality, but @file{config.h} will turn on all @acronym{GNU}
extensions to include the POSIX functionality.}.  If the compiler is
detected as @command{gcc}, @option{-std=gnu99} will be appended to
@code{CC} unless it conflicts with a setting of @code{CFLAGS}.  (Note
that options essential to run the compiler even for linking, such as
those to set the architecture, should be specified as part of @code{CC}
rather than of @code{CFLAGS}.)

Unless you do not want to view graphs on-screen you need @samp{X11}
installed, including its headers and client libraries. For recent Fedora
distributions it means (at least) @samp{libX11}, @samp{libX11-devel},
@samp{libXt} and @samp{libXt-devel}.  On Debian we recommend the
meta-package @samp{xorg-dev}.  If you really do not want these you will
need to explicitly configure @R{} without X11, using
@option{--with-x=no}.

The command-line editing (and command completion) depends on the
@code{readline} library available from any @acronym{GNU} mirror: version
4.2 or later is needed for all the features to be enabled.  Otherwise
you will need to configure with @option{--with-readline=no} (or
equivalent).

A suitably comprehensive @code{iconv} function is essential.  The @R{}
usage requires @code{iconv} to be able to translate between
@code{"latin1"} and @code{"UTF-8"}, to recognize @code{""} as the
current encoding and @code{"ASCII"}, and to translate to and from the
Unicode wide-character formats @code{"UCS-[24][BL]E"} --- this is true
for @code{glibc} but not of most commercial Unixes.  However, you can
make use of @acronym{GNU} @code{libiconv} (possibly as a plug-in
replacement: see @uref{http://www.gnu.org/@/software/@/libiconv/}).

The OS needs to have enough support@footnote{specifically, the C99
functionality of headers @file{wchar.h} and @file{wctype.h}, types
@code{wctans_t} and @code{mbstate_t} and functions @code{mbrtowc},
@code{mbstowcs}, @code{wcrtomb}, @code{wcscoll}, @code{wcstombs},
@code{wctrans}, @code{wctype}, and @code{iswctype}.} for wide-character
types: this is checked at configuration.

A @command{tar} program is needed to unpack the sources and packages
(including the recommended packages).  A version@footnote{Such as
@acronym{GNU} @command{tar} 1.15 or later, @command{bsdtar} (from
@uref{http://code.google.com/p/libarchive/}, as used by Mac OS 10.6) or
@command{tar} from the Heirloom Toolchest
(@uref{http://heirloom.sourceforge.net/tools.html}.} that can
automagically detect compressed archives is preferred for use with
@code{untar()}: the configure script looks for @command{gtar} and
@command{gnutar} before
@enindex TAR
@command{tar}: use environment variable @env{TAR} to override this.

There need to be suitable versions of the tools @command{grep},
@command{sed} and @command{tr}: the problems are usually with old AT&T
and BSD variants.  @command{configure} will try to find suitable
versions (including looking in @file{/usr/xpg4/bin} which is used on
some commercial Unixes).

You will not be able to build most of the manuals unless you have
@command{makeinfo} version 4.7 or later installed , and if not some of
the @HTML{} manuals will be linked to @acronym{CRAN}.  (Version 4.6 is
known to create incorrect @HTML{} files.)  To make DVI or PDF versions
of the manuals you will also need file @file{texinfo.tex} installed
(which is part of the @acronym{GNU} @pkg{texinfo} distribution but is
often made part of the @TeX{} package in re-distributions) as well as
@command{texi2dvi}.@footnote{@command{texi2dvi} is normally a shell
script.  Some versions, e.g.@: that from @pkg{texinfo} 4.13a, need to be
run under @command{bash} rather than a Bourne shell as on, say,
Solaris.}  Further, the versions of @command{texi2dvi} and
@file{texinfo.tex} need to be compatible: we have seen problems with
older @TeX{} distributions (TeXLive 2007 and MiKTeX 2.8) used with
@pkg{texinfo} 4.13.

@cindex Vignettes
The DVI and PDF documentation and building vignettes needs @command{tex}
and @command{latex}, or @command{pdftex} and @command{pdflatex}.  As
from @R{} 2.11.0 we require @LaTeX{} version @code{2005/12/01} or later
(for UTF-8 support).  Building PDF package manuals (including the @R{}
reference manual) and vignettes is sensitive to the version of the
@LaTeX{} package @pkg{hyperref} and we recommend that the @TeX{}
distribution used is keep up-to-date.


If you want to build from the R Subversion repository you need both
@command{makeinfo} and @command{pdflatex}.
@cindex Subversion

The essential programs should be in your @env{PATH} at the time
@command{configure} is run: this will capture the full paths.

@node Useful libraries and programs, Linear algebra, Essential programs and libraries, Essential and useful other programs under Unix
@section Useful libraries and programs

The ability to use translated messages makes use of @code{gettext} and
most likely needs @acronym{GNU} @code{gettext}: you do need this to work
with new translations, but otherwise the version contained in the R
sources will be used if no suitable external @code{gettext} is found.

The `modern' version of the @code{X11}, @code{jpeg()}, @code{png()} and
@code{tiff()} graphics devices uses the @code{cairo} and (optionally)
@code{Pango} libraries.  Cairo version 1.2.0 or later is required.
Pango needs to be at least version 1.10, and 1.12 is the earliest
version we have tested.  (For Fedora users we believe the
@code{pango-devel} RPM and its dependencies suffice.)  @R{} checks for
@command{pkg-config}, and uses that to check first that the
@samp{pangocairo} package is installed (and if not, @samp{cairo}) and if
additional flags are needed for the @samp{cairo-xlib} package, then if
suitable code can be compiled.  These tests will fail if
@command{pkg-config} is not installed, and are likely to fail if
@code{cairo} was built statically (unusual).  Most systems with
@code{Gtk+} 2.8 or later installed will have suitable libraries.  Mac OS
X comes with none of these libraries, but @code{cairo} support (without
@code{Pango}) has been added to the binary distribution:
@code{pkg-config} is still needed and can be installed from the sources.

For the best font experience with these devices you need suitable fonts
installed: Linux users will want the @code{urw-fonts} package.  Another
useful set of fonts is the `liberation' truetype fonts available at
@uref{https://www.redhat.com/@/promo/@/fonts/},@footnote{@code{ttf-liberation}
in Debian/Ubuntu.} which cover the Latin, Greek and Cyrillic alphabets
plus a fair range of signs.  These share metrics with Arial, Times New
Roman and Courier New, and contain fonts rather similar to the first two
(@uref{http://en.wikipedia.org/@/wiki/@/Liberation_fonts}).

The bitmapped graphics devices @code{jpeg()}, @code{png()} and
@code{tiff()} need the appropriate headers and libraries installed:
@code{jpeg} (version 6b or 7 or 8) or @code{libpng} (version 1.2.3 or
later, including 1.4.0) and @code{zlib} (version 1.1.3 or later) or
@code{libtiff} (any recent version of 3.x.y -- 3.8.2 and 3.9.1 have been
tested) respectively.

If you have them installed (including the appropriate headers and of
suitable versions), @code{zlib}, @code{libbz2} and PCRE will be used if
specified by @option{--with-system-zlib}, @option{--with-system-bzlib}
or @option{--with-system-pcre}: otherwise versions in the @R{} sources
will be compiled in.  As the latter suffice and are tested with @R{} you
should not need to change this.  In particular, the version of
@code{zlib} 1.2.3 in the @R{} sources has enhancements to work with
large file systems on 32-bit platforms.

@code{liblzma} from @code{xz-utils} version 4.999 or later will be used
if installed: the version in the @R{} sources can be selected instead by
configuring with @option{--with-system-xz=no}.

Use of the X11 clipboard selection requires the @code{Xmu} headers and
libraries.  These are normally part of an X11 installation (e.g.@: the
Debian meta-package @samp{xorg-dev}), but some distributions have split
this into smaller parts, so for example recent versions of Fedora
require the @samp{libXmu} and @samp{libXmu-devel} RPMs.

Some systems (notably Mac OS X and at least some FreeBSD systems) have
inadequate support for collation in multibyte locales.  It is possible
to replace the OS's collation support by that from ICU (International
Components for Unicode, @uref{http://site.icu-project.org/}), and this
provides much more precise control over collation on all systems.  ICU
is available as sources and as binary distributions for (at least) most
Linux distributions, Solaris 9/10, AIX and Windows, usually as
@code{libicu} or @code{icu4c}.  It will be used by default where
available (including on Mac OS X >= 10.4): should a broken version of
ICU be found this can be suppressed by @option{--without-ICU}.

Code developers will need Perl version 5.8.0 or later, available via
@uref{http://www.perl.com/CPAN/}.  This is used to package and check
packages, for @command{R CMD Rprof}@footnote{for which the @R{} function
@code{summmaryProf} is an alternative.} and the rarely used @command{R
CMD Sd2Rd}: it should be in your @env{PATH} when @command{configure} is
run.

@c The Perl scripts left are
@c src/scripts: Rprof Sd2Rd build check
@c tools: help2man.pl install-info.pl
@c src/gnuwin32/fixed/rwver.pl
@c src/gnuwin32/installer: JRins.pl WIXins.pl

The @code{bitmap} and @code{dev2bitmap} devices and also
@code{embedFonts()} use ghostscript
(@uref{http://www.cs.wisc.edu/@/~ghost}).  This should either be in your
path when the command is run, or its full path specified by the
environment variable @env{R_GSCMD} at that time.


@menu
* Tcl/Tk::                      
* Java support::                
@end menu

@node Tcl/Tk, Java support, Useful libraries and programs, Useful libraries and programs
@subsection Tcl/Tk

The @pkg{tcltk} package needs Tcl/Tk >= 8.3 installed: the sources are
available at @uref{http://@/www.tcl.tk/}.  To specify the locations of the
Tcl/Tk files you may need the configuration options

@table @option
@item --with-tcltk
use Tcl/Tk, or specify its library directory
@item --with-tcl-config=@var{TCL_CONFIG}
specify location of @file{tclConfig.sh}
@item --with-tk-config=@var{TK_CONFIG}
specify location of @file{tkConfig.sh}
@end table

@noindent
or use the configure variables @code{TCLTK_LIBS} and
@code{TCLTK_CPPFLAGS} to specify the flags needed for linking against
the Tcl and Tk libraries and for finding the @file{tcl.h} and
@file{tk.h} headers, respectively.  If you have both 32- and 64-bit
versions of Tcl/Tk installed, setting the paths to the correct config
files may be necessary to avoid confusion between them.

Versions of Tcl/Tk up to 8.5.8 have been tested (including most versions
of 8.4.x, but not recently).

@node Java support,  , Tcl/Tk, Useful libraries and programs
@subsection Java support

@command{configure} looks for Java support on the host system, and if it
finds it sets some settings which are useful for Java-using packages.
@enindex JAVA_HOME
@env{JAVA_HOME} can be set during the @command{configure} run to point
to a specific JRE/JDK.

Principal amongst these are setting some library paths to the Java
libraries and JVM, which are stored in environment variable
@enindex R_JAVA_LD_LIBRARY_PATH
@env{R_JAVA_LD_LIBRARY_PATH} in file @file{@var{R_HOME}/etc/ldpaths} (or
a sub-architecture-specific version).  A typical setting for
@cputype{x86_64} Linux is

@smallexample
JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre
R_JAVA_LD_LIBRARY_PATH=$@{JAVA_HOME@}/lib/amd64/server:$@{JAVA_HOME@}/lib/amd64
@end smallexample

Note that this unfortunately depends on the exact version of the JRE/JDK
installed, and so may need updating if the Java installation is
updated.  This can be done by running @code{R CMD javareconf}. The
script re-runs Java detection in a manner similar to that of the
@code{configure} script and updates settings in both @file{Makeconf} and
@file{@var{R_HOME}/etc/ldpaths}. See @code{R CMD javareconf --help} for
details.

Another alternative of overriding those setting is to  set
@enindex R_JAVA_LD_LIBRARY_PATH
@env{R_JAVA_LD_LIBRARY_PATH} (e.g.@: in @file{~/.Renviron}), or use
@file{/etc/ld.so.conf} to specify the Java runtime library paths to
the system. Other settings are recorded in @file{etc/Makeconf} (or a
sub-architecture-specific version), e.g.

@smallexample
JAVA = /usr/bin/java
JAVAC = /usr/bin/javac
JAVA_HOME = /usr/java/jdk1.5.0_06/jre
JAVA_LD_LIBRARY_PATH = $(JAVA_HOME)/lib/amd64/server:$(JAVA_HOME)/lib/amd64:\
$(JAVA_HOME)/../lib/amd64:/usr/local/lib64
JAVA_LIBS = -L$(JAVA_HOME)/lib/amd64/server -L$(JAVA_HOME)/lib/amd64 
            -L$(JAVA_HOME)/../lib/amd64 -L/usr/local/lib64 -ljvm
@end smallexample

@noindent
where @samp{JAVA_LIBS} contains flags necessary to link JNI programs.
Some of the above variables can be queried using @code{R CMD config}.


@node Linear algebra,  , Useful libraries and programs, Essential and useful other programs under Unix
@section Linear algebra
@cindex BLAS library

@menu
* BLAS::                        
* LAPACK::                      
* Caveats::                     
@end menu

@node BLAS, LAPACK, Linear algebra, Linear algebra
@subsection BLAS

The linear algebra routines in @R{} can make use of enhanced BLAS (Basic
Linear Algebra Subprograms,
@uref{http://www.netlib.org/@/blas/@/faq.html}) routines.  However,
these have to be explicitly requested at configure time: @R{} provides
an internal BLAS which is well-tested and will be adequate for most uses
of @R{}.

You can specify a particular BLAS library @emph{via} a value for the
configuration option @option{--with-blas} and not to use an external
BLAS library by @option{--without-blas} (the default).  If
@option{--with-blas} is given with no, its value is taken from the
@enindex BLAS_LIBS
environment variable @env{BLAS_LIBS}, set for example in
@file{config.site}.  If neither the option nor the environment variable
supply a value, a search is made for a suitable BLAS.  If the value is
not obviously a linker command (starting with a dash or giving the path
to a library), it is prefixed by @samp{-l}, so 

@example
--with-blas="foo"
@end example

@noindent
is an instruction to link against @samp{-lfoo} to find an external BLAS
(which needs to be found both at link time and run time).

The configure code checks that the external BLAS is complete (it must
include all double precision and double complex routines@footnote{unless
FORTRAN double complex is not supported on the platform}, as well as
@code{LSAME}), and appears to be usable.  However, an external BLAS has
to be usable from a shared object (so must contain position-independent
code), and that is not checked.

Some enhanced BLASes are compiler-system-specific (@code{libsunperf} on
Sun Sparc@footnote{Using the Sun Studio @command{cc} and @command{f95}
compilers}, @code{libessl} on IBM, @code{vecLib} on Mac OS X).  The
correct incantation for these is usually found @emph{via}
@option{--with-blas} with no value on the appropriate platforms.

Some of the external BLASes are multi-threaded.  One issue is that @R{}
profiling (which uses the @code{SIGPROF} signal) may cause problems, and
you may want to disable profiling if you use a multi-threaded BLAS.
Note that using a multi-threaded BLAS can result in taking more
@acronym{CPU} time and even more elapsed time (occasionally dramatically
so) than using a similar single-threaded BLAS.

Note that under Unix (but not under Windows) if @R{} is compiled against
a non-default BLAS and @option{--enable-BLAS-shlib} is @strong{not}
used, then all BLAS-using packages must also be.  So if @R{} is re-built
to use an enhanced BLAS then packages such as @pkg{quantreg} will need
to be re-installed.


@menu
* ATLAS::                       
* ACML::                        
* Goto BLAS::                   
* MKL::                         
* Shared BLAS::                 
@end menu

@node ATLAS, ACML, BLAS, BLAS
@subsubsection ATLAS

ATLAS (@uref{http://math-atlas.sourceforge.net/}) is a ``tuned'' BLAS
that runs on a wide range of Unix-alike platforms.  Unfortunately it is
usually built as a static library that on some platforms cannot be used
with shared libraries such as are used in @R{} packages.  Be careful
when using pre-built versions of ATLAS (they seem to work on
@cputype{ix86} platforms, but not on @cputype{x86_64} ones).

The usual way to specify ATLAS will be via

@example
--with-blas="-lf77blas -latlas"
@end example

@noindent
if the libraries are in the library path, otherwise by

@example
--with-blas="-L/path/to/ATLAS/libs -lf77blas -latlas"
@end example

@noindent
For systems with multiple processors it is possible to use a
multi-threaded version of ATLAS, by specifying

@example
--with-blas="-lptf77blas -lpthread -latlas"
@end example

Consult its file @file{INSTALL.txt} for how to build ATLAS with
position-independent code (at least on version 3.8.0 and later): that
file also describes how to build ATLAS as a shared library.

ATLAS can also be used on Windows: see @pxref{Getting the source files}
when building from source, and
@uref{http://CRAN.R-project.org/bin/windows/rw-FAQ.html, R Windows
@acronym{FAQ}} for adding pre-compiled support to binary versions.

@node ACML, Goto BLAS, ATLAS, BLAS
@subsubsection ACML

@c : there used to be Solaris 10, but it seems no more.
For @cputype{x86_64} and @cputype{i686} processors under Linux there is
the AMD Core Math Library (ACML) @uref{http://www.amd.com/@/acml}.  For
the @command{gcc} version we could use

@example
--with-blas="-lacml"
@end example

@noindent
if the appropriate library directory (such as
@enindex LD_LIBRARY_PATH
@file{/opt/acml4.4.0/gfortran64/lib}) is in the @env{LD_LIBRRARY_PATH}.
For other compilers, see the ACML documentation.  There is a
multithreaded Linux version of ACML available for recent versions of
@command{gfortran}.  To make use of this you will need something like

@example
--with-blas="-L/opt/acml4.4.0/gfortran64_mp/lib -lacml_mp"
@end example

See @pxref{Shared BLAS} for an alternative (and in many ways preferable)
way to use ACML.

@node Goto BLAS, MKL, ACML, BLAS
@subsubsection Goto BLAS

Dr Kazushige Goto has written another tuned BLAS which is available for
several processors and OSes.  The current version is known as GotoBLAS2.

This has been made available in several formats, but is currently
available only as source code.  For @emph{academic use only} (after
registering) it can be obtained via
@uref{http://www.tacc.utexas.edu/@/tacc-projects}.
Once this is built and installed, it can be used by configuring with

@example
--with-blas="-lgoto2"
@end example

See @pxref{Shared BLAS} for an alternative (and in many ways preferable)
way to use it.

Note that currently a multi-threaded Goto BLAS will be built by default
if and only if the building is on a multi-processor system (counting
multiple cores and hyperthreading), and at run time the default number
of threads is the number of @acronym{CPU}s detected.

@node MKL, Shared BLAS, Goto BLAS, BLAS
@subsubsection Intel MKL

For Intel processors under Linux, there is Intel's Math Kernel Library
(@uref{http://www.intel.com/@/software/@/products/@/mkl/}).  You are
strongly encouraged to read the MKL User's Guide, which is installed
with the library, before attempting to link to MKL.

Version 10 of MKL supports two linking models: the default model, which
is backward compatible with version 9 (see below), and the pure layered
model.  The layered model gives the user fine-grained control over four
different library layers: interface, threading, computation, and
run-time library support. Some examples of linking to MKL using this
layered model are given below. These examples are for GCC compilers on
@cputype{x86_64}.  The choice of interface layer is important on
@cputype{x86_64} since the Intel Fortran compiler returns complex values
differently from the @acronym{GNU} Fortran compiler. You must therefore
use the interface layer that matches your compiler (@code{mkl_intel*} or
@code{mkl_gf*}).

R can be linked to a sequential version of MKL by

@example
MKL_LIB_PATH=/opt/intel/mkl/10.0.3.020/lib/em64t/
export LD_LIBRARY_PATH=$MKL_LIB_PATH
MKL="-L$@{MKL_LIB_PATH@} -lmkl_gf_lp64 -lmkl_sequential -lmkl_lapack -lmkl_core"
./configure --with-blas="$MKL" --with-lapack
@end example

@noindent
The order of the libraries is important.  The option
@option{--with-lapack} is used since MKL contains a copy of LAPACK as
well as BLAS (@pxref{LAPACK}).

Threaded MKL may be used by replacing the line defining the variable @code{MKL} 
with

@example
MKL="-L$@{MKL_LIB_PATH@} -lmkl_gf_lp64 -lmkl_gnu_thread \
     -lmkl_lapack -lmkl_core -liomp5 -lpthread"
@end example

@noindent
The default number of threads will be chosen by the OpenMP* software,
but can be controlled by setting @code{OMP_NUM_THREADS} or
@code{MKL_NUM_THREADS}.

Static MKL may be used with

@example
MKL="   -L$@{MKL_LIB_PATH@}                               \
        -Wl,--start-group                               \
                $@{MKL_LIB_PATH@}/libmkl_gf_lp64.a        \
                $@{MKL_LIB_PATH@}/libmkl_gnu_thread.a     \
                $@{MKL_LIB_PATH@}/libmkl_core.a           \
        -Wl,--end-group                                 \
        -lgomp -lpthread"
@end example

@noindent
(Thanks to Ei-ji Nakama).

The default linking model, which is also used by version 9 of MKL, can
be used by

@example
--with-blas="-lmkl -lguide -lpthread"
@end example

@noindent
This is multi-threaded, but in version 9 the number of threads defaults
to 1.  It can be increased by setting @code{OMP_NUM_THREADS}.  (Thanks to
Andy Liaw for the information.)

@node Shared BLAS,  , MKL, BLAS
@subsubsection Shared BLAS

Note that the BLAS library will be used for many of the add-on packages
as well as for @R{} itself.  This means that it is better to use a
shared/dynamic BLAS library, as most of a static library will be
compiled into the @R{} executable and each BLAS-using package.

@R{} offers the option of compiling the BLAS into a dynamic library
@code{libRblas} stored in @file{@var{R_HOME}/lib} and linking both @R{}
itself and all the add-on packages against that library.

This is the default on all platforms except AIX unless an external BLAS
is specified and found: for the latter it can be used by specifying the
option @option{--enable-BLAS-shlib}, and it can always be disabled via
@option{--disable-BLAS-shlib}.

This has both advantages and disadvantages.

@itemize
@item
It saves space by having only a single copy of the BLAS routines, which
is helpful if there is an external static BLAS such as is standard for
ATLAS.

@item
There may be performance disadvantages in using a shared BLAS.  Probably
the most likely is when @R{}'s internal BLAS is used and @R{} is
@emph{not} built as a shared library, when it is possible to build the
BLAS into @file{R.bin} (and @file{libR.a}) without using
position-independent code.  However, experiments showed that in many
cases using a shared BLAS was as fast, provided high levels (e.g.,
@option{-O3}) of compiler optimization are used.

@item
It is easy to change the BLAS without making to re-install @R{} and all
the add-on packages, since all references to the BLAS go through
@code{libRblas}, and that can be replaced.  Note though that any dynamic
libraries the replacement links to will need to be found by the linker:
this may need the library path to be changed in
@file{@var{R_HOME}/etc/ldpaths}.
@end itemize

Another option to change the BLAS in use is to symlink a dynamic BLAS
library (such as ACML or Goto's) to @file{@var{R_HOME}/lib/libRblas.so}.
For example, just

@example
mv @var{R_HOME}/lib/libRblas.so @var{R_HOME}/lib/libRblas.so.keep
ln -s /opt/acml4.4.0/gfortran64_mp/lib/libacml_mp.so @var{R_HOME}/lib/libRblas.so
@end example

will change the BLAS in use to multithreaded ACML.  A similar link works
for recent versions of the Goto BLAS and perhaps for MKL (provided the
appropriate @file{lib} directory is in the run-time library path or
@command{ld.so} cache).


@node LAPACK, Caveats, BLAS, Linear algebra
@subsection LAPACK

@cindex LAPACK library
Provision is made for using an external LAPACK library, principally to
cope with BLAS libraries which contain a copy of LAPACK (such as
@code{libsunperf} on Solaris, @code{vecLib} on Mac OS X and ACML on
@cputype{ix86}/@cputype{x86_64} Linux and Solaris).  However, the likely
performance gains are thought to be small (and may be negative), and the
default is not to search for a suitable LAPACK library, and this is
definitely @strong{not} recommended.  You can specify a specific LAPACK
library or a search for a generic library by the configuration option
@option{--with-lapack}.  The default for @option{--with-lapack} is to
check the BLAS library and then look for an external library
@samp{-llapack}.  Sites searching for the fastest possible linear
algebra may want to build a LAPACK library using the ATLAS-optimized
subset of LAPACK.  To do so specify something like

@example
--with-lapack="-L/path/to/libs -llapack -lcblas"
@end example

@noindent
since the ATLAS subset of LAPACK depends on @code{libcblas}.  A value
for @option{--with-lapack} can be set @emph{via} the environment
variable
@enindex LAPACK_LIBS
@env{LAPACK_LIBS}, but this will only be used if @option{--with-lapack}
is specified (as the default value is @code{no}) and the BLAS library
does not contain LAPACK.

Since ACML contains a full LAPACK, if selected as the BLAS it can be
used as the LAPACK @emph{via} @option{--with-lapack}.

If you do use @option{--with-lapack}, be aware of potential problems
with bugs in the LAPACK 3.0 sources (or in the posted corrections to those
sources).  In particular, bugs in @code{DGEEV} and @code{DGESDD} have
resulted in error messages such as

@example
DGEBRD gave error code -10
@end example

@noindent
(seen with the Debian @samp{-llapack} which was current in late 2002,
Fedora Core 4 Extras @samp{-llapack} in September 2005 and 64-bit
@code{libsunperf} in Forte 7).  Other potential problems are incomplete
versions of the libraries: for example @code{libsunperf} from Sun Forte
6.x was missing the entry point for @code{DLANGE} and @code{vecLib} has
omitted the BLAS routine @code{LSAME}.  For problems compiling LAPACK
using recent versions of @code{gcc} on @cputype{ix86} Linux, see
@ref{New platforms}.

Please @strong{do} bear in mind that using @option{--with-lapack} is
`definitely @strong{not} recommended': it is provided @strong{only}
because it is necessary on some platforms.

@node Caveats,  , LAPACK, Linear algebra
@subsection Caveats

As with all libraries, you need to ensure that they and @R{} were
compiled with compatible compilers and flags.  For example, this has
meant that on Sun Sparc using the native compilers the flag
@option{-dalign} is needed so @code{libsunperf} can be used.

On some systems it is necessary that an external BLAS/LAPACK was built
with the same FORTRAN compiler used to build @R{}: known problems are
with @R{} built with @command{gfortran}, @pxref{Using gfortran}.

@node Configuration on Unix, Platform notes, Essential and useful other programs under Unix, Top
@appendix Configuration on Unix

@menu
* Configuration options::       
* Internationalization support::  
* Configuration variables::     
* Setting the shell::           
* Using make::                  
* Using FORTRAN::               
* Compile and load flags::      
@end menu

@node Configuration options, Internationalization support, Configuration on Unix, Configuration on Unix
@section Configuration options

@command{configure} has many options: running

@example
./configure --help
@end example

@noindent
will give a list.  Probably the most important ones not covered
elsewhere are (defaults in brackets)

@table @option
@item --with-x
use the X Window System [yes]
@item --x-includes=@var{DIR}
X include files are in @var{DIR}
@item --x-libraries=@var{DIR}
X library files are in @var{DIR}
@item --with-readline
use readline library (if available) [yes]
@item --enable-R-profiling
attempt to compile support for @code{Rprof()} [yes]
@item --enable-memory-profiling
attempt to compile support for @code{Rprofmem()} and @code{tracemem()} [no]
@item --enable-R-shlib
build @R{} as a shared/dynamic library [no]
@item --enable-BLAS-shlib
build the BLAS as a shared/dynamic library [yes, except on AIX]
@end table

@noindent
You can use @option{--without-foo} or @option{--disable-foo} for the
negatives.

You will want to use @option{--disable-R-profiling} if you are building
a profiled executable of @R{} (e.g.@: with @samp{-pg)}.

Flag @option{--enable-R-shlib} causes the make process to build @R{} as
a dynamic (shared) library, typically called @file{libR.so}, and link
the main @R{} executable @file{R.bin} against that library.  This can
only be done if all the code (including system libraries) can be
compiled into a dynamic library, and there may be a
performance@footnote{We have measured 15--20% on @cputype{i686} Linux
and around 10% on @cputype{x86_64} Linux.} penalty.  So you probably
only want this if you will be using an application which embeds @R{}.
Note that C code in packages installed on an @R{} system linked with
@option{--enable-R-shlib} is linked against the dynamic library and so
such packages cannot be used from an @R{} system built in the default
way.  Also, because packages are linked against @R{} they are on some
OSes also linked against the dynamic libraries @R{} itself is linked
against, and this can lead to symbol conflicts.

If you need to re-configure @R{} with different options you may need to run
@code{make clean} or even @code{make distclean} before doing so.


@node Internationalization support, Configuration variables, Configuration options, Configuration on Unix
@section Internationalization support

Translation of messages is supported via @acronym{GNU} @code{gettext}
unless disabled by the configure option @option{--disable-nls} or the
underlying OS has insufficiently standard C functions to support it.
The @code{configure} report will show @code{NLS} as one of the
`Additional capabilities' if support has been compiled in, and running
in an English locale (but not the @code{C} locale) will include

@example
  Natural language support but running in an English locale
@end example

@noindent
in the greeting on starting R.


@node Configuration variables, Setting the shell, Internationalization support, Configuration on Unix
@section Configuration variables

@findex configure
If you need or want to set certain configure variables to something
other than their default, you can do that by either editing the file
@file{config.site} (which documents many of the variables you might want
to set: others can be seen in file @file{etc/Renviron.in}) or on the
command line as

@example
./configure @var{VAR}=@var{value}
@end example

@noindent
If you are building in a directory different from the sources, there can
be copies of @file{config.site} in the source and the build directories,
and both will be read (in that order).  In addition, if there is a file
@file{~/.R/config} (or failing that@footnote{@file{~/.Rconf} was
supported (but undocumented) in versions of @R{} prior to 2.10.0.}
@code{~/.Rconfig}), it is read between the @file{config.site} files in
the source and the build directories.

There is also a general @command{autoconf} mechanism for
@file{config.site} files, which are read before any of those mentioned
in the previous paragraph.  This looks first at a file specified by the
@enindex CONFIG_SITE
environment variable @env{CONFIG_SITE}, and if not is set at files such
as @file{/usr/local/share/config.site} and
@file{/usr/local/etc/config.site} in the area (exemplified by
@file{/usr/local} where @R{}) would be installed.

These variables are @emph{precious}, implying that they do not have to
be exported to the environment, are kept in the cache even if not
specified on the command line and checked for consistency between two
configure runs (provided that caching is used), and are kept during
automatic reconfiguration as if having been passed as command line
arguments, even if no cache is used.

See the variable output section of @code{configure --help} for a list of
all these variables.

If you find you need to alter configure variables, it is worth noting
that some settings may be cached in the file @file{config.cache}, and it
is a good idea to remove that file (if it exists) before re-configuring.
Note that caching is turned @emph{off} by default: use the command line
option @option{--config-cache} (or @option{-C}) to enable caching.

@menu
* Setting paper size::          
* Setting the browsers::        
* Compilation flags::           
* Making manuals::              
@end menu

@node Setting paper size, Setting the browsers, Configuration variables, Configuration variables
@subsection Setting paper size
@enindex R_PAPERSIZE
One common variable to change is @env{R_PAPERSIZE}, which defaults to
@samp{a4}, not @samp{letter}.  (Valid values are @samp{a4},
@samp{letter}, @samp{legal} and @samp{executive}.)

This is used both when configuring @R{} to set the default, and when
running @R{} to override the default.  It is also used to set the
papersize when making DVI and PDF manuals.

The configure default will most often be @samp{a4} if @env{R_PAPERSIZE}
is unset.  (If the (Debian Linux) program @command{paperconf} is found
@enindex PAPERSIZE
or the environment variable @env{PAPERSIZE} is set, these are used to
produce the default.)

@node Setting the browsers, Compilation flags, Setting paper size, Configuration variables
@subsection Setting the browsers

@enindex R_BROWSER
Another precious variable is @env{R_BROWSER}, the default HTML browser, which
should take a value of an executable in the user's path or specify
a full path.

@enindex R_PDFVIEWER
Its counterpart for PDF files is  @env{R_PDFVIEWER}.

@node Compilation flags, Making manuals, Setting the browsers, Configuration variables
@subsection Compilation flags

If you have libraries and header files, e.g., for @acronym{GNU}
readline, in non-system directories, use the variables @code{LDFLAGS}
(for libraries, using @samp{-L} flags to be passed to the linker) and
@code{CPPFLAGS} (for header files, using @samp{-I} flags to be passed to
the C/C++ preprocessors), respectively, to specify these locations.
These default to @samp{-L/usr/local/lib} (@code{LDFLAGS},
@samp{-L/usr/local/lib64} on most 64-bit Linux OSes) and
@samp{-I/usr/local/include} (@code{CPPFLAGS}) to catch the most common
cases.  If libraries are still not found, then maybe your
compiler/linker does not support re-ordering of @option{-L} and
@option{-l} flags (this has been reported to be a problem on HP-UX with
the native @command{cc}).  In this case, use a different compiler (or a
front end shell script which does the re-ordering).

These flags can also be used to build a faster-running version of @R{}.
On most platforms using @command{gcc}, having @samp{-O3} in
@code{CFLAGS} produces worthwhile performance gains.  On systems using
the @acronym{GNU} linker (especially those using @R{} as a shared
library), it is likely that including @samp{-Wl,-O1} in @code{LDFLAGS}
is worthwhile, and on recent systems
@samp{'-Bdirect,--hash-style=both,-Wl,-O1'} is recommended at
@uref{http://lwn.net/@/Articles/@/192624/}.  Tuning compilation to a
specific @acronym{CPU} family (e.g.@: @samp{-mtune=native} for
@command{gcc}) can give worthwhile performance gains, especially on
older architectures such as @cputype{ix86}.

@node Making manuals,  , Compilation flags, Configuration variables
@subsection Making manuals

@enindex R_RD4DVI
@enindex R_RD4PDF
@enindex R_PAPERSIZE
The default settings for making the manuals are controlled by
@env{R_RD4PDF}, @env{R_RD4DVI} and @env{R_PAPERSIZE}.

@node Setting the shell, Using make, Configuration variables, Configuration on Unix
@section Setting the shell

By default the shell scripts such as @file{R} will be @samp{#!/bin/sh}
scripts (or using the @env{SHELL} chosen by @file{configure}).  This is
almost always satisfactory, but on a few systems @file{/bin/sh} is not a
Bourne shell or clone, and the shell to be used can be changed by
setting the configure variable @env{R_SHELL} to a suitable value (a full
path to a shell, e.g. @file{/usr/local/bin/bash}).

@node Using make, Using FORTRAN, Setting the shell, Configuration on Unix
@section Using make
@findex make

To compile @R{}, you will most likely find it easiest to use
@acronym{GNU} @command{make}, although the Sun @command{make} works on
Solaris.  The native @command{make} has been reported to fail on SGI
Irix 6.5 and Alpha/OSF1 (aka Tru64).

To build in a separate directory you need a @command{make} that uses the
@code{VPATH} variable, for example @acronym{GNU} @command{make}, or Sun
@command{make} on Solaris 2.7 or later.

@command{dmake} has also been used. e.g,@: on Solaris 10.

If you want to use a @command{make} by another name, for example if your
@acronym{GNU} @command{make} is called @samp{gmake}, you need to set the
variable @code{MAKE} at configure time, for example

@findex configure
@example
./configure MAKE=gmake
@end example

Note the comment in @ref{Installation} about using a parallel
@command{make}.

@node Using FORTRAN, Compile and load flags, Using make, Configuration on Unix
@section Using FORTRAN
@cindex FORTRAN

@menu
* Using gfortran::              
@end menu

To compile @R{}, you need a FORTRAN compiler.  The default
is to search for
@c F95_compilers in m4/R.m4:
@command{f95}, @command{fort}, @command{xlf95},
@command{ifort}, @command{ifc}, @command{efc}, @command{pgf95}
@command{lf95}, @command{gfortran}, @command{ftn}, @command{g95},
@c F90 compilers in m4/R.m4:
@command{f90}, @command{xlf90}, @command{pghpf}, @command{pgf90},
@command{epcf90},
@c F77 compilers in m4/R.m4:
@command{g77}, @command{f77}, @command{xlf}, @command{frt},
@command{pgf77}, @command{cf77}, @command{fort77}, @command{fl32},
@command{af77} (in that order)@footnote{On HP-UX @command{fort77} is the
@acronym{POSIX} compliant FORTRAN compiler, and comes after
@command{g77}.}, and use whichever is found first; if none is found,
@R{} cannot be compiled.
@c GCC_Fortran_compiler in m4/R.m4:
However, if @command{CC} is @command{gcc}, the matching FORTRAN compiler
(@command{g77} for @command{gcc}@tie{}3 and @code{gfortran} for
@command{gcc}@tie{}4) is used if available.

The search mechanism can be changed using the configure variable
@code{F77} which specifies the command that runs the FORTRAN 77
compiler.  If your FORTRAN compiler is in a non-standard location, you
@enindex PATH
should set the environment variable @env{PATH} accordingly before
running @command{configure}, or use the configure variable @code{F77} to
specify its full path.

If your FORTRAN libraries are in slightly peculiar places, you should
@enindex LD_LIBRARY_PATH
also look at @env{LD_LIBRARY_PATH} or your system's equivalent to make
sure that all libraries are on this path.

Note that only FORTRAN compilers which convert identifiers to lower case
are supported.

You must set whatever compilation flags (if any) are needed to ensure
that FORTRAN @code{integer} is equivalent to a C @code{int} pointer and
FORTRAN @code{double precision} is equivalent to a C @code{double}
pointer.  This is checked during the configuration process.

Some of the FORTRAN code makes use of @code{COMPLEX*16} variables, which
is a Fortran 90 extension.  This is checked for at configure
time@footnote{as well as its equivalence to the @code{Rcomplex}
structure defined in @file{R_ext/Complex.h}.}, but you may need to avoid
compiler flags@footnote{In particular, avoid @command{g77}'s
@option{-pedantic}, which gives confusing error messages.} asserting
FORTRAN 77 compliance.

@cindex BLAS library
For performance reasons@footnote{e.g., to use an optimized BLAS on
Sun/Sparc} you may want to choose a FORTRAN 90/95 compiler.

It is possible to use @command{f2c}, the FORTRAN-to-C converter
(@uref{http://www.netlib.org/@/f2c}), via a script.  (An example script
is given in @file{scripts/f77_f2c}: this can be customized by setting
@enindex F2C
@enindex F2CLIBS
@enindex CC
the environment variables @env{F2C}, @env{F2CLIBS}, @env{CC} and
@enindex CPP
@env{CPP}.)  You may need to ensure that the FORTRAN type @code{integer}
is translated to the C type @code{int}.  Normally @file{f2c.h} contains
@samp{typedef long int integer;}, which will work on a 32-bit platform
but not on a 64-bit platform.  If your compiler is not @command{gcc} you
@enindex FPICFLAGS
will need to set @env{FPICFLAGS} appropriately.


@node Using gfortran,  , Using FORTRAN, Using FORTRAN
@subsection Using gfortran

@command{gfortran} is the F95 compiler that is part of
@command{gcc}@tie{}4.@var{x}.@var{y}.  There were problems compiling
@R{} with the first release (@command{gcc}@tie{}4.0.0) and more with
pre-releases, but these are resolved in later versions.

On Linux @cputype{x86_64} systems there is an incompatibility in the
return conventions for double-complex functions between
@command{gfortran} and @command{g77} which results in the final example
in @code{example(eigen)} hanging or segfaulting under external BLASs
built under @command{g77}.  This should be detected by a
@command{configure} test.

The default @code{FFLAGS} chosen (by @command{autoconf}) for a
@acronym{GNU} FORTRAN compiler is @samp{-g -O2}.  This seems not to be
documented for @command{gfortran}, and has caused problems (segfaults
and infinite loops) on @cputype{x86_64} Linux (and the optimizer will be
shared with other platforms on that @acronym{CPU} type).  A maximum
optimization of @samp{-O} is recommended there.


@node Compile and load flags,  , Using FORTRAN, Configuration on Unix
@section Compile and load flags

A wide range of flags can be set in the file @file{config.site} or as
configure variables on the command line.  We have already mentioned

@table @code
@item CPPFLAGS
header file search directory (@option{-I}) and any other miscellaneous
options for the C and C++ preprocessors and compilers
@item LDFLAGS
path (@option{-L}), stripping (@option{-s}) and any other miscellaneous
options for the linker
@end table

@noindent
and others include

@table @code
@item CFLAGS
debugging and optimization flags, C
@item MAIN_CFLAGS
ditto, for compiling the main program
@item SHLIB_CFLAGS
for shared libraries
@item FFLAGS
debugging and optimization flags, FORTRAN
@item SAFE_FFLAGS
ditto for source files which need exact floating point behaviour
@item MAIN_FFLAGS
ditto, for compiling the main program
@item SHLIB_FFLAGS
for shared libraries
@item MAIN_LDFLAGS
additional flags for the main link
@item SHLIB_LDFLAGS
additional flags for linking the shared libraries
@item LIBnn
the primary library directory, @file{lib} or @file{lib64}
@item CPICFLAGS
special flags for compiling C code to be turned into a shared library
@item FPICFLAGS
special flags for compiling Fortran code to be turned into a shared library
@item CXXPICFLAGS
special flags for compiling C++ code to be turned into a shared library
@item FCPICFLAGS
special flags for compiling Fortran 95 code to be turned into a shared library
@item DEFS
defines to be used when compiling C code in R itself
@end table

@noindent
Library paths specified as @option{-L/lib/path} in @code{LDFLAGS} are
@enindex LD_LIBRARY_PATH
collected together and prepended to @env{LD_LIBRARY_PATH} (or your
system's equivalent), so there should be no need for @option{-R} or
@option{-rpath} flags.

Variables such as @env{CPICFLAGS} are determined where possible by
@command{configure}.  Some systems allows two types of PIC flags, for
example @samp{-fpic} and @samp{-fPIC}, and if they differ the first
allows only a limited number of symbols in a shared library.  Since @R{}
as a shared library has about 6200 symbols, if in doubt use the larger
version.

To compile a profiling version of @R{}, one might for example want to
use @samp{MAIN_CFLAGS=-pg}, @samp{MAIN_FFLAGS=-pg},
@samp{MAIN_LDFLAGS=-pg} on platforms where @samp{-pg} cannot be used
with position-independent code.

@strong{Beware}: it may be necessary to set @code{CFLAGS} and
@code{FFLAGS} in ways compatible with the libraries to be used: one
possible issue is the alignment of doubles, another is the way
structures are passed.

On some platforms @command{configure} will select additional flags for
@code{CFLAGS}, @code{CPPFLAGS}, @code{FFLAGS}, @code{CXXFLAGS} and
@code{LIBS} in @code{R_XTRA_CFLAGS} (and so on).  These are for options
which are always required, for example to force @acronym{IEC}@tie{}60559
compliance.


@node Platform notes, The Windows toolset, Configuration on Unix, Top
@appendix Platform notes

This section provides some notes on building @R{} on different Unix-like
platforms.  These notes are based on tests run on one or two systems in
each case with particular sets of compilers and support libraries.
Success in building @R{} depends on the proper installation and functioning
of support software; your results may differ if you have other versions
of compilers and support libraries.

Older versions of this manual (for @R{} < 2.10.0) contain notes on
platforms such as HP-UX, IRIX and Alpha/OSF1 for which we have had no
recent reports.

@menu
* X11 issues::                  
* Linux::                       
* FreeBSD::                     
* Mac OS X::                    
* Solaris::                     
* AIX::                         
* Cygwin::                      
* New platforms::               
@end menu

@node X11 issues, Linux, Platform notes, Platform notes
@section X11 issues

The @samp{X11()} graphics device is the one started automatically on
Unix-alikes when plotting.  As its name implies, it displays on a (local
or remote) X server, and relies on the services and in particular the
fonts provided by the X server.  So if you sometimes use @R{} at a
console and sometimes remotely from an X11 session running on a Windows
machine, you may have to setup the fonts differently for the two usages.

The `modern' version of the @samp{X11()} device is based on @samp{cairo}
graphics and uses @samp{fontconfig} to pick and render fonts.  This is
done on the server, and although there can be selection issues, they are
more amenable than the issues with @samp{X11()} discussed in the rest of
this section.

When X11 was designed, most displays were around 75dpi, whereas today
they are of the order of 100dpi or even higher.  If you find that X11()
is reporting@footnote{for example, @code{X11 font at size 14 could not
be loaded}.} missing font sizes, especially larger ones, it is likely
that you are not using scalable fonts and have not installed the 100dpi
versions of the X11 fonts.  The names and details differ by system, but
will likely have something like Fedora's
@example
xorg-x11-fonts-75dpi
xorg-x11-fonts-100dpi
xorg-x11-fonts-truetype
xorg-x11-fonts-Type1
xorg-x11-fonts-cyrillic
@end example

@noindent
and you need to ensure that the @samp{-100dpi} versions are installed
and on the X11 font path (check via @command{xset -q}).  The
@samp{X11()} device does try to set a pointsize and not a pixel size:
laptop users may find the default setting of 12 too large (although very
frequently laptop screens are set to a fictitious dpi to appear like a
scaled-down desktop screen).

More complicated problems can occur in non-Western-European locales, so
if you are using one, the first thing to check is that things work in
the @code{C} locale.  The likely issues are a failure to find any fonts
or glyphs being rendered incorrectly (often as a pair of ASCII
characters).  X11 works by being asked for a font specification and
coming up with its idea of a close match.  For text (as distinct from
the symbols used by plotmath), the specification is the first element of
the option @code{"X11fonts"} which defaults to

@example
"-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"
@end example

If you are using a single-byte encoding, for example ISO 8859-2 in
Eastern Europe or KOI8-R in Russian, use @command{xlsfonts} to find an
appropriate family of fonts in your encoding (the last field in the
listing).  If you find none, it is likely that you need to install
further font packages, such as @samp{xorg-x11-fonts-cyrillic} shown in
the listing above.

Multi-byte encodings (most commonly UTF-8) are even more complicated.
There are few fonts in @samp{iso10646-1}, the Unicode encoding, and they
only contain a subset of the available glyphs (and are often fixed-width
designed for use in terminals).  In such locales @emph{fontsets} are
used, made up of fonts encoded in other encodings.  If the locale you
are using has an entry in the @samp{XLC_LOCALE} directory (typically
@file{/usr/X11R6/lib/X11/locale}, it is likely that all you need to do
is to pick a suitable font specification that has fonts in the encodings
specified there.  If not, you may have to get hold of a suitable locale
entry for X11.  This may mean that, for example, Japanese text can be
displayed when running in @samp{ja_JP.utf8} but not when running in
@samp{en_GB.utf8} on the same machine (although on some systems many
UTF-8 X11 locales are aliased to @samp{en_US.utf8} which covers several
character sets, e.g.@: ISO 8859-1 (Western European), JISX0208 (Kanji),
KSC5601 (Korean), GB2312 (Chinese Han) and JISX0201 (Kana)).

On some systems scalable fonts are available covering a wide range of
glyphs.  One source is TrueType fonts, and these can provide high
coverage.  Another is Type 1 fonts: the URW set of Type 1 fonts provides
standard typefaces such as Helvetica with a larger coverage of Unicode
glyphs than the standard X11 bitmaps, including Cyrillic.  These are
generally not part of the default install, and the X server may need to
be configured to use them.  They might be under the X11 @file{fonts}
directory or elsewhere, for example,

@example
/usr/share/fonts/default/Type1
/usr/share/fonts/ja/TrueType
@end example


@node Linux, FreeBSD, X11 issues, Platform notes
@section Linux
@cindex Linux

Linux is the main development platform for @R{}, so compilation from the
sources is normally straightforward with the standard compilers.

Remember that some package management systems (such as @acronym{RPM} and
deb) make a distinction between the user version of a package and the
developer version.  The latter usually has the same name but with the
extension @samp{-devel} or @samp{-dev}: you need both versions
installed.  So please check the @code{configure} output to see if the
expected features are detected: if for example @samp{readline} is
missing add the developer package.  (On most systems you will also need
@samp{ncurses} and its developer package, although these should be
dependencies of the @samp{readline} package(s).)

When @R{} has been installed from a binary distribution there are
sometimes problems with missing components such as the FORTRAN
compiler.  Searching the @samp{R-help} archives will normally reveal
what is needed.

It seems that @cputype{ix86} Linux accepts non-PIC code in shared
libraries, but this is not necessarily so on other platforms, in
particular for 64-bit @acronym{CPU}s such as @cputype{x86_64}.  So care
can be needed with BLAS libraries and when building @R{} as a shared
library to ensure that position-independent code is used in any static
libraries (such as the Tcl/Tk libraries, @code{libpng}, @code{libjpeg}
and @code{zlib}) which might be linked against.  Fortunately these are
normally built as shared libraries with the exception of the ATLAS BLAS
libraries.

For platforms with both 64- and 32-bit support, it is likely that

@example
LDFLAGS="-L/usr/local/lib64 -L/usr/local/lib"
@end example

@noindent
is appropriate since most (but not all) software installs its 64-bit
libraries in @file{/usr/local/lib64}.  To build a 32-bit version of @R{}
on @cputype{x86_64} with Fedora 8 we used

@example
CC="gcc -m32"
CXX="g++ -m32"
F77="gfortran -m32"
FC=$@{F77@}
OBJC=$@{CC@}
LDFLAGS="-L/usr/local/lib"
LIBnn=lib
@end example

64-bit versions of Linux are built with support for files > 2Gb, and
32-bit versions will be if possible unless @option{--disable-largefile}
is specified.

There are known problems with several early versions of
@command{gcc}@tie{}4, but @command{gcc}@tie{}4.1.0 and later have been
tested extensively.

To build a 64-bit version of @R{} on @cputype{ppc64} (also known as
@cputype{powerpc64}) with @command{gcc}@tie{}4.1.1, Ei-ji Nakama used

@example
CC="gcc -m64"
CXX="gxx -m64"
F77="gfortran -m64"
FC="gfortran -m64"
CFLAGS="-mminimal-toc -fno-optimize-sibling-calls -g -O2"
FFLAGS="-mminimal-toc -fno-optimize-sibling-calls -g -O2"
@end example

@noindent
the additional flags being needed to resolve problems linking against
@file{libnmath.a} and when linking @R{} as a shared library.

@menu
* Intel compilers::             
* PGI compilers::               
* Sun Studio compilers::        
@end menu

@node Intel compilers, PGI compilers, Linux, Linux
@subsection Intel compilers

Intel compilers have been used under @cputype{ix86} and @cputype{x86_64}
Linux and @R{} contains code to set the FPU options suitably.  Brian
Ripley tried version 9.0 of the compilers for @cputype{ix86} on
Fedora Core 3 @emph{via}

@example
CC=icc
F77=ifort
CXX=icpc
ICC_LIBS=/opt/compilers/intel/cc/9.0/lib
IFC_LIBS=/opt/compilers/intel/fc/9.0/lib
LDFLAGS="-L$ICC_LIBS -L$IFC_LIBS -L/usr/local/lib"
SHLIB_CXXLD=icpc
@end example

@noindent
and adding optimization flags failed: at least @file{src/@/main/@/regex.c}
and @file{src/@/modules/@/lapack/@/dlamc.f} needed to be compiled without
optimization.  For @cputype{x86_64} on Fedora Core 5 he used

@example
CC=icc
CFLAGS="-g -O3 -wd188 -ip"
F77=ifort
FLAGS="-g -O3"
CXX=icpc
CXXFLAGS="-g -O3"
FC=ifort
FCFLAGS="-g -O3 -mp"
ICC_LIBS=/opt/compilers/intel/cce/9.1.039/lib
IFC_LIBS=/opt/compilers/intel/fce/9.1.033/lib
LDFLAGS="-L$ICC_LIBS -L$IFC_LIBS -L/usr/local/lib64"
SHLIB_CXXLD=icpc
@end example

@noindent
@command{configure} will add @samp{-c99} to @code{CC} for
C99-compliance.  This causes warnings with @code{icc} 10, so use
@code{CC="icc -std=c99"} there.  @R{} will add @samp{-mp} in
@code{R_XTRA_@{C,F,CXX@}FLAGS} to maintain correct
@acronym{IEC}@tie{}60559 arithmetic.  The flag @option{-wd188}
suppresses a large number of warnings about the enumeration type
@samp{Rboolean}.  Because the Intel C compiler sets @samp{__GNUC__}
without complete emulation of @command{gcc}, we suggest adding
@code{CPPFLAGS=-no-gcc}.

For some comments on building on an Itanium (@cputype{ia64}) Linux
system with @command{gcc} or the Intel compilers see
@uref{http://www.nakama.ne.jp/@/memo/@/ia64_linux/}.

Others have reported success with version 10.0.


@node PGI compilers, Sun Studio compilers, Intel compilers, Linux
@subsection PGI compilers

Jennifer Lai used the Portland Group compilers on @cputype{x86_64} to
build pre-2.2.0.  Updated versions of the settings she used are

@example
PG_HOME=/usr/pgi/linux86-64/6.0
CC=pgcc
CFLAGS="-g -O2 -Kieee"
CPPFLAGS="-I$PG_HOME/include -I$PG_HOME/include/CC"
F77=pgf77
FFLAGS="-g -O2 -Kieee"
CXX=pgCC
CXXFLAGS="-g -O2 -Kieee"
FC=pgf95
FCFLAGS="-g -O2 -Kieee"
SHLIB_CXXLDFLAGS=-shared
SHLIB_LDFLAGS=-shared
LDFLAGS="-L$PG_HOME/libso -L/usr/lib64"
@end example

@noindent
Note particularly the last, which is needed to ensure that a shared
version of @code{libc} is found.  The flag @option{-Kieee} ensures strict
compliance to IEC60659.  Also,
@uref{http://@/www.amd.com/@/us-en/@/assets/@/content_type/@/DownloadableAssets/@/dwamd_PGI_nov603.pdf}
suggests that @option{-pc64} may be desirable.

@node Sun Studio compilers,  , PGI compilers, Linux
@subsection Sun Studio compilers

Brian Ripley tested the Sun Studio 12 compilers
(@uref{http://developers.sun.com/@/sunstudio/@/index.jsp}) on
@cputype{x86_64} Linux with

@example
CC=suncc
CFLAGS="-xO5 -xc99 -xlibmil -nofstore"
CPICFLAGS=-Kpic
F77=sunf95
FFLAGS="-O5 -libmil -nofstore"
FPICFLAGS=-Kpic
CXX="sunCC -library=stlport4"
CXXFLAGS="-xO5 -xlibmil -nofstore -features=tmplrefstatic"
CXXPICFLAGS=-Kpic
FC=sunf95
FCFLAGS=$FFLAGS
FCPICFLAGS=-Kpic
LDFLAGS=-L/opt/sunstudio12.1/rtlibs/amd64
SHLIB_LDFLAGS=-shared
SHLIB_CXXLDFLAGS=-G
SHLIB_FCLDFLAGS=-G
SAFE_FFLAGS="-O5 -libmil"
@end example

@noindent
@option{-m64} could be added, but was the default.  Do not use
@option{-fast}: see the warnings under Solaris. (The C++ options are
also explained under Solaris.)

The resulting build of @R{} was not quite as fast as that built with
@command{gcc}@tie{}4.4.3 at @samp{-O3}.

Others have found on at least some versions of @cputype{i386} Linux that
the configure flag @option{--disable-largefile} was needed (since
@file{glob.h} on that platform presumed @command{gcc} was being used).

@node FreeBSD, Mac OS X, Linux, Platform notes
@section FreeBSD
@cindex FreeBSD

Rainer Hurling has reported success on @cputype{x86_64} FreeBSD 7.2 (and
on 6.x in the past).  Since Darwin (the base OS of Mac OS X) is based on
FreeBSD we find testing on Darwin tends to pick up any potential
problems on FreeBSD problems quite early.

The native BSD @command{make} suffices to build @R{} but a small number
of packages require @acronym{GNU} @command{make}.  (The BSD version is
@command{bsdmake} on Darwin.)

@node Mac OS X, Solaris, FreeBSD, Platform notes
@section Mac OS X
@cindex Mac OS X

You can build @R{} as a Unix application on Mac OS X using the Apple
Developer Tools (`Xcode') and @command{gfortran}.  You will also need to
install an X sub-system or configure with @option{--without-x}.  The X
window manager is part of the standard Mac OS X distribution since Mac
OS X version 10.3 (Panther), but it is typically not pre-installed prior
to 10.5 (Leopard).  You will also need @command{gfortran} and
@code{libreadline} (or configure with @option{--without-readline}).

For more information on how to find these tools please read the
@uref{http://CRAN.R-project.org/bin/macosx/RMacOSX-FAQ.html, R for Mac
OS X @acronym{FAQ}}.

@c If you use the X window manager and prefer @code{Terminal.app} to
@c @code{xterm}, you should be aware that @R{}, like many Unix tools, uses
@c @enindex DISPLAY
@c the existence of a @env{DISPLAY} environment variable to determine
@c whether an X system is running.  This affects the default graphics
@c device for the command-line version of R and the behaviour of the
@c @code{png} and @code{jpeg} devices.


@cindex BLAS library
@cindex LAPACK library
The @code{vecLib} library can be used @emph{via} the (default)
configuration options

@example
--with-blas="-framework vecLib" --with-lapack
@end example

@noindent
to provide higher-performance versions of the BLAS and LAPACK
routines.  Building @R{} without these options @emph{via}

@example
--without-blas --without-lapack
@end example

@noindent
can be done (and is provided as an alternative in the binary
distribution).

@menu
* 64-bit Leopard builds::       
* Snow Leopard::                
@end menu

@node 64-bit Leopard builds, Snow Leopard, Mac OS X, Mac OS X
@subsection 64-bit Leopard builds

64-bit builds are supported on 10.5.x (Leopard) and later.  All that is
needed is to select suitable compiler options, e.g.@: for recent Intel
Macs

@example
CC='gcc -arch x86_64'
CXX='g++ -arch x86_64'
F77='gfortran -arch x86_64'
FC='gfortran -arch x86_64'
OBJC='gcc -arch x86_64'
@end example

@noindent
in @file{config.site} or on the @command{configure} command line.  (Or
on Leopard specify @option{gcc-4.2} etc: those versions are the default
on Snow Leopard.)

@node Snow Leopard,  , 64-bit Leopard builds, Mac OS X
@subsection Snow Leopard

On Snow Leopard you will most likely to need to specify @option{-arch
i386} for a 32-bit build and @option{-arch x86_64} for a 64-bit build:
the Apple compilers default to 64-bit but the @command{gfortran}
supplied on @url{http://r.research.att.com} defaults to 32-bit.

Another quirk is that the X11 libraries are not in the default linking
path, so something like @samp{LIBS=-L/usr/X11/lib} may be required (or
you can use the @command{configure} options
@option{--x-includes=/usr/X11/include --x-libraries=/usr/X11/lib.}).

So for a 64-bit build of @R{} you need a file @file{config.site} in the
top-level build directory containing

@example
CC='gcc -arch x86_64'
CXX='g++ -arch x86_64'
F77='gfortran -arch x86_64'
FC='gfortran -arch x86_64'
OBJC='gcc -arch x86_64'
LIBS=-L/usr/X11/lib
@end example

@noindent
whereas for a 32-bit build it should have

@example
CC='gcc -arch i386'
CXX='g++ -arch i386'
F77='gfortran -arch i386'
FC='gfortran -arch i386'
OBJC='gcc -arch i386'
LIBS=-L/usr/X11/lib
@end example


@node Solaris, AIX, Mac OS X, Platform notes
@section Solaris
@cindex Solaris

@R{} has been built successfully on Solaris 10 (both Sparc and
@cputype{x86}) using the (zero cost) Sun Studio 12 compilers: there has
been some success with @command{gcc}@tie{}4/@command{gfortran}, mainly
on Sparc.  Sun packages for @R{} used to be available from
@uref{http://www.sunfreeware.com/} for both architectures, but have not
been updated recently.  (Recent Sun machines are Opterons
(@cputype{amd64}) rather than @cputype{x86}, but 32-bit @cputype{x86}
executables are the default.)

There are also reports of success on OpenSolaris (also known as Solaris
Express Community Edition, and sometimes as Solaris 11) on
@cputype{x86}.

The Solaris versions of several of the tools needed to build @R{}
(e.g.@: @command{make}, @command{ar} and @command{ld}) are in
@file{/usr/ccs/bin}, so if using those tools ensure this is in your
path.  Also, a version of the preferred @acronym{GNU} @command{tar} is
(if installed) in @file{/usr/sfw/bin}, as sometimes are tools like
@command{makeinfo}.

It is necessary to avoid the tools in @file{/usr/ucb}: in particular
@file{/usr/ucb/tr} causes @command{R CMD Rdiff} to malfunction.
@command{configure} looks for a POSIX-compliant @command{tr}, e.g. in
@file{/usr/xpg4/bin}.

Modern Solaris systems allow a large selection of Open Source software
to be installed from @url{http://www.opencsw.org} (formerly
@url{http://www.blastwave.org}) via @command{pkg-get}, by default
installed under @file{/opt/csw}.  You will need @acronym{GNU}
@code{libiconv} and @code{readline}: the Solaris version of @code{iconv}
is not sufficiently powerful.

The native @command{make} suffices to build @R{} but a small number
of packages require @acronym{GNU} @command{make}.

Some people have reported that the Solaris @code{libintl} needs to be
avoided, for example by using @option{--disable-nls} or
@option{--with-included-gettext} or using @code{libintl} from OpenCSW.

When using the Sun compilers@footnote{including @command{gcc} for Sparc
from Sun.} do @emph{not} specify @option{-fast}, as this disables
@acronym{IEEE} arithmetic and @command{make check} will fail.

For the Sun Studio compilers a little juggling of paths was needed to
ensure @acronym{GNU} @code{libiconv} is used rather than the Solaris
@code{iconv}: at one time we used

@example
CC="cc -xc99"
CPPFLAGS=-I/opt/csw/include
CFLAGS="-O -xlibmieee"
F77=f95
FFLAGS=-O4
CXX="CC -library=stlport"
CXXFLAGS=-O
FC=f95
FCFLAGS=$FFLAGS
LDFLAGS=-L/opt/csw/lib
@end example

@noindent
to ensure that the @code{libiconv} version of @file{iconv.h} was found,
and later used self-compiled versions of @code{libiconv} and
@code{readline} from @file{/usr/local} (replacing @file{/opt/csw} in the
flags).  For a 64-bit target add @option{-m64} to the compiler macros
and use something like @code{LDFLAGS=-L/opt/csw/lib/sparcv9} or
@code{LDFLAGS=-L/usr/local/lib/amd64} as appropriate.

By default the Sun Studio compilers do not by default conform to the C99
standard (appendix F 8.9) on the return values of functions such as
@code{log}: use @option{-xlibmieee} to ensure this.  Also, errors have
been reported on @cputype{amd64} if @option{-xc99} is omitted.

You can target specific Sparc architectures for (slightly) higher
performance: Sun recommend

@example
@r{32-bit:} -xtarget=ultra3 -xarch=v8plusa
@r{64-bit:} -xtarget=ultra3 -xarch=sparcvis2
@end example

@noindent
(in @code{CFLAGS} etc.) as a good compromise for recent Sparc chipsets.

On @cputype{x86} you will get marginally higher performance @emph{via}

@example
CFLAGS="-O5 -xc99 -xlibmieee -xlibmil -nofstore -xtarget=native"
FFLAGS="-O5 -libmil -nofstore -xtarget=native"
CXXFLAGS="-O5 -xlibmil -nofstore -xtarget=native"
SAFE_FFLAGS="-libmil -fstore -xtarget=native"
@end example

The Sun Studio compilers provide several implementations of the C++
standard which select both the set of headers and a C++ runtime library.
These are selected by the @option{-library} flag, which as it is needed
for both compiling and linking is best specified as part of the
compiler.  The examples above use @samp{stlport4}, currently the most
modern of the options: the default (but still needed to be specified as
it is needed for linking) is @samp{Cstd} which dates from 1998: see
@uref{http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html}.
Note though that most external Solaris C++ libraries will have been
built with @samp{Cstd} and so an @R{} package using such a package also
needs to be.  Occasionally the library flag @option{-lCrun} is needed.

Several CRAN packages using C++ need the more liberal interpretation
given by adding

@example
CXXFLAGS="-features=tmplrefstatic"
@end example


@cindex BLAS library
@cindex LAPACK library

The Sun performance library @code{libsunperf} is available for use with
the Sun Studio compilers.  If selected as a BLAS, it must also be
selected as LAPACK via

@example
./configure --with-blas=sunperf --with-lapack
@end example

@noindent
@c However, our tests were none too successful.  Sparc 32-bit builds
@c worked, but 64-bit Sparc builds failed in @code{example(eigen)},
@c and @cputype{x86} builds failed in @code{example(bw.SJ)}.
This has often given test failures in the past, in several different
places.  For Solaris 10 in early 2010 this worked with 32-bit Sparc and
32-bit Opteron, but failed on both 64-bit Sparc and Opteron in
@code{example(eigen)}.


@menu
* Using gcc::                   
@end menu

@node Using gcc,  , Solaris, Solaris
@subsection Using gcc

If using @command{gcc}, ensure that the compiler was compiled for the
version of Solaris in use.  (This can be ascertained from @command{gcc
-v}.)  @command{gcc} makes modified versions of some header files, and
several reports of problems were due to using @command{gcc} compiled on
one version of Solaris on a later version.  A version of @command{gcc}
optimized for Sparc (using technology from Sun's compilers) is available
from
Sun.@footnote{@uref{http://www.sun.com/download/index.jsp?@/cat=Application%20Development&tab=3&subcat=Development%20Tools}}

Compilation for a 32-bit Sparc target with @command{gcc}@tie{}4.3.3 needed

@example
CPPFLAGS=-I/opt/csw/include
LDFLAGS="-L/opt/csw/gcc4/lib -L/opt/csw/lib"
@end example

@noindent
and for a 64-bit Sparc target
@example
CC="gcc -m64"
F77="gfortran -m64"
CXX="g++ -m64"
FC=$F77
CPPFLAGS=-I/opt/csw/include
LDFLAGS="-L/opt/csw/gcc4/lib/sparcv9 -L/opt/csw/lib/sparcv9"
@end example

@noindent
Note that paths such as @file{/opt/csw/gcc4/lib/sparcv9} may need to
be in the
@enindex LD_LIBRARY_PATH
@env{LD_LIBRARY_PATH} during configuration.

Tests with @command{gcc}@footnote{specifically @command{gcc}@tie{}4.3.3
from OpenCSW.} on @cputype{x86} and @cputype{amd64} have been less
successful: @cputype{x86} builds have failed on tests using complex
arithmetic@footnote{such as those of @code{fft}: these can often be
resolved by turning off optimization}, whereas on @cputype{amd64} the
builds have failed to complete in several different ways, most recently
with relocation errors for @file{libRblas.so}.


@node AIX, Cygwin, Solaris, Platform notes
@section AIX
@cindex AIX

We no longer support AIX prior to 4.2, and @command{configure} will
throw an error on such systems.

Ei-ji Nakama was able to build under AIX 5.2 on @cputype{powerpc} with
GCC 4.0.3 in several configurations.  32-bit versions could be
configured with @option{--without-iconv} as well as
@option{--enable-R-shlib}.  For 64-bit versions he used

@example
OBJECT_MODE=64
CC="gcc -maix64"
CXX="g++ -maix64"
F77="gfortran -maix64"
FC="gfortran -maix64"
@end example

@noindent
and was also able to build with the IBM @code{xlc} and Hitachi
@code{f90} compilers by

@example
OBJECT_MODE=64
CC="xlc -q64"
CXX="g++ -maix64"
F77="f90 -cpu=pwr4 -hf77 -parallel=0 -i,L -O3 -64"
FC="f90 -cpu=pwr4 -hf77 -parallel=0 -i,L -O3 -64"
FLIBS="-L/opt/ofort90/lib -lhf90vecmath -lhf90math -lf90"
@end example

Some systems have @command{f95} as an IBM compiler that does not by
default accept FORTRAN 77.  It needs the flag @option{-qfixed=72}, or to
be invoked as @command{xlf_r}.
@c http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.xlf91a.doc/xlfug/hu00465.htm

The AIX native @code{iconv} does not support encodings @samp{latin1} nor
@samp{""} and so cannot be used.  (As far as we know @acronym{GNU}
@code{libiconv} could be installed.)

Fan Long reports success on AIX 5.3 using
@c https://stat.ethz.ch/pipermail/r-devel/2008-August/050318.html

@example
OBJECT_MODE=64
LIBICONV=@var{/where/libiconv/installed}
CC="xlc_r -q64" 
CFLAGS="-O -qstrict"
CXX="xlC_r -q64"
CXXFLAGS="-O -qstrict"
F77="xlf_r -q64"
AR="ar -X64"
CPPFLAGS="-I$LIBICONV/include -I/usr/lpp/X11/include/X11"
LDFLAGS="-L$LIBICONV/lib -L/usr/lib -L/usr/X11R6/lib"
@end example

@enindex R_SHELL
On one AIX 6.x system it was necessary to use @env{R_SHELL} to set the
default shell to be Bash rather than Zsh.

Kurt Hornik and Stefan Theussl at WU (Wirtschaftsuniversit@"at Wien)
successfully built R on a @cputype{powerpc} (8-@acronym{CPU} Power6
system) running AIX 6.1, configuring with or without
@option{--enable-R-shlib} (Ei-ji Nakama's support is gratefully
acknowledged).

It helps to describe the WU build environment first.  A small part of
the software needed to build R and/or install packages is available
directly from the AIX Installation DVDs, e.g., Java 6, X11, and Perl.
Additional open source software (OSS) is packaged for AIX in @file{.rpm}
files and available from both IBM's ``AIX Toolbox for Linux
Applications''
(@uref{http://www-03.ibm.com/systems/power/software/aix/linux/}) and
@uref{http://www.@/oss4aix.org/download/}.  The latter website typically
offers more recent versions of the available OSS.  All tools needed and
libraries downloaded from these repositories (e.g., GCC, Make,
@code{libreadline}, etc.) are typically installed to
@file{/opt/freeware}, hence corresponding executables are found in
@enindex PATH
@file{/opt/freeware/bin} which thus needs to be in @env{PATH} for using
these tools.  As on other Unix systems one needs @acronym{GNU}
@code{libiconv} as the AIX version of iconv is not sufficiently
powerful.  Additionally, for proper Unicode compatibility one should
install the corresponding package from the ICU project
(@uref{http://www.icu-project.org/download/}), which offers pre-compiled
binaries for various platforms which in case of AIX can be installed via
unpacking the tarball to the root file system.  For full @LaTeX{}
support one can install the @TeX{} Live DVD distribution
(@uref{http://www.tug.org/texlive/}): it is recommended to update the
distribution using the @code{tlmgr} update manager.  For 64-bit R builds
supporting Tcl/Tk this needs to installed from the sources as available
pre-compiled binaries supply only 32-bit shared objects.

The recent WU testing was done using compilers from both the
@acronym{GNU} Compiler Collection (version 4.2.4) which is available
from one of the above OSS repositories, and the IBM C/C++ (XL C/C++
10.01) as well as FORTRAN (XL Fortran 12.01) compilers
(@url{http://www14.software.ibm.com/webapp/download/byproduct.jsp#X}).

To compile for a 64-bit @cputype{powerpc} (Power6 @acronym{CPU}) target
one can use

@example
CC ="gcc -maix64 -pthread"
CXX="g++ -maix64 -pthread"
FC="gfortran -maix64 -pthread"
F77="gfortran -maix64 -pthread"
CFLAGS="-O2 -g -mcpu=power6"
FFLAGS="-O2 -g -mcpu=power6"
FCFLAGS="-O2 -g -mcpu=power6"
@end example

@noindent
for the GCC and

@example
CC=xlc
CXX=xlc++
FC=xlf
F77=xlf
CFLAGS="-qarch=auto -qcache=auto -qtune=auto -O3 -qstrict -ma"
FFLAGS="-qarch=auto -qcache=auto -qtune=auto -O3 -qstrict"
FCFLAGS="-qarch=auto -qcache=auto -qtune=auto -O3 -qstrict"
CXXFLAGS="-qarch=auto -qcache=auto -qtune=auto -O3 -qstrict"
@end example

@noindent
for the IBM XL compilers.  For the latter, it is important to note that
the decision for generating 32-bit or 64-bit code is done by setting the
@enindex OBJECT_MODE
@env{OBJECT_MODE} environment variable appropriately (recommended) or
using an additional compiler flag (@option{-q32} or @option{-q64}).  By
default the IBM XL compilers produce 32 bit code.  Thus, to build R with
64-bit support one needs to either export @env{OBJECT_MODE=64} in the
environment or, alternatively, use the @option{-q64} compiler options.

It is strongly recommended to install Bash and use it as the configure
shell, e.g., via setting @code{CONFIG_SHELL=/usr/bin/bash} in the
environment, and to use @acronym{GNU} Make (e.g., via
(@code{MAKE=/opt/freeware/bin/make}).

Further installation instructions to set up a proper R development
environment can be found in the ``R on AIX'' project on R-Forge
(@uref{http://R-Forge.R-project.org/projects/aix/}).

@node Cygwin, New platforms, AIX, Platform notes
@section Cygwin

The Cygwin emulation layer on Windows can be treated as a Unix-alike OS.
This is unsupported, but experiments have been conducted and a few
workarounds added for @R{} 2.6.0.  Currently (mid 2010) the build
produced with Cygwin's @command{gcc 4.3.4} fails to handles regular
expressions correctly.

Only building as a shared library works,@footnote{Windows DLLs need to
have all links resolved at build time and so cannot resolve against
@file{R.bin}.} so use e.g@:

@example
./configure --disable-nls --enable-R-shlib
make
@end example

@noindent
NLS does work if required, although adding
@option{--with-included-gettext} is preferable.  You will see many
warnings about the use of auto-import.

Note that this gives you a command-line application using @code{readline}
for command editing.  The @samp{X11} graphics device will work if a
suitable X server is running, and the standard Unix-alike ways of
installing source packages work.  There was a bug in the
@file{/usr/lib/tkConfig.sh} script in the version we looked at, which
needs to have

@example
TK_LIB_SPEC='-ltk84'
@end example

The overhead of using shell scripts makes this noticeably slower than a
native build of @R{} on Windows.


@node New platforms,  , Cygwin, Platform notes
@section New platforms

There are a number of sources of problems when installing @R {} on a new
hardware/OS platform.  These include

@strong{Floating Point Arithmetic}: @R{} requires arithmetic compliant
with @acronym{IEC}@tie{}60559, also know as @acronym{IEEE}@tie{}754.
This mandates the use of plus and minus infinity and @code{NaN} (not a
number) as well as specific details of rounding.  Although almost all
current FPUs can support this, selecting such support can be a pain.
The problem is that there is no agreement on how to set the signalling
behaviour; Sun/Sparc, SGI/IRIX and @cputype{ix86} Linux require no
special action, FreeBSD requires a call to (the macro)
@code{fpsetmask(0)} and OSF1 requires that computation be done with a
@option{-ieee_with_inexact} flag etc.  On a new platform you must find
out the magic recipe and add some code to make it work.  This can often
be done via the file @file{config.site} which resides in the top level
directory.

Beware of using high levels of optimization, at least initially.  On
many compilers these reduce the degree of compliance to the
@acronym{IEEE} model.  For example, using @option{-fast} on the Solaris
SunPro compilers causes @R{}'s @code{NaN} to be set incorrectly.

@strong{Shared Libraries}: There seems to be very little agreement
across platforms on what needs to be done to build shared libraries.
there are many different combinations of flags for the compilers and
loaders.  @acronym{GNU} libtool cannot be used (yet), as it currently
does not fully support FORTRAN: one would need a shell wrapper for
this).  The technique we use is to first interrogate the X window system
about what it does (using @command{xmkmf}), and then override this in
situations where we know better (for tools from the @acronym{GNU}
Compiler Collection and/or platforms we know about).  This typically
works, but you may have to manually override the results.  Scanning the
manual entries for @command{cc} and @command{ld} usually reveals the
correct incantation.  Once you know the recipe you can modify the file
@file{config.site} (following the instructions therein) so that the
build will use these options.

It seems that @command{gcc}@tie{}3.4.x and later on @cputype{ix86} Linux
defeat attempts by the LAPACK code to avoid computations entirely in
extended-precision registers, so file @file{src/modules/lapack/dlamc.f}
may need to be compiled without optimization.  Set the configure
variable @env{SAFE_FFLAGS} to the flags to be used for this file.  If
configure detects @acronym{GNU} FORTRAN it adds flag
@option{-ffloat-store} to @env{FFLAGS}.  (Other settings are needed when
using @command{icc} on @cputype{ix86} Linux, for example.)

If you do manage to get @R{} running on a new platform please let us
know about it so we can modify the configuration procedures to include
that platform.

If you are having trouble getting @R{} to work on your platform please
feel free to use the @samp{R-devel} mailing list to ask questions.  We
have had a fair amount of practice at porting @R{} to new platforms
@enddots{}


@node The Windows toolset, Function and variable index, Platform notes, Top
@appendix The Windows toolset

If you want to build @R{} or add-on packages from source in Windows, you
will need to collect, install and test an extensive set of tools.  See
@uref{http://www.murdoch-sutherland.com/@/Rtools/} for the current
locations and other updates to these instructions.  (Most Windows users
will not need to build add-on packages from source; see @ref{Add-on
packages} for details.)

Only building with MinGW @command{gcc 4.2.1} is supported.  It is
possible to build with the recently released MinGW @command{gcc
4.5.0}---beware that this by default uses DLLs for Fortran and C++
runtimes which need to be distributed with @R{} or packages built with
that compiler.@footnote{In our experiments, packages using C++ crash
frequently unless linked statically @emph{via} @code{-static-libstdc++}.}

We have found that the build process for @R{} is quite sensitive to
the choice of tools: please follow our instructions @strong{exactly},
even to the choice of particular versions of the tools.@footnote{For
example, the Cygwin version of @code{make 3.81} fails to work
correctly.}  The build process for add-on packages is somewhat more
forgiving, but we recommend using the exact toolset at first, and only
substituting other tools once you are familiar with the process.

@emph{This appendix contains a lot of prescriptive comments.  They are
here as a result of bitter experience.  Please do not report problems to
the @R{} mailing lists unless you have followed all the prescriptions.}

We have collected most of the necessary tools (unfortunately not all,
due to license or size limitations) into an executable installer named
@file{Rtools.exe}, available from
@uref{http://www.murdoch-sutherland.com/@/Rtools/}. You should download
and run it, choosing the default ``Package authoring installation'' to
build add-on packages, or the ``full installation'' if you intend to
build @R{}.

You will need the following items to build @R{} and packages.
See the subsections below for detailed descriptions.
@itemize
@item
Perl (in @file{Rtools.exe})
@c Current Perl scripts used on Windows are
@c src/scripts: Rprof Sd2Rd build check
@c src/gnuwin32/fixed/rwver.pl, used to make version.texi
@c src/gnuwin32/installer: JRins.pl WIXins.pl
@item
The command line tools (in @file{Rtools.exe})
@item
The MinGW compilers (for 32-bit, in @file{Rtools.exe})
@end itemize
For installing simple packages containing data or @R{} source
but no compiled code, none of these are needed.
Perl is essential to build @R{}, and needed to develop packages.

A complete build of @R{} including PDF manuals, and producing the
standalone installer @file{@value{RWVERSION}-win32.exe} will also need
the following:
@itemize
@item
@LaTeX{}
@item
The Inno Setup installer
@end itemize

@enindex PATH
It is important to set your @env{PATH} properly.  The @file{Rtools.exe}
optionally sets the path to components that it installs.

Your @env{PATH} may include @file{.} first, then the @file{bin}
directories of the tools, Perl, MinGW and @LaTeX{}.  Do not use
filepaths containing spaces: you can always use the short forms (found
by @code{dir /x} at the Windows command line).  Network shares (with
paths starting @code{\\}) are not supported.  For example, all on one
line,

@smallexample
PATH=c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;c:\texmf\miktex\bin;
     c:\R\bin;c:\windows;c:\windows\system32
@end smallexample

It is essential that the directory containing the command line tools
comes first or second in the path: there are typically like-named
tools in other directories, and they will @strong{not} work.  The
ordering of the other directories is less important, but if in doubt,
use the order above.

Our toolset contains copies of Cygwin DLLs that may conflict with other
ones on your system if both are in the path at once.  The normal
recommendation is to delete the older ones; however, at one time we
found our tools did not work with a newer version of the Cygwin DLLs, so
it may be safest not to have any other version of the Cygwin DLLs in your
path.

@menu
* Perl::                        
* LaTeX::                       
* The Inno Setup installer::    
* The command line tools::      
* The MinGW compilers::         
* Useful additional programs::  
@end menu

@node Perl, LaTeX, The Windows toolset, The Windows toolset
@section Perl

Perl is needed to build @R{} and to use @command{R CMD
build|check|Rprof}: it is no longer needed to install packages.

You will need a Windows port of @code{perl5} (but only the basic
functionality, not any of the third-party Win32 extensions).  The
Vanilla Perl (version 5.8.x) package is included in @file{Rtools.exe}. A
more full- featured distribution is available from
@uref{http://www.activestate.com/@/Products/@/ActivePerl/}: other
alternatives are listed at @uref{http://win32.perl.org/}.

@strong{Beware}: you do need a @emph{Windows} port and not the Cygwin
one.  Users of 64-bit Windows can use a Win64 Perl (such as that from
ActiveState) if they prefer.


@node LaTeX, The Inno Setup installer, Perl, The Windows toolset
@section @LaTeX{}

The @samp{MiKTeX} (@uref{http://www.miktex.org/}) distribution of
@LaTeX{} includes a suitable port of @code{pdftex}.  The `basic' version
of @samp{MiKTeX} almost suffices (the @pkg{grid} vignettes need
@file{fancyvrb.sty}), but it will install the 15Mb @samp{lm} package if
allowed to (although that is not actually used).  The @file{Rtools.exe}
installer does @emph{not} include any version of @LaTeX{}.

@enindex R_RD4DVI
@enindex R_RD4PDF
Please read @ref{Making the manuals} about how to make @file{refman.pdf}
and set the environment variables @env{R_RD4DVI} and @env{R_RD4PDF}
suitably; ensure you have the required fonts installed.

@node The Inno Setup installer, The command line tools, LaTeX, The Windows toolset
@section The Inno Setup installer

To make the installer package (@file{@value{RWVERSION}-win32.exe}) we
currently require Inno Setup 5.3.7 or later from
@uref{http://jrsoftware.org/}.  We use the Unicode version and recommend
that version be used.  This is @emph{not} included in @file{Rtools.exe}.
(Earlier versions can be used, but the set of languages will need to be
edited in @file{src/gnuwin32/installer/JRins.pl}, at least to change the
location of @file{Japanese.isl} or remove the reference to it.)

Copy file @file{src/gnuwin32/MkRules.dist} to
@file{src/gnuwin32/MkRules.local} and edit it to set @code{ISDIR} to the
location where Inno Setup was installed.

@node The command line tools, The MinGW compilers, The Inno Setup installer, The Windows toolset
@section The command line tools

This item is installed by the @file{Rtools.exe} installer.

@c date is used when building base and tools
@c find is used in building the installers
@c comm, sort, uniq are used in producing .def files
@c cmp is used in src/include/Makefile.win
@c cut is used to make RVER
@c gzip and tar used to be needed to unpack recommended packages
@c rsync is only needed if building from svn
@c tr is used by Rdiff
@c awk, egrep, head, rmdir, tail, wc are no longer used
If you choose to install these yourself, you will need suitable versions
of at least @code{basename}, @code{cat}, @code{cmp}, @code{comm},
@code{cp}, @code{cut}, @code{date}, @code{diff}, @code{echo},
@code{find}, @code{grep}, @code{gzip}, @code{ls}, @code{make},
@code{makeinfo}, @code{mkdir}, @code{mv}, @code{rm}, @code{rsync},
@code{sed}, @code{sh}, @code{sort}, @code{tar}, @code{texindex},
@code{touch}, @code{tr} and @code{uniq}; we use those from the Cygwin
distribution (@uref{http://www.cygwin.com/}) or compiled from the
sources.  You will also need @code{zip} and @code{unzip} from the
Info-ZIP project (@uref{http://www.info-zip.org/}).  All of these tools
are in @file{Rtools.exe}.

@strong{Beware}: `Native' ports of make are @strong{not} suitable
(including that called `MinGW make' at the MinGW SourceForge site).
There were also problems with several earlier versions of the Cygwin
tools and DLLs.  To avoid frustration, please use our tool set, and make
sure it is at the front of your path (including before the Windows
system directories).  If you are using a Windows shell, type @code{PATH}
at the prompt to find out.

@node The MinGW compilers, Useful additional programs, The command line tools, The Windows toolset
@section The MinGW compilers

This is the only step which differs between 32- and 64-bit builds.
Technically you need more than just a compiler so the set of tools is
referred to as a `toolchain'.

The 32-bit build for this version of @R{} is set up to use @command{gcc}
@code{4.2.1} for which MinGW compilers were released in August 2007.
The @file{Rtools.exe} installer currently includes the @code{-sjlj}
version of @code{4.2.1} of the MinGW port of @command{gcc} from
@uref{http://sourceforge.net/@/project/@/showfiles.php?@/group_id=2435}.

If you would like to install your own copy, we recommend downloading
from the URL above, as the download links from
@uref{http://www.mingw.org/} have led to obsolete versions. See the
notes on @uref{http://www.murdoch-sutherland.com/@/Rtools/} for updates.

See above for comments about the MinGW build of @code{gcc 4.5.0}.

For the 64-bit toolchain, see @ref{Building R for 64-bit Windows}.

@node Useful additional programs,  , The MinGW compilers, The Windows toolset
@section Useful additional programs

Developers of packages will find some of the `goodies' at
@uref{http://www.stats.ox.ac.uk/pub/Rtools/goodies} useful.  

There is a version of the @command{file} command that identifies the
type of files, and is used by @command{Rcmd check} if available.

The file @file{xzutils.zip} contains the program @command{xz} which can
be used to (de)compress files with that form of compression.

@node Function and variable index, Concept index, The Windows toolset, Top
@unnumbered Function and variable index

@printindex vr

@node Concept index, Environment variable index, Function and variable index, Top
@unnumbered Concept index

@printindex cp

@node Environment variable index,  , Concept index, Top
@unnumbered Environment variable index

@printindex en


@bye

@c Local Variables: ***
@c mode: TeXinfo ***
@c End: ***
