#========================================================================
#
# cmake-config.txt
#
# CMake script to do misc cmake config and platform-specific config.
#
# Copyright 2015 Glyph & Cog, LLC
#
#========================================================================

include(CheckFunctionExists)
include(CheckCXXSourceCompiles)

enable_language(CXX)

#--- set default C/C++ compiler flags for Unix
#    and add a 'Profiling' build mode
if (CMAKE_GENERATOR STREQUAL "Unix Makefiles")
  if (CYGWIN)
    set(PIC_FLAG "")
  else ()
    set(PIC_FLAG "-fPIC")
  endif ()
  set(CMAKE_C_FLAGS_DEBUG "-g -O -Wall ${PIC_FLAG}")
  set(CMAKE_CXX_FLAGS_DEBUG "-g -O -Wall ${PIC_FLAG}")
  set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g -O -Wall ${PIC_FLAG}")
  set(CMAKE_C_FLAGS_RELEASE "-O3 -Wall ${PIC_FLAG}")
  set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall ${PIC_FLAG}")
  set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-O2 -Wall ${PIC_FLAG}")
  set(CMAKE_C_FLAGS_PROFILING "-g -pg -O -Wall ${PIC_FLAG}"
      CACHE STRING "C compiler flags for profiling mode")
  set(CMAKE_CXX_FLAGS_PROFILING "-g -pg -O -Wall ${PIC_FLAG}"
      CACHE STRING "C++ compiler flags for profiling mode")
  set(CMAKE_EXE_LINKER_FLAGS_PROFILING "-g -pg -O -Wall ${PIC_FLAG}"
      CACHE STRING "linker flags for profiling mode")
  mark_as_advanced(
    CMAKE_C_FLAGS_PROFILING
    CMAKE_CXX_FLAGS_PROFILING
    CMAKE_EXE_LINKER_FLAGS_PROFILING)
  set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
      "Choose build mode - options are: None Debug Release RelWithDebInfo MinSiz
eRel Profiling")
endif ()

#--- set default C/C++ compiler flags for Windows
if (WIN32)
  foreach (var CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG
               CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE
               CMAKE_C_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_MINSIZEREL
               CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    # note: this converts /MD to /MT and /MDd to /MTd
    string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
  endforeach ()
  set(CMAKE_EXE_LINKER_FLAGS_DEBUG "/NODEFAULTLIB:libcmt ${CMAKE_EXE_LINKER_FLAGS_DEBUG}")
  set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NODEFAULTLIB:libcmt ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}")
  foreach (var CMAKE_C_FLAGS_DEBUG
               CMAKE_C_FLAGS_RELEASE
               CMAKE_C_FLAGS_MINSIZEREL
               CMAKE_C_FLAGS_RELWITHDEBINFO)
    set(${var} "${${var}} /D_CRT_SECURE_NO_WARNINGS /wd4290 /wd4996")
  endforeach ()
  foreach (var CMAKE_CXX_FLAGS_DEBUG
               CMAKE_CXX_FLAGS_RELEASE
               CMAKE_CXX_FLAGS_MINSIZEREL
               CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    set(${var} "${${var}} /D_CRT_SECURE_NO_WARNINGS /wd4290 /wd4996 /EHsc")
  endforeach ()
endif ()

#--- don't set an rpath
set(CMAKE_SKIP_RPATH FALSE)

#--- miscellaneous options
option(A4_PAPER "use A4 (instead of Letter) paper size by default for PostScript output" OFF)
option(NO_TEXT_SELECT "do not allow text selection" OFF)
option(OPI_SUPPORT "include support for OPI comments" OFF)
option(MULTITHREADED "include support for multithreading" ON)
option(USE_EXCEPTIONS "use C++ exceptions" ON)
option(USE_FIXEDPOINT "use fixed point (instead of floating point) arithmetic" OFF)
option(SPLASH_CMYK "include support for CMYK rasterization" OFF)
option(SPLASH_DEVICEN "include support for DeviceN rasterization" OFF)
if (SPLASH_DEVICEN)
  set(SPLASH_CMYK ON)
endif ()
option(USE_LCMS "enable color management using lcms2" OFF)
option(HIGHLIGHTED_REGIONS "include support for highlighted regions" OFF)
option(SYSTEM_XPDFRC "full path for system-wide xpdfrc file" "")
if (SYSTEM_XPDFRC)
  set(SYSTEM_XPDFRC_DEFINE "#define SYSTEM_XPDFRC \"${SYSTEM_XPDFRC}\"")
else ()
  set(SYSTEM_XPDFRC_DEFINE "/* #undef SYSTEM_XPDFRC */")
endif ()
option(XPDFWIDGET_PRINTING "include printing support in XpdfWidget" OFF)
option(EVAL_MODE "enable evaluation mode" OFF)
option(BUILDING_XPDFREADER "enable closed source XpdfReader build" OFF)

#--- check for various library functions
check_function_exists(mkstemp HAVE_MKSTEMP)
check_function_exists(mkstemps HAVE_MKSTEMPS)
check_function_exists(popen HAVE_POPEN)
check_cxx_source_compiles(
    "#include <algorithm>
    bool cmp(const int &x, const int &y) { return x < y; }
    int a[100];
    int main() { std::sort(a, a+100, &cmp); }"
  HAVE_STD_SORT)

#--- set up 64-bit file seeks
check_function_exists(fseeko HAVE_FSEEKO)
check_function_exists(fseek64 HAVE_FSEEK64)
check_function_exists(_fseeki64 HAVE_FSEEKI64)

#--- look for FreeType
# This could use cmake's FindFreetype, except that it would require
# a newer version of cmake -- because FreeType changed their include
# paths around.  Older versions of FreeType require two separate
# -I switches (typically ..../include and ..../include/freetype2);
# while newer versions require just one (typically ..../include/freetype2)
find_path(FREETYPE_INCLUDE_DIR_ft2build ft2build.h
  HINTS
  ${FREETYPE_DIR}
  PATHS
  /usr/local/include
  /usr/local/X11R6/include
  /usr/local/X11/include
  /usr/freeware/include
  PATH_SUFFIXES freetype2 include/freetype2 include
)
find_path(FREETYPE_INCLUDE_DIR_freetype freetype.h
  HINTS
  ${FREETYPE_DIR}
  PATHS
  /usr/local/include
  /usr/local/X11R6/include
  /usr/local/X11/include
  /usr/freeware/include
  PATH_SUFFIXES freetype2 include/freetype2 include
)
find_path(FREETYPE_INCLUDE_DIR_freetype_freetype freetype/freetype.h
  HINTS
  ${FREETYPE_DIR}
  PATHS
  /usr/local/include
  /usr/local/X11R6/include
  /usr/local/X11/include
  /usr/freeware/include
  PATH_SUFFIXES freetype2 include/freetype2 include
)
find_library(FREETYPE_LIBRARY
  NAMES freetype libfreetype freetype2 freetype219
  HINTS
  ${FREETYPE_DIR}
  PATH_SUFFIXES lib64 lib
  PATHS
  /usr/local
  /usr/local/X11R6
  /usr/local/X11
  /usr/freeware
)
if (FREETYPE_INCLUDE_DIR_ft2build
    AND FREETYPE_INCLUDE_DIR_freetype
    AND (FREETYPE_INCLUDE_DIR_ft2build STREQUAL FREETYPE_INCLUDE_DIR_freetype)
    AND FREETYPE_LIBRARY)
  set(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_ft2build}")
  set(HAVE_FREETYPE_H TRUE)
  message(STATUS "Found FreeType (new-style includes): ${FREETYPE_LIBRARY}")
elseif (FREETYPE_INCLUDE_DIR_ft2build
        AND FREETYPE_INCLUDE_DIR_freetype_freetype
        AND FREETYPE_LIBRARY)
  set(FREETYPE_INCLUDE_DIRS
      "${FREETYPE_INCLUDE_DIR_ft2build};${FREETYPE_INCLUDE_DIR_freetype_freetype}")
  set(HAVE_FREETYPE_H TRUE)
  message(STATUS "Found FreeType (old-style includes): ${FREETYPE_LIBRARY}")
else ()
  message(STATUS "FreeType not found")
endif ()
if (HAVE_FREETYPE_H)
  set(HAVE_SPLASH TRUE)
endif ()

#--- look for D-Type
if (DTYPE_INCLUDE_DIR AND DTYPE_LIBRARY)
  set(HAVE_DTYPE4_H TRUE)
  message(STATUS "Found D-Type: ${DTYPE_LIBRARY}")
endif ()

#--- look for zlib
find_package(ZLIB)

#--- look for libpng
find_package(PNG)

#--- look for libjpeg
find_package(JPEG)

#--- look for libtiff
find_package(TIFF)

#--- look for lcms2
if (USE_LCMS)
  find_path(LCMS_INCLUDE_DIR lcms2.h
    HINTS
    ${LCMS_DIR}
    PATHS
    /usr/local/include
    /usr/freeware/include
    PATH_SUFFIXES include
  )
  find_library(LCMS_LIBRARY
    NAMES lcms2 liblcms2
    HINTS
    ${LCMS_DIR}
    PATHS
    /usr/local
    /usr/freeware
  )
endif ()
if (USE_LCMS AND LCMS_INCLUDE_DIR AND LCMS_LIBRARY)
  set(HAVE_LCMS TRUE)
  message(STATUS "Found lcms2: ${LCMS_LIBRARY}")
else ()
  set(HAVE_LCMS FALSE)
  message(STATUS "lcms2 not found")
endif ()

#--- look for Qt
find_package(Qt5Widgets QUIET)
if (Qt5Widgets_FOUND)
  find_package(Qt5PrintSupport)
else ()
  find_package(Qt4)
endif ()
if(Qt5Widgets_FOUND)
  message(STATUS "Qt5 found")
  if (XPDFWIDGET_PRINTING)
    set(QT_INCLUDES "${Qt5Widgets_INCLUDE_DIRS} ${Qt5PrintSupport_INCLUDE_DIRS}")
    set(QT_DEFINITIONS "${Qt5Widgets_DEFINITIONS} ${Qt5PrintSupport_DEFINITIONS}")
    if (APPLE)
      set(QT_LIBRARIES Qt5::Widgets Qt5::PrintSupport "-framework ApplicationServices")
    elseif (UNIX)
      set(QT_LIBRARIES Qt5::Widgets Qt5::PrintSupport cups)
    else ()
      set(QT_LIBRARIES Qt5::Widgets Qt5::PrintSupport)
    endif ()
  else ()
    set(QT_INCLUDES "${Qt5Widgets_INCLUDE_DIRS}")
    set(QT_DEFINITIONS "${Qt5Widgets_DEFINITIONS}")
    set(QT_LIBRARIES Qt5::Widgets)
  endif ()
  if (XPDFWIDGET_PRINTING)
    if (APPLE)
      set(EXTRA_QT_LIBRARIES "-framework ApplicationServices")
    elseif (UNIX)
      set(EXTRA_QT_LIBRARIES cups)
    else ()
      set(EXTRA_QT_LIBRARIES "")
    endif ()
  endif ()
  # remove "-fPIE" here because we added "-fPIC" above
  string(REPLACE "-fPIE" "" QT_CFLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
  if (POLICY CMP0020)
    # do not link to qtmain.lib
    cmake_policy(SET CMP0020 OLD)
  endif ()
elseif(QT4_FOUND)
  message(STATUS "Qt4 found")
  if (XPDFWIDGET_PRINTING)
    if (APPLE)
      set(EXTRA_QT_LIBRARIES "-framework ApplicationServices")
    elseif (UNIX)
      set(EXTRA_QT_LIBRARIES cups)
    else ()
      set(EXTRA_QT_LIBRARIES "")
    endif ()
  endif ()
else()
  message(STATUS "No Qt library found")
endif()

#--- look for libpaper
find_library(HAVE_PAPER_H
             NAMES paper libpaper
             PATH_SUFFIXES lib64 lib
)

#--- look for pthreads
find_package(Threads)

#--- create aconf.h
configure_file("aconf.h.in" "aconf.h")
