Skip to content

Commit

Permalink
(#13889) microtar: conan v2 support
Browse files Browse the repository at this point in the history
* conan v2 support

* use rm_safe
  • Loading branch information
SpaceIm authored Nov 8, 2022
1 parent 5b975cd commit c71c8fa
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 57 deletions.
25 changes: 14 additions & 11 deletions recipes/microtar/all/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
cmake_minimum_required(VERSION 3.4)
project(cmake_wrapper)
project(microtar LANGUAGES C)

include("conanbuildinfo.cmake")
conan_basic_setup()
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
include(GNUInstallDirs)

set(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/source_subfolder/src/microtar.c")
set(HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/source_subfolder/src/microtar.h")
add_library(microtar ${MICROTAR_SRC_DIR}/src/microtar.c)
set_target_properties(microtar PROPERTIES
PUBLIC_HEADER "${MICROTAR_SRC_DIR}/src/microtar.h"
WINDOWS_EXPORT_ALL_SYMBOLS ON
)

add_library(microtar ${SOURCES} ${HEADERS})

set_target_properties(microtar PROPERTIES PUBLIC_HEADER ${HEADERS})

install(TARGETS microtar)
install(
TARGETS microtar
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
72 changes: 37 additions & 35 deletions recipes/microtar/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get
import os
from conans import ConanFile, CMake, tools

required_conan_version = ">=1.53.0"


class MicrotarConan(ConanFile):
Expand All @@ -8,52 +12,50 @@ class MicrotarConan(ConanFile):
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/rxi/microtar"
description = "A lightweight tar library written in ANSI C"
topics = ("conan", "tar", "archive")
settings = "os", "arch", "compiler", "build_type"
options = {"shared": [True, False],
"fPIC": [True, False]}
default_options = {"shared": False,
"fPIC": True}
exports_sources = ["CMakeLists.txt"]
generators = "cmake",
_cmake = None

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

@property
def _build_subfolder(self):
return "build_subfolder"
topics = ("tar", "archive")

def source(self):
tools.get(**self.conan_data["sources"][self.version], strip_root=True, destination=self._source_subfolder)
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
}

def configure(self):
if self.options.shared:
del self.options.fPIC
del self.settings.compiler.libcxx
del self.settings.compiler.cppstd
exports_sources = "CMakeLists.txt"

def config_options(self):
if self.settings.os == "Windows":
del self.options.fPIC

def _configure_cmake(self):
if self._cmake:
return self._cmake
cmake = CMake(self)
cmake.configure(build_folder=self._build_subfolder)
self._cmake = cmake
return self._cmake
def configure(self):
if self.options.shared:
self.options.rm_safe("fPIC")
self.settings.rm_safe("compiler.cppstd")
self.settings.rm_safe("compiler.libcxx")

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

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

def generate(self):
tc = CMakeToolchain(self)
tc.variables["MICROTAR_SRC_DIR"] = self.source_folder.replace("\\", "/")
tc.generate()

def build(self):
cmake = self._configure_cmake()
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, os.pardir))
cmake.build()

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

def package_info(self):
Expand Down
7 changes: 2 additions & 5 deletions recipes/microtar/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(test_package C)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
project(test_package LANGUAGES C)

find_package(microtar REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.c)
target_link_libraries(${PROJECT_NAME} microtar::microtar)
target_link_libraries(${PROJECT_NAME} PRIVATE microtar::microtar)
21 changes: 15 additions & 6 deletions recipes/microtar/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", "compiler", "build_type", "arch"
generators = "cmake", "cmake_find_package_multi"
settings = "os", "arch", "compiler", "build_type"
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.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.bindirs[0], "test_package")
self.run(bin_path, env="conanrun")
8 changes: 8 additions & 0 deletions recipes/microtar/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)
17 changes: 17 additions & 0 deletions recipes/microtar/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)

0 comments on commit c71c8fa

Please sign in to comment.