CMakeLists.txt 29.8 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.4)
8
project(dlib)
9

10
# default to a Release build (except if CMAKE_BUILD_TYPE is set)
11
12
include(cmake_utils/release_build_by_default)
include(cmake_utils/use_cpp_11.cmake)
13

14

Davis King's avatar
Davis King committed
15
set(CPACK_PACKAGE_VERSION_MAJOR "19")
Davis King's avatar
Davis King committed
16
set(CPACK_PACKAGE_VERSION_MINOR "2")
17
set(CPACK_PACKAGE_VERSION_PATCH "99")
18
set(VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
19
20
21
22
23
# 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()
24

25
26
27
28
29
30
31
32
33
34
set(dlib_needed_includes)
# This macro sets include directory paths that are needed by dlib and also by
# applications that include dlib.  So the main point of this macro is to set
# dlib_needed_includes which will get pushed into the parent cmake scope at the
# end of this CMakeLists.txt file.  This way, it is available to users of dlib/cmake.
macro(add_include_directories dir)
   include_directories(${dir})
   set(dlib_needed_includes ${dlib_needed_includes} ${dir})
endmacro()

35
# Suppress cmake warnings about changes in new versions.
36
37
if(COMMAND cmake_policy) 
   cmake_policy(SET CMP0003 NEW) 
38
39
40
   if (POLICY CMP0054)
      cmake_policy(SET CMP0054 NEW)
   endif()
41
endif()
42

43
include(cmake_utils/add_global_compiler_switch.cmake)
44
45


46
47
48
49
50
51
52
53
54
55
56
57
if (DLIB_IN_PROJECT_BUILD)
   # Make sure ENABLE_ASSERTS is defined for debug builds, but only for uses
   # who are building an application.  If they are just building dlib as a
   # stand alone library then don't set this because it will conflict with the
   # settings in config.h if we did.
   if (NOT CMAKE_CXX_FLAGS_DEBUG MATCHES "-DENABLE_ASSERTS")
      set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DENABLE_ASSERTS" 
         CACHE STRING "Flags used by the compiler during C++ debug builds." 
         FORCE)
   endif()
endif()

58
59
60
61
62
63
64
macro (toggle_preprocessor_switch option_name)
   if (${option_name})
      add_global_define(${option_name})
   else()
      remove_global_define(${option_name})
   endif()
endmacro()
65

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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_ENABLE_ASSERTS_STR 
   "Enable this if you want to turn on the DLIB_ASSERT macro" )
   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" )
86
87
   set (DLIB_USE_CUDA_STR
   "Disable this if you don't want to use NVIDIA CUDA" )
88
   set (DLIB_PNG_SUPPORT_STR
89
   "Disable this if you don't want to link against libpng" )
90
91
   set (DLIB_GIF_SUPPORT_STR
   "Disable this if you don't want to link against libgif" )
92
   set (DLIB_JPEG_SUPPORT_STR
93
94
95
   "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" )
96
   #set (DLIB_USE_FFTW_STR "Disable this if you don't want to link against fftw" )
Duncan Palmer's avatar
Duncan Palmer committed
97
98
   set (DLIB_USE_MKL_FFT_STR
   "Disable this is you don't want to use the MKL DFTI FFT implementation" )
99
100

   option(DLIB_ISO_CPP_ONLY ${DLIB_ISO_CPP_ONLY_STR} OFF)
101
   toggle_preprocessor_switch(DLIB_ISO_CPP_ONLY)
102
   option(DLIB_NO_GUI_SUPPORT ${DLIB_NO_GUI_SUPPORT_STR} OFF)
103
   toggle_preprocessor_switch(DLIB_NO_GUI_SUPPORT)
104
   option(DLIB_ENABLE_STACK_TRACE ${DLIB_ENABLE_STACK_TRACE_STR} OFF)
105
   toggle_preprocessor_switch(DLIB_ENABLE_STACK_TRACE)
106

107
108
   option(DLIB_ENABLE_ASSERTS ${DLIB_ENABLE_ASSERTS_STR} OFF)
   if(DLIB_ENABLE_ASSERTS)
109
110
111
112
      # 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) 
113
      add_global_define(ENABLE_ASSERTS)
114
      remove_global_define(DLIB_DISABLE_ASSERTS)
115
116
117
118
119
   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) 
120
      remove_global_define(ENABLE_ASSERTS)
121
122
123
124
125
126
127
128
      # Never force the asserts off when doing an in project build.  Instead,
      # let the debug/release mode setting toggle asserts on or off (or the
      # DLIB_ENABLE_ASSERTS option obviously).  That is, even if the
      # DLIB_ENABLE_ASSERTS option is off debug mode can still cause the
      # asserts to turn on when using an in project build. 
      if (NOT DLIB_IN_PROJECT_BUILD)
         add_global_define(DLIB_DISABLE_ASSERTS)
      endif()
129
130
131
132
133
134
135
136
137
   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)
138
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} OFF)
139
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} OFF)
Duncan Palmer's avatar
Duncan Palmer committed
140
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} OFF)
141
   else()
142
143
144
145
146
147
      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)
148
      option(DLIB_GIF_SUPPORT ${DLIB_GIF_SUPPORT_STR} ON)
149
      #option(DLIB_USE_FFTW ${DLIB_USE_FFTW_STR} ON)
Duncan Palmer's avatar
Duncan Palmer committed
150
      option(DLIB_USE_MKL_FFT ${DLIB_USE_MKL_FFT_STR} ON)
151
   endif()
152
153
154
155
156
   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) 
157
   toggle_preprocessor_switch(DLIB_GIF_SUPPORT) 
158
   #toggle_preprocessor_switch(DLIB_USE_FFTW)
Duncan Palmer's avatar
Duncan Palmer committed
159
   toggle_preprocessor_switch(DLIB_USE_MKL_FFT)
160

161

162
163
164
165
166
167
168
169
170
171
172
173
   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
174
175
         data_io/image_dataset_metadata.cpp
         data_io/mnist.cpp)
176

177
178
179
   if (COMPILER_CAN_DO_CPP_11)
      set(source_files ${source_files}
         dnn/cpu_dlib.cpp
180
         dnn/tensor_tools.cpp
181
182
183
         )
   endif()

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
       endif()
189
   else()
190
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

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

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

228
229
230
      # we want to link to the right stuff depending on our platform.  
      if (WIN32 AND NOT CYGWIN) ###############################################################################
         if (DLIB_NO_GUI_SUPPORT)
231
            set (dlib_needed_libraries ws2_32 winmm)
232
         else()
233
            set (dlib_needed_libraries ws2_32 winmm comctl32 gdi32 imm32)
234
235
         endif()
      elseif(APPLE) ############################################################################
Davis King's avatar
Davis King committed
236
         set(CMAKE_MACOSX_RPATH 1)
237
         if (NOT DLIB_NO_GUI_SUPPORT)
238
239
240
241
            find_package(X11 QUIET)
            if (X11_FOUND)
               include_directories(${X11_INCLUDE_DIR})
               set (dlib_needed_libraries ${dlib_needed_libraries} ${X11_LIBRARIES})
242
            else()
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
               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)
261
262
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
263
264
               message(" *** Make sure XQuartz is installed if you want GUI support.               ***")
               message(" *** You can download XQuartz from: http://xquartz.macosforge.org/landing/ ***")
265
               message(" *****************************************************************************")
266
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
267
               add_global_define(DLIB_NO_GUI_SUPPORT)
268
            endif()
269
270
         endif()

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
         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()
291
292
               message(" *****************************************************************************")
               message(" *** DLIB GUI SUPPORT DISABLED BECAUSE X11 DEVELOPMENT LIBRARIES NOT FOUND ***")
293
294
               message(" *** Make sure libx11-dev is installed if you want GUI support.            ***")
               message(" *** On Ubuntu run: sudo apt-get install libx11-dev                        ***")
295
               message(" *****************************************************************************")
296
               set(DLIB_NO_GUI_SUPPORT ON CACHE STRING ${DLIB_NO_GUI_SUPPORT_STR} FORCE )
297
               add_global_define(DLIB_NO_GUI_SUPPORT)
298
            endif()
299
300
         endif()

301
302
         mark_as_advanced(nsllib pthreadlib socketlib)
      endif () ##################################################################################
303

304
305
306
307
308
309
310
311
312
313
314
315
      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()
316

317
      INCLUDE (CheckFunctionExists)
318

319
320
321
      if (DLIB_GIF_SUPPORT)
         find_package(GIF QUIET)
         if (GIF_FOUND)
322
            add_include_directories(${GIF_INCLUDE_DIR})
323
324
325
326
327
328
329
            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()

330
      if (DLIB_PNG_SUPPORT)
331
         # try to find libpng 
Davis King's avatar
Davis King committed
332
         find_package(PNG QUIET)
333
334
         # Make sure there isn't something wrong with the version of LIBPNG
         # installed on this system.  
335
336
337
338
         if (PNG_FOUND)
            set(CMAKE_REQUIRED_LIBRARIES ${PNG_LIBRARY})
            CHECK_FUNCTION_EXISTS(png_create_read_struct LIBPNG_IS_GOOD)
         endif()
339
         if (PNG_FOUND AND LIBPNG_IS_GOOD)
340
341
            include_directories(${PNG_INCLUDE_DIR})
            set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARY})
342
            set(REQUIRES_LIBS " libpng")
343
         else()
344
345
346
347
348
349
350
351
352
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
            # 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
               )
378
379
380
381
382
383
384
385
386
            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()
387
            set(REQUIRES_LIBS "")
388
         endif()
389
390
391
392
         set(source_files ${source_files}
            image_loader/png_loader.cpp
            image_saver/save_png.cpp
            )
393
      endif()
394

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

464

465
466
      if (DLIB_USE_BLAS OR DLIB_USE_LAPACK OR DLIB_USE_MKL_FFT)
          # Try to find BLAS, LAPACK and MKL
467
         include(cmake_utils/cmake_find_blas.txt)
468

469
470
471
472
473
         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 )
474
               toggle_preprocessor_switch(DLIB_USE_BLAS)
475
            endif()
476
477
         endif()

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

487
         if (DLIB_USE_MKL_FFT)
488
            if (found_intel_mkl AND found_intel_mkl_headers)
489
               set (dlib_needed_libraries ${dlib_needed_libraries} ${mkl_libraries})
490
               add_include_directories(${mkl_include_dir})
491
492
493
494
495
            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
496
497
      endif()

498

499
      if (DLIB_USE_CUDA)
500
         find_package(CUDA 7.5)
501

502
503
504
505
         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()
506

507
         if (CUDA_FOUND AND COMPILER_CAN_DO_CPP_11)
508

509
            set(CUDA_HOST_COMPILATION_CPP ON)
510
511
512
            # 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).
513
            list(APPEND CUDA_NVCC_FLAGS "-arch=sm_30;-std=c++11;-D__STRICT_ANSI__;-D_MWAITXINTRIN_H_INCLUDED;-D_FORCE_INLINES")
514

515
            include(cmake_utils/test_for_cudnn/find_cudnn.txt)
516

517
            if (cudnn AND NOT DEFINED cuda_test_compile_worked AND NOT DEFINED cudnn_test_compile_worked)
518
519
520
               # 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...")
               try_compile(cuda_test_compile_worked ${PROJECT_BINARY_DIR}/cuda_test_build 
521
                           ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cuda cuda_test
522
523
524
                           CMAKE_FLAGS "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
                                       "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}"
                                       "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}"
525
                                       "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}"
526
                           )
527
               if (NOT cuda_test_compile_worked)
528
529
530
531
                  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 
532
                              ${PROJECT_SOURCE_DIR}/cmake_utils/test_for_cudnn cudnn_test
533
534
535
                              CMAKE_FLAGS "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}"
                                          "-DCMAKE_INCLUDE_PATH=${CMAKE_INCLUDE_PATH}"
                                          "-DCMAKE_LIBRARY_PATH=${CMAKE_LIBRARY_PATH}"
536
                                          "-DCUDA_HOST_COMPILER=${CUDA_HOST_COMPILER}"
537
                                          )
538
539
540
541
542
                  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()
543
544
               endif()
            endif()
545
546
         endif()

547

548
         if (CUDA_FOUND AND cudnn AND cudnn_include AND COMPILER_CAN_DO_CPP_11 AND cuda_test_compile_worked AND cudnn_test_compile_worked)
549
            set(source_files ${source_files} 
Davis King's avatar
Davis King committed
550
551
552
               dnn/cuda_dlib.cu 
               dnn/cudnn_dlibapi.cpp
               dnn/cublas_dlibapi.cpp
Davis King's avatar
Davis King committed
553
               dnn/curand_dlibapi.cpp
554
               dnn/gpu_data.cpp
555
               )
Davis King's avatar
Davis King committed
556
557
558
559
560
            set(dlib_needed_libraries ${dlib_needed_libraries} 
                                      ${CUDA_CUBLAS_LIBRARIES} 
                                      ${cudnn}
                                      ${CUDA_curand_LIBRARY}
                                      )
561
562
563
            include_directories(${cudnn_include})
         else()
            set(DLIB_USE_CUDA OFF CACHE STRING ${DLIB_USE_BLAS_STR} FORCE )
564
            toggle_preprocessor_switch(DLIB_USE_CUDA)
565
566
567
568
569
570
            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 ()
571
               message(STATUS "*** Dlib CUDA support requires C++11 but your compiler doesn't support it. ***")
572
            endif()
573
574
575
         endif()
      endif()
      
576

577
578
579
580
581
582
      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)
583
            add_include_directories(${sqlite_path2})
584
585
586
587
588
            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)
589
590
591
      endif()


592

593
      if (DLIB_USE_FFTW)
594
595
596
597
         find_library(fftw fftw3)
         # make sure fftw3.h is in the include path
         find_path(fftw_path fftw3.h)
         if (fftw AND fftw_path)
598
            add_include_directories(${fftw_path})
599
600
            set(dlib_needed_libraries ${dlib_needed_libraries} ${fftw} )
         else()
601
602
            set(DLIB_USE_FFTW OFF CACHE STRING ${DLIB_USE_FFTW_STR} FORCE )
            toggle_preprocessor_switch(DLIB_USE_FFTW)
603
         endif()
604
         mark_as_advanced(fftw fftw_path)
605
606
      endif()

607

608
609
610
611
612
613
614
615

      # Tell CMake to build dlib via add_library()/cuda_add_library()
      if (DLIB_USE_CUDA)
         cuda_add_library(dlib STATIC ${source_files} )
      else()
         add_library(dlib STATIC ${source_files} )
      endif()
      target_link_libraries(dlib ${dlib_needed_libraries} )
616
      if (UNIX AND NOT DLIB_IN_PROJECT_BUILD)
617
         if (DLIB_USE_CUDA)
618
            cuda_add_library(dlib_shared SHARED ${source_files} )
619
         else()
620
            add_library(dlib_shared SHARED ${source_files} )
621
         endif()
622
         target_link_libraries(dlib_shared ${dlib_needed_libraries} )
623
      endif()
624

625
   endif ()  ##### end of if NOT DLIB_ISO_CPP_ONLY ##########################################################
626

627
628
629
630
631
   # 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) 
   endif()

632
   # Install the library
633
   if (NOT DLIB_IN_PROJECT_BUILD)
634
       set (LIB_INSTALL_DIR lib CACHE STRING "Install location of libraries (e.g. lib32 or lib64 for multilib installations)")
635
       cmake_minimum_required(VERSION 2.8.8)
636
       if(UNIX)
637
           set_target_properties(dlib_shared PROPERTIES
638
639
                                        OUTPUT_NAME dlib 
                                        VERSION ${VERSION})
640
           install(TARGETS dlib dlib_shared
641
642
                   EXPORT dlib 
                   RUNTIME DESTINATION bin # Windows (including cygwin) considers .dll to be runtime artifacts
643
644
                   LIBRARY DESTINATION "${LIB_INSTALL_DIR}"
                   ARCHIVE DESTINATION "${LIB_INSTALL_DIR}")
645
646
647
648
649
650
651
       else()
           install(TARGETS dlib
                   EXPORT dlib 
                   RUNTIME DESTINATION bin # Windows considers .dll to be runtime artifacts
                   LIBRARY DESTINATION lib
                   ARCHIVE DESTINATION lib)
       endif()
652

653
       install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION include/dlib
654
655
656
               FILES_MATCHING PATTERN "*.h"
               REGEX "${CMAKE_CURRENT_BINARY_DIR}" EXCLUDE)

657

Davis King's avatar
Davis King committed
658
       configure_file(${PROJECT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
659
660
661
       # overwrite config.h with the configured one
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h DESTINATION include/dlib)

662
       configure_file(${PROJECT_SOURCE_DIR}/revision.h.in ${CMAKE_CURRENT_BINARY_DIR}/revision.h)
663
664
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/revision.h DESTINATION include/dlib)

665
       install(FILES "LICENSE.txt" DESTINATION share/doc/dlib)
666

667
668
       ## Config.cmake generation and installation

669
       set(ConfigPackageLocation "${LIB_INSTALL_DIR}/cmake/dlib")
670
671
672
673
674
       install(EXPORT dlib
            NAMESPACE dlib::
            DESTINATION ${ConfigPackageLocation})

       set(CONF_INSTALL_PATH "\${dlib_CMAKE_DIR}/../../../")
675
       configure_file(cmake_utils/dlibConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" @ONLY)
676
677
678

       include(CMakePackageConfigHelpers)
       write_basic_package_version_file(
679
           "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake"
680
681
682
683
684
           VERSION ${VERSION}
           COMPATIBILITY AnyNewerVersion
           )

       install(FILES 
685
686
                    "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfig.cmake" 
                    "${CMAKE_CURRENT_BINARY_DIR}/config/dlibConfigVersion.cmake" 
687
               DESTINATION ${ConfigPackageLocation})
688
689
690

       ## dlib-1.pc generation and installation

691
       configure_file("cmake_utils/dlib.pc.in" "dlib-1.pc" @ONLY)
692
693
694
       install(FILES "${CMAKE_CURRENT_BINARY_DIR}/dlib-1.pc"
           DESTINATION "${LIB_INSTALL_DIR}/pkgconfig")

695
696
   endif()

697
endif()
698
699
700
701
702
703
704


# put dlib_needed_includes into the parent scope so the dlib/cmake file can use it.
if(has_parent)
   set(dlib_needed_includes ${dlib_needed_includes} PARENT_SCOPE)
endif()