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

create a recipe template #9841

Merged
merged 5 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 7 additions & 0 deletions docs/package/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(cmake_wrapper C)

include(conanbuildinfo.cmake)
conan_basic_setup(TARGETS KEEP_RPATHS)

add_subdirectory(source_folder)
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions docs/package/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sources:
# Newer versions at the top
"1.2.0":
url: [
"https://mirror1.net/package-1.2.0.tar.gz",
"https://mirror2.net/package-1.2.0.tar.gz",
]
sha256: "________________________________________________________________"
"1.1.0":
url: [
"https://mirror1.net/package-1.1.0.tar.gz",
"https://mirror2.net/package-1.1.0.tar.gz",
]
sha256: "________________________________________________________________"
patches:
# Newer versions at the top
"1.1.0":
- patch_file: "patches/0001-fix-cmake.patch"
base_path: "source_subfolder"
- patch_file: "patches/0002-fix-linux.patch"
base_path: "source_subfolder"
134 changes: 134 additions & 0 deletions docs/package/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from conans import CMake, ConanFile, tools
from conans.errors import ConanInvalidConfiguration
from conan.tools.microsoft import msvc_runtime_flag, is_msvc
import functools
import os

required_conan_version = ">=1.45.0"

class packageConan(ConanFile):
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
name = "package"
description = "shortd escription"
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
license = "" # conform to SPDX License List: https://spdx.org/licenses/
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/project/package"
topics = ("topic1", "topic2") # no "conan" and project name in topics
settings = "os", "arch", "compiler", "build_type" # even for header only
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}
generators = "cmake", "cmake_find_package_multi"
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved

# no manual caching of build helper like _cmake = None


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

@property
def _build_subfolder(self):
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
return "build_subfolder"

# don't use self.settings_build
@property
def _settings_build(self):
return getattr(self, "settings_build", self.settings)

# don't use self.user_info_build
@property
def _user_info_build(self):
return getattr(self, "user_info_build", self.deps_user_info)

# no exports_sources attribute, but export_sources(self) method instead
# this allows finer grain exportation of patches per version
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

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx # for plain C projects only
del self.settings.compiler.cppstd # for plain C projects only

def requirements(self):
self.requires("dependency/0.8.1")

def validate(self):
if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, 11)
if is_msvc(self) and self.options.shared:
raise ConanInvalidConfiguration("package can't be built as shared on visual studio")

ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
def source(self):
tools.get(**self.conan_data["sources"][self.version],
destination=self._source_subfolder, strip_root=True)

def _patch_sources(self):
for patch in self.conan_data.get("patches", {}).get(self.version, []):
tools.patch(**patch)
# remove bundled xxhash
tools.remove_files_by_mask(os.path.join(self._source_subfolder, "lib"), "whateer.*")
tools.replace_in_file(
os.path.join(self._cmakelists_subfolder, "CMakeLists.txt"),
"...",
"",
)

@functools.lru_cache(1)
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
def _configure_cmake(self):
cmake = CMake(self)
if is_msvc(self):
# don't use self.settings.compiler.runtime
cmake.definitions["USE_MSVC_RUNTIME_LIBRARY_DLL"] = "MD" in msvc_runtime_flag(self)
cmake.configure(build_folder=self._build_subfolder)
return cmake

def build(self):
self._patch_sources()
cmake = self._configure_cmake()
cmake.build()

def package(self):
self.copy(pattern="LICENSE", dst="licenses", src=self._source_subfolder)
cmake = self._configure_cmake()
cmake.install()

tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
tools.rmdir(os.path.join(self.package_folder, "share"))
tools.remove_files_by_mask(os.path.join(self.package_folder, "lib"), "*.pdb")
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved

def package_info(self):
self.cpp_info.libs = ["package_lib"]

self.cpp_info.set_property("cmake_find_mode", "both")
self.cpp_info.set_property("cmake_module_file_name", "PACKAGE")
self.cpp_info.set_property("cmake_file_name", "package")
self.cpp_info.set_property("cmake_target_name", "PACKAGE::PACKAGE")
self.cpp_info.set_property("pkg_config_name", "package")
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved

# If they are needed on Linux, m, pthread and dl are usually needed on FreeBSD too
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.system_libs.append("m")
self.cpp_info.system_libs.append("pthread")
self.cpp_info.system_libs.append("dl")



# TODO: to remove in conan v2 once cmake_find_package_* generators removed
self.cpp_info.filenames["cmake_find_package"] = "PACKAGE"
self.cpp_info.filenames["cmake_find_package_multi"] = "PACKAGE"
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
self.cpp_info.names["cmake_find_package"] = "PACKAGE"
self.cpp_info.names["cmake_find_package_multi"] = "PACKAGE"
ericLemanissier marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 15 additions & 0 deletions docs/package/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.1)

project(test_package C) # if the project is pure C
project(test_package CXX) # if the project uses c++


include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)

conan_basic_setup(TARGETS)

find_package(package REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
# don't link to ${CONAN_LIBS} or CONAN_PKG::package
target_link_libraries(${PROJECT_NAME} package::package)
17 changes: 17 additions & 0 deletions docs/package/all/test_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)
6 changes: 6 additions & 0 deletions docs/package/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
versions:
# Newer versions at the top
"1.2.0":
folder: all
"1.1.0":
folder: all