Files
conan-center-index/docs/package_templates/msbuild_package/all/conanfile.py

108 lines
4.0 KiB
Python

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.microsoft import is_msvc, MSBuildDeps, MSBuildToolchain, MSBuild, VCVars
from conan.tools.layout import vs_layout
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, copy, rm, replace_in_file
import os
required_conan_version = ">=1.53.0"
class PackageConan(ConanFile):
name = "package"
description = "short description"
# Use short name only, conform to SPDX License List: https://spdx.org/licenses/
# In case not listed there, use "LicenseRef-<license-file-name>"
license = ""
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/project/package"
# no "conan" and project name in topics. Use topics from the upstream listed on GH
topics = ("topic1", "topic2", "topic3")
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}
# no exports_sources attribute, but export_sources(self) method instead
# this allows finer grain exportation of patches per version
def export_sources(self):
export_conandata_patches(self)
def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC
def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
# for plain C projects only
self.settings.rm_safe("compiler.libcxx")
self.settings.rm_safe("compiler.cppstd")
def layout(self):
vs_layout(self)
def requirements(self):
# prefer self.requires method instead of requires attribute
self.requires("dependency/0.8.1")
def validate(self):
# in case it does not work in another configuration, it should validated here too
if not is_msvc(self):
raise ConanInvalidConfiguration(f"{self.ref} can be built only by Visual Studio and msvc.")
# if another tool than the compiler or CMake is required to build the project (pkgconf, bison, flex etc)
def build_requirements(self):
self.tool_requires("tool/x.y.z")
def source(self):
get(self, **self.conan_data["sources"][self.version], destination=self.source_folder, strip_root=True)
def generate(self):
tc = MSBuildToolchain(self)
tc.generate()
tc = MSBuildDeps(self)
tc.generate()
tc = VCVars(self)
tc.generate()
def _patch_sources(self):
apply_conandata_patches(self)
# remove bundled xxhash
rm(self, "whateer.*", os.path.join(self.source_folder, "lib"))
replace_in_file(self, os.path.join(self.source_folder, "CMakeLists.txt"), "...", "")
def build(self):
self._patch_sources() # It can be apply_conandata_patches(self) only in case no more patches are needed
msbuild = MSBuild(self)
# customize to Release when RelWithDebInfo
msbuild.build_type = "Debug" if self.settings.build_type == "Debug" else "Release"
# use Win32 instead of the default value when building x86
msbuild.platform = "Win32" if self.settings.arch == "x86" else msbuild.platform
# customize according the solution file and compiler version
msbuild.build(sln="project_2017.sln")
def package(self):
copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
copy(
self, pattern="*.lib", dst=os.path.join(self.package_folder, "lib"), src=self.build_folder, keep_path=False
)
copy(
self, pattern="*.dll", dst=os.path.join(self.package_folder, "bin"), src=self.build_folder, keep_path=False
)
copy(
self,
pattern="*.h",
dst=os.path.join(self.package_folder, "include"),
src=os.path.join(self.source_folder, "include"),
)
def package_info(self):
self.cpp_info.libs = ["package_lib"]