include(ExternalProject)

set(JPEG_GIT_REPOSITORY "https://github.com/libjpeg-turbo/libjpeg-turbo.git")
set(JPEG_GIT_TAG "0a9b9721782d3a60a5c16c8c9a7abf3d4b1ecd42") # tag: 2.1.1

set(JPEG_DEPS ZLIB)
if(NOT WIN32)
    set(JPEG_DEPS ${JPEG_DEPS} NASM)
endif()

set(JPEG_ENABLE_SHARED ON)
set(JPEG_ENABLE_STATIC OFF)
if(NOT BUILD_SHARED_LIBS)
    set(JPEG_ENABLE_SHARED OFF)
    set(JPEG_ENABLE_STATIC ON)
endif()

set(JPEG_ARGS
    ${TLR_EXTERNAL_ARGS}
    -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_PREFIX}/lib
    -DENABLE_SHARED=${JPEG_ENABLE_SHARED}
    -DENABLE_STATIC=${JPEG_ENABLE_STATIC})
if(NOT WIN32)
    set(JPEG_ARGS ${JPEG_ARGS} -DCMAKE_ASM_NASM_COMPILER=${CMAKE_INSTALL_PREFIX}/bin/nasm)
endif()

ExternalProject_Add(
    JPEG
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/JPEG
    DEPENDS ${JPEG_DEPS}
    GIT_REPOSITORY ${JPEG_GIT_REPOSITORY}
    GIT_TAG ${JPEG_GIT_TAG}
    LIST_SEPARATOR |
    CMAKE_ARGS ${JPEG_ARGS})
