Blame docs/source/installation.rst

Packit ea1746
.. _chapter-installation:
Packit ea1746
Packit ea1746
============
Packit ea1746
Installation
Packit ea1746
============
Packit ea1746
Packit ea1746
Getting the source code
Packit ea1746
=======================
Packit ea1746
.. _section-source:
Packit ea1746
Packit ea1746
You can start with the `latest stable release
Packit ea1746
<http://ceres-solver.org/ceres-solver-1.13.0.tar.gz>`_ . Or if you want
Packit ea1746
the latest version, you can clone the git repository
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
       git clone https://ceres-solver.googlesource.com/ceres-solver
Packit ea1746
Packit ea1746
.. _section-dependencies:
Packit ea1746
Packit ea1746
Dependencies
Packit ea1746
============
Packit ea1746
Packit ea1746
Ceres relies on a number of open source libraries, some of which are
Packit ea1746
optional. For details on customizing the build process, see
Packit ea1746
:ref:`section-customizing` .
Packit ea1746
Packit ea1746
- `Eigen <http://eigen.tuxfamily.org/index.php?title=Main_Page>`_
Packit ea1746
  3.2.2 or later **strongly** recommended, 3.1.0 or later **required**.
Packit ea1746
Packit ea1746
  .. NOTE ::
Packit ea1746
Packit ea1746
    Ceres can also use Eigen as a sparse linear algebra
Packit ea1746
    library. Please see the documentation for ``EIGENSPARSE`` for
Packit ea1746
    more details.
Packit ea1746
Packit ea1746
- `CMake <http://www.cmake.org>`_ 2.8.0 or later.
Packit ea1746
  **Required on all platforms except for Android.**
Packit ea1746
Packit ea1746
- `glog <https://github.com/google/glog>`_ 0.3.1 or
Packit ea1746
  later. **Recommended**
Packit ea1746
Packit ea1746
  ``glog`` is used extensively throughout Ceres for logging detailed
Packit ea1746
  information about memory allocations and time consumed in various
Packit ea1746
  parts of the solve, internal error conditions etc. The Ceres
Packit ea1746
  developers use it extensively to observe and analyze Ceres's
Packit ea1746
  performance. `glog <https://github.com/google/glog>`_ allows you to
Packit ea1746
  control its behaviour from the command line. Starting with
Packit ea1746
  ``-logtostderr`` you can add ``-v=N`` for increasing values of ``N``
Packit ea1746
  to get more and more verbose and detailed information about Ceres
Packit ea1746
  internals.
Packit ea1746
Packit ea1746
  Unfortunately, the current version of `google-glog
Packit ea1746
  <https://github.com/google/glog>`_ does not build using the Android
Packit ea1746
  NDK. So, Ceres also ships with a minimal replacement of ``glog``
Packit ea1746
  called ``miniglog`` that can be enabled with the ``MINIGLOG`` build
Packit ea1746
  option.
Packit ea1746
Packit ea1746
  So, in an attempt to reduce dependencies, it is tempting to use
Packit ea1746
  `miniglog` on platforms other than Android. While there is nothing
Packit ea1746
  preventing the user from doing so, we strongly recommend against
Packit ea1746
  it. ``miniglog`` has worse performance than ``glog`` and is much
Packit ea1746
  harder to control and use.
Packit ea1746
Packit ea1746
  .. NOTE ::
Packit ea1746
Packit ea1746
     If you are compiling ``glog`` from source, please note that
Packit ea1746
     currently, the unit tests for ``glog`` (which are enabled by
Packit ea1746
     default) do not compile against a default build of ``gflags`` 2.1
Packit ea1746
     as the gflags namespace changed from ``google::`` to
Packit ea1746
     ``gflags::``.  A patch to fix this is available from `here
Packit ea1746
     <https://code.google.com/p/google-glog/issues/detail?id=194>`_.
Packit ea1746
Packit ea1746
- `gflags <https://github.com/gflags/gflags>`_. Needed to build
Packit ea1746
  examples and tests.
Packit ea1746
Packit ea1746
- `SuiteSparse
Packit ea1746
  <http://faculty.cse.tamu.edu/davis/suitesparse.html>`_. Needed for
Packit ea1746
  solving large sparse linear systems. **Optional; strongly recomended
Packit ea1746
  for large scale bundle adjustment**
Packit ea1746
Packit ea1746
- `CXSparse <http://faculty.cse.tamu.edu/davis/suitesparse.html>`_.
Packit ea1746
  Similar to ``SuiteSparse`` but simpler and slower. CXSparse has
Packit ea1746
  no dependencies on ``LAPACK`` and ``BLAS``. This makes for a simpler
Packit ea1746
  build process and a smaller binary. **Optional**
Packit ea1746
Packit ea1746
- `BLAS <http://www.netlib.org/blas/>`_ and `LAPACK
Packit ea1746
  <http://www.netlib.org/lapack/>`_ routines are needed by
Packit ea1746
  ``SuiteSparse``, and optionally used by Ceres directly for some
Packit ea1746
  operations.
Packit ea1746
Packit ea1746
  On ``UNIX`` OSes other than Mac OS X we recommend `ATLAS
Packit ea1746
  <http://math-atlas.sourceforge.net/>`_, which includes ``BLAS`` and
Packit ea1746
  ``LAPACK`` routines. It is also possible to use `OpenBLAS
Packit ea1746
  <https://github.com/xianyi/OpenBLAS>`_ . However, one needs to be
Packit ea1746
  careful to `turn off the threading
Packit ea1746
  <https://github.com/xianyi/OpenBLAS/wiki/faq#wiki-multi-threaded>`_
Packit ea1746
  inside ``OpenBLAS`` as it conflicts with use of threads in Ceres.
Packit ea1746
Packit ea1746
  Mac OS X ships with an optimized ``LAPACK`` and ``BLAS``
Packit ea1746
  implementation as part of the ``Accelerate`` framework. The Ceres
Packit ea1746
  build system will automatically detect and use it.
Packit ea1746
Packit ea1746
  For Windows things are much more complicated. `LAPACK For
Packit ea1746
  Windows <http://icl.cs.utk.edu/lapack-for-windows/lapack/>`_
Packit ea1746
  has detailed instructions..
Packit ea1746
Packit ea1746
  **Optional but required for** ``SuiteSparse``.
Packit ea1746
Packit ea1746
.. _section-linux:
Packit ea1746
Packit ea1746
Linux
Packit ea1746
=====
Packit ea1746
Packit ea1746
We will use `Ubuntu <http://www.ubuntu.com>`_ as our example linux
Packit ea1746
distribution.
Packit ea1746
Packit ea1746
.. NOTE::
Packit ea1746
Packit ea1746
 Up to at least Ubuntu 14.04, the SuiteSparse package in the official
Packit ea1746
 package repository (built from SuiteSparse v3.4.0) **cannot** be used
Packit ea1746
 to build Ceres as a *shared* library.  Thus if you want to build
Packit ea1746
 Ceres as a shared library using SuiteSparse, you must perform a
Packit ea1746
 source install of SuiteSparse or use an external PPA (see `bug report
Packit ea1746
 here
Packit ea1746
 <https://bugs.launchpad.net/ubuntu/+source/suitesparse/+bug/1333214>`_).
Packit ea1746
 It is recommended that you use the current version of SuiteSparse
Packit ea1746
 (4.2.1 at the time of writing).
Packit ea1746
Packit ea1746
Packit ea1746
Start by installing all the dependencies.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
     # CMake
Packit ea1746
     sudo apt-get install cmake
Packit ea1746
     # google-glog + gflags
Packit ea1746
     sudo apt-get install libgoogle-glog-dev
Packit ea1746
     # BLAS & LAPACK
Packit ea1746
     sudo apt-get install libatlas-base-dev
Packit ea1746
     # Eigen3
Packit ea1746
     sudo apt-get install libeigen3-dev
Packit ea1746
     # SuiteSparse and CXSparse (optional)
Packit ea1746
     # - If you want to build Ceres as a *static* library (the default)
Packit ea1746
     #   you can use the SuiteSparse package in the main Ubuntu package
Packit ea1746
     #   repository:
Packit ea1746
     sudo apt-get install libsuitesparse-dev
Packit ea1746
     # - However, if you want to build Ceres as a *shared* library, you must
Packit ea1746
     #   add the following PPA:
Packit ea1746
     sudo add-apt-repository ppa:bzindovic/suitesparse-bugfix-1319687
Packit ea1746
     sudo apt-get update
Packit ea1746
     sudo apt-get install libsuitesparse-dev
Packit ea1746
Packit ea1746
We are now ready to build, test, and install Ceres.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
 tar zxf ceres-solver-1.13.0.tar.gz
Packit ea1746
 mkdir ceres-bin
Packit ea1746
 cd ceres-bin
Packit ea1746
 cmake ../ceres-solver-1.13.0
Packit ea1746
 make -j3
Packit ea1746
 make test
Packit ea1746
 # Optionally install Ceres, it can also be exported using CMake which
Packit ea1746
 # allows Ceres to be used without requiring installation, see the documentation
Packit ea1746
 # for the EXPORT_BUILD_DIR option for more information.
Packit ea1746
 make install
Packit ea1746
Packit ea1746
You can also try running the command line bundling application with one of the
Packit ea1746
included problems, which comes from the University of Washington's BAL
Packit ea1746
dataset [Agarwal]_.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
 bin/simple_bundle_adjuster ../ceres-solver-1.13.0/data/problem-16-22106-pre.txt
Packit ea1746
Packit ea1746
This runs Ceres for a maximum of 10 iterations using the
Packit ea1746
``DENSE_SCHUR`` linear solver. The output should look something like
Packit ea1746
this.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
    iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
Packit ea1746
       0  4.185660e+06    0.00e+00    1.09e+08   0.00e+00   0.00e+00  1.00e+04       0    7.59e-02    3.37e-01
Packit ea1746
       1  1.062590e+05    4.08e+06    8.99e+06   5.36e+02   9.82e-01  3.00e+04       1    1.65e-01    5.03e-01
Packit ea1746
       2  4.992817e+04    5.63e+04    8.32e+06   3.19e+02   6.52e-01  3.09e+04       1    1.45e-01    6.48e-01
Packit ea1746
       3  1.899774e+04    3.09e+04    1.60e+06   1.24e+02   9.77e-01  9.26e+04       1    1.43e-01    7.92e-01
Packit ea1746
       4  1.808729e+04    9.10e+02    3.97e+05   6.39e+01   9.51e-01  2.78e+05       1    1.45e-01    9.36e-01
Packit ea1746
       5  1.803399e+04    5.33e+01    1.48e+04   1.23e+01   9.99e-01  8.33e+05       1    1.45e-01    1.08e+00
Packit ea1746
       6  1.803390e+04    9.02e-02    6.35e+01   8.00e-01   1.00e+00  2.50e+06       1    1.50e-01    1.23e+00
Packit ea1746
Packit ea1746
    Ceres Solver v1.13.0 Solve Report
Packit ea1746
    ----------------------------------
Packit ea1746
                                         Original                  Reduced
Packit ea1746
    Parameter blocks                        22122                    22122
Packit ea1746
    Parameters                              66462                    66462
Packit ea1746
    Residual blocks                         83718                    83718
Packit ea1746
    Residual                               167436                   167436
Packit ea1746
Packit ea1746
    Minimizer                        TRUST_REGION
Packit ea1746
Packit ea1746
    Dense linear algebra library            EIGEN
Packit ea1746
    Trust region strategy     LEVENBERG_MARQUARDT
Packit ea1746
Packit ea1746
                                            Given                     Used
Packit ea1746
    Linear solver                     DENSE_SCHUR              DENSE_SCHUR
Packit ea1746
    Threads                                     1                        1
Packit ea1746
    Linear solver threads                       1                        1
Packit ea1746
    Linear solver ordering              AUTOMATIC                22106, 16
Packit ea1746
Packit ea1746
    Cost:
Packit ea1746
    Initial                          4.185660e+06
Packit ea1746
    Final                            1.803390e+04
Packit ea1746
    Change                           4.167626e+06
Packit ea1746
Packit ea1746
    Minimizer iterations                        6
Packit ea1746
    Successful steps                            6
Packit ea1746
    Unsuccessful steps                          0
Packit ea1746
Packit ea1746
    Time (in seconds):
Packit ea1746
    Preprocessor                            0.261
Packit ea1746
Packit ea1746
      Residual evaluation                   0.082
Packit ea1746
      Jacobian evaluation                   0.412
Packit ea1746
      Linear solver                         0.442
Packit ea1746
    Minimizer                               1.051
Packit ea1746
Packit ea1746
    Postprocessor                           0.002
Packit ea1746
    Total                                   1.357
Packit ea1746
Packit ea1746
    Termination:                      CONVERGENCE (Function tolerance reached. |cost_change|/cost: 1.769766e-09 <= 1.000000e-06)
Packit ea1746
Packit ea1746
.. section-osx:
Packit ea1746
Packit ea1746
Mac OS X
Packit ea1746
========
Packit ea1746
.. NOTE::
Packit ea1746
Packit ea1746
 Ceres will not compile using Xcode 4.5.x (Clang version 4.1) due to a
Packit ea1746
 bug in that version of Clang.  If you are running Xcode 4.5.x, please
Packit ea1746
 update to Xcode >= 4.6.x before attempting to build Ceres.
Packit ea1746
Packit ea1746
Packit ea1746
On OS X, you can either use `MacPorts <https://www.macports.org/>`_ or
Packit ea1746
`Homebrew <http://mxcl.github.com/homebrew/>`_ to install Ceres Solver.
Packit ea1746
Packit ea1746
If using `MacPorts <https://www.macports.org/>`_, then
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
   sudo port install ceres-solver
Packit ea1746
Packit ea1746
will install the latest version.
Packit ea1746
Packit ea1746
If using `Homebrew <http://mxcl.github.com/homebrew/>`_ and assuming
Packit ea1746
that you have the ``homebrew/science`` [#f1]_ tap enabled, then
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
      brew install ceres-solver
Packit ea1746
Packit ea1746
will install the latest stable version along with all the required
Packit ea1746
dependencies and
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
      brew install ceres-solver --HEAD
Packit ea1746
Packit ea1746
will install the latest version in the git repo.
Packit ea1746
Packit ea1746
You can also install each of the dependencies by hand using `Homebrew
Packit ea1746
<http://mxcl.github.com/homebrew/>`_. There is no need to install
Packit ea1746
``BLAS`` or ``LAPACK`` separately as OS X ships with optimized
Packit ea1746
``BLAS`` and ``LAPACK`` routines as part of the `vecLib
Packit ea1746
<https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/vecLib/Reference/reference.html>`_
Packit ea1746
framework.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
      # CMake
Packit ea1746
      brew install cmake
Packit ea1746
      # google-glog and gflags
Packit ea1746
      brew install glog
Packit ea1746
      # Eigen3
Packit ea1746
      brew install eigen
Packit ea1746
      # SuiteSparse and CXSparse
Packit ea1746
      brew install suite-sparse
Packit ea1746
Packit ea1746
We are now ready to build, test, and install Ceres.
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
   tar zxf ceres-solver-1.13.0.tar.gz
Packit ea1746
   mkdir ceres-bin
Packit ea1746
   cd ceres-bin
Packit ea1746
   cmake ../ceres-solver-1.13.0
Packit ea1746
   make -j3
Packit ea1746
   make test
Packit ea1746
   # Optionally install Ceres, it can also be exported using CMake which
Packit ea1746
   # allows Ceres to be used without requiring installation, see the
Packit ea1746
   # documentation for the EXPORT_BUILD_DIR option for more information.
Packit ea1746
   make install
Packit ea1746
Packit ea1746
Like the Linux build, you should now be able to run
Packit ea1746
``bin/simple_bundle_adjuster``.
Packit ea1746
Packit ea1746
Packit ea1746
.. rubric:: Footnotes
Packit ea1746
Packit ea1746
.. [#f1] Ceres and many of its dependencies are in `homebrew/science
Packit ea1746
   <https://github.com/Homebrew/homebrew-science>`_ tap. So, if you
Packit ea1746
   don't have this tap enabled, then you will need to enable it as
Packit ea1746
   follows before executing any of the commands in this section.
Packit ea1746
Packit ea1746
   .. code-block:: bash
Packit ea1746
Packit ea1746
      brew tap homebrew/science
Packit ea1746
Packit ea1746
Packit ea1746
.. _section-windows:
Packit ea1746
Packit ea1746
Windows
Packit ea1746
=======
Packit ea1746
Packit ea1746
.. NOTE::
Packit ea1746
Packit ea1746
  If you find the following CMake difficult to set up, then you may
Packit ea1746
  be interested in a `Microsoft Visual Studio wrapper
Packit ea1746
  <https://github.com/tbennun/ceres-windows>`_ for Ceres Solver by Tal
Packit ea1746
  Ben-Nun.
Packit ea1746
Packit ea1746
On Windows, we support building with Visual Studio 2010 or newer. Note
Packit ea1746
that the Windows port is less featureful and less tested than the
Packit ea1746
Linux or Mac OS X versions due to the lack of an officially supported
Packit ea1746
way of building SuiteSparse and CXSparse.  There are however a number
Packit ea1746
of unofficial ways of building these libraries. Building on Windows
Packit ea1746
also a bit more involved since there is no automated way to install
Packit ea1746
dependencies.
Packit ea1746
Packit ea1746
.. NOTE:: Using ``google-glog`` & ``miniglog`` with windows.h.
Packit ea1746
Packit ea1746
 The windows.h header if used with GDI (Graphics Device Interface)
Packit ea1746
 defines ``ERROR``, which conflicts with the definition of ``ERROR``
Packit ea1746
 as a LogSeverity level in ``google-glog`` and ``miniglog``.  There
Packit ea1746
 are at least two possible fixes to this problem:
Packit ea1746
Packit ea1746
 #. Use ``google-glog`` and define ``GLOG_NO_ABBREVIATED_SEVERITIES``
Packit ea1746
    when building Ceres and your own project, as documented `here
Packit ea1746
    <http://google-glog.googlecode.com/svn/trunk/doc/glog.html>`__.
Packit ea1746
    Note that this fix will not work for ``miniglog``, but use of
Packit ea1746
    ``miniglog`` is strongly discouraged on any platform for which
Packit ea1746
    ``google-glog`` is available (which includes Windows).
Packit ea1746
 #. If you do not require GDI, then define ``NOGDI`` **before**
Packit ea1746
    including windows.h.  This solution should work for both
Packit ea1746
    ``google-glog`` and ``miniglog`` and is documented for
Packit ea1746
    ``google-glog`` `here
Packit ea1746
    <https://code.google.com/p/google-glog/issues/detail?id=33>`__.
Packit ea1746
Packit ea1746
#. Make a toplevel directory for deps & build & src somewhere: ``ceres/``
Packit ea1746
#. Get dependencies; unpack them as subdirectories in ``ceres/``
Packit ea1746
   (``ceres/eigen``, ``ceres/glog``, etc)
Packit ea1746
Packit ea1746
   #. ``Eigen`` 3.1 (needed on Windows; 3.0.x will not work). There is
Packit ea1746
      no need to build anything; just unpack the source tarball.
Packit ea1746
Packit ea1746
   #. ``google-glog`` Open up the Visual Studio solution and build it.
Packit ea1746
   #. ``gflags`` Open up the Visual Studio solution and build it.
Packit ea1746
Packit ea1746
   #. (Experimental) ``SuiteSparse`` Previously SuiteSparse was not
Packit ea1746
      available on Windows, recently it has become possible to build
Packit ea1746
      it on Windows using the `suitesparse-metis-for-windows
Packit ea1746
      <https://github.com/jlblancoc/suitesparse-metis-for-windows>`_
Packit ea1746
      project.  If you wish to use ``SuiteSparse``, follow their
Packit ea1746
      instructions for obtaining and building it.
Packit ea1746
Packit ea1746
   #. (Experimental) ``CXSparse`` Previously CXSparse was not
Packit ea1746
      available on Windows, there are now several ports that enable it
Packit ea1746
      to be, including: `[1] <https://github.com/PetterS/CXSparse>`_
Packit ea1746
      and `[2] <https://github.com/TheFrenchLeaf/CXSparse>`_.  If you
Packit ea1746
      wish to use ``CXSparse``, follow their instructions for
Packit ea1746
      obtaining and building it.
Packit ea1746
Packit ea1746
#. Unpack the Ceres tarball into ``ceres``. For the tarball, you
Packit ea1746
   should get a directory inside ``ceres`` similar to
Packit ea1746
   ``ceres-solver-1.3.0``. Alternately, checkout Ceres via ``git`` to
Packit ea1746
   get ``ceres-solver.git`` inside ``ceres``.
Packit ea1746
Packit ea1746
#. Install ``CMake``,
Packit ea1746
Packit ea1746
#. Make a dir ``ceres/ceres-bin`` (for an out-of-tree build)
Packit ea1746
Packit ea1746
#. Run ``CMake``; select the ``ceres-solver-X.Y.Z`` or
Packit ea1746
   ``ceres-solver.git`` directory for the CMake file. Then select the
Packit ea1746
   ``ceres-bin`` for the build dir.
Packit ea1746
Packit ea1746
#. Try running ``Configure``. It won't work. It'll show a bunch of options.
Packit ea1746
   You'll need to set:
Packit ea1746
Packit ea1746
   #. ``EIGEN_INCLUDE_DIR_HINTS``
Packit ea1746
   #. ``GLOG_INCLUDE_DIR_HINTS``
Packit ea1746
   #. ``GLOG_LIBRARY_DIR_HINTS``
Packit ea1746
   #. ``GFLAGS_INCLUDE_DIR_HINTS``
Packit ea1746
   #. ``GFLAGS_LIBRARY_DIR_HINTS``
Packit ea1746
   #. (Optional) ``SUITESPARSE_INCLUDE_DIR_HINTS``
Packit ea1746
   #. (Optional) ``SUITESPARSE_LIBRARY_DIR_HINTS``
Packit ea1746
   #. (Optional) ``CXSPARSE_INCLUDE_DIR_HINTS``
Packit ea1746
   #. (Optional) ``CXSPARSE_LIBRARY_DIR_HINTS``
Packit ea1746
Packit ea1746
   to the appropriate directories where you unpacked/built them. If
Packit ea1746
   any of the variables are not visible in the ``CMake`` GUI, create a
Packit ea1746
   new entry for them.  We recommend using the
Packit ea1746
   ``<NAME>_(INCLUDE/LIBRARY)_DIR_HINTS`` variables rather than
Packit ea1746
   setting the ``<NAME>_INCLUDE_DIR`` & ``<NAME>_LIBRARY`` variables
Packit ea1746
   directly to keep all of the validity checking, and to avoid having
Packit ea1746
   to specify the library files manually.
Packit ea1746
Packit ea1746
#. You may have to tweak some more settings to generate a MSVC
Packit ea1746
   project.  After each adjustment, try pressing Configure & Generate
Packit ea1746
   until it generates successfully.
Packit ea1746
Packit ea1746
#. Open the solution and build it in MSVC
Packit ea1746
Packit ea1746
Packit ea1746
To run the tests, select the ``RUN_TESTS`` target and hit **Build
Packit ea1746
RUN_TESTS** from the build menu.
Packit ea1746
Packit ea1746
Like the Linux build, you should now be able to run
Packit ea1746
``bin/simple_bundle_adjuster``.
Packit ea1746
Packit ea1746
Notes:
Packit ea1746
Packit ea1746
#. The default build is Debug; consider switching it to release mode.
Packit ea1746
#. Currently ``system_test`` is not working properly.
Packit ea1746
#. CMake puts the resulting test binaries in ``ceres-bin/examples/Debug``
Packit ea1746
   by default.
Packit ea1746
#. The solvers supported on Windows are ``DENSE_QR``, ``DENSE_SCHUR``,
Packit ea1746
   ``CGNR``, and ``ITERATIVE_SCHUR``.
Packit ea1746
#. We're looking for someone to work with upstream ``SuiteSparse`` to
Packit ea1746
   port their build system to something sane like ``CMake``, and get a
Packit ea1746
   fully supported Windows port.
Packit ea1746
Packit ea1746
Packit ea1746
.. _section-android:
Packit ea1746
Packit ea1746
Android
Packit ea1746
=======
Packit ea1746
Packit ea1746
Download the ``Android NDK`` version ``r9d`` or later. Run
Packit ea1746
``ndk-build`` from inside the ``jni`` directory. Use the
Packit ea1746
``libceres.a`` that gets created.
Packit ea1746
Packit ea1746
.. _section-ios:
Packit ea1746
Packit ea1746
iOS
Packit ea1746
===
Packit ea1746
Packit ea1746
.. NOTE::
Packit ea1746
Packit ea1746
   You need iOS version 7.0 or higher to build Ceres Solver.
Packit ea1746
Packit ea1746
To build Ceres for iOS, we need to force ``CMake`` to find the
Packit ea1746
toolchains from the iOS SDK instead of using the standard ones. For
Packit ea1746
example:
Packit ea1746
Packit ea1746
.. code-block:: bash
Packit ea1746
Packit ea1746
   cmake \
Packit ea1746
   -DCMAKE_TOOLCHAIN_FILE=../ceres-solver/cmake/iOS.cmake \
Packit ea1746
   -DEIGEN_INCLUDE_DIR=/path/to/eigen/header \
Packit ea1746
   -DIOS_PLATFORM=<PLATFORM> \
Packit ea1746
   <PATH_TO_CERES_SOURCE>
Packit ea1746
Packit ea1746
``PLATFORM`` can be: ``OS``, ``SIMULATOR`` or ``SIMULATOR64``. You can
Packit ea1746
build for ``OS`` (``armv7``, ``armv7s``, ``arm64``), ``SIMULATOR``
Packit ea1746
(``i386``) or ``SIMULATOR64`` (``x86_64``) separately and use ``lipo``
Packit ea1746
to merge them into one static library.  See ``cmake/iOS.cmake`` for
Packit ea1746
more options.
Packit ea1746
Packit ea1746
After building, you will get a ``libceres.a`` library, which you will
Packit ea1746
need to add to your Xcode project.
Packit ea1746
Packit ea1746
The default CMake configuration builds a bare bones version of Ceres
Packit ea1746
Solver that only depends on Eigen (``MINIGLOG`` is compiled into Ceres
Packit ea1746
if it is used), this should be sufficient for solving small to
Packit ea1746
moderate sized problems (No ``SPARSE_SCHUR``,
Packit ea1746
``SPARSE_NORMAL_CHOLESKY`` linear solvers and no ``CLUSTER_JACOBI``
Packit ea1746
and ``CLUSTER_TRIDIAGONAL`` preconditioners).
Packit ea1746
Packit ea1746
If you decide to use ``LAPACK`` and ``BLAS``, then you also need to
Packit ea1746
add ``Accelerate.framework`` to your Xcode project's linking
Packit ea1746
dependency.
Packit ea1746
Packit ea1746
.. _section-customizing:
Packit ea1746
Packit ea1746
Customizing the build
Packit ea1746
=====================
Packit ea1746
Packit ea1746
It is possible to reduce the libraries needed to build Ceres and
Packit ea1746
customize the build process by setting the appropriate options in
Packit ea1746
``CMake``.  These options can either be set in the ``CMake`` GUI, or
Packit ea1746
via ``-D<OPTION>=<ON/OFF>`` when running ``CMake`` from the command
Packit ea1746
line.  In general, you should only modify these options from their
Packit ea1746
defaults if you know what you are doing.
Packit ea1746
Packit ea1746
.. NOTE::
Packit ea1746
Packit ea1746
 If you are setting variables via ``-D<VARIABLE>=<VALUE>`` when
Packit ea1746
 calling ``CMake``, it is important to understand that this forcibly
Packit ea1746
 **overwrites** the variable ``<VARIABLE>`` in the ``CMake`` cache at
Packit ea1746
 the start of *every configure*.
Packit ea1746
Packit ea1746
 This can lead to confusion if you are invoking the ``CMake`` `curses
Packit ea1746
 <http://www.gnu.org/software/ncurses/ncurses.html>`_ terminal GUI
Packit ea1746
 (via ``ccmake``, e.g. ```ccmake -D<VARIABLE>=<VALUE>
Packit ea1746
 <PATH_TO_SRC>``).  In this case, even if you change the value of
Packit ea1746
 ``<VARIABLE>`` in the ``CMake`` GUI, your changes will be
Packit ea1746
 **overwritten** with the value passed via ``-D<VARIABLE>=<VALUE>``
Packit ea1746
 (if one exists) at the start of each configure.
Packit ea1746
Packit ea1746
 As such, it is generally easier not to pass values to ``CMake`` via
Packit ea1746
 ``-D`` and instead interactively experiment with their values in the
Packit ea1746
 ``CMake`` GUI.  If they are not present in the *Standard View*,
Packit ea1746
 toggle to the *Advanced View* with ``<t>``.
Packit ea1746
Packit ea1746
Options controlling Ceres configuration
Packit ea1746
---------------------------------------
Packit ea1746
Packit ea1746
#. ``LAPACK [Default: ON]``: By default Ceres will use ``LAPACK`` (&
Packit ea1746
   ``BLAS``) if they are found.  Turn this ``OFF`` to build Ceres
Packit ea1746
   without ``LAPACK``. Turning this ``OFF`` also disables
Packit ea1746
   ``SUITESPARSE`` as it depends on ``LAPACK``.
Packit ea1746
Packit ea1746
#. ``SUITESPARSE [Default: ON]``: By default, Ceres will link to
Packit ea1746
   ``SuiteSparse`` if it and all of its dependencies are present. Turn
Packit ea1746
   this ``OFF`` to build Ceres without ``SuiteSparse``. Note that
Packit ea1746
   ``LAPACK`` must be ``ON`` in order to build with ``SuiteSparse``.
Packit ea1746
Packit ea1746
#. ``CXSPARSE [Default: ON]``: By default, Ceres will link to
Packit ea1746
   ``CXSparse`` if all its dependencies are present. Turn this ``OFF``
Packit ea1746
   to build Ceres without ``CXSparse``.
Packit ea1746
Packit ea1746
#. ``EIGENSPARSE [Default: OFF]``: By default, Ceres will not use
Packit ea1746
   Eigen's sparse Cholesky factorization. The is because this part of
Packit ea1746
   the code is licensed under the ``LGPL`` and since ``Eigen`` is a
Packit ea1746
   header only library, including this code will result in an ``LGPL``
Packit ea1746
   licensed version of Ceres.
Packit ea1746
Packit ea1746
   .. NOTE::
Packit ea1746
Packit ea1746
      For good performance, use Eigen version 3.2.2 or later.
Packit ea1746
Packit ea1746
#. ``GFLAGS [Default: ON]``: Turn this ``OFF`` to build Ceres without
Packit ea1746
   ``gflags``. This will also prevent some of the example code from
Packit ea1746
   building.
Packit ea1746
Packit ea1746
#. ``MINIGLOG [Default: OFF]``: Ceres includes a stripped-down,
Packit ea1746
   minimal implementation of ``glog`` which can optionally be used as
Packit ea1746
   a substitute for ``glog``, thus removing ``glog`` as a required
Packit ea1746
   dependency. Turn this ``ON`` to use this minimal ``glog``
Packit ea1746
   implementation.
Packit ea1746
Packit ea1746
#. ``SCHUR_SPECIALIZATIONS [Default: ON]``: If you are concerned about
Packit ea1746
   binary size/compilation time over some small (10-20%) performance
Packit ea1746
   gains in the ``SPARSE_SCHUR`` solver, you can disable some of the
Packit ea1746
   template specializations by turning this ``OFF``.
Packit ea1746
Packit ea1746
#. ``OPENMP [Default: ON]``: On certain platforms like Android,
Packit ea1746
   multi-threading with ``OpenMP`` is not supported. Turn this ``OFF``
Packit ea1746
   to disable multi-threading.
Packit ea1746
Packit ea1746
#. ``CXX11 [Default: OFF]`` *Non-MSVC compilers only*.
Packit ea1746
Packit ea1746
   Although Ceres does not currently use C++11, it does use
Packit ea1746
   ``shared_ptr`` (required) and ``unordered_map`` (if available);
Packit ea1746
   both of which existed in the previous iterations of what became the
Packit ea1746
   C++11 standard: TR1 & C++0x.  As such, Ceres can compile on
Packit ea1746
   pre-C++11 compilers, using the TR1/C++0x versions of ``shared_ptr``
Packit ea1746
   & ``unordered_map``.
Packit ea1746
Packit ea1746
   Note that when using GCC & Clang, compiling against the TR1/C++0x
Packit ea1746
   versions: ``CXX11=OFF`` (the default) *does not* require
Packit ea1746
   ``-std=c++11`` when compiling Ceres, *nor* does it require that any
Packit ea1746
   client code using Ceres use ``-std=c++11``.  However, this will
Packit ea1746
   cause compile errors if any client code that uses Ceres also uses
Packit ea1746
   C++11 (mismatched versions of ``shared_ptr`` & ``unordered_map``).
Packit ea1746
Packit ea1746
   Enabling this option: ``CXX11=ON`` forces Ceres to use the C++11
Packit ea1746
   versions of ``shared_ptr`` & ``unordered_map`` if they are
Packit ea1746
   available, and thus imposes the requirement that all client code
Packit ea1746
   using Ceres also compile with ``-std=c++11``.  This requirement is
Packit ea1746
   handled automatically through CMake target properties on the
Packit ea1746
   exported Ceres target for CMake >= 2.8.12 (when it was introduced).
Packit ea1746
   Thus, any client code which uses CMake will automatically be
Packit ea1746
   compiled with ``-std=c++11``.  **On CMake versions < 2.8.12, you
Packit ea1746
   are responsible for ensuring that any code which uses Ceres is
Packit ea1746
   compiled with** ``-std=c++11``.
Packit ea1746
Packit ea1746
   On OS X 10.9+, Clang will use the C++11 versions of ``shared_ptr``
Packit ea1746
   & ``unordered_map`` without ``-std=c++11`` and so this option does
Packit ea1746
   not change the versions detected, although enabling it *will*
Packit ea1746
   require that client code compile with ``-std=c++11``.
Packit ea1746
Packit ea1746
   The following table summarises the effects of the ``CXX11`` option:
Packit ea1746
Packit ea1746
   ===================  ==========  ================  ======================================
Packit ea1746
   OS                   CXX11       Detected Version  Ceres & client code require ``-std=c++11``
Packit ea1746
   ===================  ==========  ================  ======================================
Packit ea1746
   Linux (GCC & Clang)  OFF         tr1               **No**
Packit ea1746
   Linux (GCC & Clang)  ON          std               **Yes**
Packit ea1746
   OS X 10.9+           OFF         std               **No**
Packit ea1746
   OS X 10.9+           ON          std               **Yes**
Packit ea1746
   ===================  ==========  ================  ======================================
Packit ea1746
Packit ea1746
   The ``CXX11`` option does does not exist when using MSVC, as there
Packit ea1746
   any new C++ features available are enabled by default, and there is
Packit ea1746
   no analogue of ``-std=c++11``.  It will however be available on
Packit ea1746
   MinGW & CygWin, which can support ``-std=c++11``.
Packit ea1746
Packit ea1746
#. ``BUILD_SHARED_LIBS [Default: OFF]``: By default Ceres is built as
Packit ea1746
   a static library, turn this ``ON`` to instead build Ceres as a
Packit ea1746
   shared library.
Packit ea1746
Packit ea1746
#. ``EXPORT_BUILD_DIR [Default: OFF]``: By default Ceres is configured
Packit ea1746
   solely for installation, and so must be installed in order for
Packit ea1746
   clients to use it.  Turn this ``ON`` to export Ceres' build
Packit ea1746
   directory location into the `user's local CMake package registry
Packit ea1746
   <http://www.cmake.org/cmake/help/v3.2/manual/cmake-packages.7.html#user-package-registry>`_
Packit ea1746
   where it will be detected **without requiring installation** in a
Packit ea1746
   client project using CMake when `find_package(Ceres)
Packit ea1746
   <http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_
Packit ea1746
   is invoked.
Packit ea1746
Packit ea1746
#. ``BUILD_DOCUMENTATION [Default: OFF]``: Use this to enable building
Packit ea1746
   the documentation, requires `Sphinx <http://sphinx-doc.org/>`_ and
Packit ea1746
   the `sphinx-better-theme
Packit ea1746
   <https://pypi.python.org/pypi/sphinx-better-theme>`_ package
Packit ea1746
   available from the Python package index. In addition, ``make
Packit ea1746
   ceres_docs`` can be used to build only the documentation.
Packit ea1746
Packit ea1746
#. ``MSVC_USE_STATIC_CRT [Default: OFF]`` *Windows Only*: By default
Packit ea1746
   Ceres will use the Visual Studio default, *shared* C-Run Time (CRT)
Packit ea1746
   library.  Turn this ``ON`` to use the *static* C-Run Time library
Packit ea1746
   instead.
Packit ea1746
Packit ea1746
#. ``LIB_SUFFIX [Default: "64" on non-Debian/Arch based 64-bit Linux,
Packit ea1746
   otherwise: ""]``: The suffix to append to the library install
Packit ea1746
   directory, built from:
Packit ea1746
   ``${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}``.
Packit ea1746
Packit ea1746
   The filesystem hierarchy standard recommends that 64-bit systems
Packit ea1746
   install native libraries to lib64 rather than lib.  Most Linux
Packit ea1746
   distributions follow this convention, but Debian and Arch based
Packit ea1746
   distros do not.  Note that the only generally sensible values for
Packit ea1746
   ``LIB_SUFFIX`` are "" and "64".
Packit ea1746
Packit ea1746
   Although by default Ceres will auto-detect non-Debian/Arch based
Packit ea1746
   64-bit Linux distributions and default ``LIB_SUFFIX`` to "64", this
Packit ea1746
   can always be overridden by manually specifying LIB_SUFFIX using:
Packit ea1746
   ``-DLIB_SUFFIX=<VALUE>`` when invoking CMake.
Packit ea1746
Packit ea1746
Packit ea1746
Options controlling Ceres dependency locations
Packit ea1746
----------------------------------------------
Packit ea1746
Packit ea1746
Ceres uses the ``CMake`` `find_package
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_
Packit ea1746
function to find all of its dependencies using
Packit ea1746
``Find<DEPENDENCY_NAME>.cmake`` scripts which are either included in
Packit ea1746
Ceres (for most dependencies) or are shipped as standard with
Packit ea1746
``CMake`` (for ``LAPACK`` & ``BLAS``).  These scripts will search all
Packit ea1746
of the "standard" install locations for various OSs for each
Packit ea1746
dependency.  However, particularly for Windows, they may fail to find
Packit ea1746
the library, in this case you will have to manually specify its
Packit ea1746
installed location.  The ``Find<DEPENDENCY_NAME>.cmake`` scripts
Packit ea1746
shipped with Ceres support two ways for you to do this:
Packit ea1746
Packit ea1746
#. Set the *hints* variables specifying the *directories* to search in
Packit ea1746
   preference, but in addition, to the search directories in the
Packit ea1746
   ``Find<DEPENDENCY_NAME>.cmake`` script:
Packit ea1746
Packit ea1746
   - ``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR_HINTS``
Packit ea1746
   - ``<DEPENDENCY_NAME (CAPS)>_LIBRARY_DIR_HINTS``
Packit ea1746
Packit ea1746
   These variables should be set via ``-D=<VALUE>``
Packit ea1746
   ``CMake`` arguments as they are not visible in the GUI.
Packit ea1746
Packit ea1746
#. Set the variables specifying the *explicit* include directory
Packit ea1746
   and library file to use:
Packit ea1746
Packit ea1746
   - ``<DEPENDENCY_NAME (CAPS)>_INCLUDE_DIR``
Packit ea1746
   - ``<DEPENDENCY_NAME (CAPS)>_LIBRARY``
Packit ea1746
Packit ea1746
   This bypasses *all* searching in the
Packit ea1746
   ``Find<DEPENDENCY_NAME>.cmake`` script, but validation is still
Packit ea1746
   performed.
Packit ea1746
Packit ea1746
   These variables are available to set in the ``CMake`` GUI. They are
Packit ea1746
   visible in the *Standard View* if the library has not been found
Packit ea1746
   (but the current Ceres configuration requires it), but are always
Packit ea1746
   visible in the *Advanced View*.  They can also be set directly via
Packit ea1746
   ``-D=<VALUE>`` arguments to ``CMake``.
Packit ea1746
Packit ea1746
Building using custom BLAS & LAPACK installs
Packit ea1746
----------------------------------------------
Packit ea1746
Packit ea1746
If the standard find package scripts for ``BLAS`` & ``LAPACK`` which
Packit ea1746
ship with ``CMake`` fail to find the desired libraries on your system,
Packit ea1746
try setting ``CMAKE_LIBRARY_PATH`` to the path(s) to the directories
Packit ea1746
containing the ``BLAS`` & ``LAPACK`` libraries when invoking ``CMake``
Packit ea1746
to build Ceres via ``-D=<VALUE>``.  This should result in the
Packit ea1746
libraries being found for any common variant of each.
Packit ea1746
Packit ea1746
If you are building on an exotic system, or setting
Packit ea1746
``CMAKE_LIBRARY_PATH`` does not work, or is not appropriate for some
Packit ea1746
other reason, one option would be to write your own custom versions of
Packit ea1746
``FindBLAS.cmake`` & ``FindLAPACK.cmake`` specific to your
Packit ea1746
environment.  In this case you must set ``CMAKE_MODULE_PATH`` to the
Packit ea1746
directory containing these custom scripts when invoking ``CMake`` to
Packit ea1746
build Ceres and they will be used in preference to the default
Packit ea1746
versions.  However, in order for this to work, your scripts must
Packit ea1746
provide the full set of variables provided by the default scripts.
Packit ea1746
Also, if you are building Ceres with ``SuiteSparse``, the versions of
Packit ea1746
``BLAS`` & ``LAPACK`` used by ``SuiteSparse`` and Ceres should be the
Packit ea1746
same.
Packit ea1746
Packit ea1746
.. _section-using-ceres:
Packit ea1746
Packit ea1746
Using Ceres with CMake
Packit ea1746
======================
Packit ea1746
Packit ea1746
In order to use Ceres in client code with CMake using `find_package()
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_
Packit ea1746
then either:
Packit ea1746
Packit ea1746
#. Ceres must have been installed with ``make install``.  If the
Packit ea1746
    install location is non-standard (i.e. is not in CMake's default
Packit ea1746
    search paths) then it will not be detected by default, see:
Packit ea1746
    :ref:`section-local-installations`.
Packit ea1746
Packit ea1746
    Note that if you are using a non-standard install location you
Packit ea1746
    should consider exporting Ceres instead, as this will not require
Packit ea1746
    any extra information to be provided in client code for Ceres to
Packit ea1746
    be detected.
Packit ea1746
Packit ea1746
#. Or Ceres' build directory must have been exported by enabling the
Packit ea1746
    ``EXPORT_BUILD_DIR`` option when Ceres was configured.
Packit ea1746
Packit ea1746
Packit ea1746
As an example of how to use Ceres, to compile `examples/helloworld.cc
Packit ea1746
<https://ceres-solver.googlesource.com/ceres-solver/+/master/examples/helloworld.cc>`_
Packit ea1746
in a separate standalone project, the following CMakeList.txt can be
Packit ea1746
used:
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    cmake_minimum_required(VERSION 2.8)
Packit ea1746
Packit ea1746
    project(helloworld)
Packit ea1746
Packit ea1746
    find_package(Ceres REQUIRED)
Packit ea1746
    include_directories(${CERES_INCLUDE_DIRS})
Packit ea1746
Packit ea1746
    # helloworld
Packit ea1746
    add_executable(helloworld helloworld.cc)
Packit ea1746
    target_link_libraries(helloworld ${CERES_LIBRARIES})
Packit ea1746
Packit ea1746
Irrespective of whether Ceres was installed or exported, if multiple
Packit ea1746
versions are detected, set: ``Ceres_DIR`` to control which is used.
Packit ea1746
If Ceres was installed ``Ceres_DIR`` should be the path to the
Packit ea1746
directory containing the installed ``CeresConfig.cmake`` file
Packit ea1746
(e.g. ``/usr/local/share/Ceres``).  If Ceres was exported, then
Packit ea1746
``Ceres_DIR`` should be the path to the exported Ceres build
Packit ea1746
directory.
Packit ea1746
Packit ea1746
Specify Ceres components
Packit ea1746
-------------------------------------
Packit ea1746
Packit ea1746
You can specify particular Ceres components that you require (in order
Packit ea1746
for Ceres to be reported as found) when invoking
Packit ea1746
``find_package(Ceres)``.  This allows you to specify, for example,
Packit ea1746
that you require a version of Ceres built with SuiteSparse support.
Packit ea1746
By definition, if you do not specify any components when calling
Packit ea1746
``find_package(Ceres)`` (the default) any version of Ceres detected
Packit ea1746
will be reported as found, irrespective of which components it was
Packit ea1746
built with.
Packit ea1746
Packit ea1746
The Ceres components which can be specified are:
Packit ea1746
Packit ea1746
#. ``LAPACK``: Ceres built using LAPACK (``LAPACK=ON``).
Packit ea1746
Packit ea1746
#. ``SuiteSparse``: Ceres built with SuiteSparse (``SUITESPARSE=ON``).
Packit ea1746
Packit ea1746
#. ``CXSparse``: Ceres built with CXSparse (``CXSPARSE=ON``).
Packit ea1746
Packit ea1746
#. ``EigenSparse``: Ceres built with Eigen's sparse Cholesky factorization
Packit ea1746
   (``EIGENSPARSE=ON``).
Packit ea1746
Packit ea1746
#. ``SparseLinearAlgebraLibrary``: Ceres built with *at least one* sparse linear
Packit ea1746
   algebra library.  This is equivalent to ``SuiteSparse`` **OR** ``CXSparse``
Packit ea1746
   **OR** ``EigenSparse``.
Packit ea1746
Packit ea1746
#. ``SchurSpecializations``: Ceres built with Schur specializations
Packit ea1746
   (``SCHUR_SPECIALIZATIONS=ON``).
Packit ea1746
Packit ea1746
#. ``OpenMP``: Ceres built with OpenMP (``OPENMP=ON``).
Packit ea1746
Packit ea1746
#. ``C++11``: Ceres built with C++11 (``CXX11=ON``).
Packit ea1746
Packit ea1746
To specify one/multiple Ceres components use the ``COMPONENTS`` argument to
Packit ea1746
`find_package()
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_ like so:
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    # Find a version of Ceres compiled with SuiteSparse & EigenSparse support.
Packit ea1746
    #
Packit ea1746
    # NOTE: This will report Ceres as **not** found if the detected version of
Packit ea1746
    #            Ceres was not compiled with both SuiteSparse & EigenSparse.
Packit ea1746
    #            Remember, if you have multiple versions of Ceres installed, you
Packit ea1746
    #            can use Ceres_DIR to specify which should be used.
Packit ea1746
    find_package(Ceres REQUIRED COMPONENTS SuiteSparse EigenSparse)
Packit ea1746
Packit ea1746
Packit ea1746
Specify Ceres version
Packit ea1746
---------------------
Packit ea1746
Packit ea1746
Additionally, when CMake has found Ceres it can optionally check the package
Packit ea1746
version, if it has been specified in the `find_package()
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_
Packit ea1746
call.  For example:
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    find_package(Ceres 1.2.3 REQUIRED)
Packit ea1746
Packit ea1746
.. _section-local-installations:
Packit ea1746
Packit ea1746
Local installations
Packit ea1746
-------------------
Packit ea1746
Packit ea1746
If Ceres was installed in a non-standard path by specifying
Packit ea1746
``-DCMAKE_INSTALL_PREFIX="/some/where/local"``, then the user should
Packit ea1746
add the **PATHS** option to the ``find_package()`` command, e.g.,
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
   find_package(Ceres REQUIRED PATHS "/some/where/local/")
Packit ea1746
Packit ea1746
Note that this can be used to have multiple versions of Ceres
Packit ea1746
installed.  However, particularly if you have only a single version of
Packit ea1746
Ceres which you want to use but do not wish to install to a system
Packit ea1746
location, you should consider exporting Ceres using the
Packit ea1746
``EXPORT_BUILD_DIR`` option instead of a local install, as exported
Packit ea1746
versions of Ceres will be automatically detected by CMake,
Packit ea1746
irrespective of their location.
Packit ea1746
Packit ea1746
Understanding the CMake Package System
Packit ea1746
----------------------------------------
Packit ea1746
Packit ea1746
Although a full tutorial on CMake is outside the scope of this guide,
Packit ea1746
here we cover some of the most common CMake misunderstandings that
Packit ea1746
crop up when using Ceres.  For more detailed CMake usage, the
Packit ea1746
following references are very useful:
Packit ea1746
Packit ea1746
- The `official CMake tutorial <http://www.cmake.org/cmake-tutorial/>`_
Packit ea1746
Packit ea1746
   Provides a tour of the core features of CMake.
Packit ea1746
Packit ea1746
- `ProjectConfig tutorial
Packit ea1746
  <http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file>`_
Packit ea1746
  and the `cmake-packages documentation
Packit ea1746
  <http://www.cmake.org/cmake/help/git-master/manual/cmake-packages.7.html>`_
Packit ea1746
Packit ea1746
   Cover how to write a ``ProjectConfig.cmake`` file, discussed below,
Packit ea1746
   for your own project when installing or exporting it using CMake.
Packit ea1746
   It also covers how these processes in conjunction with
Packit ea1746
   ``find_package()`` are actually handled by CMake.  The
Packit ea1746
   `ProjectConfig tutorial
Packit ea1746
   <http://www.cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file>`_
Packit ea1746
   is the older style, currently used by Ceres for compatibility with
Packit ea1746
   older versions of CMake.
Packit ea1746
Packit ea1746
  .. NOTE :: **Targets in CMake.**
Packit ea1746
Packit ea1746
    All libraries and executables built using CMake are represented as
Packit ea1746
    *targets* created using `add_library()
Packit ea1746
    <http://www.cmake.org/cmake/help/v3.2/command/add_library.html>`_
Packit ea1746
    and `add_executable()
Packit ea1746
    <http://www.cmake.org/cmake/help/v3.2/command/add_executable.html>`_.
Packit ea1746
    Targets encapsulate the rules and dependencies (which can be other
Packit ea1746
    targets) required to build or link against an object.  This allows
Packit ea1746
    CMake to implicitly manage dependency chains.  Thus it is
Packit ea1746
    sufficient to tell CMake that a library target: ``B`` depends on a
Packit ea1746
    previously declared library target ``A``, and CMake will
Packit ea1746
    understand that this means that ``B`` also depends on all of the
Packit ea1746
    public dependencies of ``A``.
Packit ea1746
Packit ea1746
When a project like Ceres is installed using CMake, or its build
Packit ea1746
directory is exported into the local CMake package registry (see
Packit ea1746
:ref:`section-install-vs-export`), in addition to the public headers
Packit ea1746
and compiled libraries, a set of CMake-specific project configuration
Packit ea1746
files are also installed to: ``<INSTALL_ROOT>/share/Ceres`` (if Ceres
Packit ea1746
is installed), or created in the build directory (if Ceres' build
Packit ea1746
directory is exported).  When `find_package
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/find_package.html>`_ is
Packit ea1746
invoked, CMake checks various standard install locations (including
Packit ea1746
``/usr/local`` on Linux & UNIX systems), and the local CMake package
Packit ea1746
registry for CMake configuration files for the project to be found
Packit ea1746
(i.e. Ceres in the case of ``find_package(Ceres)``).  Specifically it
Packit ea1746
looks for:
Packit ea1746
Packit ea1746
- ``<PROJECT_NAME>Config.cmake`` (or
Packit ea1746
  ``<lower_case_project_name>-config.cmake``)
Packit ea1746
Packit ea1746
   Which is written by the developers of the project, and is
Packit ea1746
   configured with the selected options and installed locations when
Packit ea1746
   the project is built and defines the CMake variables:
Packit ea1746
   ``<PROJECT_NAME>_INCLUDE_DIRS`` & ``<PROJECT_NAME>_LIBRARIES``
Packit ea1746
   which are used by the caller to import the project.
Packit ea1746
Packit ea1746
The ``<PROJECT_NAME>Config.cmake`` typically includes a second file
Packit ea1746
installed to the same location:
Packit ea1746
Packit ea1746
- ``<PROJECT_NAME>Targets.cmake``
Packit ea1746
Packit ea1746
   Which is autogenerated by CMake as part of the install process and defines
Packit ea1746
   **imported targets** for the project in the caller's CMake scope.
Packit ea1746
Packit ea1746
An **imported target** contains the same information about a library
Packit ea1746
as a CMake target that was declared locally in the current CMake
Packit ea1746
project using ``add_library()``.  However, imported targets refer to
Packit ea1746
objects that have already been built by a different CMake project.
Packit ea1746
Principally, an imported target contains the location of the compiled
Packit ea1746
object and all of its public dependencies required to link against it.
Packit ea1746
Any locally declared target can depend on an imported target, and
Packit ea1746
CMake will manage the dependency chain, just as if the imported target
Packit ea1746
had been declared locally by the current project.
Packit ea1746
Packit ea1746
Crucially, just like any locally declared CMake target, an imported target is
Packit ea1746
identified by its **name** when adding it as a dependency to another target.
Packit ea1746
Packit ea1746
Thus, if in a project using Ceres you had the following in your CMakeLists.txt:
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    find_package(Ceres REQUIRED)
Packit ea1746
    message("CERES_LIBRARIES = ${CERES_LIBRARIES}")
Packit ea1746
Packit ea1746
You would see the output: ``CERES_LIBRARIES = ceres``.  **However**,
Packit ea1746
here ``ceres`` is an **imported target** created when
Packit ea1746
``CeresTargets.cmake`` was read as part of ``find_package(Ceres
Packit ea1746
REQUIRED)``.  It does **not** refer (directly) to the compiled Ceres
Packit ea1746
library: ``libceres.a/so/dylib/lib``.  This distinction is important,
Packit ea1746
as depending on the options selected when it was built, Ceres can have
Packit ea1746
public link dependencies which are encapsulated in the imported target
Packit ea1746
and automatically added to the link step when Ceres is added as a
Packit ea1746
dependency of another target by CMake.  In this case, linking only
Packit ea1746
against ``libceres.a/so/dylib/lib`` without these other public
Packit ea1746
dependencies would result in a linker error.
Packit ea1746
Packit ea1746
Note that this description applies both to projects that are
Packit ea1746
**installed** using CMake, and to those whose **build directory is
Packit ea1746
exported** using `export()
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/export.html>`_ (instead
Packit ea1746
of `install()
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/command/install.html>`_).  Ceres
Packit ea1746
supports both installation and export of its build directory if the
Packit ea1746
``EXPORT_BUILD_DIR`` option is enabled, see
Packit ea1746
:ref:`section-customizing`.
Packit ea1746
Packit ea1746
.. _section-install-vs-export:
Packit ea1746
Packit ea1746
Installing a project with CMake vs Exporting its build directory
Packit ea1746
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Packit ea1746
Packit ea1746
When a project is **installed**, the compiled libraries and headers
Packit ea1746
are copied from the source & build directory to the install location,
Packit ea1746
and it is these copied files that are used by any client code.  When a
Packit ea1746
project's build directory is **exported**, instead of copying the
Packit ea1746
compiled libraries and headers, CMake creates an entry for the project
Packit ea1746
in the `user's local CMake package registry
Packit ea1746
<http://www.cmake.org/cmake/help/v3.2/manual/cmake-packages.7.html#user-package-registry>`_,
Packit ea1746
``<USER_HOME>/.cmake/packages`` on Linux & OS X, which contains the
Packit ea1746
path to the project's build directory which will be checked by CMake
Packit ea1746
during a call to ``find_package()``.  The effect of which is that any
Packit ea1746
client code uses the compiled libraries and headers in the build
Packit ea1746
directory directly, **thus not requiring the project to be installed
Packit ea1746
to be used**.
Packit ea1746
Packit ea1746
Installing / Exporting a project that uses Ceres
Packit ea1746
--------------------------------------------------
Packit ea1746
Packit ea1746
As described in `Understanding the CMake Package System`_, the contents of
Packit ea1746
the ``CERES_LIBRARIES`` variable is the **name** of an imported target which
Packit ea1746
represents Ceres.  If you are installing / exporting your *own* project which
Packit ea1746
*uses* Ceres, it is important to understand that:
Packit ea1746
Packit ea1746
**Imported targets are not (re)exported when a project which imported them is
Packit ea1746
exported**.
Packit ea1746
Packit ea1746
Thus, when a project ``Foo`` which uses Ceres is exported, its list of
Packit ea1746
dependencies as seen by another project ``Bar`` which imports ``Foo``
Packit ea1746
via: ``find_package(Foo REQUIRED)`` will contain: ``ceres``.  However,
Packit ea1746
the definition of ``ceres`` as an imported target is **not
Packit ea1746
(re)exported** when Foo is exported.  Hence, without any additional
Packit ea1746
steps, when processing ``Bar``, ``ceres`` will not be defined as an
Packit ea1746
imported target.  Thus, when processing ``Bar``, CMake will assume
Packit ea1746
that ``ceres`` refers only to: ``libceres.a/so/dylib/lib`` (the
Packit ea1746
compiled Ceres library) directly if it is on the current list of
Packit ea1746
search paths.  In which case, no CMake errors will occur, but ``Bar``
Packit ea1746
will not link properly, as it does not have the required public link
Packit ea1746
dependencies of Ceres, which are stored in the imported target
Packit ea1746
defintion.
Packit ea1746
Packit ea1746
The solution to this is for ``Foo`` (i.e., the project that uses
Packit ea1746
Ceres) to invoke ``find_package(Ceres)`` in ``FooConfig.cmake``, thus
Packit ea1746
``ceres`` will be defined as an imported target when CMake processes
Packit ea1746
``Bar``.  An example of the required modifications to
Packit ea1746
``FooConfig.cmake`` are show below:
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    # Importing Ceres in FooConfig.cmake using CMake 2.8.x style.
Packit ea1746
    #
Packit ea1746
    # When configure_file() is used to generate FooConfig.cmake from
Packit ea1746
    # FooConfig.cmake.in, @Ceres_DIR@ will be replaced with the current
Packit ea1746
    # value of Ceres_DIR being used by Foo.  This should be passed as a hint
Packit ea1746
    # when invoking find_package(Ceres) to ensure that the same install of
Packit ea1746
    # Ceres is used as was used to build Foo.
Packit ea1746
    set(CERES_DIR_HINTS @Ceres_DIR@)
Packit ea1746
Packit ea1746
    # Forward the QUIET / REQUIRED options.
Packit ea1746
    if (Foo_FIND_QUIETLY)
Packit ea1746
       find_package(Ceres QUIET HINTS ${CERES_DIR_HINTS})
Packit ea1746
    elseif (Foo_FIND_REQUIRED)
Packit ea1746
       find_package(Ceres REQUIRED HINTS ${CERES_DIR_HINTS})
Packit ea1746
    else ()
Packit ea1746
       find_package(Ceres HINTS ${CERES_DIR_HINTS})
Packit ea1746
    endif()
Packit ea1746
Packit ea1746
.. code-block:: cmake
Packit ea1746
Packit ea1746
    # Importing Ceres in FooConfig.cmake using CMake 3.x style.
Packit ea1746
    #
Packit ea1746
    # In CMake v3.x, the find_dependency() macro exists to forward the REQUIRED
Packit ea1746
    # / QUIET parameters to find_package() when searching for dependencies.
Packit ea1746
    #
Packit ea1746
    # Note that find_dependency() does not take a path hint, so if Ceres was
Packit ea1746
    # installed in a non-standard location, that location must be added to
Packit ea1746
    # CMake's search list before this call.
Packit ea1746
    include(CMakeFindDependencyMacro)
Packit ea1746
    find_dependency(Ceres)