CMakeLists.txt 42.1 KB
Newer Older
1
2
3
4
5
#
# This is a CMake makefile.  You can find the cmake utility and
# information about it at http://www.cmake.org
#

6

7
cmake_minimum_required(VERSION 2.8.12)
8
9
10
11
12

if(POLICY CMP0048)
   cmake_policy(SET CMP0048 NEW)
endif()

13
project(dlib)
14

15
16
set(CPACK_PACKAGE_NAME "dlib")
set(CPACK_PACKAGE_VERSION_MAJOR "19")
Davis King's avatar
Davis King committed
17
set(CPACK_PACKAGE_VERSION_MINOR "16")
18
set(CPACK_PACKAGE_VERSION_PATCH "99")
19
20
21
22
23
24
25
set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
# Only print these messages once, even if dlib is added multiple times via add_subdirectory()
if (NOT TARGET dlib)
   message(STATUS "Using CMake version: ${CMAKE_VERSION}")
   message(STATUS "Compiling dlib version: ${VERSION}")
endif()

26
27
28
29

include(cmake_utils/set_compiler_specific_options.cmake)


David Seifert's avatar
David Seifert committed
30
31
32
# Adhere to GNU filesystem layout conventions
include(GNUInstallDirs)

Davis King's avatar
Davis King committed
33
34
35
36
37
if (POLICY CMP0063)
   # This policy tells cmake to really honor CXX_VISIBILITY_PRESET rather than
   # ignore it for backwards compatibility.
   cmake_policy(SET CMP0063 NEW)
endif()
38
39
40
if (POLICY CMP0075)
   cmake_policy(SET CMP0075 NEW)
endif()
Davis King's avatar
Davis King committed
41

42
# default to a Release build (except if CMAKE_BUILD_TYPE is set)
43
44
include(cmake_utils/release_build_by_default)
include(cmake_utils/use_cpp_11.cmake)
45

46
47
48
49
# Set DLIB_VERSION in the including CMake file so they can use it to do whatever they want. 
get_directory_property(has_parent PARENT_DIRECTORY)
if(has_parent)
   set(DLIB_VERSION ${VERSION} PARENT_SCOPE)
50
51
52
   if (NOT DEFINED DLIB_IN_PROJECT_BUILD)
      set(DLIB_IN_PROJECT_BUILD true)
   endif()
53
endif()
54

55
56
57
58
59
60
61
62
63
64
65
66
if (COMMAND pybind11_add_module AND MSVC)
   # True when building a python extension module using Visual Studio.  We care
   # about this because a huge number of windows users have broken systems, and
   # in particular, they have broken or incompatibly installed copies of things
   # like libjpeg or libpng.  So if we detect we are in this mode we will never
   # ever link to those libraries.  Instead, we link to the copy included with
   # dlib.
   set (BUILDING_PYTHON_IN_MSVC true)
else()
   set (BUILDING_PYTHON_IN_MSVC false)
endif()

67
if (DLIB_IN_PROJECT_BUILD)
Davis King's avatar
Davis King committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81

   # Check if we are being built as part of a pybind11 module. 
   if (COMMAND pybind11_add_module)
      set(CMAKE_POSITION_INDEPENDENT_CODE True)
      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()
      # Make DLIB_ASSERT statements not abort the python interpreter, but just return an error.
      list(APPEND active_preprocessor_switches "-DDLIB_NO_ABORT_ON_2ND_FATAL_ERROR")
   endif()

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
   # 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)
102
103
104
105
106

elseif(BUILD_SHARED_LIBS)
   if (MSVC)
      message(FATAL_ERROR "Building dlib as a standalone dll is not supported when using Visual Studio.  You are highly encouraged to use static linking instead.  See https://github.com/davisking/dlib/issues/1483 for a discussion.") 
   endif()
107
108
109
endif()


110
111
112
113
114
115
116
117
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
   # is used.  In newer versions of CMake we can instead set CUDA_LINK_LIBRARIES_KEYWORD which fixes this issue.
   cmake_policy(SET CMP0023 OLD)
else()
   set(CUDA_LINK_LIBRARIES_KEYWORD PUBLIC)
endif()
118

119

120
121
122
macro (enable_preprocessor_switch option_name)
   list(APPEND active_preprocessor_switches "-D${option_name}")
endmacro()
123

124
125
126
macro (disable_preprocessor_switch option_name)
   if (active_preprocessor_switches)
      list(REMOVE_ITEM active_preprocessor_switches "-D${option_name}")
127
   endif()
128
endmacro()
129

130
131
macro (toggle_preprocessor_switch option_name)
   if (${option_name})
132
      enable_preprocessor_switch(${option_name})
133
   else()
134
      disable_preprocessor_switch(${option_name})
135
136
   endif()
endmacro()
137

138

139

140
141
# Suppress superfluous randlib warnings about libdlib.a having no symbols on MacOSX.
if (APPLE)
Davis King's avatar
Davis King committed
142
143
144
145
   set(CMAKE_C_ARCHIVE_CREATE   "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
   set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> Scr <TARGET> <LINK_FLAGS> <OBJECTS>")
   set(CMAKE_C_ARCHIVE_FINISH   "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
   set(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
146
147
endif()

148
149
150
151
152
153
154
155
156
# Don't try to call add_library(dlib) and setup dlib's stuff if it has already
# been done by some other part of the current cmake project.  We do this
# because it avoids getting warnings/errors about cmake policy CMP0002.  This
# happens when a project tries to call add_subdirectory() on dlib more than
# once.  This most often happens when the top level of a project depends on two
# or more other things which both depend on dlib. 
if (NOT TARGET dlib)

   set (DLIB_ISO_CPP_ONLY_STR 
Davis King's avatar
Davis King committed
157
      "Enable this if you don't want to compile any non-ISO C++ code (i.e. you don't use any of the API Wrappers)" )
158
   set (DLIB_NO_GUI_SUPPORT_STR 
Davis King's avatar
Davis King committed
159
      "Enable this if you don't want to compile any of the dlib GUI code" )
160
   set (DLIB_ENABLE_STACK_TRACE_STR 
Davis King's avatar
Davis King committed
161
      "Enable this if you want to turn on the DLIB_STACK_TRACE macros" )
162
   set (DLIB_USE_BLAS_STR
Davis King's avatar
Davis King committed
163
      "Disable this if you don't want to use a BLAS library" )
164
   set (DLIB_USE_LAPACK_STR
Davis King's avatar
Davis King committed
165
      "Disable this if you don't want to use a LAPACK library" )
166
   set (DLIB_USE_CUDA_STR
Davis King's avatar
Davis King committed
167
      "Disable this if you don't want to use NVIDIA CUDA" )
168
   set (DLIB_USE_MKL_SEQUENTIAL_STR
Davis King's avatar
Davis King committed
169
      "Enable this if you have MKL installed and want to use the sequential version instead of the multi-core version." )
170
171
   set (DLIB_USE_MKL_WITH_TBB_STR
      "Enable this if you have MKL installed and want to use the tbb version instead of the openmp version." )
172
   set (DLIB_PNG_SUPPORT_STR
Davis King's avatar
Davis King committed
173
      "Disable this if you don't want to link against libpng" )
174
   set (DLIB_GIF_SUPPORT_STR
Davis King's avatar
Davis King committed
175
      "Disable this if you don't want to link against libgif" )
176
   set (DLIB_JPEG_SUPPORT_STR
Davis King's avatar
Davis King committed
177
      "Disable this if you don't want to link against libjpeg" )
178
   set (DLIB_LINK_WITH_SQLITE3_STR
Davis King's avatar
Davis King committed
179
      "Disable this if you don't want to link against sqlite3" )
180
   #set (DLIB_USE_FFTW_STR "Disable this if you don't want to link against fftw" )
Duncan Palmer's avatar
Duncan Palmer committed
181
   set (DLIB_USE_MKL_FFT_STR
Davis King's avatar
Davis King committed
182
      "Disable this is you don't want to use the MKL DFTI FFT implementation" )
183
   set (DLIB_ENABLE_ASSERTS_STR
Davis King's avatar
Davis King committed
184
      "Enable this if you want to turn on the DLIB_ASSERT macro" )
185

186

187
   option(DLIB_ENABLE_ASSERTS ${DLIB_ENABLE_ASSERTS_STR} OFF)
188
   option(DLIB_ISO_CPP_ONLY ${DLIB_ISO_CPP_ONLY_STR} OFF)
189
   toggle_preprocessor_switch(DLIB_ISO_CPP_ONLY)
190
   option(DLIB_NO_GUI_SUPPORT ${DLIB_NO_GUI_SUPPORT_STR} OFF)
191
   toggle_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
192
   option(DLIB_ENABLE_STACK_TRACE ${DLIB_ENABLE_STACK_TRACE_STR} OFF)
193
   toggle_preprocessor_switch(DLIB_ENABLE_STACK_TRACE)
194
   option(DLIB_USE_MKL_SEQUENTIAL ${DLIB_USE_MKL_SEQUENTIAL_STR} OFF)
195
   option(DLIB_USE_MKL_WITH_TBB ${DLIB_USE_MKL_WITH_TBB_STR} OFF)
196

197
   if(DLIB_ENABLE_ASSERTS)
198
199
200
201
      # Set these variables so they are set in the config.h.in file when dlib
      # is installed.
      set (DLIB_DISABLE_ASSERTS false)
      set (ENABLE_ASSERTS true) 
202
203
      enable_preprocessor_switch(ENABLE_ASSERTS)
      disable_preprocessor_switch(DLIB_DISABLE_ASSERTS)
204
205
206
207
208
   else()
      # Set these variables so they are set in the config.h.in file when dlib
      # is installed.
      set (DLIB_DISABLE_ASSERTS true)
      set (ENABLE_ASSERTS false) 
209
      disable_preprocessor_switch(ENABLE_ASSERTS)
210
211
212
213
214
215
216
217
218
219
220
221
222
223
      # Never force the asserts off when doing an in project build.  The only
      # time this matters is when using visual studio.  The visual studio IDE
      # has a drop down that lets the user select either release or debug
      # builds.  The DLIB_ASSERT macro is setup to enable/disable automatically
      # based on this drop down (via preprocessor magic).  However, if
      # DLIB_DISABLE_ASSERTS is defined it permanently disables asserts no
      # matter what, which would defeat the visual studio drop down.  So here
      # we make a point to not do that kind of severe disabling when in a
      # project build.  It should also be pointed out that DLIB_DISABLE_ASSERTS
      # is only needed when building and installing dlib as a separately
      # installed library.  It doesn't matter when doing an in project build. 
      if (NOT DLIB_IN_PROJECT_BUILD)
         enable_preprocessor_switch(DLIB_DISABLE_ASSERTS)
      endif()
224
225
226
227
228
229
230
231
232
   endif()

   if (DLIB_ISO_CPP_ONLY)
      option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} OFF)
      option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} OFF)
      option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} OFF)
      option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} OFF)
      option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} OFF)
      option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} OFF)
233
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} OFF)
234
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF)
Duncan Palmer's avatar
Duncan Palmer committed
235
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} OFF)
236
   else()
237
238
239
240
241
242
      option(DLIB_JPEG_SUPPORT ${DLIB_JPEG_SUPPORT_STR} ON)
      option(DLIB_LINK_WITH_SQLITE3 ${DLIB_LINK_WITH_SQLITE3_STR} ON)
      option(DLIB_USE_BLAS ${DLIB_USE_BLAS_STR} ON)
      option(DLIB_USE_LAPACK ${DLIB_USE_LAPACK_STR} ON)
      option(DLIB_USE_CUDA ${DLIB_USE_CUDA_STR} ON)
      option(DLIB_PNG_SUPPORT ${DLIB_PNG_SUPPORT_STR} ON)
243
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} ON)
244
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON)
Duncan Palmer's avatar
Duncan Palmer committed
245
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} ON)
246
   endif()
247
248
249
250
251
   toggle_preprocessor_switch(DLIB_JPEG_SUPPORT)
   toggle_preprocessor_switch(DLIB_USE_BLAS)
   toggle_preprocessor_switch(DLIB_USE_LAPACK)
   toggle_preprocessor_switch(DLIB_USE_CUDA)
   toggle_preprocessor_switch(DLIB_PNG_SUPPORT) 
252
   toggle_preprocessor_switch(DLIB_GIF_SUPPORT) 
253
   #toggle_preprocessor_switch(DLIB_USE_FFTW)
Duncan Palmer's avatar
Duncan Palmer committed
254
   toggle_preprocessor_switch(DLIB_USE_MKL_FFT)
255

256

257
   set(source_files 
Davis King's avatar
Davis King committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
      base64/base64_kernel_1.cpp
      bigint/bigint_kernel_1.cpp
      bigint/bigint_kernel_2.cpp
      bit_stream/bit_stream_kernel_1.cpp
      entropy_decoder/entropy_decoder_kernel_1.cpp
      entropy_decoder/entropy_decoder_kernel_2.cpp
      entropy_encoder/entropy_encoder_kernel_1.cpp
      entropy_encoder/entropy_encoder_kernel_2.cpp
      md5/md5_kernel_1.cpp
      tokenizer/tokenizer_kernel_1.cpp
      unicode/unicode.cpp
      data_io/image_dataset_metadata.cpp
      data_io/mnist.cpp
      global_optimization/global_function_search.cpp
      filtering/kalman_filter.cpp
      test_for_odr_violations.cpp
Davis King's avatar
Davis King committed
274
      svm/auto.cpp
Davis King's avatar
Davis King committed
275
      )
Davis King's avatar
Davis King committed
276

277

278
279
280
   set(dlib_needed_libraries)
   set(dlib_needed_includes)

281
   if (DLIB_ISO_CPP_ONLY)
Davis King's avatar
Davis King committed
282
      add_library(dlib ${source_files} )
283
   else()
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

      set(source_files ${source_files}
         sockets/sockets_kernel_1.cpp
         bsp/bsp.cpp
         dir_nav/dir_nav_kernel_1.cpp
         dir_nav/dir_nav_kernel_2.cpp
         dir_nav/dir_nav_extensions.cpp
         linker/linker_kernel_1.cpp
         logger/extra_logger_headers.cpp
         logger/logger_kernel_1.cpp
         logger/logger_config_file.cpp
         misc_api/misc_api_kernel_1.cpp
         misc_api/misc_api_kernel_2.cpp
         sockets/sockets_extensions.cpp
         sockets/sockets_kernel_2.cpp
         sockstreambuf/sockstreambuf.cpp
         sockstreambuf/sockstreambuf_unbuffered.cpp
         server/server_kernel.cpp
         server/server_iostream.cpp
         server/server_http.cpp
         threads/multithreaded_object_extension.cpp
         threads/threaded_object_extension.cpp
         threads/threads_kernel_1.cpp
         threads/threads_kernel_2.cpp
         threads/threads_kernel_shared.cpp
         threads/thread_pool_extension.cpp
310
         threads/async.cpp
311
312
         timer/timer.cpp
         stack_trace.cpp
313
314
         cuda/cpu_dlib.cpp
         cuda/tensor_tools.cpp
315
316
         )

317
318
319
320
321
322
      if(UNIX)
         set(CMAKE_THREAD_PREFER_PTHREAD ON)
         find_package(Threads REQUIRED)
         set(dlib_needed_libraries ${dlib_needed_libraries} ${CMAKE_THREAD_LIBS_INIT})
      endif()

323
324
325
      # we want to link to the right stuff depending on our platform.  
      if (WIN32 AND NOT CYGWIN) ###############################################################################
         if (DLIB_NO_GUI_SUPPORT)
326
            set (dlib_needed_libraries ws2_32 winmm)
327
         else()
328
            set (dlib_needed_libraries ws2_32 winmm comctl32 gdi32 imm32)
329
330
         endif()
      elseif(APPLE) ############################################################################
Davis King's avatar
Davis King committed
331
         set(CMAKE_MACOSX_RPATH 1)
332
         if (NOT DLIB_NO_GUI_SUPPORT)
333
334
            find_package(X11 QUIET)
            if (X11_FOUND)
335
               # If both X11 and anaconda are installed, it's possible for the
Davis King's avatar
merged  
Davis King committed
336
337
               # anaconda path to appear before /opt/X11, so we remove anaconda.
               foreach (ITR ${X11_INCLUDE_DIR})
338
                  if ("${ITR}" MATCHES "(.*)(Ana|ana|mini)conda(.*)")
339
                     list (REMOVE_ITEM X11_INCLUDE_DIR ${ITR})
Davis King's avatar
merged  
Davis King committed
340
                  endif ()
341
               endforeach(ITR)
342
343
               include_directories(${X11_INCLUDE_DIR})
               set (dlib_needed_libraries ${dlib_needed_libraries} ${X11_LIBRARIES})
344
            else()
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
               find_library(xlib X11)
               # Make sure X11 is in the include path.  Note that we look for
               # Xlocale.h rather than Xlib.h because it avoids finding a partial
               # copy of the X11 headers on systems with anaconda installed.
               find_path(xlib_path Xlocale.h
                  PATHS 
                  /Developer/SDKs/MacOSX10.4u.sdk/usr/X11R6/include
                  /opt/local/include
                  PATH_SUFFIXES X11
                  )
               if (xlib AND xlib_path)
                  get_filename_component(x11_path ${xlib_path} PATH CACHE)
                  include_directories(${x11_path})
                  set(dlib_needed_libraries ${dlib_needed_libraries} ${xlib} )
                  set(X11_FOUND 1)
               endif()
            endif()
            if (NOT X11_FOUND)
363
364
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
365
               message(" *** Make sure XQuartz is installed if you want GUI support.               ***")
Davis King's avatar
Davis King committed
366
               message(" *** You can download XQuartz from: https://www.xquartz.org/               ***")
367
               message(" *****************************************************************************")
368
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
369
               enable_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
370
            endif()
371
372
         endif()

373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
         mark_as_advanced(pthreadlib xlib xlib_path x11_path)
      else () ##################################################################################
         # link to the nsl library if it exists.  this is something you need sometimes 
         find_library(nsllib nsl)
         if (nsllib)
            set (dlib_needed_libraries ${dlib_needed_libraries} ${nsllib})
         endif ()

         # link to the socket library if it exists.  this is something you need on solaris
         find_library(socketlib socket)
         if (socketlib)
            set (dlib_needed_libraries ${dlib_needed_libraries} ${socketlib})
         endif ()

         if (NOT DLIB_NO_GUI_SUPPORT)
            include(FindX11)
            if (X11_FOUND)
               include_directories(${X11_INCLUDE_DIR})
               set (dlib_needed_libraries ${dlib_needed_libraries} ${X11_LIBRARIES})
            else()
393
394
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
395
396
               message(" *** Make sure libx11-dev is installed if you want GUI support.            ***")
               message(" *** On Ubuntu run: sudo apt-get install libx11-dev                        ***")
397
               message(" *****************************************************************************")
398
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
399
               enable_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
400
            endif()
401
402
         endif()

403
404
         mark_as_advanced(nsllib pthreadlib socketlib)
      endif () ##################################################################################
405

406
407
408
409
410
411
412
413
414
415
416
417
      if (NOT DLIB_NO_GUI_SUPPORT)
         set(source_files ${source_files}
            gui_widgets/fonts.cpp
            gui_widgets/widgets.cpp
            gui_widgets/drawable.cpp
            gui_widgets/canvas_drawing.cpp
            gui_widgets/style.cpp
            gui_widgets/base_widgets.cpp
            gui_core/gui_core_kernel_1.cpp
            gui_core/gui_core_kernel_2.cpp
            )
      endif()
418

419
      INCLUDE (CheckFunctionExists)
420

421
422
423
      if (DLIB_GIF_SUPPORT)
         find_package(GIF QUIET)
         if (GIF_FOUND)
424
            set (dlib_needed_includes ${dlib_needed_includes} ${GIF_INCLUDE_DIR})
425
426
427
428
429
430
431
            set (dlib_needed_libraries ${dlib_needed_libraries} ${GIF_LIBRARY})
         else()
            set(DLIB_GIF_SUPPORT OFF CACHE STRING ${DLIB_GIF_SUPPORT_STR} FORCE )
            toggle_preprocessor_switch(DLIB_GIF_SUPPORT)
         endif()
      endif()

432
      if (DLIB_PNG_SUPPORT)
433
         # try to find libpng 
Davis King's avatar
Davis King committed
434
         find_package(PNG QUIET)
435
         # Make sure there isn't something wrong with the version of LIBPNG
436
437
         # installed on this system.  Also never link to anything from anaconda
         # since it's probably broken.
438
         if (PNG_FOUND AND NOT ("${PNG_INCLUDE_DIR}" MATCHES "(.*)(Ana|ana|mini)conda(.*)") AND NOT BUILDING_PYTHON_IN_MSVC)
439
            set(CMAKE_REQUIRED_LIBRARIES ${PNG_LIBRARIES})
440
441
            CHECK_FUNCTION_EXISTS(png_create_read_struct LIBPNG_IS_GOOD)
         endif()
442
         if (PNG_FOUND AND LIBPNG_IS_GOOD)
443
            include_directories(${PNG_INCLUDE_DIR})
444
            set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARIES})
445
            set(REQUIRES_LIBS " libpng")
446
         else()
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
            # If we can't find libpng then statically compile it in.
            include_directories(external/libpng external/zlib)
            set(source_files ${source_files}
               external/libpng/png.c
               external/libpng/pngerror.c
               external/libpng/pngget.c
               external/libpng/pngmem.c
               external/libpng/pngpread.c
               external/libpng/pngread.c
               external/libpng/pngrio.c
               external/libpng/pngrtran.c
               external/libpng/pngrutil.c
               external/libpng/pngset.c
               external/libpng/pngtrans.c
               external/libpng/pngwio.c
               external/libpng/pngwrite.c
               external/libpng/pngwtran.c
               external/libpng/pngwutil.c
               external/zlib/adler32.c
               external/zlib/compress.c
               external/zlib/crc32.c
               external/zlib/deflate.c
               external/zlib/gzclose.c
               external/zlib/gzlib.c
               external/zlib/gzread.c
               external/zlib/gzwrite.c
               external/zlib/infback.c
               external/zlib/inffast.c
               external/zlib/inflate.c
               external/zlib/inftrees.c
               external/zlib/trees.c
               external/zlib/uncompr.c
               external/zlib/zutil.c
               )
481
482

            include(cmake_utils/check_if_neon_available.cmake)
483
484
            if (ARM_NEON_IS_AVAILABLE)
               message (STATUS "NEON instructions will be used for libpng.")
485
486
487
488
489
490
491
492
               enable_language(ASM)
               set(source_files ${source_files}
                  external/libpng/arm/arm_init.c
                  external/libpng/arm/filter_neon_intrinsics.c
                  external/libpng/arm/filter_neon.S
                  )
               set_source_files_properties(external/libpng/arm/filter_neon.S PROPERTIES COMPILE_FLAGS "${CMAKE_ASM_FLAGS} ${CMAKE_CXX_FLAGS} -x assembler-with-cpp")
            endif()
493
            set(REQUIRES_LIBS "")
494
         endif()
495
496
497
498
         set(source_files ${source_files}
            image_loader/png_loader.cpp
            image_saver/save_png.cpp
            )
499
      endif()
500

501
      if (DLIB_JPEG_SUPPORT)
502
         # try to find libjpeg 
503
         find_package(JPEG QUIET)
504
         # Make sure there isn't something wrong with the version of libjpeg 
505
506
         # installed on this system.  Also don't use the installed libjpeg
         # if this is an APPLE system because apparently it's broken (as of 2015/01/01).
507
         if (JPEG_FOUND AND NOT ("${JPEG_INCLUDE_DIR}" MATCHES "(.*)(Ana|ana|mini)conda(.*)") AND NOT BUILDING_PYTHON_IN_MSVC)
508
509
510
            set(CMAKE_REQUIRED_LIBRARIES ${JPEG_LIBRARY})
            CHECK_FUNCTION_EXISTS(jpeg_read_header LIBJPEG_IS_GOOD)
         endif()
511
         if (JPEG_FOUND AND LIBJPEG_IS_GOOD AND NOT APPLE)
512
513
514
            include_directories(${JPEG_INCLUDE_DIR})
            set (dlib_needed_libraries ${dlib_needed_libraries} ${JPEG_LIBRARY})
         else()
515
            # If we can't find libjpeg then statically compile it in.
516
            add_definitions(-DDLIB_JPEG_STATIC)
517
            set(source_files ${source_files}
Davis King's avatar
Davis King committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
               external/libjpeg/jcomapi.cpp
               external/libjpeg/jdapimin.cpp
               external/libjpeg/jdapistd.cpp
               external/libjpeg/jdatasrc.cpp
               external/libjpeg/jdcoefct.cpp
               external/libjpeg/jdcolor.cpp
               external/libjpeg/jddctmgr.cpp
               external/libjpeg/jdhuff.cpp
               external/libjpeg/jdinput.cpp
               external/libjpeg/jdmainct.cpp
               external/libjpeg/jdmarker.cpp
               external/libjpeg/jdmaster.cpp
               external/libjpeg/jdmerge.cpp
               external/libjpeg/jdphuff.cpp
               external/libjpeg/jdpostct.cpp
               external/libjpeg/jdsample.cpp
               external/libjpeg/jerror.cpp
               external/libjpeg/jidctflt.cpp
               external/libjpeg/jidctfst.cpp
               external/libjpeg/jidctint.cpp
               external/libjpeg/jidctred.cpp
               external/libjpeg/jmemmgr.cpp
               external/libjpeg/jmemnobs.cpp
               external/libjpeg/jquant1.cpp
               external/libjpeg/jquant2.cpp
               external/libjpeg/jutils.cpp  
               external/libjpeg/jcapimin.cpp
               external/libjpeg/jdatadst.cpp
               external/libjpeg/jcparam.cpp
               external/libjpeg/jcapistd.cpp
               external/libjpeg/jcmarker.cpp
               external/libjpeg/jcinit.cpp
               external/libjpeg/jcmaster.cpp
               external/libjpeg/jcdctmgr.cpp
               external/libjpeg/jccoefct.cpp  
               external/libjpeg/jccolor.cpp  
               external/libjpeg/jchuff.cpp  
               external/libjpeg/jcmainct.cpp  
               external/libjpeg/jcphuff.cpp  
               external/libjpeg/jcprepct.cpp  
               external/libjpeg/jcsample.cpp
               external/libjpeg/jfdctint.cpp
               external/libjpeg/jfdctflt.cpp
               external/libjpeg/jfdctfst.cpp
               )
563
         endif()
564
565
         set(source_files ${source_files}
            image_loader/jpeg_loader.cpp
Davis King's avatar
Davis King committed
566
            image_saver/save_jpeg.cpp
567
            )
568
569
      endif()

570

571
      if (DLIB_USE_BLAS OR DLIB_USE_LAPACK OR DLIB_USE_MKL_FFT)
572
573
574
575
576
577
578
         if (DLIB_USE_MKL_WITH_TBB AND DLIB_USE_MKL_SEQUENTIAL)
            set(DLIB_USE_MKL_SEQUENTIAL OFF CACHE STRING ${DLIB_USE_MKL_SEQUENTIAL_STR} FORCE )
            toggle_preprocessor_switch(DLIB_USE_MKL_SEQUENTIAL)
            message(STATUS "Disabling DLIB_USE_MKL_SEQUENTIAL. It cannot be used simultaneously with DLIB_USE_MKL_WITH_TBB.")
         endif()


Davis King's avatar
Davis King committed
579
         # Try to find BLAS, LAPACK and MKL
Davis King's avatar
Davis King committed
580
         include(cmake_utils/find_blas.cmake)
581

582
583
584
585
586
         if (DLIB_USE_BLAS)
            if (blas_found)
               set (dlib_needed_libraries ${dlib_needed_libraries} ${blas_libraries})
            else()
               set(DLIB_USE_BLAS OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
587
               toggle_preprocessor_switch(DLIB_USE_BLAS)
588
            endif()
589
590
         endif()

591
592
593
         if (DLIB_USE_LAPACK)
            if (lapack_found)
               set (dlib_needed_libraries ${dlib_needed_libraries} ${lapack_libraries})
594
595
596
597
598
599
600
               if (lapack_with_underscore)
                  set(LAPACK_FORCE_UNDERSCORE 1)
                  enable_preprocessor_switch(LAPACK_FORCE_UNDERSCORE)
               elseif (lapack_without_underscore)
                  set(LAPACK_FORCE_NOUNDERSCORE 1)
                  enable_preprocessor_switch(LAPACK_FORCE_NOUNDERSCORE)
               endif ()
601
602
            else()
               set(DLIB_USE_LAPACK OFF CACHE STRING ${DLIB_USE_LAPACK_STR} FORCE )
603
               toggle_preprocessor_switch(DLIB_USE_LAPACK)
604
            endif()
605
         endif()
606

607
         if (DLIB_USE_MKL_FFT)
608
            if (found_intel_mkl AND found_intel_mkl_headers)
609
               set (dlib_needed_includes ${dlib_needed_includes} ${mkl_include_dir})
610
611
612
613
614
615
               set (dlib_needed_libraries ${dlib_needed_libraries} ${mkl_libraries})
            else()
               set(DLIB_USE_MKL_FFT OFF CACHE STRING ${DLIB_USE_MKL_FFT_STR} FORCE )
               toggle_preprocessor_switch(DLIB_USE_MKL_FFT)
            endif()
         endif()
Duncan Palmer's avatar
Duncan Palmer committed
616
617
      endif()

618

619
      if (DLIB_USE_CUDA)
620
         find_package(CUDA 7.5)
621

622
623
624
625
626
627
         if (CUDA_VERSION VERSION_GREATER 9.1 AND CMAKE_VERSION VERSION_LESS 3.12.2)
            # This bit of weirdness is to work around a bug in cmake 
            list(REMOVE_ITEM CUDA_CUBLAS_LIBRARIES "CUDA_cublas_device_LIBRARY-NOTFOUND")
         endif()


628
629
630
631
         if (CUDA_FOUND AND MSVC AND NOT CUDA_CUBLAS_LIBRARIES AND "${CMAKE_SIZEOF_VOID_P}" EQUAL "4")
            message(WARNING "You have CUDA installed, but we can't use it unless you put visual studio in 64bit mode.")
            set(CUDA_FOUND 0)
         endif()
632

633
         if (CUDA_FOUND AND (NOT USING_OLD_VISUAL_STUDIO_COMPILER))
634

635
636
637
638
            # There is some bug in cmake that causes it to mess up the
            # -std=c++11 option if you let it propagate it to nvcc in some
            # cases.  So instead we disable this and manually include
            # things from CMAKE_CXX_FLAGS in the CUDA_NVCC_FLAGS list below.
639
            if (APPLE)
640
               set(CUDA_PROPAGATE_HOST_FLAGS OFF)
641
642
               # Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them
               # to nvcc.
643
               string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC "${CMAKE_CXX_FLAGS}")
644
645
646
647
648
649

               # Check if we are being built as part of a pybind11 module. 
               if (COMMAND pybind11_add_module)
                  # Don't export unnecessary symbols.
                  list(APPEND FLAGS_FOR_NVCC "-Xcompiler=-fvisibility=hidden")
               endif()
650
651
            endif()

652

653
            set(CUDA_HOST_COMPILATION_CPP ON)
654
655
656
            # Note that we add __STRICT_ANSI__ to avoid freaking out nvcc with gcc specific
            # magic in the standard C++ header files (since nvcc uses gcc headers on
            # linux).
657
            list(APPEND CUDA_NVCC_FLAGS "-arch=sm_30;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}")
658
            list(APPEND CUDA_NVCC_FLAGS ${active_preprocessor_switches})
659
660
661
            if (NOT DLIB_IN_PROJECT_BUILD)
               LIST(APPEND CUDA_NVCC_FLAGS -DDLIB__CMAKE_GENERATED_A_CONFIG_H_FILE)
            endif()
662
663
664
            if (NOT MSVC)
               list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
            endif()
665
666
667
            if (CMAKE_POSITION_INDEPENDENT_CODE)
               # sometimes this setting isn't propagated to NVCC, which then causes the
               # compile to fail.  So make sure it's propagated.
668
669
670
               if (NOT MSVC) # Visual studio doesn't have -fPIC so don't do it in that case.
                  list(APPEND CUDA_NVCC_FLAGS "-Xcompiler -fPIC")
               endif()
671
            endif()
672

673
            include(cmake_utils/test_for_cudnn/find_cudnn.txt)
674

675
            if (cudnn AND cudnn_include AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked)
676
677
               # make sure cuda is really working by doing a test compile
               message(STATUS "Building a CUDA test project to see if your compiler is compatible with CUDA...")
678

Davis King's avatar
Davis King committed
679
680
681
682
               set(CUDA_TEST_CMAKE_FLAGS 
                  "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
                  "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}"
                  "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}")
683

684
               if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363
685
686
687
                  list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}")
               endif()

Davis King's avatar
Davis King committed
688
689
690
691
692
693
               try_compile(cuda_test_compile_worked 
                  ${PROJECT_BINARY_DIR}/cuda_test_build 
                  ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test
                  CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
                  OUTPUT_VARIABLE try_compile_output_message
                  )
694
               if (NOT cuda_test_compile_worked)
695
696
697
698
699
700
                  string(REPLACE "\n" "\n   ***   " try_compile_output_message "${try_compile_output_message}")
                  message(STATUS "*****************************************************************************************************************")
                  message(STATUS "*** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ")
                  message(STATUS "*** The output of the failed CUDA test compile is shown below: ")
                  message(STATUS "***   ${try_compile_output_message}")
                  message(STATUS "*****************************************************************************************************************")
701
702
               else()
                  message(STATUS "Checking if you have the right version of cuDNN installed.")
Davis King's avatar
Davis King committed
703
704
705
706
707
                  try_compile(cudnn_test_compile_worked 
                     ${PROJECT_BINARY_DIR}/cudnn_test_build 
                     ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test
                     CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
                     )
708
709
710
711
                  if (NOT cudnn_test_compile_worked)
                     message(STATUS "*** Found cuDNN, but it looks like the wrong version so dlib will not use it.              ***")
                     message(STATUS "*** Dlib requires cuDNN V5.0 OR GREATER.  Since cuDNN is not found DLIB WILL NOT USE CUDA. ***")
                     message(STATUS "*** If you have cuDNN then set CMAKE_PREFIX_PATH to include cuDNN's folder.                ***")
712
                  endif()
713
714
               endif()
            endif()
715

716
717
            # Find where cuSOLVER is since the FindCUDA cmake package doesn't
            # bother to look for it.
718
            get_filename_component(cuda_blas_path "${CUDA_CUBLAS_LIBRARIES}" DIRECTORY)
719
            find_library(cusolver cusolver HINTS ${cuda_blas_path})
720
721
722
723
724
            mark_as_advanced(cusolver)
            # Also find OpenMP since cuSOLVER needs it.  Importantly, we only
            # look for one to link to if our use of BLAS, specifically the
            # Intel MKL, hasn't already decided what to use.  This is because
            # it makes the MKL bug out if you link to another openmp lib other
725
726
727
728
729
730
731
732
            # than Intel's when you use the MKL. I'm also not really sure when
            # explicit linking to openmp became unnecessary, but for
            # sufficiently older versions of cuda it was needed.  Then in
            # versions of cmake newer than 3.11 linking to openmp started to
            # mess up the switches passed to nvcc, so you can't just leave
            # these "try to link to openmp" statements here going forward.  Fun
            # times.
            if (CUDA_VERSION VERSION_LESS "9.1" AND NOT openmp_libraries AND NOT MSVC AND NOT XCODE AND NOT APPLE)
733
734
               find_package(OpenMP)
               if (OPENMP_FOUND)
735
                  set(openmp_libraries ${OpenMP_CXX_FLAGS}) 
736
               else()
737
738
                  message(STATUS "*** Didn't find OpenMP, which is required to use CUDA. ***")
                  set(CUDA_FOUND 0)
739
               endif()
740
741
            endif()
         endif()
742

743
         if (CUDA_FOUND AND cudnn AND (NOT USING_OLD_VISUAL_STUDIO_COMPILER) AND cuda_test_compile_worked AND cudnn_test_compile_worked AND cudnn_include)
744
            set(source_files ${source_files} 
745
746
747
748
749
750
751
               cuda/cuda_dlib.cu 
               cuda/cudnn_dlibapi.cpp
               cuda/cublas_dlibapi.cpp
               cuda/cusolver_dlibapi.cu
               cuda/curand_dlibapi.cpp
               cuda/cuda_data_ptr.cpp
               cuda/gpu_data.cpp
752
               )
Davis King's avatar
Davis King committed
753
            set(dlib_needed_libraries ${dlib_needed_libraries} 
Davis King's avatar
Davis King committed
754
755
756
757
758
               ${CUDA_CUBLAS_LIBRARIES} 
               ${cudnn}
               ${CUDA_curand_LIBRARY}
               ${cusolver}
               )
Davis King's avatar
Davis King committed
759
            if(openmp_libraries)
760
761
               list(APPEND dlib_needed_libraries ${openmp_libraries})
            endif()
Davis King's avatar
Davis King committed
762

763
            include_directories(${cudnn_include})
764
            message(STATUS "Enabling CUDA support for dlib.  DLIB WILL USE CUDA")
765
766
         else()
            set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
767
            toggle_preprocessor_switch(DLIB_USE_CUDA)
768
            if (USING_OLD_VISUAL_STUDIO_COMPILER)
769
               message(STATUS "*** Dlib CUDA support requires C++11 but your compiler doesn't support it. ***")
770
            endif()
771
            message(STATUS "Disabling CUDA support for dlib.  DLIB WILL NOT USE CUDA")
772
773
         endif()
      endif()
Davis King's avatar
Davis King committed
774

775

776
777
778
779
780
      if (DLIB_LINK_WITH_SQLITE3)
         find_library(sqlite sqlite3)
         # make sure sqlite3.h is in the include path
         find_path(sqlite_path sqlite3.h)
         if (sqlite AND sqlite_path)
781
            set(dlib_needed_includes ${dlib_needed_includes} ${sqlite_path})
782
783
784
785
            set(dlib_needed_libraries ${dlib_needed_libraries} ${sqlite} )
         else()
            set(DLIB_LINK_WITH_SQLITE3 OFF CACHE STRING ${DLIB_LINK_WITH_SQLITE3_STR} FORCE )
         endif()
786
         mark_as_advanced(sqlite sqlite_path)
787
788
789
      endif()


790

791
      if (DLIB_USE_FFTW)
792
793
794
795
         find_library(fftw fftw3)
         # make sure fftw3.h is in the include path
         find_path(fftw_path fftw3.h)
         if (fftw AND fftw_path)
796
            set(dlib_needed_includes ${dlib_needed_includes} ${fftw_path})
797
798
            set(dlib_needed_libraries ${dlib_needed_libraries} ${fftw} )
         else()
799
800
            set(DLIB_USE_FFTW OFF CACHE STRING ${DLIB_USE_FFTW_STR} FORCE )
            toggle_preprocessor_switch(DLIB_USE_FFTW)
801
         endif()
802
         mark_as_advanced(fftw fftw_path)
803
804
      endif()

805

806
807
808

      # Tell CMake to build dlib via add_library()/cuda_add_library()
      if (DLIB_USE_CUDA)
809
810
811
         # 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})
812
         cuda_add_library(dlib ${source_files} )
813
      else()
814
         add_library(dlib ${source_files} )
815
      endif()
816

817
   endif ()  ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################
818

819

820
   target_include_directories(dlib
Davis King's avatar
Davis King committed
821
822
823
824
      INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
      INTERFACE $<INSTALL_INTERFACE:include>
      PUBLIC ${dlib_needed_includes}
      )
825
   target_link_libraries(dlib PUBLIC ${dlib_needed_libraries})
826
827
828
   if (DLIB_IN_PROJECT_BUILD)
      target_compile_options(dlib PUBLIC ${active_preprocessor_switches})
   else()
829
830
831
      # 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.
832
      target_compile_options(dlib PRIVATE ${active_preprocessor_switches})
833
834
835
836
      # Do this so that dlib/config.h won't set DLIB_NOT_CONFIGURED. This will then allow
      # the code in dlib/threads_kernel_shared.cpp to emit a linker error for users who
      # don't use the configured config.h file generated by cmake.
      target_compile_options(dlib PRIVATE -DDLIB__CMAKE_GENERATED_A_CONFIG_H_FILE)
837
838
839
840

      # Do this so that dlib/config.h can record the version of dlib it's configured with
      # and ultimately issue a linker error to people who try to use a binary dlib that is
      # the wrong version.
841
842
843
      set(DLIB_CHECK_FOR_VERSION_MISMATCH 
         DLIB_VERSION_MISMATCH_CHECK__EXPECTED_VERSION_${CPACK_PACKAGE_VERSION_MAJOR}_${CPACK_PACKAGE_VERSION_MINOR}_${CPACK_PACKAGE_VERSION_PATCH})
      target_compile_options(dlib PRIVATE "-DDLIB_CHECK_FOR_VERSION_MISMATCH=${DLIB_CHECK_FOR_VERSION_MISMATCH}")
844
845
846
   endif()


847
848
   # Allow the unit tests to ask us to compile the all/source.cpp file just to make sure it compiles.
   if (DLIB_TEST_COMPILE_ALL_SOURCE_CPP)
849
      add_library(dlib_all_source_cpp STATIC all/source.cpp) 
850
      target_link_libraries(dlib_all_source_cpp dlib)
851
      target_compile_options(dlib_all_source_cpp PUBLIC ${active_preprocessor_switches})
852
853
854
      enable_cpp11_for_target(dlib_all_source_cpp)
   endif()

Davis King's avatar
Davis King committed
855
856
857
   enable_cpp11_for_target(dlib)
   if((MSVC AND CMAKE_VERSION VERSION_LESS 3.11) OR CMAKE_VERSION VERSION_LESS 3.3)
      target_compile_options(dlib PUBLIC ${active_compile_opts})
858
      target_compile_options(dlib PRIVATE ${active_compile_opts_private})
Davis King's avatar
Davis King committed
859
860
   else()
      target_compile_options(dlib PUBLIC $<$<COMPILE_LANGUAGE:CXX>:${active_compile_opts}>)
861
      target_compile_options(dlib PRIVATE $<$<COMPILE_LANGUAGE:CXX>:${active_compile_opts_private}>)
862
   endif()
863

864
   # Install the library
865
   if (NOT DLIB_IN_PROJECT_BUILD)
Davis King's avatar
Davis King committed
866
867
868
869
870
871
872
      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})
873

Davis King's avatar
Davis King committed
874
      install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
         FILES_MATCHING 
            PATTERN "*.h" 
            PATTERN "*.cmake"
            PATTERN "*_tutorial.txt"
            PATTERN "cassert"
            PATTERN "cstring"
            PATTERN "fstream"
            PATTERN "iomanip"
            PATTERN "iosfwd"
            PATTERN "iostream"
            PATTERN "istream"
            PATTERN "locale"
            PATTERN "ostream"
            PATTERN "sstream"
            REGEX "${CMAKE_CURRENT_BINARY_DIR}" EXCLUDE)
890

891

Davis King's avatar
Davis King committed
892
893
894
      configure_file(${PROJECT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
      # overwrite config.h with the configured one
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib)
895

Davis King's avatar
Davis King committed
896
897
      configure_file(${PROJECT_SOURCE_DIR}/revision.h.in ${CMAKE_CURRENT_BINARY_DIR}/revision.h)
      install(FILES ${CMAKE_CURRENT_BINARY_DIR}/revision.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib)
898

Davis King's avatar
Davis King committed
899
      ## Config.cmake generation and installation
900

Davis King's avatar
Davis King committed
901
902
903
904
      set(ConfigPackageLocation "${CMAKE_INSTALL_LIBDIR}/cmake/dlib")
      install(EXPORT dlib
         NAMESPACE dlib::
         DESTINATION ${ConfigPackageLocation})
905

Davis King's avatar
Davis King committed
906
      configure_file(cmake_utils/dlibConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" @ONLY)
907

Davis King's avatar
Davis King committed
908
909
910
911
912
913
      include(CMakePackageConfigHelpers)
      write_basic_package_version_file(
         "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake"
         VERSION ${VERSION}
         COMPATIBILITY AnyNewerVersion
         )
914

Davis King's avatar
Davis King committed
915
916
917
918
      install(FILES 
         "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" 
         "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake" 
         DESTINATION ${ConfigPackageLocation})
919

Davis King's avatar
Davis King committed
920
      ## dlib-1.pc generation and installation
921

Davis King's avatar
Davis King committed
922
923
924
      configure_file("cmake_utils/dlib.pc.in" "dlib-1.pc" @ONLY)
      install(FILES "${CMAKE_CURRENT_BINARY_DIR}/dlib-1.pc"
         DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
925

926
927
928
929
930
      # Add a cpack "package" target. This will create an archive containing
      # the built library file, the header files, and cmake and pkgconfig
      # configuration files.
      include(CPack)

931
932
   endif()

933
endif()
934

935
936
937
938
939
940
941
942
if (MSVC)
   # Give the output library files names that are unique functions of the
   # visual studio mode that compiled them.  We do this so that people who
   # compile dlib and then copy the .lib files around (which they shouldn't be
   # doing in the first place!) will hopefully be slightly less confused by
   # what happens since, at the very least, the filenames will indicate what
   # visual studio runtime they go with.
   math(EXPR numbits ${CMAKE_SIZEOF_VOID_P}*8)
943
944
945
946
   set_target_properties(dlib PROPERTIES DEBUG_POSTFIX "${VERSION}_debug_${numbits}bit_msvc${MSVC_VERSION}")
   set_target_properties(dlib PROPERTIES RELEASE_POSTFIX "${VERSION}_release_${numbits}bit_msvc${MSVC_VERSION}")
   set_target_properties(dlib PROPERTIES MINSIZEREL_POSTFIX "${VERSION}_minsizerel_${numbits}bit_msvc${MSVC_VERSION}")
   set_target_properties(dlib PROPERTIES RELWITHDEBINFO_POSTFIX "${VERSION}_relwithdebinfo_${numbits}bit_msvc${MSVC_VERSION}")
947
948
endif()

Davis King's avatar
Davis King committed
949
950
951
952
# Check if we are being built as part of a pybind11 module. 
if (COMMAND pybind11_add_module)
   # Don't export unnecessary symbols.
   set_target_properties(dlib PROPERTIES CXX_VISIBILITY_PRESET "hidden")
953
   set_target_properties(dlib PROPERTIES CUDA_VISIBILITY_PRESET "hidden")
Davis King's avatar
Davis King committed
954
955
endif()

956
add_library(dlib::dlib ALIAS dlib)