# Clip Library
# Copyright (c) 2015-2024 David Capello

cmake_minimum_required(VERSION 3.5)

project(clip LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  # Use libc++ explicitly so we can compile for
  # CMAKE_OSX_DEPLOYMENT_TARGET=10.7 or 10.8
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()

option(CLIP_ENABLE_IMAGE "Compile with support to copy/paste images" on)
if(WIN32)
  option(CLIP_ENABLE_LIST_FORMATS "Compile with support to list clipboard formats" off)
endif()
option(CLIP_EXAMPLES "Compile clip examples" on)
option(CLIP_TESTS "Compile clip tests" on)
if(UNIX AND NOT APPLE)
  option(CLIP_X11_WITH_PNG "Compile with libpng to support copy/paste image in png format" on)
endif()

add_library(clip clip.cpp)

if(CLIP_ENABLE_IMAGE)
  target_sources(clip PRIVATE image.cpp)
  target_compile_definitions(clip PUBLIC -DCLIP_ENABLE_IMAGE=1)
endif()

if(CLIP_ENABLE_LIST_FORMATS)
  target_compile_definitions(clip PUBLIC -DCLIP_ENABLE_LIST_FORMATS=1)
endif()

if(WIN32)
  option(CLIP_SUPPORT_WINXP "Enable Windows XP support" OFF)

  target_sources(clip PRIVATE clip_win.cpp)
  if(CLIP_ENABLE_IMAGE)
    target_sources(clip PRIVATE clip_win_bmp.cpp clip_win_wic.cpp)
    target_link_libraries(clip shlwapi)
  endif()

  if(MSVC)
    target_compile_definitions(clip PRIVATE -D_SCL_SECURE_NO_WARNINGS)
  endif()
  if (CLIP_SUPPORT_WINXP)
    target_compile_definitions(clip PRIVATE -DCLIP_SUPPORT_WINXP)
  endif()

  # MinGW requires the windowscodecs just because CLSIDs are defined
  # in the windowscodecs.a file instead of the wincodec.h file (?!)
  if(MINGW)
    find_library(CLIP_WINDOWSCODECS_LIBRARY windowscodecs)
    if(CLIP_WINDOWSCODECS_LIBRARY)
      target_link_libraries(clip ${CLIP_WINDOWSCODECS_LIBRARY})
    endif()
  endif()
elseif(APPLE)
  target_compile_options(clip PRIVATE -fobjc-arc)

  find_library(COCOA_LIBRARY Cocoa)
  if(COCOA_LIBRARY)
    target_sources(clip PRIVATE clip_osx.mm)
    target_link_libraries(clip ${COCOA_LIBRARY})
  else()
    target_sources(clip PRIVATE clip_none.cpp)
  endif()
elseif(UNIX)
  include(CheckIncludeFiles)
  check_include_files(xcb/xcb.h HAVE_XCB_XLIB_H)

  if(HAVE_XCB_XLIB_H)
    target_compile_definitions(clip PRIVATE -DHAVE_XCB_XLIB_H)
    target_link_libraries(clip xcb pthread)

    if(CLIP_ENABLE_IMAGE AND CLIP_X11_WITH_PNG)
      check_include_files(png.h HAVE_PNG_H)
      if(CLIP_X11_PNG_LIBRARY)
        set(PNG_LIBRARY ${CLIP_X11_PNG_LIBRARY})
      else()
        find_library(PNG_LIBRARY png)
      endif()
      if(HAVE_PNG_H AND PNG_LIBRARY)
        target_compile_definitions(clip PRIVATE -DHAVE_PNG_H)
      endif()
      target_link_libraries(clip ${PNG_LIBRARY})
    endif()
    target_sources(clip PRIVATE clip_x11.cpp)
  else()
    target_sources(clip PRIVATE clip_none.cpp)
  endif()
else()
  target_sources(clip PRIVATE clip_none.cpp)
endif()

if(CLIP_EXAMPLES)
  add_subdirectory(examples)
endif()

if(CLIP_TESTS)
  enable_testing()
  add_subdirectory(tests)
endif()
