Commit ab50ab84 authored by Davis King's avatar Davis King

Make CMake build dlib statically whenever DLIB_IN_PROJECT_BUILD==true,

regardless of the state of BUILD_SHARED_LIBS.  This means projects that
use dlib by saying add_subdirectory(dlib) will always statically link to
dlib, unless DLIB_IN_PROJECT_BUILD is explicltly set to false and
BUILD_SHARED_LIBS set to true.
parent 941d764c
......@@ -2,14 +2,35 @@ cmake_minimum_required(VERSION 2.8.12)
#############################################################################
# #
# READ examples/CMakeLists.txt TO SEE HOW TO USE DLIB FROM C++ WITH CMAKE #
# #
#############################################################################
get_directory_property(has_parent PARENT_DIRECTORY)
if(NOT has_parent)
# Set this so that the dlib subfolder will build both a static and shared
# library, since the only reason to build this CMakeLists.txt by itself is
# if you want to install dlib. It should be noted however that installing
# dlib is not necessary. A simpler approach is to use it the way shown in
# examples/CMakeLists.txt
# When you call add_subdirectory(dlib) from a parent CMake project dlib's
# CMake scripts will assume you want to statically compile dlib into
# whatever you are building rather than create a standalone copy of dlib.
# This means CMake will build dlib as a static library, disable dlib's
# install targets so they don't clutter your project, and adjust a few other
# minor things that are convenient when statically building dlib as part of
# your own projects.
#
# On the other hand, if there is no parent CMake project or if
# DLIB_IN_PROJECT_BUILD is set to false, CMake will compile dlib as a normal
# standalone library (either shared or static, based on the state of CMake's
# BUILD_SHARED_LIBS flag), and include the usual install targets so you can
# install dlib on your computer via `make install`. Since the only reason
# to build this CMakeLists.txt (the one you are reading right now) by itself
# is if you want to install dlib, we indicate as such by setting
# DLIB_IN_PROJECT_BUILD to false.
set(DLIB_IN_PROJECT_BUILD false)
endif()
add_subdirectory(dlib)
......@@ -32,6 +32,31 @@ if(has_parent)
endif()
endif()
if (DLIB_IN_PROJECT_BUILD)
# DLIB_IN_PROJECT_BUILD==true means you are using dlib by invoking
# add_subdirectory(dlib) in the parent project. In this case, we always want
# to build dlib as a static library so the parent project doesn't need to
# deal with some random dlib shared library file. It is much better to
# statically compile dlib into the parent project. So the following bit of
# CMake ensures that happens. However, we have to take care to compile dlib
# with position independent code if appropriate (i.e. if the parent project
# is a shared library).
if (BUILD_SHARED_LIBS)
if (CMAKE_COMPILER_IS_GNUCXX)
# Just setting CMAKE_POSITION_INDEPENDENT_CODE should be enough to set
# -fPIC for GCC but sometimes it still doesn't get set, so make sure it
# does.
add_definitions("-fPIC")
endif()
set(CMAKE_POSITION_INDEPENDENT_CODE true)
endif()
# Tell cmake to build dlib as a static library
set(BUILD_SHARED_LIBS false)
endif()
if (CMAKE_VERSION VERSION_LESS "3.9.0")
# Set only because there are old target_link_libraries() statements in the
# FindCUDA.cmake file that comes with CMake that error out if the new behavior
......@@ -710,6 +735,9 @@ if (NOT TARGET dlib)
if (DLIB_IN_PROJECT_BUILD)
target_compile_options(dlib PUBLIC ${active_preprocessor_switches})
else()
# These are private in this case because they will be controlled by the
# contents of dlib/config.h once it's installed. But for in project
# builds, there is no real config.h so they are public in the above case.
target_compile_options(dlib PRIVATE ${active_preprocessor_switches})
endif()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment