cmake_minimum_required(VERSION 2.6...3.29)
project(libjpeg-turbo)

add_subdirectory(simd)

set(JPEG_SOURCES 
    jcomapi.c jdapimin.c jdapistd.c jdatasrc.c jdcoefct.c jdcolor.c
    jddctmgr.c jdhuff.c jdinput.c jdmainct.c jdmarker.c jdmaster.c jdmerge.c
    jdphuff.c jdpostct.c jdsample.c jerror.c jidctflt.c jidctfst.c jidctint.c
    jidctred.c jquant1.c jquant2.c jutils.c jmemmgr.c jmemnobs.c jaricom.c jdarith.c
)
file(GLOB_RECURSE JPEG_HEADERS *.h)

#NOTE-jip: Filehandler somehow can't find jpegturbo_SOURCE_DIR, so we expose another variable so it can include the headers
set(jpeg_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")

#TODO-jip: What if we want 32bit on 64 bit system?
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # 64 bits
    message("libjpeg-turbo building for 64 bit.")
    set(JPEG_SOURCES ${JPEG_SOURCES} simd/jsimd_x86_64.c)
    add_definitions(-DWIN64 -D__x86_64__ -DPIC)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32 bits
    message("libjpeg-turbo building for 32 bit.")
    set(JPEG_SOURCES ${JPEG_SOURCES} simd/jsimd_i386.c)
endif()

set_source_files_properties(${SIMD_OBJS} PROPERTIES GENERATED 1)

add_library(jpegturbo STATIC ${JPEG_SOURCES} ${JPEG_HEADERS} ${SIMD_OBJS}) # remove static, cmake should take care of this
target_include_directories(jpegturbo PUBLIC ${JPEG_HEADERS})
add_dependencies(jpegturbo jpegturbo_simd)

set_target_properties(jpegturbo PROPERTIES FOLDER deps)
