cjpeg 的 cmake配置

Ding-yixia發表於2024-06-10
# 設定構建型別

set(CMAKE_BUILD_TYPE Debug)
set(jpeg_lib "${CMAKE_CURRENT_SOURCE_DIR}/lib/jpeg.lib")
set(libpng_lib "C:/Users/20021/Desktop/vcpkg/packages/libpng_x64-windows/lib/libpng16.lib")
set(zlib_lib "C:/Users/20021/Desktop/vcpkg/packages/zlib_x64-windows/lib/zlib.lib")
set(src_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)

# 新增前處理器定義
add_definitions(
  -DWIN32
  -D_WINDOWS
  -DBMP_SUPPORTED
  -DGIF_SUPPORTED
  -DPPM_SUPPORTED
  -DTARGA_SUPPORTED
  -DUSE_SETMODE
  -DPNG_SUPPORTED
  -D_CRT_NONSTDC_NO_WARNINGS
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..)

if(MSVC)
  # Build all configurations against shared C library
  foreach(var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
    CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
    if(${var} MATCHES "/MT")
      string(REGEX REPLACE "/MT" "/MD" ${var} "${${var}}")
    endif()
  endforeach()
endif()

if(WITH_SIMD AND(MSVC_IDE OR XCODE))
  # This tells CMake that the "source" files haven't been generated yet
  set_source_files_properties(${SIMD_OBJS} PROPERTIES GENERATED 1)
endif()

if(WIN32)
  set(USE_SETMODE "-DUSE_SETMODE")
endif()

if(WITH_12BIT)
  set(COMPILE_FLAGS "-DGIF_SUPPORTED -DPPM_SUPPORTED ${USE_SETMODE}")
else()
  set(COMPILE_FLAGS "-DBMP_SUPPORTED -DGIF_SUPPORTED -DPPM_SUPPORTED -DTARGA_SUPPORTED ${USE_SETMODE}")
  set(CJPEG_BMP_SOURCES ${src_dir}/rdbmp.c ${src_dir}/rdtarga.c)

  if(PNG_SUPPORTED)
    report_option(PNG_SUPPORTED "PNG reading support")
    set(COMPILE_FLAGS "${COMPILE_FLAGS} -DPNG_SUPPORTED")
    set(CJPEG_BMP_SOURCES ${CJPEG_BMP_SOURCES} ${src_dir}/rdpng.c)
  endif()
endif()

add_executable(cjpeg ${src_dir}/cjpeg.c ${src_dir}/cdjpeg.c ${src_dir}/rdgif.c ${src_dir}/rdppm.c ${src_dir}/rdjpeg.c
  ${src_dir}/rdswitch.c
  ${CMAKE_CURRENT_SOURCE_DIR}/include/cdjpeg.h
  ${CMAKE_CURRENT_SOURCE_DIR}/include/png.h
  ${CMAKE_CURRENT_SOURCE_DIR}/include/pngconf.h
  ${CMAKE_CURRENT_SOURCE_DIR}/include/pnglibconf.h
  ${CJPEG_BMP_SOURCES})
set_property(TARGET cjpeg PROPERTY COMPILE_FLAGS ${COMPILE_FLAGS})

# target_link_libraries(cjpeg jpeg)
target_link_libraries(cjpeg PUBLIC ${jpeg_lib} ${zlib_lib} ${libpng_lib})

target_include_directories(cjpeg PUBLIC
  "C:/Code/mozjpeg-4.1.5/build"
  "C:/Code/mozjpeg-4.1.5"
  "C:/Users/20021/Desktop/vcpkg/packages/libpng_x64-windows/include"
  "C:/Users/20021/Desktop/vcpkg/packages/zlib_x64-windows/include"
  "C:/Users/20021/Desktop/vcpkg/packages/libpng_x64-windows/include"
)

if(PNG_SUPPORTED)
  # to avoid finding static library from CMake cache
  unset(PNG_LIBRARY CACHE)
  unset(PNG_LIBRARY_RELEASE CACHE)
  unset(PNG_LIBRARY_DEBUG CACHE)
  unset(ZLIB_LIBRARY CACHE)
  unset(ZLIB_LIBRARY_RELEASE CACHE)
  unset(ZLIB_LIBRARY_DEBUG CACHE)
  set(CMAKE_PREFIX_PATH "C:/Users/20021/Desktop/vcpkg/packages/libpng_x64-windows;C:/Users/20021/Desktop/vcpkg/packages/zlib_x64-windows;C:/Users/20021/Desktop/vcpkg/packages/libpng_x64-windows/bin;C:/Users/20021/Desktop/vcpkg/packages/zlib_x64-windows/bin")
  find_package(PNG 1.6 REQUIRED)
  find_package(ZLIB REQUIRED)
  target_include_directories(cjpeg PUBLIC ${PNG_INCLUDE_DIR} ${ZLIB_INCLUDE_DIR})
  target_link_libraries(cjpeg ${PNG_LIBRARY} ${ZLIB_LIBRARY})
endif()

相關文章