Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdcm: modernize #9194

Merged
merged 3 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions recipes/gdcm/all/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ cmake_minimum_required(VERSION 3.9.2)
project(cmake_wrapper)

include(conanbuildinfo.cmake)
conan_basic_setup()

if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
conan_basic_setup(KEEP_RPATHS)

add_subdirectory(source_subfolder)
84 changes: 51 additions & 33 deletions recipes/gdcm/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from conan.tools.microsoft import msvc_runtime_flag
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
import os
import textwrap

required_conan_version = ">=1.33.0"
required_conan_version = ">=1.43.0"


class GDCMConan(ConanFile):
name = "gdcm"
Expand All @@ -12,6 +14,7 @@ class GDCMConan(ConanFile):
url = "https://github.com/conan-io/conan-center-index"
license = "BSD-3-Clause"
description = "C++ library for DICOM medical files"

settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
Expand All @@ -21,8 +24,8 @@ class GDCMConan(ConanFile):
"shared": False,
"fPIC": True,
}
generators = "cmake", "cmake_find_package"

generators = "cmake", "cmake_find_package"
_cmake = None

@property
Expand All @@ -33,6 +36,15 @@ def _source_subfolder(self):
def _build_subfolder(self):
return "build_subfolder"

@property
def _is_msvc(self):
return str(self.settings.compiler) in ["Visual Studio", "msvc"]

def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
Expand All @@ -41,23 +53,16 @@ def configure(self):
if self.options.shared:
del self.options.fPIC

def validate(self):
if (self.settings.compiler == "Visual Studio"
and self.options.shared
and str(self.settings.compiler.runtime).startswith("MT")):
raise ConanInvalidConfiguration("shared gdcm can't be built with MT or MTd")
if self.settings.compiler.cppstd:
tools.check_min_cppstd(self, "11")

def requirements(self):
self.requires("expat/2.4.1")
self.requires("expat/2.4.3")
self.requires("openjpeg/2.4.0")
self.requires("zlib/1.2.11")

def export_sources(self):
self.copy("CMakeLists.txt")
for patch in self.conan_data.get("patches", {}).get(self.version, []):
self.copy(patch["patch_file"])
def validate(self):
if self.options.shared and self._is_msvc and "MT" in msvc_runtime_flag(self):
raise ConanInvalidConfiguration("shared gdcm can't be built with MT or MTd")
if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, "11")

def source(self):
tools.get(**self.conan_data["sources"][self.version],
Expand All @@ -78,6 +83,8 @@ def _configure_cmake(self):
self._cmake.definitions["GDCM_USE_SYSTEM_EXPAT"] = True
self._cmake.definitions["GDCM_USE_SYSTEM_OPENJPEG"] = True
self._cmake.definitions["GDCM_USE_SYSTEM_ZLIB"] = True
if not tools.valid_min_cppstd(self, 11):
self._cmake.definitions["CMAKE_CXX_STANDARD"] = 11

self._cmake.configure(build_folder=self._build_subfolder)
return self._cmake
Expand All @@ -98,11 +105,13 @@ def package(self):
lib_dir = os.path.join(self.package_folder, "lib")
tools.rmdir(os.path.join(self.package_folder, "share"))
tools.remove_files_by_mask(os.path.join(lib_dir, self._gdcm_subdir), "[!U]*.cmake") #leave UseGDCM.cmake untouched
self._create_cmake_variables(os.path.join(self.package_folder, self._gdcm_cmake_variables_path))

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self._create_cmake_module_alias_targets(
os.path.join(self.package_folder, self._gdcm_cmake_module_aliases_path),
self._gdcm_libraries
{library: "GDCM::{}".format(library) for library in self._gdcm_libraries},
)
self._create_cmake_variables(os.path.join(self.package_folder, self._gdcm_cmake_variables_path))

def _create_cmake_variables(self, variables_file):
v = tools.Version(self.version)
Expand Down Expand Up @@ -143,16 +152,18 @@ def _create_cmake_variables(self, variables_file):
tools.save(variables_file, content)

@staticmethod
def _create_cmake_module_alias_targets(targets_file, libraries):
content = "\n".join([textwrap.dedent("""\
if(TARGET {aliased} AND NOT TARGET {alias})
add_library({alias} INTERFACE IMPORTED)
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""".format(alias=library, aliased="GDCM::"+library)) for library in libraries])
tools.save(targets_file, content)

@property
def _create_cmake_module_alias_targets(module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent("""\
if(TARGET {aliased} AND NOT TARGET {alias})
add_library({alias} INTERFACE IMPORTED)
set_property(TARGET {alias} PROPERTY INTERFACE_LINK_LIBRARIES {aliased})
endif()
""".format(alias=alias, aliased=aliased))
tools.save(module_file, content)

@property
def _gdcm_subdir(self):
v = tools.Version(self.version)
return "gdcm-{}.{}".format(v.major, v.minor)
Expand Down Expand Up @@ -193,14 +204,17 @@ def _gdcm_libraries(self):
return gdcm_libs

def package_info(self):
self.cpp_info.names["cmake_find_package"] = "GDCM"
self.cpp_info.names["cmake_find_package_multi"] = "GDCM"
gdcm_libs = self._gdcm_libraries
for lib in gdcm_libs:
self.cpp_info.set_property("cmake_file_name", "GDCM")
self.cpp_info.set_property("cmake_build_modules", [self._gdcm_cmake_variables_path])

for lib in self._gdcm_libraries:
self.cpp_info.components[lib].set_property("cmake_target_name", lib)
self.cpp_info.components[lib].libs = [lib]
self.cpp_info.components[lib].includedirs = [os.path.join("include", self._gdcm_subdir)]
self.cpp_info.components[lib].builddirs = [self._gdcm_builddir]
self.cpp_info.components[lib].build_modules["cmake"] = self._gdcm_build_modules
self.cpp_info.components[lib].builddirs.append(self._gdcm_builddir)

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.components[lib].build_modules["cmake"] = [self._gdcm_cmake_module_aliases_path]
self.cpp_info.components[lib].build_modules["cmake_find_package"] = self._gdcm_build_modules
self.cpp_info.components[lib].build_modules["cmake_find_package_multi"] = self._gdcm_build_modules

Expand All @@ -222,3 +236,7 @@ def package_info(self):
self.cpp_info.components["gdcmCommon"].system_libs = ["dl"]
if tools.is_apple_os(self.settings.os):
self.cpp_info.components["gdcmCommon"].frameworks = ["CoreFoundation"]

# TODO: to remove in conan v2 once cmake_find_package* generators removed
self.cpp_info.names["cmake_find_package"] = "GDCM"
self.cpp_info.names["cmake_find_package_multi"] = "GDCM"
10 changes: 3 additions & 7 deletions recipes/gdcm/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
cmake_minimum_required(VERSION 3.9.2)
cmake_minimum_required(VERSION 3.8)
project(test_package)

if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

find_package(GDCM REQUIRED)
find_package(GDCM REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
include(${GDCM_USE_FILE})
target_link_libraries(${PROJECT_NAME} gdcmMSFF)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
7 changes: 4 additions & 3 deletions recipes/gdcm/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from conans import ConanFile, CMake, tools
import os

class GDCMTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package"

class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"

def build(self):
cmake = CMake(self)
Expand Down