Skip to content

Commit

Permalink
Merge pull request #672 from alicevision/modernCmakeFixes
Browse files Browse the repository at this point in the history
[cmake] Modern CMake use as 3rd library
  • Loading branch information
fabiencastan authored Aug 7, 2019
2 parents db280ae + 92aae33 commit dba8eb1
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 195 deletions.
29 changes: 17 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@ before_script:
- >
cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DCMAKE_INSTALL_PREFIX:PATH=${ALICEVISION_INSTALL} \
-DTARGET_ARCHITECTURE=core \
-DALICEVISION_BUILD_TESTS=ON \
-DALICEVISION_BUILD_EXAMPLES=ON \
-DALICEVISION_USE_OPENCV=ON \
-DALICEVISION_USE_CUDA=OFF \
-DOpenCV_DIR="${DEPS_INSTALL_PATH}/share/OpenCV" \
-DALICEVISION_USE_OPENGV=ON \
-DOPENGV_DIR="${DEPS_INSTALL_PATH}" \
-DBOOST_NO_CXX11=ON \
-DCeres_DIR="${DEPS_INSTALL_PATH}/share/Ceres" \
-DEIGEN_INCLUDE_DIR_HINTS="${DEPS_INSTALL_PATH}" \
-DALICEVISION_USE_CCTAG=OFF \
-DALICEVISION_BUILD_TESTS:BOOL=ON \
-DALICEVISION_BUILD_EXAMPLES:BOOL=ON \
-DALICEVISION_USE_OPENCV:BOOL=ON \
-DALICEVISION_USE_CUDA:BOOL=OFF \
-DOpenCV_DIR:PATH="${DEPS_INSTALL_PATH}/share/OpenCV" \
-DALICEVISION_USE_OPENGV:BOOL=ON \
-DOPENGV_DIR:PATH="${DEPS_INSTALL_PATH}" \
-DBOOST_NO_CXX11:BOOL=ON \
-DCeres_DIR:PATH="${DEPS_INSTALL_PATH}/share/Ceres" \
-DEIGEN_INCLUDE_DIR_HINTS:PATH="${DEPS_INSTALL_PATH}" \
-DALICEVISION_USE_CCTAG:BOOL=OFF \
-DALICEVISION_USE_ALEMBIC:BOOL=ON \
-DAlembic_DIR:PATH="${DEPS_INSTALL_PATH}/lib/cmake/Alembic" \
. $ALICEVISION_SOURCE
Expand All @@ -87,10 +88,14 @@ before_script:

script:
# limit GCC builds to a reduced number of thread for the virtual machine
- make -j 2 VERBOSE=1
- make install -j 2 VERBOSE=1
- export OMP_NUM_THREADS=2
# Perform unit tests only on GCC builds
- if [ "$CC" = "gcc" ]; then make test; fi
- mkdir -p ${ALICEVISION_BUILD_AS3RDPARTY}
- cd ${ALICEVISION_BUILD_AS3RDPARTY}
- cmake -DCMAKE_PREFIX_PATH:PATH="${ALICEVISION_INSTALL};${DEPS_INSTALL_PATH}" ${ALICEVISION_SAMPLE_AS3RDPARTY}
- make
# Perform benchmark through ground truth tests with different scenes
- >
if [ -n "$TRAVIS_PULL_REQUEST" ]; then # limit to PR builds
Expand Down
54 changes: 30 additions & 24 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Build instructions
------------------

Required tools:
* CMake >= 3.4
* CMake >= 3.4
* Git
* C/C++ compiler (gcc or visual studio or clang) with C++11 support.

Expand Down Expand Up @@ -67,18 +67,18 @@ To build AliceVision using vcpkg:
1. Setup the environment

#### Windows: CUDA and Visual Studio
* CUDA >= 10.0
CUDA 10.0 is fully compatible with **Visual Studio 2017 (Update 8)** which allows to use
the most recent compiler toolset (v141) to build AliceVision and its dependencies.
VS2015 can also be used in this configuration.
* CUDA >= 10.0
CUDA 10.0 is fully compatible with **Visual Studio 2017 (Update 8)** which allows to use
the most recent compiler toolset (v141) to build AliceVision and its dependencies.
VS2015 can also be used in this configuration.
*This requires a recent version of CMake (tested with 3.12).*

* CUDA < 10.0
* CUDA < 10.0
Due to incompatibilities between CUDA and Visual Studio 2017, **Visual 2015 toolset** must be used to build AliceVision.
Visual Studio 2017 can be used as an IDE, but v140 toolset must be installed and used for this to work.
Visual Studio 2017 can be used as an IDE, but v140 toolset must be installed and used for this to work.
To setup a VS2017 command prompt using the v140 toolset:
```bash
# Windows: CUDA < 10.0 + VS2017 v140 toolset
# Windows: CUDA < 10.0 + VS2017 v140 toolset
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64 -vcvars_ver=14.0
nmake /version
# should print Version 14.00.xxx
Expand Down Expand Up @@ -148,14 +148,14 @@ In order to build the library with existing versions of the dependencies (e.g. s
* For FLANN library, `FLANN_INCLUDE_DIR_HINTS` can be passed pointing to the include directory, e.g.
`-DFLANN_INCLUDE_DIR_HINTS:PATH=/path/to/flann/1.8.4/include/`

* For Eigen library, `CMAKE_MODULE_PATH` should be passed pointing at the `<EigenInstallDir>/share/cmake/Modules/` directory of the Eigen installation, in which `Eigen-config.cmake` or `FindEigen3.cmake` can be found.
* For Eigen library, `CMAKE_MODULE_PATH` should be passed pointing at the `<EigenInstallDir>/share/cmake/Modules/` directory of the Eigen installation, in which `Eigen-config.cmake` or `FindEigen3.cmake` can be found.
In case only `FindEigen3.cmake` is available (e.g. Homebrew installations), an environment variable `EIGEN_ROOT_DIR` must be set pointing at Eigen install directory.
For example,

`-DCMAKE_MODULE_PATH:PATH=/usr/local/Cellar/eigen/3.3.4/share/cmake/Modules/`

may require to set the environment variable if only `FindEigen3.cmake`, i.e.

`export EIGEN_ROOT_DIR=/usr/local/Cellar/eigen/3.3.4/`

* For OpenEXR library, `OPENEXR_HOME` can be passed pointing to the install directory, e.g.
Expand Down Expand Up @@ -212,7 +212,7 @@ CMake Options
Build with Alembic file format support (required version >= 1.7).
`-DAlembic_DIR:PATH=/path/to/alembic/install/lib/cmake/Alembic/` (where AlembicConfig.cmake can be found)
With old Alembic versions (<1.6), you need to set many variables: `ALEMBIC_ROOT`, `ALEMBIC_HDF5_ROOT`, `ALEMBIC_ILMBASE_ROOT`, `ALEMBIC_OPENEXR_ROOT`.

* `ALICEVISION_USE_OPENMP` (default: `AUTO`)
Enable OpenMP parallelization

Expand All @@ -223,16 +223,16 @@ CMake Options
* `ALICEVISION_USE_POPSIFT` (default: `AUTO`)
Enable GPU SIFT implementation.
`-DPopSift_DIR:PATH=/path/to/popsift/install/lib/cmake/PopSift` (where PopSiftConfig.cmake can be found)

* `ALICEVISION_USE_UNCERTAINTYTE` (default: `AUTO`)
Enable Uncertainty computation.
`-DUNCERTAINTYTE_DIR:PATH=/path/to/uncertaintyTE/install/` (where `inlude` and `lib` can be found)
`-DMAGMA_ROOT:PATH=/path/to/magma/install/` (where `inlude` and `lib` can be found)

* `ALICEVISION_USE_OPENCV` (default: `OFF`)
Build with openCV
`-DOpenCV_DIR:PATH=/path/to/opencv/install/share/OpenCV/` (where OpenCVConfig.cmake can be found)

* `ALICEVISION_REQUIRE_CERES_WITH_SUITESPARSE` (default: `ON`)
By default, aliceVision requires Ceres built with SuiteSparse to ensure best performances but you can make SuiteSparse optional with this flag.

Expand Down Expand Up @@ -350,26 +350,32 @@ Using AliceVision as a third party library dependency in cmake
--------------------------------------------------------------

AliceVision can be used as a third party once it have been installed.
Because it can use its own Ceres version, it is better to install it locally and not in system files.
So please consider using the `CMAKE_INSTALL_PREFIX` cmake variable to specify a local installation directory.

Here the syntax to add the variable to the cmake command line (use absolute path), e.g.:
Consider using the `CMAKE_INSTALL_PREFIX` cmake variable to specify a local installation directory.
Here the syntax to add the variable to the cmake command line (use absolute path), e.g.:
```bash
-DCMAKE_INSTALL_PREFIX="/home/user/dev/AliceVision_install"
```

Perform `make` and `make install`

Then you will be able to use AliceVision as an external library in your `CMakeLists.txt`:
Then you will be able to use AliceVision as an external library in your `CMakeLists.txt` using
the modern CMake approach as imported target. For example, if your target `main` depends on the
AliceVision module `aliceVision_sfmDataIO`:

```cmake
find_package(AliceVision REQUIRED)
include_directories(${ALICEVISION_INCLUDE_DIRS})
find_package(AliceVision CONFIG REQUIRED)
message(STATUS "Found AliceVision : ${AliceVision_FOUND}")
message(STATUS "Found AliceVision version: ${AliceVision_VERSION}")
add_executable(main main.cpp)
target_link_libraries(main ${ALICEVISION_LIBRARIES})
target_link_libraries(main PUBLIC aliceVision_sfmDataIO)
```

Specify to CMake where AliceVision is installed by using the `AliceVision_DIR` cmake variable: `-DAliceVision_DIR:STRING="YourInstallPath"/share/aliceVision/cmake`
In general, you need to specify the list of the AliceVision modules that your library or executable
depends on.

Specify to CMake where AliceVision is installed by using the `AliceVision_DIR` cmake variable: `-DAliceVision_DIR:PATH="YourInstallPath"/share/aliceVision/cmake`
or by simply adding the installation path to your `CMAKE_PREFIX_PATH`, i.e. `-DCMAKE_PREFIX_PATH:PATH="YourInstallPath"`.
Check the sample in [samples](src/samples/aliceVisionAs3rdParty) for an example of use.

### Docker image

Expand Down
2 changes: 2 additions & 0 deletions ci/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ download_files_from_tar()
export ALICEVISION_SOURCE="${TRAVIS_BUILD_DIR}"
export ALICEVISION_BUILD="${TRAVIS_BUILD_DIR}/build"
export ALICEVISION_INSTALL="${TRAVIS_BUILD_DIR}/install"
export ALICEVISION_BUILD_AS3RDPARTY="${TRAVIS_BUILD_DIR}/buildAs3rdparty"
export ALICEVISION_SAMPLE_AS3RDPARTY="${ALICEVISION_SOURCE}/src/samples/aliceVisionAs3rdParty/"
# GT datasets for tests
export GT_TEST_ROOT="${TRAVIS_BUILD_DIR}/gt_test"
export GT_TEST_SOURCE="${GT_TEST_ROOT}/gt_source"
Expand Down
98 changes: 50 additions & 48 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
cmake_minimum_required(VERSION 3.3)

project(aliceVisionSrc LANGUAGES C CXX)
# ==============================================================================
# AliceVision version
# ==============================================================================
file(STRINGS "aliceVision/version.hpp" _ALICEVISION_VERSION_HPP_CONTENTS REGEX "#define ALICEVISION_VERSION_")
foreach(v MAJOR MINOR REVISION)
if("${_ALICEVISION_VERSION_HPP_CONTENTS}" MATCHES "#define ALICEVISION_VERSION_${v} ([0-9]+)")
set(ALICEVISION_VERSION_${v} "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Failed to retrieve the AliceVision version the source code. Missing ALICEVISION_VERSION_${v}.")
endif()
endforeach()
set(ALICEVISION_VERSION ${ALICEVISION_VERSION_MAJOR}.${ALICEVISION_VERSION_MINOR}.${ALICEVISION_VERSION_REVISION})

project(aliceVisionSrc LANGUAGES C CXX VERSION ${ALICEVISION_VERSION})

# Guard against in-source builds
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
Expand All @@ -27,19 +40,6 @@ function(trilean_option NAME DESCRIPTION DEFAULT_VALUE)
endif()
endfunction()

# ==============================================================================
# AliceVision version
# ==============================================================================
file(STRINGS "aliceVision/version.hpp" _ALICEVISION_VERSION_HPP_CONTENTS REGEX "#define ALICEVISION_VERSION_")
foreach(v MAJOR MINOR REVISION)
if("${_ALICEVISION_VERSION_HPP_CONTENTS}" MATCHES "#define ALICEVISION_VERSION_${v} ([0-9]+)")
set(ALICEVISION_VERSION_${v} "${CMAKE_MATCH_1}")
else()
message(FATAL_ERROR "Failed to retrieve the AliceVision version the source code. Missing ALICEVISION_VERSION_${v}.")
endif()
endforeach()
set(ALICEVISION_VERSION ${ALICEVISION_VERSION_MAJOR}.${ALICEVISION_VERSION_MINOR}.${ALICEVISION_VERSION_REVISION})

# C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down Expand Up @@ -96,7 +96,7 @@ if(ALICEVISION_USE_RPATH)
set(CMAKE_INSTALL_RPATH "\\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:\\$ORIGIN")
endif()
endif()


# Set build path
set(EXECUTABLE_OUTPUT_PATH "${ALICEVISION_ROOT}/${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
Expand Down Expand Up @@ -369,7 +369,7 @@ if(ALICEVISION_BUILD_SFM)
message(STATUS "CERES_LIBRARIES: ${CERES_LIBRARIES}")
message(STATUS "CERES_INCLUDE_DIRS: ${CERES_INCLUDE_DIRS}")
if(WIN32)
# avoid 'ERROR' macro clashing on Windows
# avoid 'ERROR' macro clashing on Windows
add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES)
endif()
include_directories(${CERES_INCLUDE_DIRS})
Expand Down Expand Up @@ -688,7 +688,7 @@ if(NOT ALICEVISION_USE_CUDA STREQUAL "OFF")
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
# Workaround to force deactivation of cuda static runtime for cmake < 3.10
set(CUDA_cudart_static_LIBRARY 0)
else()
else()
message(STATUS "BUILD_SHARED_LIBS OFF")
option(CUDA_USE_STATIC_CUDA_RUNTIME "Use the static version of the CUDA runtime library if available" ON)
set(CUDA_USE_STATIC_CUDA_RUNTIME ON)
Expand Down Expand Up @@ -982,52 +982,54 @@ add_subdirectory(software)
# Install rules
# ==============================================================================

install(EXPORT aliceVision-targets
DESTINATION ${CMAKE_INSTALL_DATADIR}/aliceVision/cmake FILE AliceVisionTargets.cmake)
# Include module with fuction 'write_basic_package_version_file'
include(CMakePackageConfigHelpers)

#Adapt build include paths to install path
set(ALICEVISION_INCLUDE_DIRS
"${ALICEVISION_INCLUDE_DIRS}"
"${CMAKE_INSTALL_PREFIX}/include/aliceVision")

list(REMOVE_ITEM ALICEVISION_INCLUDE_DIRS ${generatedDir})
string(REGEX REPLACE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_INSTALL_PREFIX}/include"
ALICEVISION_INCLUDE_DIRS
"${ALICEVISION_INCLUDE_DIRS}"
)
# name of the exported project (cannot use PROJECT_NAME as it is AliceVisionSrc
set(exportedProjectName "AliceVision")
# the generated cmake config file
set(cmakeVersionConfig "${generatedDir}/${exportedProjectName}ConfigVersion.cmake")
# the generated cmake version file
set(cmakeProjectConfig "${generatedDir}/${exportedProjectName}Config.cmake")
# place to install the cmake-related files
set(cmakeConfigInstallDir "${CMAKE_INSTALL_DATADIR}/aliceVision/cmake")
# the name of the cmake Target file to export
set(targetsExportName "${exportedProjectName}Targets")

string(REGEX REPLACE
"dependencies"
"aliceVision_dependencies"
ALICEVISION_INCLUDE_DIRS
"${ALICEVISION_INCLUDE_DIRS}"
)
# generate '<PROJECT-NAME>ConfigVersion.cmake'
# Note: major version number must be the same as requested
write_basic_package_version_file("${cmakeVersionConfig}" COMPATIBILITY SameMajorVersion)

# Configure '<PROJECT-NAME>Config.cmake'
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/AliceVisionConfig.cmake.in"
"${cmakeProjectConfig}"
INSTALL_DESTINATION "${cmakeConfigInstallDir}")

# Create a AliceVisionConfig.cmake file. <name>Config.cmake files are searched by
# find_package() automatically. We configure that file so that we can put any
# information we want in it, e.g. version numbers, include directories, etc.
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/AliceVisionConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/AliceVisionConfig.cmake" @ONLY)
# this exports the targets only to be used for the in-tree compilation, do not export this
export(EXPORT aliceVision-targets FILE "${generatedDir}/${targetsExportName}.cmake")

# generate and install the target.cmake file to be used when the library is installed.
install(EXPORT aliceVision-targets DESTINATION ${cmakeConfigInstallDir} FILE ${targetsExportName}.cmake)

# install ${PROJECT_NAME}ConfigVersion.cmake and ${PROJECT_NAME}Config.cmake
install(FILES "${cmakeProjectConfig}" "${cmakeVersionConfig}"
DESTINATION "${cmakeConfigInstallDir}")

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/AliceVisionConfig.cmake"
DESTINATION ${CMAKE_INSTALL_DATADIR}/aliceVision/cmake)

# create the config.hpp file containing all the preprocessor definitions
set(configfile "${generatedDir}/aliceVision/config.hpp")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.hpp.in"
"${configfile}" @ONLY)


install(FILES "${configfile}"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/aliceVision")


# Add uninstall target
set(cmakeUninstallFile "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in" ${cmakeUninstallFile}
IMMEDIATE @ONLY
)

add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_BINARY_DIR}/../cmake/cmake_uninstall.cmake")
add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${cmakeUninstallFile}")
Loading

0 comments on commit dba8eb1

Please sign in to comment.