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

mbits-args: conan v2 support #14866

Merged
merged 6 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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/mbits-args/all/CMakeLists.txt

This file was deleted.

7 changes: 6 additions & 1 deletion recipes/mbits-args/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
sources:
0.12.3:
"0.12.3":
url: "https://github.com/mbits-libs/args/archive/v0.12.3.tar.gz"
sha256: "1a1dc5793e927a7f8c34b77c776b4d4a88f7ce9557657d8e806fca2922bd07a0"
patches:
"0.12.3":
- patch_file: "patches/0.12.3-0001-export-cmake-config.patch"
patch_description: "conan v2: drop the unneeded dependency, export cmake config"
mzdun marked this conversation as resolved.
Show resolved Hide resolved
patch_type: "conan"
172 changes: 98 additions & 74 deletions recipes/mbits-args/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,127 @@
from conans import ConanFile, CMake, tools
from conans.errors import ConanInvalidConfiguration
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.microsoft import check_min_vs, is_msvc
from conan.tools.files import (
apply_conandata_patches,
export_conandata_patches,
get,
copy,
rm,
rmdir,
)
from conan.tools.build import check_min_cppstd
from conan.tools.scm import Version
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
import os


required_conan_version = ">=1.53.0"


class MBitsArgsConan(ConanFile):
name = "mbits-args"
description = "Small open-source library for program argument parser, inspired by Python's `argparse`, " \
"depending only on the standard library, with C++17 as minimum requirement."
homepage = "https://github.com/mbits-libs/args"
description = (
"Small open-source library for program argument parser, inspired by Python's `argparse`, "
"depending only on the standard library, with C++17 as minimum requirement."
)
license = "MIT"
topics = ("command-line", "commandline", "commandline-interface",
"program-arguments", "argparse", "argparser", "argument-parsing")

url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/mbits-libs/args"
topics = (
"command-line",
"commandline",
"commandline-interface",
"program-arguments",
"argparse",
"argparser",
"argument-parsing",
)
settings = "os", "compiler", "build_type", "arch"
options = {"shared": [True, False], "fPIC": [True, False]}
default_options = {"shared": False, "fPIC": True}
generators = "cmake"
exports_sources = "CMakeLists.txt"

_cmake = None
options = {"fPIC": [True, False]}
default_options = {"fPIC": True}
Comment on lines +39 to +40
Copy link
Contributor

Choose a reason for hiding this comment

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

Just as a future reference, we like to deprecate options before completely removing this to make it smoother for consumers 😄

The shared here should have been kept even though it's modified :)


_compilers_minimum_version = {
"gcc": "8",
"clang": "7.0",
"Visual Studio": "16",
"apple-clang": "10.0",
}
@property
def _min_cppstd(self):
return 17

@property
def _source_subfolder(self):
return "source_subfolder"
def _compilers_minimum_version(self):
mzdun marked this conversation as resolved.
Show resolved Hide resolved
return {
"gcc": "11",
"clang": "12",
"Visual Studio": "16",
mzdun marked this conversation as resolved.
Show resolved Hide resolved
"apple-clang": "11.0.3",
}

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:
del self.options.fPIC

if self.settings.compiler == "Visual Studio" and "MT" in str(self.settings.compiler.runtime):
def layout(self):
cmake_layout(self, src_folder="src")

def validate(self):
if self.settings.compiler.cppstd:
mzdun marked this conversation as resolved.
Show resolved Hide resolved
check_min_cppstd(self, self._min_cppstd)
check_min_vs(self, 192)
if not is_msvc(self):
minimum_version = self._compilers_minimum_version.get(
str(self.settings.compiler), False
)
if (
minimum_version
and Version(self.settings.compiler.version) < minimum_version
):
raise ConanInvalidConfiguration(
"mbits-args: combining shared library with private C++ "
"library (MT/MTd) is not supported.")

if self.settings.compiler.get_safe("cppstd"):
tools.check_min_cppstd(self, "17")

minimum_version = self._compilers_minimum_version.get(
str(self.settings.compiler), False)
if not minimum_version:
self.output.warn(
"mbits-args requires C++17. Your compiler is unknown. Assuming it supports C++17.")
elif tools.Version(self.settings.compiler.version) < minimum_version:
raise ConanInvalidConfiguration("mbits-args: Unsupported compiler: {} {}; "
"minimal version known to work is {}."
.format(self.settings.compiler, self.settings.compiler.version, minimum_version))
elif str(self.settings.compiler) == "clang" and tools.Version(self.settings.compiler.version) < "8":
libcxx = self.settings.compiler.get_safe("libcxx")
if libcxx and str(libcxx) == "libc++":
raise ConanInvalidConfiguration("mbits-args: Unsupported compiler: clang {} with libc++;\n"
"minimal version known to work is either clang 8 with "
"libc++ or clang {} with libstdc++/libstdc++11."
.format(self.settings.compiler.version, minimum_version))
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)

def source(self):
tools.get(**self.conan_data["sources"][self.version])
os.rename("args-{}".format(self.version),
self._source_subfolder)

def _configure_cmake(self):
if self._cmake is not None:
return self._cmake
self._cmake = CMake(self)
self._cmake.definitions["LIBARGS_TESTING"] = False
self._cmake.definitions["LIBARGS_INSTALL"] = True
self._cmake.definitions["LIBARGS_SHARED"] = self.options.shared
self._cmake.configure()
return self._cmake
get(self, **self.conan_data["sources"][self.version], strip_root=True)

def generate(self):
tc = CMakeToolchain(self)
tc.variables["LIBARGS_TESTING"] = False
tc.variables["LIBARGS_INSTALL"] = True
tc.generate()

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

def package(self):
cmake = self._configure_cmake()
copy(
self,
pattern="LICENSE",
dst=os.path.join(self.package_folder, "licenses"),
src=self.source_folder,
)
cmake = CMake(self)
cmake.install()
self.copy("LICENSE",
"licenses", keep_path=False, src=self._source_subfolder)

rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
mzdun marked this conversation as resolved.
Show resolved Hide resolved
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))
rmdir(self, os.path.join(self.package_folder, "share"))
rm(self, "*.la", os.path.join(self.package_folder, "lib"))
rm(self, "*.pdb", os.path.join(self.package_folder, "lib"))
rm(self, "*.pdb", os.path.join(self.package_folder, "bin"))
mzdun marked this conversation as resolved.
Show resolved Hide resolved

def package_info(self):
self.cpp_info.filenames["cmake_find_package"] = "args"
self.cpp_info.filenames["cmake_find_package_multi"] = "args"
self.cpp_info.libs = ["args"]

self.cpp_info.set_property("cmake_file_name", "mbits-args")
self.cpp_info.set_property("cmake_target_name", "mbits::args")

self.cpp_info.filenames["cmake_find_package"] = "mbits-args"
self.cpp_info.filenames["cmake_find_package_multi"] = "mbits-args"
self.cpp_info.names["cmake_find_package"] = "mbits"
self.cpp_info.names["cmake_find_package_multi"] = "mbits"
self.cpp_info.components["libargs"].names["cmake_find_package"] = "args"
self.cpp_info.components["libargs"].names["cmake_find_package_multi"] = "args"
self.cpp_info.components["libargs"].libs = tools.collect_libs(self)

# FIXME: CMake imported target shouldn't be namespaced (requires https://github.com/conan-io/conan/issues/7615)
# https://github.com/mbits-libs/args/blob/72f5f2b87ae39f26638a585fa4ad0b96b4152ae6/CMakeLists.txt#L152
self.cpp_info.components["args"].set_property(
"cmake_target_name", "mbits::args"
)
self.cpp_info.components["args"].libs = ["args"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3d9271c..28c176f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,9 +9,6 @@ set(PROJECT_VERSION_STABILITY "") # or "-alpha", or "-beta", or "-rc.5"
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
message(STATUS "Libargs: Standalone")

- include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
- conan_basic_setup(TARGETS)
-
set(LIBARG_TESTING_DEFAULT ON)
set(LIBARG_INSTALL_DEFAULT ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@@ -123,8 +120,9 @@ target_compile_definitions(args PRIVATE LIBARGS_EXPORTING)
target_compile_features(args PRIVATE cxx_std_17)
target_include_directories(args
PUBLIC
- ${CMAKE_CURRENT_SOURCE_DIR}/include
- ${CMAKE_CURRENT_BINARY_DIR}/include
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
+ $<INSTALL_INTERFACE:include>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

if (LIBARGS_SHARED)
@@ -149,7 +147,8 @@ endif()
##################################################################

if (LIBARGS_INSTALL)
- install(TARGETS args)
+ install(TARGETS args EXPORT mbits)
+ install(EXPORT mbits NAMESPACE "mbits::" DESTINATION lib/cmake)
install(DIRECTORY include/args DESTINATION include)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/args/version.hpp" DESTINATION include/args)
endif()
18 changes: 6 additions & 12 deletions recipes/mbits-args/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
cmake_minimum_required(VERSION 3.12)
project(PackageTest CXX)
cmake_minimum_required(VERSION 3.8)
project(test_package CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(mbits-args REQUIRED CONFIG)

find_package(args REQUIRED CONFIG)

add_executable(example example.cpp)
# FIXME: CMake imported target shouldn't be namespaced (requires https://github.com/conan-io/conan/issues/7615)
target_link_libraries(example mbits::args)
set_target_properties(example PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED OFF)
add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE mbits::args)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
23 changes: 15 additions & 8 deletions recipes/mbits-args/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os

from conans import ConanFile, CMake, tools

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

class LibargsTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
def requirements(self):
self.requires(self.tested_reference_str)

def layout(self):
cmake_layout(self)

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", "example")
self.run("{} --sum 1000 700 1".format(bin_path),
run_environment=True)
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindirs[0], "test_package")
self.run("{} --sum 1000 700 1".format(bin_path), env="conanrun")
8 changes: 8 additions & 0 deletions recipes/mbits-args/all/test_v1_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.1)
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/)
19 changes: 19 additions & 0 deletions recipes/mbits-args/all/test_v1_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from conans import ConanFile, CMake
from conan.tools.build import cross_building
import os


# legacy validation with Conan 1.x
class TestPackageV1Conan(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 cross_building(self):
bin_path = os.path.join("bin", "test_package")
self.run("{} --sum 1000 700 1".format(bin_path), run_environment=True)
2 changes: 1 addition & 1 deletion recipes/mbits-args/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
versions:
0.12.3:
"0.12.3":
folder: all