Skip to content

Commit

Permalink
(#24018) faker-cxx: add recipe
Browse files Browse the repository at this point in the history
* faker-cxx: add recipe

* don't use format in test_package

* update 2.0.0

* add with_std_format option

* update compiler minimum version (https://github.com/cieslarmichal/faker-cxx?tab=readme-ov-file#compiler-support)

* use wildcard on copying LICENSE files

Co-authored-by: Uilian Ries <[email protected]>

* drop support for Windows shared build

Co-authored-by: Uilian Ries <[email protected]>

---------

Co-authored-by: Uilian Ries <[email protected]>
  • Loading branch information
toge and uilianries authored Jun 29, 2024
1 parent c8e4d78 commit 7aee73a
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions recipes/faker-cxx/all/conandata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sources:
"2.0.0":
url: "https://github.com/cieslarmichal/faker-cxx/archive/refs/tags/v2.0.0.tar.gz"
sha256: "8a7f5441f4453af868444675878a2d9a74918c1595caa65d537d3ea327e46a49"
103 changes: 103 additions & 0 deletions recipes/faker-cxx/all/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.files import get, copy, rmdir
from conan.tools.build import check_min_cppstd
from conan.tools.scm import Version
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
import os

required_conan_version = ">=1.53.0"

class FakerCXXConan(ConanFile):
name = "faker-cxx"
description = "C++ Faker library based on faker-js/faker. "
license = "MIT"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://github.com/cieslarmichal/faker-cxx"
topics = ("faker", "fake",)
package_type = "library"
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"fPIC": [True, False],
"with_std_format": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"with_std_format": False,
}

@property
def _min_cppstd(self):
return 20

@property
def _compilers_minimum_version(self):
return {
"gcc": "12",
"clang": "16",
"apple-clang": "16",
"Visual Studio": "17",
"msvc": "193",
}

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")

def requirements(self):
if not self.options.with_std_format:
self.requires("fmt/10.2.1")

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

def validate(self):
if self.settings.compiler.cppstd:
check_min_cppstd(self, self._min_cppstd)
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(
f"{self.ref} requires C++{self._min_cppstd}, which your compiler does not support."
)
if self.settings.os == "Windows" and self.options.shared:
# https://github.com/cieslarmichal/faker-cxx/issues/753
raise ConanInvalidConfiguration(f"{self.ref} is not prepared to generated shared library on Windows.")

def build_requirements(self):
self.tool_requires("cmake/[>=3.22 <4]")

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

def generate(self):
tc = CMakeToolchain(self)
tc.variables["USE_SYSTEM_DEPENDENCIES"] = True
tc.variables["BUILD_TESTING"] = False
tc.variables["WARNINGS_AS_ERRORS"] = False
tc.variables["WITH_STD_FORMAT"] = self.options.with_std_format
tc.generate()
deps = CMakeDeps(self)
deps.generate()

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

def package(self):
copy(self, pattern="LICENSE*", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
cmake = CMake(self)
cmake.install()
rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

def package_info(self):
self.cpp_info.libs = ["faker-cxx"]

self.cpp_info.set_property("cmake_file_name", "faker-cxx")
self.cpp_info.set_property("cmake_target_name", "faker-cxx::faker-cxx")
8 changes: 8 additions & 0 deletions recipes/faker-cxx/all/test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)

find_package(faker-cxx REQUIRED CONFIG)

add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE faker-cxx::faker-cxx)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
26 changes: 26 additions & 0 deletions recipes/faker-cxx/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from conan import ConanFile
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout, CMake
import os


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

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 can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
17 changes: 17 additions & 0 deletions recipes/faker-cxx/all/test_package/test_package.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include "faker-cxx/Internet.h"
#include "faker-cxx/String.h"
#include "faker-cxx/Date.h"

int main()
{
const auto id = faker::string::uuid();
const auto email = faker::internet::email();
const auto password = faker::internet::password();
const auto createdAt = faker::date::pastDate(5, faker::date::DateFormat::ISO);
const auto updatedAt = faker::date::recentDate(2, faker::date::DateFormat::ISO);

std::cout << "id: " << id << ", email: " << email << ", password: " << password << ", createdAt: " << createdAt << ", updatedAt: " << updatedAt << "\n";

return 0;
}
3 changes: 3 additions & 0 deletions recipes/faker-cxx/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
versions:
"2.0.0":
folder: all

0 comments on commit 7aee73a

Please sign in to comment.