MacroJPEG.cmake 2.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
# A macro wrapper to find JPEG library version
#
# Syntax: DETECT_JPEG()
# JPEG_LIB_VERSION is set to version ID depending of libjpeg version detected.
#
# Copyright (c) 2010-2014, Gilles Caulier, <caulier dot gilles at gmail dot com>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
# check version of libjpeg so that we can use the appropriate dir
# See bug #227313 for details

function(CompileToCheckVersion LibId Ret)

    set(_jpeglib_version_source "
    #include <stddef.h>
    #include <stdio.h>
    #include <jpeglib.h>
    int main()
    {
    #if (JPEG_LIB_VERSION >= ${LibId})
    #error JPEG_LIB_VERSION >= ${LibId}
    #endif
    }
    ")

    set(_jpeglib_version_source_file ${CMAKE_BINARY_DIR}/CMakeTmp/cmake_jpeglib_version_check.cpp)
    file(WRITE "${_jpeglib_version_source_file}" "${_jpeglib_version_source}")
    set(_jpeglib_version_include_dirs "-DINCLUDE_DIRECTORIES:STRING=${JPEG_INCLUDE_DIR}")

    try_compile(_jpeglib_version_compile_result ${CMAKE_BINARY_DIR} ${_jpeglib_version_source_file}
                CMAKE_FLAGS "${_jpeglib_version_include_dirs}"
                COMPILE_OUTPUT_VARIABLE _jpeglib_version_compile_output_var)

    set(${Ret} ${_jpeglib_version_compile_result} PARENT_SCOPE)

endfunction()

macro(DETECT_JPEG)

    find_package(JPEG REQUIRED)

    if(JPEG_FOUND)

        CompileToCheckVersion(90 _CompileResult)

        if(_CompileResult)

            # Compile sucessfuly. It's not libjpeg 90. We check previous version.

            CompileToCheckVersion(80 _CompileResult)

            if(_CompileResult)

                # Compile sucessfuly. It's not libjpeg 90. We check previous version.

                CompileToCheckVersion(70 _CompileResult)

                if(_CompileResult)

                    # Compile sucessfuly. It's not libjpeg 70.
                    set(JPEG_LIB_VERSION 62)

                else()

                    set(JPEG_LIB_VERSION 70)

                endif()

            else()

                set(JPEG_LIB_VERSION 84)

            endif()

        else()

            set(JPEG_LIB_VERSION 91)

        endif()

        message(STATUS "Libjpeg version: ${JPEG_LIB_VERSION}")

    endif()

endmacro()