".github/vscode:/vscode.git/clone" did not exist on "cf7ebaf168a3932977e31b258e050c6df0fdd43a"
CMakeLists.txt 33.9 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
project(dlib)
9

David Seifert's avatar
David Seifert committed
10
11
12
# Adhere to GNU filesystem layout conventions
include(GNUInstallDirs)

13
# default to a Release build (except if CMAKE_BUILD_TYPE is set)
14
15
include(cmake_utils/release_build_by_default)
include(cmake_utils/use_cpp_11.cmake)
16

17

Davis King's avatar
Davis King committed
18
set(CPACK_PACKAGE_VERSION_MAJOR "19")
Davis King's avatar
Davis King committed
19
set(CPACK_PACKAGE_VERSION_MINOR "4")
20
set(CPACK_PACKAGE_VERSION_PATCH "99")
21
set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
22
23
24
25
26
# 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)
endif()
27

28
29
30
31
# 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.
cmake_policy(SET CMP0023 OLD)
32

33

34
35
36
macro (enable_preprocessor_switch option_name)
   list(APPEND active_preprocessor_switches "-D${option_name}")
endmacro()
37

38
39
40
macro (disable_preprocessor_switch option_name)
   if (active_preprocessor_switches)
      list(REMOVE_ITEM active_preprocessor_switches "-D${option_name}")
41
   endif()
42
endmacro()
43

44
45
macro (toggle_preprocessor_switch option_name)
   if (${option_name})
46
      enable_preprocessor_switch(${option_name})
47
   else()
48
      disable_preprocessor_switch(${option_name})
49
50
   endif()
endmacro()
51

52

53

54
55
56
57
58
59
60
61
# Suppress superfluous randlib warnings about libdlib.a having no symbols on MacOSX.
if (APPLE)
    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>")
endif()

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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 
   "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)" )
   set (DLIB_NO_GUI_SUPPORT_STR 
   "Enable this if you don't want to compile any of the dlib GUI code" )
   set (DLIB_ENABLE_STACK_TRACE_STR 
   "Enable this if you want to turn on the DLIB_STACK_TRACE macros" )
   set (DLIB_USE_BLAS_STR
   "Disable this if you don't want to use a BLAS library" )
   set (DLIB_USE_LAPACK_STR
   "Disable this if you don't want to use a LAPACK library" )
80
81
   set (DLIB_USE_CUDA_STR
   "Disable this if you don't want to use NVIDIA CUDA" )
82
   set (DLIB_PNG_SUPPORT_STR
83
   "Disable this if you don't want to link against libpng" )
84
85
   set (DLIB_GIF_SUPPORT_STR
   "Disable this if you don't want to link against libgif" )
86
   set (DLIB_JPEG_SUPPORT_STR
87
88
89
   "Disable this if you don't want to link against libjpeg" )
   set (DLIB_LINK_WITH_SQLITE3_STR
   "Disable this if you don't want to link against sqlite3" )
90
   #set (DLIB_USE_FFTW_STR "Disable this if you don't want to link against fftw" )
Duncan Palmer's avatar
Duncan Palmer committed
91
92
   set (DLIB_USE_MKL_FFT_STR
   "Disable this is you don't want to use the MKL DFTI FFT implementation" )
93
94
95
   set (DLIB_ENABLE_ASSERTS_STR
   "Enable this if you want to turn on the DLIB_ASSERT macro" )

96

97
   option(DLIB_ENABLE_ASSERTS ${DLIB_ENABLE_ASSERTS_STR} OFF)
98
   option(DLIB_ISO_CPP_ONLY ${DLIB_ISO_CPP_ONLY_STR} OFF)
99
   toggle_preprocessor_switch(DLIB_ISO_CPP_ONLY)
100
   option(DLIB_NO_GUI_SUPPORT ${DLIB_NO_GUI_SUPPORT_STR} OFF)
101
   toggle_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
102
   option(DLIB_ENABLE_STACK_TRACE ${DLIB_ENABLE_STACK_TRACE_STR} OFF)
103
   toggle_preprocessor_switch(DLIB_ENABLE_STACK_TRACE)
104

105
   if(DLIB_ENABLE_ASSERTS)
106
107
108
109
      # 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) 
110
111
      enable_preprocessor_switch(ENABLE_ASSERTS)
      disable_preprocessor_switch(DLIB_DISABLE_ASSERTS)
112
113
114
115
116
   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) 
117
      disable_preprocessor_switch(ENABLE_ASSERTS)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
      # 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()
132
133
134
135
136
137
138
139
140
   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)
141
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} OFF)
142
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF)
Duncan Palmer's avatar
Duncan Palmer committed
143
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} OFF)
144
   else()
145
146
147
148
149
150
      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)
151
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} ON)
152
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON)
Duncan Palmer's avatar
Duncan Palmer committed
153
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} ON)
154
   endif()
155
156
157
158
159
   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) 
160
   toggle_preprocessor_switch(DLIB_GIF_SUPPORT) 
161
   #toggle_preprocessor_switch(DLIB_USE_FFTW)
Duncan Palmer's avatar
Duncan Palmer committed
162
   toggle_preprocessor_switch(DLIB_USE_MKL_FFT)
163

164

165
166
167
168
169
170
171
172
173
174
175
176
   set(source_files 
         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
Davis King's avatar
Davis King committed
177
         data_io/image_dataset_metadata.cpp
Davis King's avatar
Davis King committed
178
         data_io/mnist.cpp
179
         dnn/cpu_dlib.cpp
180
         dnn/tensor_tools.cpp
Davis King's avatar
merged  
Davis King committed
181
   )
Davis King's avatar
Davis King committed
182

183

184
   if (DLIB_ISO_CPP_ONLY)
185
       add_library(dlib STATIC ${source_files} )
186
       if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
187
           add_library(dlib_shared SHARED ${source_files} )
188
           add_dependencies(dlib_shared dlib)
189
       endif()
190
   else()
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216

      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
217
         threads/async.cpp
218
219
220
221
         timer/timer.cpp
         stack_trace.cpp
         )

222
      set(dlib_needed_libraries)
223
      set(dlib_needed_includes)
224
225
226
227
228
229
      if(UNIX)
         set(CMAKE_THREAD_PREFER_PTHREAD ON)
         find_package(Threads REQUIRED)
         set(dlib_needed_libraries ${dlib_needed_libraries} ${CMAKE_THREAD_LIBS_INIT})
      endif()

230
231
232
      # we want to link to the right stuff depending on our platform.  
      if (WIN32 AND NOT CYGWIN) ###############################################################################
         if (DLIB_NO_GUI_SUPPORT)
233
            set (dlib_needed_libraries ws2_32 winmm)
234
         else()
235
            set (dlib_needed_libraries ws2_32 winmm comctl32 gdi32 imm32)
236
237
         endif()
      elseif(APPLE) ############################################################################
Davis King's avatar
Davis King committed
238
         set(CMAKE_MACOSX_RPATH 1)
239
         if (NOT DLIB_NO_GUI_SUPPORT)
240
241
            find_package(X11 QUIET)
            if (X11_FOUND)
242
               # If both X11 and anaconda are installed, it's possible for the
Davis King's avatar
merged  
Davis King committed
243
244
               # anaconda path to appear before /opt/X11, so we remove anaconda.
               foreach (ITR ${X11_INCLUDE_DIR})
marcovzla's avatar
marcovzla committed
245
                  if ("${ITR}" MATCHES "(.*)(ana|mini)conda(.*)")
246
                     list (REMOVE_ITEM X11_INCLUDE_DIR ${ITR})
Davis King's avatar
merged  
Davis King committed
247
                  endif ()
248
               endforeach(ITR)
249
250
               include_directories(${X11_INCLUDE_DIR})
               set (dlib_needed_libraries ${dlib_needed_libraries} ${X11_LIBRARIES})
251
            else()
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
               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)
270
271
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
272
273
               message(" *** Make sure XQuartz is installed if you want GUI support.               ***")
               message(" *** You can download XQuartz from: http://xquartz.macosforge.org/landing/ ***")
274
               message(" *****************************************************************************")
275
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
276
               enable_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
277
            endif()
278
279
         endif()

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
         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()
300
301
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
302
303
               message(" *** Make sure libx11-dev is installed if you want GUI support.            ***")
               message(" *** On Ubuntu run: sudo apt-get install libx11-dev                        ***")
304
               message(" *****************************************************************************")
305
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
306
               enable_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
307
            endif()
308
309
         endif()

310
311
         mark_as_advanced(nsllib pthreadlib socketlib)
      endif () ##################################################################################
312

313
314
315
316
317
318
319
320
321
322
323
324
      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()
325

326
      INCLUDE (CheckFunctionExists)
327

328
329
330
      if (DLIB_GIF_SUPPORT)
         find_package(GIF QUIET)
         if (GIF_FOUND)
331
            set (dlib_needed_includes ${dlib_needed_includes} ${GIF_INCLUDE_DIR})
332
333
334
335
336
337
338
            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()

339
      if (DLIB_PNG_SUPPORT)
340
         # try to find libpng 
Davis King's avatar
Davis King committed
341
         find_package(PNG QUIET)
342
343
         # Make sure there isn't something wrong with the version of LIBPNG
         # installed on this system.  
344
345
346
347
         if (PNG_FOUND)
            set(CMAKE_REQUIRED_LIBRARIES ${PNG_LIBRARY})
            CHECK_FUNCTION_EXISTS(png_create_read_struct LIBPNG_IS_GOOD)
         endif()
348
         if (PNG_FOUND AND LIBPNG_IS_GOOD)
349
350
            include_directories(${PNG_INCLUDE_DIR})
            set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARY})
351
            set(REQUIRES_LIBS " libpng")
352
         else()
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
            # 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
               )
387
388
389
390
391
392
393
394
395
            if (NEON)
               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()
396
            set(REQUIRES_LIBS "")
397
         endif()
398
399
400
401
         set(source_files ${source_files}
            image_loader/png_loader.cpp
            image_saver/save_png.cpp
            )
402
      endif()
403

404
      if (DLIB_JPEG_SUPPORT)
405
         # try to find libjpeg 
406
         find_package(JPEG QUIET)
407
         # Make sure there isn't something wrong with the version of libjpeg 
408
409
         # 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).
410
411
412
413
         if (JPEG_FOUND)
            set(CMAKE_REQUIRED_LIBRARIES ${JPEG_LIBRARY})
            CHECK_FUNCTION_EXISTS(jpeg_read_header LIBJPEG_IS_GOOD)
         endif()
414
         if (JPEG_FOUND AND LIBJPEG_IS_GOOD AND NOT APPLE)
415
416
417
            include_directories(${JPEG_INCLUDE_DIR})
            set (dlib_needed_libraries ${dlib_needed_libraries} ${JPEG_LIBRARY})
         else()
418
            # If we can't find libjpeg then statically compile it in.
419
            add_definitions(-DDLIB_JPEG_STATIC)
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
            set(source_files ${source_files}
                  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
Davis King's avatar
Davis King committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
                  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
                  )
466
         endif()
467
468
         set(source_files ${source_files}
            image_loader/jpeg_loader.cpp
Davis King's avatar
Davis King committed
469
            image_saver/save_jpeg.cpp
470
            )
471
472
      endif()

473

474
475
      if (DLIB_USE_BLAS OR DLIB_USE_LAPACK OR DLIB_USE_MKL_FFT)
          # Try to find BLAS, LAPACK and MKL
476
         include(cmake_utils/cmake_find_blas.txt)
477

478
479
480
481
482
         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 )
483
               toggle_preprocessor_switch(DLIB_USE_BLAS)
484
            endif()
485
486
         endif()

487
488
489
490
491
         if (DLIB_USE_LAPACK)
            if (lapack_found)
               set (dlib_needed_libraries ${dlib_needed_libraries} ${lapack_libraries})
            else()
               set(DLIB_USE_LAPACK OFF CACHE STRING ${DLIB_USE_LAPACK_STR} FORCE )
492
               toggle_preprocessor_switch(DLIB_USE_LAPACK)
493
            endif()
494
         endif()
495

496
         if (DLIB_USE_MKL_FFT)
497
            if (found_intel_mkl AND found_intel_mkl_headers)
498
               set (dlib_needed_includes ${dlib_needed_includes} ${mkl_include_dir})
499
500
501
502
503
504
               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
505
506
      endif()

507

508
      if (DLIB_USE_CUDA)
509
         find_package(CUDA 7.5)
510

511
512
513
514
         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()
515

516
         if (CUDA_FOUND AND COMPILER_CAN_DO_CPP_11)
517

518
519
520
521
            # 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.
522
            if (APPLE)
523
               set(CUDA_PROPAGATE_HOST_FLAGS OFF)
524
525
526
               # Grab all the -D flags from CMAKE_CXX_FLAGS so we can pass them
               # to nvcc.
               string(REGEX MATCHALL "-D[^ ]*" FLAGS_FOR_NVCC ${CMAKE_CXX_FLAGS})
527
528
            endif()

529

530
            set(CUDA_HOST_COMPILATION_CPP ON)
531
532
533
            # 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).
534
            list(APPEND CUDA_NVCC_FLAGS "-arch=sm_30;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES;${FLAGS_FOR_NVCC}")
535
            list(APPEND CUDA_NVCC_FLAGS ${active_preprocessor_switches})
536
537
538
            if (NOT MSVC)
               list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
            endif()
539
540
541
            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.
542
543
544
               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()
545
            endif()
546

547
            include(cmake_utils/test_for_cudnn/find_cudnn.txt)
548

549
            if (cudnn AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked)
550
551
               # 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...")
552
553
554
555
556

               set(CUDA_TEST_CMAKE_FLAGS "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
                                         "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}"
                                         "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}")

557
               if (NOT MSVC) # see https://github.com/davisking/dlib/issues/363
558
559
560
                  list(APPEND CUDA_TEST_CMAKE_FLAGS "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}")
               endif()

561
               try_compile(cuda_test_compile_worked ${PROJECT_BINARY_DIR}/cuda_test_build 
562
                           ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test
563
                           CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
564
                           )
565
               if (NOT cuda_test_compile_worked)
566
567
568
569
                  message(STATUS "*** CUDA was found but your compiler failed to compile a simple CUDA program so dlib isn't going to use CUDA. ***")
               else()
                  message(STATUS "Checking if you have the right version of cuDNN installed.")
                  try_compile(cudnn_test_compile_worked ${PROJECT_BINARY_DIR}/cudnn_test_build 
570
                              ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test
571
                              CMAKE_FLAGS ${CUDA_TEST_CMAKE_FLAGS}
572
                                          )
573
574
575
576
577
                  if (cudnn_test_compile_worked)
                     message(STATUS "Found cuDNN: " ${cudnn})
                  else()
                     message(STATUS "*** Found cuDNN, but it looks like the wrong version so dlib will not use it. ***")
                  endif()
578
579
               endif()
            endif()
580

581
582
            # Find where cuSOLVER is since the FindCUDA cmake package doesn't
            # bother to look for it.
583
            get_filename_component(cuda_blas_path "${CUDA_CUBLAS_LIBRARIES}" DIRECTORY)
584
            find_library(cusolver cusolver HINTS ${cuda_blas_path})
585
586
587
588
589
590
            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
            # than Intel's when you use the MKL.
591
592
593
594
            if (NOT openmp_libarires AND NOT MSVC)
               find_package(OpenMP)
               if (OPENMP_FOUND)
                  set(openmp_libarires ${OpenMP_CXX_FLAGS}) 
595
               else()
596
597
                  message(STATUS "*** Didn't find OpenMP, which is required to use CUDA. ***")
                  set(CUDA_FOUND 0)
598
               endif()
599
600
            endif()
         endif()
601

602
         if (CUDA_FOUND AND cudnn AND cudnn_include AND COMPILER_CAN_DO_CPP_11 AND cuda_test_compile_worked AND cudnn_test_compile_worked)
603
            set(source_files ${source_files} 
Davis King's avatar
Davis King committed
604
605
606
               dnn/cuda_dlib.cu 
               dnn/cudnn_dlibapi.cpp
               dnn/cublas_dlibapi.cpp
607
               dnn/cusolver_dlibapi.cu
Davis King's avatar
Davis King committed
608
               dnn/curand_dlibapi.cpp
609
               dnn/cuda_data_ptr.cpp
610
               dnn/gpu_data.cpp
611
               )
Davis King's avatar
Davis King committed
612
613
614
615
            set(dlib_needed_libraries ${dlib_needed_libraries} 
                                      ${CUDA_CUBLAS_LIBRARIES} 
                                      ${cudnn}
                                      ${CUDA_curand_LIBRARY}
616
                                      ${cusolver}
617
                                      ${openmp_libarires}
Davis King's avatar
Davis King committed
618
                                      )
619
620
621
            include_directories(${cudnn_include})
         else()
            set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
622
            toggle_preprocessor_switch(DLIB_USE_CUDA)
623
624
625
626
627
628
            if (COMPILER_CAN_DO_CPP_11)
               if (NOT cudnn OR NOT cudnn_include OR NOT cudnn_test_compile_worked)
                  message(STATUS "*** cuDNN V5.0 OR GREATER NOT FOUND.  DLIB WILL NOT USE CUDA. ***")
                  message(STATUS "*** If you have cuDNN then set CMAKE_PREFIX_PATH to include cuDNN's folder.")
               endif()
            else ()
629
               message(STATUS "*** Dlib CUDA support requires C++11 but your compiler doesn't support it. ***")
630
            endif()
631
632
633
         endif()
      endif()
      
634

635
636
637
638
639
640
      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)
            get_filename_component(sqlite_path2 ${sqlite_path} PATH CACHE)
641
            set(dlib_needed_includes ${dlib_needed_includes} ${sqlite_path2})
642
643
644
645
646
            set(dlib_needed_libraries ${dlib_needed_libraries} ${sqlite} )
         else()
            set(DLIB_LINK_WITH_SQLITE3 OFF CACHE STRING ${DLIB_LINK_WITH_SQLITE3_STR} FORCE )
         endif()
         mark_as_advanced(sqlite sqlite_path sqlite_path2)
647
648
649
      endif()


650

651
      if (DLIB_USE_FFTW)
652
653
654
655
         find_library(fftw fftw3)
         # make sure fftw3.h is in the include path
         find_path(fftw_path fftw3.h)
         if (fftw AND fftw_path)
656
            set(dlib_needed_includes ${dlib_needed_includes} ${fftw_path})
657
658
            set(dlib_needed_libraries ${dlib_needed_libraries} ${fftw} )
         else()
659
660
            set(DLIB_USE_FFTW OFF CACHE STRING ${DLIB_USE_FFTW_STR} FORCE )
            toggle_preprocessor_switch(DLIB_USE_FFTW)
661
         endif()
662
         mark_as_advanced(fftw fftw_path)
663
664
      endif()

665

666
667
668

      # Tell CMake to build dlib via add_library()/cuda_add_library()
      if (DLIB_USE_CUDA)
669
670
671
         # 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})
672
673
674
675
         cuda_add_library(dlib STATIC ${source_files} )
      else()
         add_library(dlib STATIC ${source_files} )
      endif()
676
677
678
679
680
681
      target_include_directories(dlib
                                 INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
                                 INTERFACE $<INSTALL_INTERFACE:include>
                                 PUBLIC ${dlib_needed_includes}
                                 )
      target_link_libraries(dlib PRIVATE ${dlib_needed_libraries})
682
683
684
685
686
      if (DLIB_IN_PROJECT_BUILD)
         target_compile_options(dlib PUBLIC ${active_preprocessor_switches})
      else()
         target_compile_options(dlib PRIVATE ${active_preprocessor_switches})
      endif()
687
      if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
688
         if (DLIB_USE_CUDA)
689
            cuda_add_library(dlib_shared SHARED ${source_files} )
690
            add_dependencies(dlib_shared dlib)
691
         else()
692
            add_library(dlib_shared SHARED ${source_files} )
693
            add_dependencies(dlib_shared dlib)
694
         endif()
695
696
697
698
699
700
         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 PRIVATE ${dlib_needed_libraries})
701
         target_compile_options(dlib_shared PRIVATE ${active_preprocessor_switches})
702
      endif()
703

704
   endif ()  ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################
705

706
707
708
   # 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)
      ADD_LIBRARY(dlib_all_source_cpp STATIC all/source.cpp) 
709
      target_link_libraries(dlib_all_source_cpp dlib)
710
      target_compile_options(dlib_all_source_cpp PUBLIC ${active_preprocessor_switches})
711
712
   endif()

713
   # Install the library
714
   if (NOT DLIB_IN_PROJECT_BUILD)
715
       cmake_minimum_required(VERSION 2.8.8)
716
       if(UNIX)
717
           set_target_properties(dlib_shared PROPERTIES
718
719
                                        OUTPUT_NAME dlib 
                                        VERSION ${VERSION})
720
           install(TARGETS dlib dlib_shared
721
                   EXPORT dlib 
David Seifert's avatar
David Seifert committed
722
723
724
                   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # Windows (including cygwin) considers .dll to be runtime artifacts
                   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
725
726
727
       else()
           install(TARGETS dlib
                   EXPORT dlib 
David Seifert's avatar
David Seifert committed
728
729
730
                   RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # Windows considers .dll to be runtime artifacts
                   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
731
       endif()
732

David Seifert's avatar
David Seifert committed
733
       install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib
734
               FILES_MATCHING PATTERN "*.h" PATTERN "*.cmake"
735
736
               REGEX "${CMAKE_CURRENT_BINARY_DIR}" EXCLUDE)

737

Davis King's avatar
Davis King committed
738
       configure_file(${PROJECT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
739
       # overwrite config.h with the configured one
David Seifert's avatar
David Seifert committed
740
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib)
741

742
       configure_file(${PROJECT_SOURCE_DIR}/revision.h.in ${CMAKE_CURRENT_BINARY_DIR}/revision.h)
David Seifert's avatar
David Seifert committed
743
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/revision.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dlib)
744

745
746
       ## Config.cmake generation and installation

David Seifert's avatar
David Seifert committed
747
       set(ConfigPackageLocation "${CMAKE_INSTALL_LIBDIR}/cmake/dlib")
748
749
750
751
       install(EXPORT dlib
            NAMESPACE dlib::
            DESTINATION ${ConfigPackageLocation})

752
       configure_file(cmake_utils/dlibConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" @ONLY)
753
754
755

       include(CMakePackageConfigHelpers)
       write_basic_package_version_file(
756
           "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake"
757
758
759
760
761
           VERSION ${VERSION}
           COMPATIBILITY AnyNewerVersion
           )

       install(FILES 
762
763
                    "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" 
                    "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake" 
764
               DESTINATION ${ConfigPackageLocation})
765
766
767

       ## dlib-1.pc generation and installation

768
       configure_file("cmake_utils/dlib.pc.in" "dlib-1.pc" @ONLY)
769
       install(FILES "${CMAKE_CURRENT_BINARY_DIR}/dlib-1.pc"
David Seifert's avatar
David Seifert committed
770
           DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
771

772
773
   endif()

774
endif()
775
776

add_library(dlib::dlib ALIAS dlib)