Unverified Commit 61f747de authored by Davis E. King's avatar Davis E. King Committed by GitHub

Only define one dlib target in CMake and shared vs. static is determined by…

Only define one dlib target in CMake and shared vs. static is determined by BUILD_SHARED_LIBS variable. (#1138)

* Changed cmake so that there is only the dlib target and it isn't forced
to be static or shared, instead, the build type will toggle based on the
state of CMake's BUILD_SHARED_LIBS variable.

* 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 2f8d7053
......@@ -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
......@@ -195,11 +220,7 @@ if (NOT TARGET dlib)
set(dlib_needed_includes)
if (DLIB_ISO_CPP_ONLY)
add_library(dlib STATIC ${source_files} )
if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
add_library(dlib_shared SHARED ${source_files} )
add_dependencies(dlib_shared dlib)
endif()
add_library(dlib ${source_files} )
else()
set(source_files ${source_files}
......@@ -697,18 +718,9 @@ if (NOT TARGET dlib)
# The old cuda_add_library() command doesn't support CMake's newer dependency
# stuff, so we have to set the include path manually still, which we do here.
include_directories(${dlib_needed_includes})
cuda_add_library(dlib STATIC ${source_files} )
cuda_add_library(dlib ${source_files} )
else()
add_library(dlib STATIC ${source_files} )
endif()
if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
if (DLIB_USE_CUDA)
cuda_add_library(dlib_shared SHARED ${source_files} )
add_dependencies(dlib_shared dlib)
else()
add_library(dlib_shared SHARED ${source_files} )
add_dependencies(dlib_shared dlib)
endif()
add_library(dlib ${source_files} )
endif()
endif () ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################
......@@ -719,21 +731,15 @@ if (NOT TARGET dlib)
INTERFACE $<INSTALL_INTERFACE:include>
PUBLIC ${dlib_needed_includes}
)
target_link_libraries(dlib PRIVATE ${dlib_needed_libraries})
target_link_libraries(dlib PUBLIC ${dlib_needed_libraries})
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()
if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
target_include_directories(dlib_shared
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
INTERFACE $<INSTALL_INTERFACE:include>
PUBLIC ${dlib_needed_includes}
)
target_link_libraries(dlib_shared PUBLIC ${dlib_needed_libraries})
target_compile_options(dlib_shared PRIVATE ${active_preprocessor_switches})
endif()
# Allow the unit tests to ask us to compile the all/source.cpp file just to make sure it compiles.
......@@ -748,29 +754,16 @@ if (NOT TARGET dlib)
enable_cpp11_for_target(dlib)
target_compile_options(dlib PUBLIC ${active_compile_opts})
endif()
if (TARGET dlib_shared)
enable_cpp11_for_target(dlib_shared)
target_compile_options(dlib_shared PUBLIC ${active_compile_opts})
endif()
# Install the library
if (NOT DLIB_IN_PROJECT_BUILD)
if(UNIX)
set_target_properties(dlib_shared PROPERTIES
OUTPUT_NAME dlib
VERSION ${VERSION})
install(TARGETS dlib dlib_shared
EXPORT dlib
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # Windows (including cygwin) considers .dll to be runtime artifacts
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
else()
install(TARGETS dlib
EXPORT dlib
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # Windows considers .dll to be runtime artifacts
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
set_target_properties(dlib PROPERTIES
VERSION ${VERSION})
install(TARGETS dlib
EXPORT dlib
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # Windows considers .dll to be runtime artifacts
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib
FILES_MATCHING PATTERN "*.h" PATTERN "*.cmake"
......
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