Skip to content

Commit

Permalink
Merge pull request OSGeo#9559 from rouault/cmake_openjpeg
Browse files Browse the repository at this point in the history
CMake tweaks for OpenJPEG and PROJ
  • Loading branch information
rouault authored Apr 14, 2024
2 parents 972de4d + d8b4bea commit 207f616
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 48 deletions.
77 changes: 38 additions & 39 deletions cmake/helpers/CheckDependentLibraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ endfunction()
# option. User control is done via a cache variable GDAL_USE_{name in upper case}
# with the default value ON for CAN_DISABLE or OFF for DISABLED_BY_DEFAULT.
# The RECOMMENDED option is used for the feature summary.
# The VERSION, CONFIG, COMPONENTS and NAMES parameters are passed to find_package().
# The VERSION, CONFIG, MODULE, COMPONENTS and NAMES parameters are passed to find_package().
# Using NAMES with find_package() implies config mode. However, gdal_check_package()
# attempts another find_package() without NAMES if the config mode attempt was not
# successful, allowing a fallback to Find modules.
Expand All @@ -100,9 +100,9 @@ endfunction()
# GDAL_CHECK_PACKAGE_${name}_TARGETS cache variables which can be used to
# overwrite the default config and targets names.
# The required find_dependency() commands for exported config are appended to
# the GDAL_IMPORT_DEPENDENCIES string.
# the GDAL_IMPORT_DEPENDENCIES string (when BUILD_SHARED_LIBS=OFF).
macro (gdal_check_package name purpose)
set(_options CONFIG CAN_DISABLE RECOMMENDED DISABLED_BY_DEFAULT ALWAYS_ON_WHEN_FOUND)
set(_options CONFIG MODULE CAN_DISABLE RECOMMENDED DISABLED_BY_DEFAULT ALWAYS_ON_WHEN_FOUND)
set(_oneValueArgs VERSION NAMES)
set(_multiValueArgs COMPONENTS TARGETS PATHS)
cmake_parse_arguments(_GCP "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
Expand All @@ -120,6 +120,9 @@ macro (gdal_check_package name purpose)
if (_GCP_CONFIG)
list(APPEND _find_package_args CONFIG)
endif ()
if (_GCP_MODULE)
list(APPEND _find_package_args MODULE)
endif ()
if (_GCP_COMPONENTS)
list(APPEND _find_package_args COMPONENTS ${_GCP_COMPONENTS})
endif ()
Expand Down Expand Up @@ -375,23 +378,36 @@ if (GDAL_USE_CRYPTOPP)
option(CRYPTOPP_USE_ONLY_CRYPTODLL_ALG "Use Only cryptoDLL alg. only work on dynamic DLL" OFF)
endif ()

set(GDAL_FIND_PACKAGE_PROJ_METHOD "CONFIG" CACHE STRING "Method to use for find_package(PROJ): CONFIG or MODULE")
set_property(CACHE GDAL_FIND_PACKAGE_PROJ_METHOD PROPERTY STRINGS "CONFIG" "MODULE")
mark_as_advanced(GDAL_FIND_PACKAGE_PROJ_METHOD)
if(GDAL_FIND_PACKAGE_PROJ_METHOD STREQUAL "MODULE")
find_package(PROJ MODULE REQUIRED)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ MODULE REQUIRED)\n")
set(GDAL_FIND_PACKAGE_PROJ_MODE "CUSTOM" CACHE STRING "Mode to use for find_package(PROJ): CUSTOM, CONFIG, MODULE or empty string")
set_property(CACHE GDAL_FIND_PACKAGE_PROJ_MODE PROPERTY STRINGS "CUSTOM" "CONFIG" "MODULE" "")
if(NOT GDAL_FIND_PACKAGE_PROJ_MODE STREQUAL "CUSTOM")
find_package(PROJ ${GDAL_FIND_PACKAGE_PROJ_MODE} REQUIRED)
if (NOT BUILD_SHARED_LIBS)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ ${GDAL_FIND_PACKAGE_PROJ_MODE})\n")
endif()
else()
# First check with CMake config files, and then fallback to the FindPROJ module.
find_package(PROJ CONFIG)
if (PROJ_FOUND AND PROJ_VERSION VERSION_LESS "8")
message(WARNING "PROJ ${PROJ_VERSION} < 8 found with Config file. As it is not trusted, retrying with module mode")
endif()
if (PROJ_FOUND)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ ${PROJ_VERSION_MAJOR} CONFIG)\n")
if (NOT BUILD_SHARED_LIBS)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ CONFIG)\n")
endif()
else()
find_package(PROJ 6.3 REQUIRED)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ 6.3)\n")
find_package(PROJ REQUIRED)
if (NOT BUILD_SHARED_LIBS)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(PROJ)\n")
endif()
endif ()
endif()

if (DEFINED PROJ_VERSION_STRING AND NOT DEFINED PROJ_VERSION)
set(PROJ_VERSION ${PROJ_VERSION_STRING})
endif()
if ("${PROJ_VERSION}" VERSION_LESS "6.3")
message(FATAL_ERROR "PROJ >= 6.3 required. Version ${PROJ_VERSION} found")
endif()

gdal_check_package(TIFF "Support for the Tag Image File Format (TIFF)." VERSION 4.1 CAN_DISABLE)
set_package_properties(
Expand Down Expand Up @@ -744,32 +760,15 @@ define_find_package2(HEIF libheif/heif.h heif PKGCONFIG_NAME libheif)
gdal_check_package(HEIF "HEIF >= 1.1" CAN_DISABLE)

# OpenJPEG's cmake-CONFIG is broken with older OpenJPEG releases, so call module explicitly
set(GDAL_FIND_PACKAGE_OPENJPEG_METHOD "MODULE" CACHE STRING "Method to use for find_package(OpenJPEG): CONFIG, MODULE or empty string")
mark_as_advanced(GDAL_FIND_PACKAGE_OPENJPEG_METHOD)
if(NOT GDAL_FIND_PACKAGE_OPENJPEG_METHOD STREQUAL "")
find_package(OpenJPEG ${GDAL_FIND_PACKAGE_OPENJPEG_METHOD})
else()
find_package(OpenJPEG)
endif()
if (OPENJPEG_VERSION_STRING AND OPENJPEG_VERSION_STRING VERSION_LESS "2.3.1")
message(WARNING "Ignoring OpenJPEG because it is at version ${OPENJPEG_VERSION_STRING}, whereas the minimum version required is 2.3.1")
set(HAVE_OPENJPEG OFF)
set(OPENJPEG_FOUND OFF)
endif()
if (GDAL_USE_OPENJPEG)
if (TARGET openjp2 AND NOT TARGET OPENJPEG::OpenJPEG)
# Triggered when using CONFIG dection
add_library(OPENJPEG::OpenJPEG INTERFACE IMPORTED)
set_target_properties(OPENJPEG::OpenJPEG PROPERTIES INTERFACE_LINK_LIBRARIES "openjp2")
endif()
if (NOT OPENJPEG_FOUND)
message(FATAL_ERROR "Configured to use GDAL_USE_OPENJPEG, but not found")
endif ()
endif ()
cmake_dependent_option(GDAL_USE_OPENJPEG "Set ON to use openjpeg" ${GDAL_USE_EXTERNAL_LIBS} OPENJPEG_FOUND OFF)
if (GDAL_USE_OPENJPEG)
string(APPEND GDAL_IMPORT_DEPENDENCIES "find_dependency(OpenJPEG MODULE)\n")
endif ()
set(GDAL_FIND_PACKAGE_OpenJPEG_MODE "MODULE" CACHE STRING "Mode to use for find_package(OpenJPEG): CONFIG, MODULE or empty string")
set_property(CACHE GDAL_FIND_PACKAGE_OpenJPEG_MODE PROPERTY STRINGS "CONFIG" "MODULE" "")
# "openjp2" target name is for the one coming from the OpenJPEG CMake configuration
# "OPENJPEG::OpenJPEG" is the one used by cmake/modules/packages/FindOpenJPEG.cmake
gdal_check_package(OpenJPEG "Enable JPEG2000 support with OpenJPEG library"
${GDAL_FIND_PACKAGE_OpenJPEG_MODE}
CAN_DISABLE
TARGETS "openjp2;OPENJPEG::OpenJPEG"
VERSION "2.3.1")

gdal_check_package(HDFS "Enable Hadoop File System through native library" CAN_DISABLE)

Expand Down
10 changes: 5 additions & 5 deletions cmake/modules/packages/FindOpenJPEG.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ endfunction()
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(PC_OPENJPEG QUIET libopenjp2)
set(OPENJPEG_VERSION_STRING ${PC_OPENJPEG_VERSION})
set(OpenJPEG_VERSION_STRING ${PC_OPENJPEG_VERSION})
endif()


Expand All @@ -56,8 +56,8 @@ find_library(OPENJPEG_LIBRARY
mark_as_advanced(OPENJPEG_LIBRARY OPENJPEG_INCLUDE_DIR)

if(OPENJPEG_INCLUDE_DIR)
if(OPENJPEG_VERSION_STRING)
string(REGEX MATCH "([0-9]+).([0-9]+).([0-9]+)" OPJ_VERSION ${OPENJPEG_VERSION_STRING})
if(OpenJPEG_VERSION_STRING)
string(REGEX MATCH "([0-9]+).([0-9]+).([0-9]+)" OPJ_VERSION ${OpenJPEG_VERSION_STRING})
if(OPJ_VERSION)
transform_version(OPENJPEG_VERSION_NUM ${CMAKE_MATCH_1} ${CMAKE_MATCH_2} ${CMAKE_MATCH_3})
else()
Expand Down Expand Up @@ -96,7 +96,7 @@ if(OPENJPEG_INCLUDE_DIR)
REV_VERSION ${REV_VERSION})
unset(VERSION_H_CONTENTS)
endif()
set(OPENJPEG_VERSION_STRING "${MAJOR_VERSION}.${MINOR_VERSION}.${REV_VERSION}")
set(OpenJPEG_VERSION_STRING "${MAJOR_VERSION}.${MINOR_VERSION}.${REV_VERSION}")
TRANSFORM_VERSION(OPENJPEG_VERSION_NUM ${MAJOR_VERSION} ${MINOR_VERSION} ${REV_VERSION})
unset(MAJOR_VERSION)
unset(MINOR_VERSION)
Expand All @@ -109,7 +109,7 @@ include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(OpenJPEG
FOUND_VAR OPENJPEG_FOUND
REQUIRED_VARS OPENJPEG_LIBRARY OPENJPEG_INCLUDE_DIR
VERSION_VAR OPENJPEG_VERSION_STRING)
VERSION_VAR OpenJPEG_VERSION_STRING)
if(OPENJPEG_FOUND)
set(OPENJPEG_LIBRARIES "${OPENJPEG_LIBRARY}")
set(OPENJPEG_INCLUDE_DIRS "${OPENJPEG_INCLUDE_DIR}")
Expand Down
22 changes: 21 additions & 1 deletion doc/source/development/building_from_source.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,15 @@ JPEG-2000 codec written in C language. It is required for the

Control whether to use OpenJPEG. Defaults to ON when OpenJPEG is found.

.. option:: GDAL_FIND_PACKAGE_OpenJPEG_MODE=MODULE/CONFIG/empty string

.. versionadded:: 3.9

Control the mode used for find_package(OpenJPEG). Defaults to MODULE
for compatibility with OpenJPEG < 2.5.1. If set to CONFIG, only Config mode
search is attempted. If set to empty string, default CMake logic
(https://cmake.org/cmake/help/latest/command/find_package.html) applies.


OpenSSL
*******
Expand Down Expand Up @@ -1672,7 +1681,7 @@ the :ref:`vector.pg` and :ref:`raster.postgisraster` drivers.
PROJ
****

`PROJ <https://github.com/OSGeo/PROJ/>`_ >= 6 is a *required* dependency for GDAL.
`PROJ <https://github.com/OSGeo/PROJ/>`_ >= 6.3 is a *required* dependency for GDAL.

.. option:: PROJ_INCLUDE_DIR

Expand All @@ -1685,6 +1694,17 @@ PROJ
``PROJ_LIBRARY_DEBUG`` can also be specified to a similar library for
building Debug releases.

.. option:: GDAL_FIND_PACKAGE_PROJ_MODE=CUSTOM/MODULE/CONFIG/empty string

.. versionadded:: 3.9

Control the mode used for find_package(PROJ).
Alters how the default CMake seach logic
(https://cmake.org/cmake/help/latest/command/find_package.html) applies.
Defaults to CUSTOM, where the CONFIG mode is applied for PROJ >= 8, and
fallbacks to default MODULE mode otherwise.
Other values are passed directly to find_package()

QHULL
*****

Expand Down
6 changes: 3 additions & 3 deletions frmts/openjpeg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ add_gdal_driver(
DEF FRMT_openjpeg PLUGIN_CAPABLE)

if(TARGET gdal_JP2OpenJPEG_core)
target_include_directories(gdal_JP2OpenJPEG_core PRIVATE $<TARGET_PROPERTY:OPENJPEG::OpenJPEG,INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(gdal_JP2OpenJPEG_core PRIVATE $<TARGET_PROPERTY:OPENJPEG::OpenJPEG,INTERFACE_COMPILE_DEFINITIONS>)
target_include_directories(gdal_JP2OpenJPEG_core PRIVATE $<TARGET_PROPERTY:${OpenJPEG_TARGET},INTERFACE_INCLUDE_DIRECTORIES>)
target_compile_definitions(gdal_JP2OpenJPEG_core PRIVATE $<TARGET_PROPERTY:${OpenJPEG_TARGET},INTERFACE_COMPILE_DEFINITIONS>)
endif()

if(NOT TARGET gdal_JP2OpenJPEG)
Expand All @@ -15,4 +15,4 @@ endif()

gdal_standard_includes(gdal_JP2OpenJPEG)
target_include_directories(gdal_JP2OpenJPEG PRIVATE ../opjlike)
gdal_target_link_libraries(gdal_JP2OpenJPEG PRIVATE OPENJPEG::OpenJPEG)
gdal_target_link_libraries(gdal_JP2OpenJPEG PRIVATE ${OpenJPEG_TARGET})

0 comments on commit 207f616

Please sign in to comment.