Skip to content

Commit

Permalink
(conan-io#14153) libpng: restore generation of FindPNG.cmake by CMake…
Browse files Browse the repository at this point in the history
…Deps & modernize more

* restore cmake_find_mode both

* use rm_safe

* sort methods by order of execution

* change patch_type

* convention & simplification of test package

* cmake_find_package in test_v1_package

* Update recipes/libpng/all/conanfile.py

Co-authored-by: Chris Mc <prince.chrismc@gmail.com>
  • Loading branch information
SpaceIm and prince-chrismc committed Nov 16, 2022
1 parent ff5e81d commit 24274d8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 81 deletions.
4 changes: 2 additions & 2 deletions recipes/libpng/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ patches:
"1.6.37":
- patch_file: "patches/0001-1.6.37-cmakefile-zlib.patch"
patch_description: "Update ZLib include and library paths for conan to provide lib. Remove Zlib dll definition."
patch_type: "backport"
patch_type: "conan"
"1.5.30":
- patch_file: "patches/0001-1.5.30-cmakefile-zlib.patch"
patch_description: "Update ZLib include and library paths for conan to provide lib. Remove Zlib dll definition."
patch_type: "backport"
patch_type: "conan"
97 changes: 44 additions & 53 deletions recipes/libpng/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.apple import is_apple_os
from conan.tools.build import cross_building
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout
from conan.tools.scm import Version
from conan.tools.files import export_conandata_patches, apply_conandata_patches, get, rmdir, replace_in_file, copy, rm
from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rm, rmdir
from conan.tools.microsoft import is_msvc
from conan.tools.build import cross_building
from conan.tools.apple import is_apple_os
from conan.errors import ConanInvalidConfiguration
from conan.tools.scm import Version
import os


required_conan_version = ">=1.52.0"
required_conan_version = ">=1.53.0"


class LibpngConan(ConanFile):
Expand Down Expand Up @@ -72,48 +71,24 @@ def config_options(self):

def configure(self):
if self.options.shared:
try:
del self.options.fPIC
except Exception:
pass
try:
del self.settings.compiler.libcxx
except Exception:
pass
try:
del self.settings.compiler.cppstd
except Exception:
pass
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")

def layout(self):
cmake_layout(self, src_folder="src")

def requirements(self):
self.requires("zlib/1.2.13")

def validate(self):
if Version(self.version) < "1.6" and self.info.settings.arch == "armv8" and is_apple_os(self):
raise ConanInvalidConfiguration(f"{self.ref} currently does not building for {self.info.settings.os} {self.info.settings.arch}. Contributions are welcomed")

def source(self):
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def _patch_source(self):
if self.settings.os == "Windows":
if Version(self.version) <= "1.5.2":
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
'set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)',
'set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME})')
else:
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
'OUTPUT_NAME "${PNG_LIB_NAME}_static',
'OUTPUT_NAME "${PNG_LIB_NAME}')
if not is_msvc(self):
if Version(self.version) < "1.6.38":
src_text = 'COMMAND "${CMAKE_COMMAND}" -E copy_if_different $<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}'
else:
src_text = '''COMMAND "${CMAKE_COMMAND}"
-E copy_if_different
$<TARGET_LINKER_FILE_NAME:${S_TARGET}>
$<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}'''
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
src_text,
'COMMAND "${CMAKE_COMMAND}" -E copy_if_different $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/$<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}')

@property
def _neon_msa_sse_vsx_mapping(self):
return {
Expand All @@ -132,9 +107,6 @@ def _libpng_cmake_system_processor(self):
return "powerpc"
return str(self.settings.arch)

def layout(self):
cmake_layout(self, src_folder="src")

def generate(self):
tc = CMakeToolchain(self)
tc.variables["PNG_TESTS"] = False
Expand Down Expand Up @@ -162,13 +134,31 @@ def generate(self):
tc = CMakeDeps(self)
tc.generate()

def validate(self):
if Version(self.version) < "1.6" and self.info.settings.arch == "armv8" and is_apple_os(self):
raise ConanInvalidConfiguration(f"{self.ref} could not be cross build on Mac.")
def _patch_sources(self):
apply_conandata_patches(self)
if self.settings.os == "Windows":
if Version(self.version) <= "1.5.2":
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
'set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)',
'set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME})')
else:
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
'OUTPUT_NAME "${PNG_LIB_NAME}_static',
'OUTPUT_NAME "${PNG_LIB_NAME}')
if not is_msvc(self):
if Version(self.version) < "1.6.38":
src_text = 'COMMAND "${CMAKE_COMMAND}" -E copy_if_different $<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}'
else:
src_text = '''COMMAND "${CMAKE_COMMAND}"
-E copy_if_different
$<TARGET_LINKER_FILE_NAME:${S_TARGET}>
$<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}'''
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"),
src_text,
'COMMAND "${CMAKE_COMMAND}" -E copy_if_different $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/$<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}')

def build(self):
apply_conandata_patches(self)
self._patch_source()
self._patch_sources()
cmake = CMake(self)
cmake.configure()
cmake.build()
Expand All @@ -188,10 +178,7 @@ def package(self):
def package_info(self):
major_min_version = f"{Version(self.version).major}{Version(self.version).minor}"

# TODO: Remove after Conan 2.0
self.cpp_info.names["cmake_find_package"] = "PNG"
self.cpp_info.names["cmake_find_package_multi"] = "PNG"

self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_file_name", "PNG")
self.cpp_info.set_property("cmake_target_name", "PNG::PNG")
self.cpp_info.set_property("pkg_config_name", "libpng")
Expand All @@ -203,3 +190,7 @@ def package_info(self):
self.cpp_info.libs = [f"{prefix}png{suffix}"]
if self.settings.os in ["Linux", "Android", "FreeBSD", "SunOS", "AIX"]:
self.cpp_info.system_libs.append("m")

# TODO: Remove after Conan 2.0
self.cpp_info.names["cmake_find_package"] = "PNG"
self.cpp_info.names["cmake_find_package_multi"] = "PNG"
4 changes: 2 additions & 2 deletions recipes/libpng/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package LANGUAGES C)

find_package(PNG CONFIG REQUIRED)
find_package(PNG REQUIRED)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE PNG::PNG)
6 changes: 3 additions & 3 deletions recipes/libpng/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class TestPackageConan(ConanFile):
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

def requirements(self):
self.requires(self.tested_reference_str)

def build(self):
cmake = CMake(self)
cmake.configure()
Expand Down
8 changes: 3 additions & 5 deletions recipes/libpng/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)
project(test_package)

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

find_package(PNG CONFIG REQUIRED)

add_executable(${PROJECT_NAME} ../test_package/test_package.c)
target_link_libraries(${PROJECT_NAME} PRIVATE PNG::PNG)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package
${CMAKE_CURRENT_BINARY_DIR}/test_package)
20 changes: 4 additions & 16 deletions recipes/libpng/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
from conans import ConanFile, CMake, tools
from conan.tools.build import cross_building
import os
import re
import subprocess


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

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()

def test(self):
if not cross_building(self):
if "arm" in self.settings.arch and not tools.os_info.is_macos:
self.test_arm()
else:
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

def test_arm(self):
file_ext = "so" if self.options["libpng"].shared else "a"
lib_path = os.path.join(self.deps_cpp_info["libpng"].libdirs[0], "libpng.%s" % file_ext)
output = subprocess.check_output(["readelf", "-h", lib_path]).decode()
assert re.search(r"Machine:\s+ARM", output)
if not tools.cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)

0 comments on commit 24274d8

Please sign in to comment.