Skip to content

Commit

Permalink
(Project) Build lib only.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdesnos committed Oct 30, 2019
1 parent 6a01701 commit 9180d23
Show file tree
Hide file tree
Showing 5 changed files with 1,183 additions and 0 deletions.
161 changes: 161 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
cmake_minimum_required(VERSION 3.5)

### CUSTOMIZATION STEPS
# 1. Change the argument to project() with the name of your library/package
# (it will be the one used for find_package() call)
# 2. Change version information
# 3. Add your library source files in the subdirectories
# - src/"LibName"/include
# - src/"LibName"/src
# 4. Add your executable source files in the subdirectories
# - src/"ExecName"
# 5. Update src/CMakeLists.txt
# 6. Add the tests to your library in /test subdirectory and modify
# test/CMakeLists.txt accordingly.
###

# This sets the PROJECT_NAME, PROJECT_VERSION as well as other variable
# listed here: https://cmake.org/cmake/help/latest/command/project.html.
# We use this name to export all the files such that is then possible to use
# find_package(LibTemplateCMake) in third party projects.
# LANGUAGES specifies which languages your project supports. To have a broad
# support with external libraries, it is good practice to list both CXX and C.
# Infact, some legacy Find<package>.cmake files that require the C language to
# be enabled and thus using only CXX may cause issues. Note that by default when
# LANGUAGES is not specified CMake enables both CXX and C.
project(tpglib
LANGUAGES CXX
VERSION 0.0.0)

# Defines the CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR and many other useful macros.
# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
include(GNUInstallDirs)

# Control where libraries and executables are placed during the build.
# With the following settings executables are placed in <the top level of the
# build tree>/bin and libraries/archives in <top level of the build tree>/lib.
# This is particularly useful to run ctests on libraries built on Windows
# machines: tests, which are executables, are placed in the same folders of
# dlls, which are treated as executables as well, so that they can properly
# find the libraries to run. This is a because of missing RPATH on Windows.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

# To build shared libraries in Windows, we set CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to TRUE.
# See https://cmake.org/cmake/help/v3.4/variable/CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS.html
# See https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# Under MSVC, we set CMAKE_DEBUG_POSTFIX to "d" to add a trailing "d" to library
# built in debug mode. In this Windows user can compile, build and install the
# library in both Release and Debug configuration avoiding naming clashes in the
# installation directories.
if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
add_definitions( -D_CRT_SECURE_NO_WARNINGS )
# Install in dedicated subfolder
set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/${PROJECT_NAME}-${PROJECT_VERSION})
endif()

# Build position independent code.
# Position Independent Code (PIC) is commonly used for shared libraries so that
# the same shared library code can be loaded in each program address space in a
# location where it will not overlap with any other uses of such memory.
# In particular, this option avoids problems occurring when a process wants to
# load more than one shared library at the same virtual address.
# Since shared libraries cannot predict where other shared libraries could be
# loaded, this is an unavoidable problem with the traditional shared library
# concept.
# Generating position-independent code is often the default behavior for most
# modern compilers.
# Moreover linking a static library that is not built with PIC from a shared
# library will fail on some compiler/architecture combinations.
# Further details on PIC can be found here:
# https://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Disable C and C++ compiler extensions.
# C/CXX_EXTENSIONS are ON by default to allow the compilers to use extended
# variants of the C/CXX language.
# However, this could expose cross-platform bugs in user code or in the headers
# of third-party dependencies and thus it is strongly suggested to turn
# extensions off.
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# We use
# - InstallBasicPackageFiles (http://robotology.github.io/ycm/gh-pages/v0.8/module/InstallBasicPackageFiles.html)
# - AddUninstallTarget (http://robotology.github.io/ycm/gh-pages/v0.8/module/AddUninstallTarget.html)
# - AddInstallRPATHSupport (http://robotology.github.io/ycm/gh-pages/v0.8/module/AddInstallRPATHSupport.html)
# from YCM. Files are under the cmake subdirectory.
# See https://github.com/robotology/ycm/
# If you don't want to ship these files with your code (recommended), you can
# instead depend on YCM.
# In this case replace the following line with
# find_package(YCM REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

### Options
# Shared/Dynamic or Static library?
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)

# Build test related commands?
option(BUILD_TESTING "Create tests using CMake" OFF)
if(BUILD_TESTING)
enable_testing()
endif()

# Enable RPATH support for installed binaries and libraries
include(AddInstallRPATHSupport)
add_install_rpath_support(BIN_DIRS "${CMAKE_INSTALL_FULL_BINDIR}"
LIB_DIRS "${CMAKE_INSTALL_FULL_LIBDIR}"
INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}"
USE_LINK_PATH)

# Encourage user to specify a build type (e.g. Release, Debug, etc.), otherwise set it to Release.
if(NOT CMAKE_CONFIGURATION_TYPES)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to 'Release' as none was specified.")
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY VALUE "Release")
endif()
endif()

### Compile- and install-related commands.
add_subdirectory(tpglib)

# Create and install CMake configuration files for your project that are
# necessary to for other projects to call find_package(LibTemplateCMake).
#
# Note that it is extremely important to use exactly the project name while
# installing configuration files (you can use PROJECT_NAME variable to avoid
# any possible error). This is required to allow find_package() to properly
# look for the installed library in system path, in particular in Windows when
# the installation is performed in the default path.
#
# install_basic_package_files() comes with many input parameters to customize
# the configuration files. The parameters used in the following call provide
# basic versions of CMake configuration files.
# See install_basic_package_files() documentation found in ./cmake folder.
#
# Note that if your library depends from other libraries, you are probably
# required to used the install_basic_package_files() DEPENDENCIES option.
include(InstallBasicPackageFiles)
install_basic_package_files(${PROJECT_NAME}
VERSION ${${PROJECT_NAME}_VERSION}
COMPATIBILITY AnyNewerVersion
VARS_PREFIX ${PROJECT_NAME}
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
# Add the uninstall target
include(AddUninstallTarget)

# Add integration tests (unit tests for each library should be in each sublibrary directory).
if(BUILD_TESTING)
add_subdirectory(test)
endif()

# Add targets related to doxygen documention generation
# add_subdirectory(doc)
167 changes: 167 additions & 0 deletions cmake/AddInstallRPATHSupport.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#.rst:
# AddInstallRPATHSupport
# ----------------------
#
# Add support to RPATH during installation to your project::
#
# add_install_rpath_support([BIN_DIRS dir [dir]]
# [LIB_DIRS dir [dir]]
# [INSTALL_NAME_DIR [dir]]
# [DEPENDS condition [condition]]
# [USE_LINK_PATH])
#
# Normally (depending on the platform) when you install a shared
# library you can either specify its absolute path as the install name,
# or leave just the library name itself. In the former case the library
# will be correctly linked during run time by all executables and other
# shared libraries, but it must not change its install location. This
# is often the case for libraries installed in the system default
# library directory (e.g. ``/usr/lib``).
# In the latter case, instead, the library can be moved anywhere in the
# file system but at run time the dynamic linker must be able to find
# it. This is often accomplished by setting environmental variables
# (i.e. ``LD_LIBRARY_PATH`` on Linux).
# This procedure is usually not desirable for two main reasons:
#
# - by setting the variable you are changing the default behaviour
# of the dynamic linker thus potentially breaking executables (not as
# destructive as ``LD_PRELOAD``)
# - the variable will be used only by applications spawned by the shell
# and not by other processes.
#
# RPATH is aimed to solve the issues introduced by the second
# installation method. Using run-path dependent libraries you can
# create a directory structure containing executables and dependent
# libraries that users can relocate without breaking it.
# A run-path dependent library is a dependent library whose complete
# install name is not known when the library is created.
# Instead, the library specifies that the dynamic loader must resolve
# the library’s install name when it loads the executable that depends
# on the library. The executable or the other shared library will
# hardcode in the binary itself the additional search directories
# to be passed to the dynamic linker. This works great in conjunction
# with relative paths.
# This command will enable support to RPATH to your project.
# It will enable the following things:
#
# - If the project builds shared libraries it will generate a run-path
# enabled shared library, i.e. its install name will be resolved
# only at run time.
# - In all cases (building executables and/or shared libraries)
# dependent shared libraries with RPATH support will be properly
#
# The command has the following parameters:
#
# Options:
# - ``USE_LINK_PATH``: if passed the command will automatically adds to
# the RPATH the path to all the dependent libraries.
#
# Arguments:
# - ``BIN_DIRS`` list of directories when the targets (executable and
# plugins) will be installed.
# - ``LIB_DIRS`` list of directories to be added to the RPATH. These
# directories will be added "relative" w.r.t. the ``BIN_DIRS`` and
# ``LIB_DIRS``.
# - ``INSTALL_NAME_DIR`` directory where the libraries will be installed.
# This variable will be used only if ``CMAKE_SKIP_RPATH`` or
# ``CMAKE_SKIP_INSTALL_RPATH`` is set to ``TRUE`` as it will set the
# ``INSTALL_NAME_DIR`` on all targets
# - ``DEPENDS`` list of conditions that should be ``TRUE`` to enable
# RPATH, for example ``FOO; NOT BAR``.
#
# Note: see https://gitlab.kitware.com/cmake/cmake/issues/16589 for further
# details.

#=======================================================================
# Copyright 2014 Istituto Italiano di Tecnologia (IIT)
# @author Francesco Romano <[email protected]>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=======================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)


include(CMakeParseArguments)


function(ADD_INSTALL_RPATH_SUPPORT)

set(_options USE_LINK_PATH)
set(_oneValueArgs INSTALL_NAME_DIR)
set(_multiValueArgs BIN_DIRS
LIB_DIRS
DEPENDS)

cmake_parse_arguments(_ARS "${_options}"
"${_oneValueArgs}"
"${_multiValueArgs}"
"${ARGN}")

# if either RPATH or INSTALL_RPATH is disabled
# and the INSTALL_NAME_DIR variable is set, then hardcode the install name
if(CMAKE_SKIP_RPATH OR CMAKE_SKIP_INSTALL_RPATH)
if(DEFINED _ARS_INSTALL_NAME_DIR)
set(CMAKE_INSTALL_NAME_DIR ${_ARS_INSTALL_NAME_DIR} PARENT_SCOPE)
endif()
endif()

if (CMAKE_SKIP_RPATH OR (CMAKE_SKIP_INSTALL_RPATH AND CMAKE_SKIP_BUILD_RPATH))
return()
endif()


set(_rpath_available 1)
if(DEFINED _ARS_DEPENDS)
foreach(_dep ${_ARS_DEPENDS})
string(REGEX REPLACE " +" ";" _dep "${_dep}")
if(NOT (${_dep}))
set(_rpath_available 0)
endif()
endforeach()
endif()

if(_rpath_available)

# Enable RPATH on OSX.
set(CMAKE_MACOSX_RPATH TRUE PARENT_SCOPE)

# Find system implicit lib directories
set(_system_lib_dirs ${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES})
if(EXISTS "/etc/debian_version") # is this a debian system ?
if(CMAKE_LIBRARY_ARCHITECTURE)
list(APPEND _system_lib_dirs "/lib/${CMAKE_LIBRARY_ARCHITECTURE}"
"/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
endif()
endif()
# This is relative RPATH for libraries built in the same project
foreach(lib_dir ${_ARS_LIB_DIRS})
list(FIND _system_lib_dirs "${lib_dir}" isSystemDir)
if("${isSystemDir}" STREQUAL "-1")
foreach(bin_dir ${_ARS_LIB_DIRS} ${_ARS_BIN_DIRS})
file(RELATIVE_PATH _rel_path ${bin_dir} ${lib_dir})
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
list(APPEND CMAKE_INSTALL_RPATH "@loader_path/${_rel_path}")
else()
list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/${_rel_path}")
endif()
endforeach()
endif()
endforeach()
if(NOT "${CMAKE_INSTALL_RPATH}" STREQUAL "")
list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH)
endif()
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} PARENT_SCOPE)

# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ${_ARS_USE_LINK_PATH} PARENT_SCOPE)

endif()

endfunction()
Loading

0 comments on commit 9180d23

Please sign in to comment.