From 3ec7531aaee84e0baf57065b0ce4f658198c7bc1 Mon Sep 17 00:00:00 2001 From: Martin Valgur Date: Tue, 12 Mar 2024 19:42:48 +0200 Subject: [PATCH] (#18788) openvr: migrate to Conan v2 * openvr: migrate to Conan v2 * openvr: transitive_libs=True * openvr: fix jsoncpp target * openvr: fix shared install on Windows * openvr: add CoreFoundation framework dep --- recipes/openvr/all/CMakeLists.txt | 9 -- recipes/openvr/all/conandata.yml | 1 - recipes/openvr/all/conanfile.py | 127 +++++++++++------- .../openvr/all/test_package/CMakeLists.txt | 9 +- recipes/openvr/all/test_package/conanfile.py | 22 ++- .../openvr/all/test_v1_package/CMakeLists.txt | 8 ++ .../openvr/all/test_v1_package/conanfile.py | 16 +++ 7 files changed, 119 insertions(+), 73 deletions(-) delete mode 100644 recipes/openvr/all/CMakeLists.txt create mode 100644 recipes/openvr/all/test_v1_package/CMakeLists.txt create mode 100644 recipes/openvr/all/test_v1_package/conanfile.py diff --git a/recipes/openvr/all/CMakeLists.txt b/recipes/openvr/all/CMakeLists.txt deleted file mode 100644 index c4553e469fbbd..0000000000000 --- a/recipes/openvr/all/CMakeLists.txt +++ /dev/null @@ -1,9 +0,0 @@ -cmake_minimum_required(VERSION 2.8.12) -project(cmake_wrapper) - -include(conanbuildinfo.cmake) -conan_basic_setup(TARGETS) - -link_libraries(CONAN_PKG::jsoncpp) - -add_subdirectory(source_subfolder) diff --git a/recipes/openvr/all/conandata.yml b/recipes/openvr/all/conandata.yml index 8306fe238fc2a..8a94700e08a51 100644 --- a/recipes/openvr/all/conandata.yml +++ b/recipes/openvr/all/conandata.yml @@ -11,4 +11,3 @@ sources: patches: "1.16.8": - patch_file: "patches/fix-includes-and-assert-1.16.8.patch" - base_path: "source_subfolder" diff --git a/recipes/openvr/all/conanfile.py b/recipes/openvr/all/conanfile.py index 35ae1e9ea460d..482f050fca0c4 100644 --- a/recipes/openvr/all/conanfile.py +++ b/recipes/openvr/all/conanfile.py @@ -1,16 +1,30 @@ import os -from conans import ConanFile, tools, CMake -from conans.errors import ConanInvalidConfiguration + +from conan import ConanFile +from conan.errors import ConanInvalidConfiguration +from conan.tools.apple import is_apple_os +from conan.tools.build import check_min_cppstd, stdcpp_library +from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout +from conan.tools.files import apply_conandata_patches, copy, export_conandata_patches, get, replace_in_file, rmdir, \ + save, mkdir +from conan.tools.scm import Version + +required_conan_version = ">=1.53.0" + class OpenvrConan(ConanFile): name = "openvr" - description = "API and runtime that allows access to VR hardware from applications have specific knowledge of the hardware they are targeting." - topics = ("conan", "openvr", "vr", ) + description = ( + "API and runtime that allows access to VR hardware from applications " + "have specific knowledge of the hardware they are targeting." + ) + license = "BSD-3-Clause" url = "https://github.com/conan-io/conan-center-index" homepage = "https://github.com/ValveSoftware/openvr" - license = "BSD-3-Clause" + topics = ("vr", "virtual reality") - settings = "os", "compiler", "build_type", "arch" + package_type = "library" + settings = "os", "arch", "compiler", "build_type" options = { "shared": [True, False], "fPIC": [True, False], @@ -20,13 +34,8 @@ class OpenvrConan(ConanFile): "fPIC": True, } - exports_sources = ["CMakeLists.txt", "patches/**"] - generators = "cmake" - _cmake = None - - @property - def _source_subfolder(self): - return "source_subfolder" + def export_sources(self): + export_conandata_patches(self) def config_options(self): if self.settings.os == "Windows": @@ -34,69 +43,83 @@ def config_options(self): def configure(self): if self.options.shared: - del self.options.fPIC - if self.settings.compiler.cppstd: - tools.check_min_cppstd(self, "11") + self.options.rm_safe("fPIC") - if self.settings.compiler == "gcc" and tools.Version(self.settings.compiler.version) < "5": - raise ConanInvalidConfiguration("OpenVR can't be compiled by {0} {1}".format(self.settings.compiler, - self.settings.compiler.version)) + def layout(self): + cmake_layout(self, src_folder="src") def requirements(self): - self.requires("jsoncpp/1.9.4") + self.requires("jsoncpp/1.9.5", transitive_headers=True, transitive_libs=True) + + def validate(self): + if self.settings.compiler.cppstd: + check_min_cppstd(self, 11) + if self.settings.compiler == "gcc" and Version(self.settings.compiler.version) < "5": + raise ConanInvalidConfiguration( + f"OpenVR can't be compiled by {self.settings.compiler} {self.settings.compiler.version}" + ) def source(self): - tools.get(**self.conan_data["sources"][self.version]) - extracted_dir = "{}-{}".format(self.name, self.version) - os.rename(extracted_dir, self._source_subfolder) + get(self, **self.conan_data["sources"][self.version], strip_root=True) + + def generate(self): + tc = CMakeToolchain(self) + tc.cache_variables["BUILD_SHARED"] = self.options.shared + tc.cache_variables["BUILD_UNIVERSAL"] = False + # Let Conan handle the stdlib setting, even if we are using libc++ + tc.cache_variables["USE_LIBCXX"] = False + tc.generate() + tc = CMakeDeps(self) + tc.generate() def _patch_sources(self): - for patch in self.conan_data.get("patches", {}).get(self.version, []): - tools.patch(**patch) + apply_conandata_patches(self) # Honor fPIC=False - tools.replace_in_file(os.path.join(self._source_subfolder, "CMakeLists.txt"), - "-fPIC", "") - # Unvendor jsoncpp (we rely on our CMake wrapper for jsoncpp injection) - tools.replace_in_file(os.path.join(self._source_subfolder, "src", "CMakeLists.txt"), - "jsoncpp.cpp", "") - tools.rmdir(os.path.join(self._source_subfolder, "src", "json")) - - def _configure_cmake(self): - if self._cmake: - return self._cmake - self._cmake = CMake(self) - self._cmake.definitions["BUILD_SHARED"] = self.options.shared - self._cmake.definitions["BUILD_UNIVERSAL"] = False - self._cmake.definitions["USE_LIBCXX"] = False - self._cmake.configure() - - return self._cmake + replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "-fPIC", "") + # Unvendor jsoncpp + replace_in_file(self, os.path.join(self.source_folder, "src", "CMakeLists.txt"), "jsoncpp.cpp", "") + rmdir(self, os.path.join(self.source_folder, "src", "json")) + # Add jsoncpp dependency from Conan + save(self, os.path.join(self.source_folder, "src", "CMakeLists.txt"), + "find_package(jsoncpp REQUIRED CONFIG)\n" + "target_link_libraries(${LIBNAME} JsonCpp::JsonCpp)", + append=True) def build(self): self._patch_sources() - cmake = self._configure_cmake() + cmake = CMake(self) + cmake.configure() cmake.build() + @property + def _lib_name(self): + if self.settings.os == "Windows" and self.settings.arch == "x86_64": + return "openvr_api64" + return "openvr_api" + def package(self): - self.copy("LICENSE", src=os.path.join(self.source_folder, self._source_subfolder), dst="licenses") - cmake = self._configure_cmake() + copy(self, "LICENSE", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses")) + cmake = CMake(self) cmake.install() - self.copy(pattern="openvr_api*.dll", dst="bin", src="bin", keep_path=False) - tools.rmdir(os.path.join(self.package_folder, "share")) + if self.settings.os == "Windows" and self.options.shared: + mkdir(self, os.path.join(self.package_folder, "bin")) + os.rename(os.path.join(self.package_folder, "lib", f"{self._lib_name}.dll"), + os.path.join(self.package_folder, "bin", f"{self._lib_name}.dll")) + rmdir(self, os.path.join(self.package_folder, "share")) def package_info(self): - self.cpp_info.names["pkg_config"] = "openvr" - self.cpp_info.libs = tools.collect_libs(self) + self.cpp_info.set_property("pkg_config_name", "openvr") + self.cpp_info.libs = [self._lib_name] self.cpp_info.includedirs.append(os.path.join("include", "openvr")) if not self.options.shared: self.cpp_info.defines.append("OPENVR_BUILD_STATIC") - libcxx = tools.stdcpp_library(self) + libcxx = stdcpp_library(self) if libcxx: self.cpp_info.system_libs.append(libcxx) if self.settings.os in ["Linux", "FreeBSD"]: self.cpp_info.system_libs.append("dl") - if tools.is_apple_os(self.settings.os): - self.cpp_info.frameworks.append("Foundation") + if is_apple_os(self): + self.cpp_info.frameworks.extend(["Foundation", "CoreFoundation"]) diff --git a/recipes/openvr/all/test_package/CMakeLists.txt b/recipes/openvr/all/test_package/CMakeLists.txt index aede60e3b1e4a..8759620a94936 100644 --- a/recipes/openvr/all/test_package/CMakeLists.txt +++ b/recipes/openvr/all/test_package/CMakeLists.txt @@ -1,9 +1,8 @@ -cmake_minimum_required(VERSION 3.1) -project(test_package) +cmake_minimum_required(VERSION 3.15) +project(test_package LANGUAGES CXX) -include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) -conan_basic_setup() +find_package(openvr REQUIRED CONFIG) add_executable(${CMAKE_PROJECT_NAME} test_package.cpp) -target_link_libraries(${CMAKE_PROJECT_NAME} ${CONAN_LIBS}) +target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE openvr::openvr) set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY CXX_STANDARD 11) diff --git a/recipes/openvr/all/test_package/conanfile.py b/recipes/openvr/all/test_package/conanfile.py index 4903f1a7e8fa0..ef5d7042163ec 100644 --- a/recipes/openvr/all/test_package/conanfile.py +++ b/recipes/openvr/all/test_package/conanfile.py @@ -1,9 +1,19 @@ -from conans import ConanFile, CMake, tools +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.cmake import cmake_layout, CMake import os + class TestPackageConan(ConanFile): - settings = "os", "compiler", "build_type", "arch" - generators = "cmake" + settings = "os", "arch", "compiler", "build_type" + generators = "CMakeDeps", "CMakeToolchain", "VirtualRunEnv" + test_type = "explicit" + + def requirements(self): + self.requires(self.tested_reference_str) + + def layout(self): + cmake_layout(self) def build(self): cmake = CMake(self) @@ -11,6 +21,6 @@ def build(self): cmake.build() def test(self): - if not tools.cross_building(self.settings): - bin_path = os.path.join("bin", "test_package") - self.run(bin_path, run_environment=True) + if can_run(self): + bin_path = os.path.join(self.cpp.build.bindir, "test_package") + self.run(bin_path, env="conanrun") diff --git a/recipes/openvr/all/test_v1_package/CMakeLists.txt b/recipes/openvr/all/test_v1_package/CMakeLists.txt new file mode 100644 index 0000000000000..91630d79f4abb --- /dev/null +++ b/recipes/openvr/all/test_v1_package/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.15) +project(test_package) + +include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) +conan_basic_setup(TARGETS) + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test_package/ + ${CMAKE_CURRENT_BINARY_DIR}/test_package/) diff --git a/recipes/openvr/all/test_v1_package/conanfile.py b/recipes/openvr/all/test_v1_package/conanfile.py new file mode 100644 index 0000000000000..6c9d5dba712c7 --- /dev/null +++ b/recipes/openvr/all/test_v1_package/conanfile.py @@ -0,0 +1,16 @@ +from conans import ConanFile, CMake, tools +import os + +class TestPackageConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "cmake", "cmake_find_package_multi" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def test(self): + if not tools.cross_building(self.settings): + bin_path = os.path.join("bin", "test_package") + self.run(bin_path, run_environment=True)