Development/Building LibreOffice with Clang

From The Document Foundation Wiki
Jump to navigation Jump to search

Mac works with clang by default.

On Linux systems add the following to your autogen.input:

CC=clang
CXX=clang++

ccache users will need to set the following e.g. in ~/.bashrc (ccache is used automatically assuming it is installed and configured correctly) -- autogen will warn you if this isn't set.

export CCACHE_CPP2=1

You should also install the clang headers (clang-devel or your distro's equivalent) for the clang loplugin to build. Autogen will warn you if these are missing.

For example on Debian, install these packages:

  • 'clang' for main clang programs
  • 'libclang-<number>-dev' (number corresponding to clang version installed) for clang headers
  • 'llvm' mandatory to build compiler-plugins, if not you'll have error message 'Cannot find Clang headers to build compiler plugins'

openSuse 12.3 is known to work without any further setup (assuming you have all official updates), read the rest of this page to ensure you have the correct dependencies when working with other linux systems.

Building LibreOffice with Clang

It is possible to build LibreOffice using the Clang compiler instead of the GCC compiler.

Bootstraping clang on Linux

See this article how to build clang on linux: https://btorpey.github.io/blog/2015/01/02/building-clang/

Using libc++/libc++abi with clang on Linux

Building LO against libc++ with CXX=clang++ -stdlib=libc++ is know to work from 5.2 on. The previous issues were resolved in https://bugs.documentfoundation.org/show_bug.cgi?id=97712

Setup

  • LibreOffice - Version 3.6 has been tested to build and work without problems on Linux. See below for macOS details. Older LibreOffice versions may or may not build and work.
  • Clang - version 3.1+ works. Older versions may not work.
  • Some libraries may need to be upgraded, mostly because of Clang's stricter/earlier implementation of C++11.
    • Libstdc++ 4.6 is known to work. Other libstdc++ versions may need patching (see Clang C++11 status page for information and patches).
    • Boost 1.47 and older may need patching (see this page). Internal copy of Boost shipped with LibreOffice builds fine.
    • NSS may need a patch or upgrade. Internal copy of NSS shipped with LibreOffice builds fine.
    • Qt4 may need a patch or upgrade. See Qt bugreport for a patch.
  • Ccache [1] works. At least version 3.1.10 (not yet released as of January 8th 2013) is needed when using compiler plugins (--enable-dbgutil or --enable-compiler-plugins), 3.1.9 needs a patch, older versions need also another patch). Older versions (version 3.1.7, possibly older) work when not using plugins.

Use

export CCACHE_CPP2=1

otherwise Clang will report preprocessed sources in error messages, print some warnings in header files it wouldn't print otherwise, and print warnings about unused options -D and -I.

  • Icecream [2] needs to be at least version 1.0
  • On recent Fedora 17, the included Clang (3.0) is unusable due to clang++ chokes on <complex>. However, a home-built Clang 3.1 works fine.

Building

Configuring LibreOffice is done normally, except that Clang is used as the compiler:

$ ./autogen.sh CC=clang CXX=clang++  ... plus the usual configure options, see Development/BuildingOnLinux

Otherwise the build should work just like when building with GCC.

If you have done previous builds with gcc, sometimes it's necessary to run:

$ make distclean

Problems

Although Clang can be mostly used as a drop-in replacement for GCC, it is an entirely different compiler, so there may be Clang-specific problems. In general it is however normally usable.

There should be also a tinderbox using Clang as the compiler [3], so if there is a problem, it might be useful to check its status.

macOS

Setup

XCode 4 has been installed on the machine and using gcc-4.2 (non-LLVM) the configuration will do a successfull LibreOffice build, which will also launch correctly. Make sure any 64-bit binaries in the PATH (e.g. pkg-config) are hidden. If you happen to have the Mono Framework installed, hide /usr/bin/pkg-config.

For the clang build, the following worked:

 CC="clang -m32" CXX="clang++ -m32" ./autogen.sh --with-num-cpus=8 --with-max-jobs=8 --prefix=/source/libo/prefix --with-macOS-sdk=10.6 --with-macOS-version-max-allowed=10.6 --with-macOS-version-min-required=10.5 --disable-mozilla --without-help --without-doxygen --without-myspell-dicts

Example setup

One scenario that at least works for me (Stephan Bergmann) is the below script, slightly updated from “Building on macOS 10.7”:

#!/bin/bash
set -ex -o pipefail

# On latest macOS 10.7.4 with latest Xcode 4.3.2 (incl. Command Line Tools):

mkdir "${HOME?}"/Software/lo

svn checkout https://svn.macports.org/repository/macports/trunk \
 "${HOME?}"/Software/lo/mports-src
 # head revision 93808 as of 2012-05-31 19:00:20 +0000
 # "https" instead of "http" fails to work non-interactively due to:
 #   Error validating server certificate for 'https://svn.macports.org:443':
 #    - The certificate is not issued by a trusted authority. Use the
 #      fingerprint to validate the certificate manually!
 #   Certificate information:
 #    - Hostname: *.macports.org
 #    - Valid: from Tue, 22 Feb 2011 17:29:43 GMT until Tue, 18 Mar 2014
 #      23:36:56 GMT
 #    - Issuer: 07969287, http://certificates.godaddy.com/repository,
 #      GoDaddy.com, Inc., Scottsdale, Arizona, US
 #    - Fingerprint:
 #      4d:ea:4a:77:55:ac:8e:2e:9e:11:8a:59:3d:ec:c7:45:7d:b0:72:19
 #   (R)eject, accept (t)emporarily or accept (p)ermanently?
(cd "${HOME?}"/Software/lo/mports-src/base && ./configure \
 --prefix="${HOME?}"/Software/lo/mports --with-no-root-privileges \
 --with-applications-dir="${HOME?}"/Software/lo/mports-apps \
 --with-tclpackage="${HOME?}"/Software/lo/mports-tcl && make && make install) \
 || exit 1
"${HOME?}"/Software/lo/mports/bin/port selfupdate
"${HOME?}"/Software/lo/mports/bin/port install libidl +universal
"${HOME?}"/Software/lo/mports/bin/port install automake doxygen grep pkgconfig

mkdir "${HOME?}"/Software/lo/bin
ln -s "${HOME?}"/Software/lo/mports/bin/aclocal "${HOME?}"/Software/lo/bin/
ln -s "${HOME?}"/Software/lo/mports/bin/autoconf "${HOME?}"/Software/lo/bin/
ln -s "${HOME?}"/Software/lo/mports/bin/autom4te "${HOME?}"/Software/lo/bin/
ln -s "${HOME?}"/Software/lo/mports/bin/doxygen "${HOME?}"/Software/lo/bin/

mkdir "${HOME?}"/Software/lo/pkgconfig
ln -s "${HOME?}"/Software/lo/mports/lib/pkgconfig/glib-2.0.pc \
 "${HOME?}"/Software/lo/pkgconfig/
ln -s "${HOME?}"/Software/lo/mports/lib/pkgconfig/libIDL-2.0.pc \
 "${HOME?}"/Software/lo/pkgconfig/

git clone ssh://git.freedesktop.org/git/libreoffice/core \
 "${HOME?}"/Software/lo/core

# moz/unxmacxi/misc/build/mozilla/configure:20225 fails with "egrep: Regular
# expression too big" with default /usr/bin/egrep ("egrep (GNU grep) 2.5.1"),
# and adding this replacement to "${HOME?}"/Software/lo/bin (which is added to
# PATH) does not help, as config_host.mk contains a rewritten PATH (where
# "${HOME?}"/Software/lo/bin would always go after /usr/bin):
ln -s "${HOME?}"/Software/lo/mports/bin/egrep \
 "${HOME?}"/Software/lo/core/solenv/bin/

# moz/unxmacxi/misc/build/mozilla/gfx/src/mac/nsRegionPool.cpp uses NewRgn,
# DisposeRgn, etc. that got removed in 10.7 SDK:
cat >"${HOME?}"/Software/lo/core/autogen.input <<\EOF
--disable-ccache
--disable-linkoo
--disable-zenity
--enable-binfilter
--enable-dbgutil
--enable-epm
--enable-mozilla
--enable-python=internal
--enable-werror
--with-lang=de en-US
--with-macOS-sdk=10.6
--with-macOS-version-min-required=10.6
--with-max-jobs=2
--with-num-cpus=2
CC=clang -m32
CXX=clang++ -m32
EOF

ulimit -c unlimited

cd "${HOME?}"/Software/lo/core
printf '%s' "${HOME?}" | grep -Fqv : || exit 1
PATH=${PATH?}:${HOME?}/Software/lo/bin \
 PKG_CONFIG=${HOME?}/Software/lo/mports/bin/pkg-config \
 PKG_CONFIG_LIBDIR=${HOME?}/Software/lo/pkgconfig make check