
# Don't add dlib if it's already been added to the cmake project
if (NOT TARGET dlib)

    # Determine the path to dlib.
    string(REPLACE "cmake" "" dlib_path ${CMAKE_CURRENT_LIST_FILE})

    if (CMAKE_COMPILER_IS_GNUCXX)
        # By default, g++ won't warn or error if you forget to return a value in a
        # function which requires you to do so.  This option makes it give a warning
        # for doing this. 
        add_definitions("-Wreturn-type")
    endif()

    # Setup some options to allow a user to enable SSE and AVX instruction use.  
    option(USE_SSE4_INSTRUCTIONS "Compile your program with SSE4 instructions" OFF)
    option(USE_AVX_INSTRUCTIONS "Compile your program with AVX instructions" OFF)
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" OFF)
        if(USE_AVX_INSTRUCTIONS)
            add_definitions(-mavx)
        elseif (USE_SSE4_INSTRUCTIONS)
            add_definitions(-msse4)
        elseif(USE_SSE2_INSTRUCTIONS)
            add_definitions(-msse2)
        endif()
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # Visual Studio
        # Use SSE2 by default when using Visual Studio
        option(USE_SSE2_INSTRUCTIONS "Compile your program with SSE2 instructions" ON)
        if(USE_AVX_INSTRUCTIONS)
            add_definitions(/arch:AVX)
        elseif (USE_SSE4_INSTRUCTIONS)
            # There isn't an /arch:SSE4 flag in visual studio.  But we can tell
            # dlib to use those instructions anyway with the DLIB_HAVE_SSE3 and
            # DLIB_HAVE_SSE41 #defines.
            add_definitions(/arch:SSE2)  
            add_definitions(-DDLIB_HAVE_SSE3)
            add_definitions(-DDLIB_HAVE_SSE41)
        elseif(USE_SSE2_INSTRUCTIONS)
            add_definitions(/arch:SSE2)
        endif()
    endif()


    # Add folder containing dlib to the include search path.
    INCLUDE_DIRECTORIES(${dlib_path}/..)

    # This is really optional, but nice.  It will make sure the build mode 
    # created by cmake is always release by default.
    include(${dlib_path}/release_build_by_default)

    add_subdirectory(${dlib_path} dlib_build)
endif()

