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

tensorpipe: conan v2 support #13662

Merged
merged 1 commit into from
Nov 2, 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: 0 additions & 7 deletions recipes/tensorpipe/all/CMakeLists.txt

This file was deleted.

120 changes: 65 additions & 55 deletions recipes/tensorpipe/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from conans import ConanFile, CMake, tools
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
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, replace_in_file, rmdir, save
import os
import textwrap

required_conan_version = ">=1.43.0"
required_conan_version = ">=1.51.3"


class TensorpipeConan(ConanFile):
Expand Down Expand Up @@ -37,14 +41,6 @@ class TensorpipeConan(ConanFile):
"cma": True,
}

exports_sources = "CMakeLists.txt"
generators = "cmake", "pkg_config"
_cmake = None

@property
def _source_subfolder(self):
return "source_subfolder"

def config_options(self):
if self.settings.os != "Linux":
del self.options.ibv
Expand All @@ -57,80 +53,94 @@ def configure(self):
if not self.options.cuda:
del self.options.cuda_ipc

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

def requirements(self):
self.requires("libnop/cci.20200728")
self.requires("libuv/1.42.0")
if self.options.cuda:
raise ConanInvalidConfiguration("cuda recipe not yet available in CCI")
self.requires("cuda/11.2")
self.requires("libuv/1.44.1")

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, 14)
if self.settings.os == "Windows":
raise ConanInvalidConfiguration("tensorpipe doesn't support Windows")

def build_requirements(self):
self.build_requires("pkgconf/1.7.4")
if self.info.settings.compiler.get_safe("cppstd"):
check_min_cppstd(self, 14)
if self.info.settings.os == "Windows":
raise ConanInvalidConfiguration(f"{self.ref} doesn't support Windows")
if self.info.options.cuda:
raise ConanInvalidConfiguration("cuda recipe not yet available in CCI")

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

def _configure_cmake(self):
if self._cmake:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["TP_USE_CUDA"] = self.options.cuda
self._cmake.definitions["TP_ENABLE_IBV"] = self.options.get_safe("ibv", False)
self._cmake.definitions["TP_ENABLE_SHM"] = self.options.get_safe("shm", False)
self._cmake.definitions["TP_ENABLE_CMA"] = self.options.get_safe("cma", False)
self._cmake.definitions["TP_ENABLE_CUDA_IPC"] = self.options.get_safe("cuda_ipc", False)
self._cmake.definitions["TP_BUILD_BENCHMARK"] = False
self._cmake.definitions["TP_BUILD_PYTHON"] = False
self._cmake.definitions["TP_BUILD_TESTING"] = False
self._cmake.definitions["TP_BUILD_LIBUV"] = False
self._cmake.configure()
return self._cmake
get(self, **self.conan_data["sources"][self.version],
destination=self.source_folder, strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["TP_USE_CUDA"] = self.options.cuda
tc.variables["TP_ENABLE_IBV"] = self.options.get_safe("ibv", False)
tc.variables["TP_ENABLE_SHM"] = self.options.get_safe("shm", False)
tc.variables["TP_ENABLE_CMA"] = self.options.get_safe("cma", False)
tc.variables["TP_ENABLE_CUDA_IPC"] = self.options.get_safe("cuda_ipc", False)
tc.variables["TP_BUILD_BENCHMARK"] = False
tc.variables["TP_BUILD_PYTHON"] = False
tc.variables["TP_BUILD_TESTING"] = False
tc.variables["TP_BUILD_LIBUV"] = False
tc.generate()
deps = CMakeDeps(self)
deps.generate()

def _patch_sources(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a patch file 🙏

cmakelists = os.path.join(self.source_folder, "tensorpipe", "CMakeLists.txt")
replace_in_file(self, cmakelists, "find_package(uv REQUIRED)", "find_package(libuv REQUIRED CONFIG)")
replace_in_file(
self,
cmakelists,
"target_link_libraries(tensorpipe PRIVATE uv::uv)",
"target_link_libraries(tensorpipe PRIVATE $<IF:$<TARGET_EXISTS:uv>,uv,uv_a>)",
)
replace_in_file(
self,
cmakelists,
"target_include_directories(tensorpipe PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/libnop/include>)",
"find_package(libnop REQUIRED CONFIG)\ntarget_link_libraries(tensorpipe PUBLIC libnop::libnop)",
)

def build(self):
with tools.run_environment(self):
cmake = self._configure_cmake()
cmake.build()
self._patch_sources()
cmake = CMake(self)
cmake.configure()
cmake.build()

def package(self):
self.copy("LICENSE.txt", dst="licenses", src=self._source_subfolder)
with tools.run_environment(self):
cmake = self._configure_cmake()
cmake.install()
tools.rmdir(os.path.join(self.package_folder, "share"))
copy(self, "LICENSE.txt", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "share"))

# 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._module_file_rel_path),
{"tensorpipe": "Tensorpipe::Tensorpipe"},
)

@staticmethod
def _create_cmake_module_alias_targets(module_file, targets):
def _create_cmake_module_alias_targets(self, module_file, targets):
content = ""
for alias, aliased in targets.items():
content += textwrap.dedent("""\
content += textwrap.dedent(f"""\
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)
""")
save(self, module_file, content)

@property
def _module_file_rel_path(self):
return os.path.join("lib", "cmake", "conan-official-{}-targets.cmake".format(self.name))
return os.path.join("lib", "cmake", f"conan-official-{self.name}-targets.cmake")

def package_info(self):
self.cpp_info.set_property("cmake_file_name", "Tensorpipe")
self.cpp_info.set_property("cmake_target_name", "tensorpipe")
self.cpp_info.libs = ["tensorpipe"]
if tools.is_apple_os(self.settings.os):
if is_apple_os(self):
self.cpp_info.frameworks = ["CoreFoundation", "IOKit"]

# TODO: to remove in conan v2 once cmake_find_package* generators removed
Expand Down
11 changes: 4 additions & 7 deletions recipes/tensorpipe/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
cmake_minimum_required(VERSION 3.1)
project(test_package)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES CXX)

find_package(Tensorpipe REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} tensorpipe)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
target_link_libraries(${PROJECT_NAME} PRIVATE tensorpipe)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
19 changes: 14 additions & 5 deletions recipes/tensorpipe/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
from conans import ConanFile, CMake, tools
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import CMake, cmake_layout
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake", "cmake_find_package_multi"
generators = "CMakeToolchain", "CMakeDeps", "VirtualRunEnv"
test_type = "explicit"

def layout(self):
cmake_layout(self)

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

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

def test(self):
if not tools.cross_building(self):
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.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
11 changes: 11 additions & 0 deletions recipes/tensorpipe/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.8)
project(test_package LANGUAGES CXX)

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

find_package(Tensorpipe REQUIRED CONFIG)

add_executable(${PROJECT_NAME} ../test_package/test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE tensorpipe)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
17 changes: 17 additions & 0 deletions recipes/tensorpipe/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from conans import ConanFile, CMake, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
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):
bin_path = os.path.join("bin", "test_package")
self.run(bin_path, run_environment=True)