|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import textwrap |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from conan.test.utils.tools import TestClient |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.tool("cmake", "4.2") |
| 11 | +def test_cps(): |
| 12 | + c = TestClient() |
| 13 | + c.run("new cmake_lib") |
| 14 | + conanfile = textwrap.dedent("""\ |
| 15 | + from conan import ConanFile |
| 16 | + from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout |
| 17 | + from conan.cps import CPS |
| 18 | + import glob |
| 19 | +
|
| 20 | + class mypkgRecipe(ConanFile): |
| 21 | + name = "mypkg" |
| 22 | + version = "0.1" |
| 23 | + package_type = "library" |
| 24 | +
|
| 25 | + settings = "os", "compiler", "build_type", "arch" |
| 26 | + options = {"shared": [True, False], "fPIC": [True, False]} |
| 27 | + default_options = {"shared": False, "fPIC": True} |
| 28 | +
|
| 29 | + exports_sources = "CMakeLists.txt", "src/*", "include/*" |
| 30 | + implements = ["auto_shared_fpic"] |
| 31 | + generators = "CMakeToolchain" |
| 32 | +
|
| 33 | + def layout(self): |
| 34 | + cmake_layout(self) |
| 35 | +
|
| 36 | + def build(self): |
| 37 | + cmake = CMake(self) |
| 38 | + cmake.configure() |
| 39 | + cmake.build() |
| 40 | +
|
| 41 | + def package(self): |
| 42 | + cmake = CMake(self) |
| 43 | + cmake.install() |
| 44 | +
|
| 45 | + def package_info(self): |
| 46 | + file_loc = glob.glob("**/mypkg.cps", recursive=True) |
| 47 | + self.cpp_info = CPS.load(file_loc[0]).to_conan() |
| 48 | + """) |
| 49 | + |
| 50 | + cmake = textwrap.dedent("""\ |
| 51 | + cmake_minimum_required(VERSION 4.2) |
| 52 | + project(mypkg CXX) |
| 53 | +
|
| 54 | + set(CMAKE_EXPERIMENTAL_EXPORT_PACKAGE_INFO "b80be207-778e-46ba-8080-b23bba22639e") |
| 55 | +
|
| 56 | + add_library(mypkg src/mypkg.cpp) |
| 57 | + target_include_directories(mypkg PUBLIC |
| 58 | + $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
| 59 | + $<INSTALL_INTERFACE:include>) |
| 60 | +
|
| 61 | + set_target_properties(mypkg PROPERTIES PUBLIC_HEADER "include/mypkg.h") |
| 62 | + install(TARGETS mypkg EXPORT mypkg) |
| 63 | +
|
| 64 | + install(PACKAGE_INFO mypkg EXPORT mypkg) |
| 65 | + """) |
| 66 | + |
| 67 | + # First, try with the standard mypkg-config.cmake consumption |
| 68 | + c.save({"conanfile.py": conanfile, |
| 69 | + "CMakeLists.txt": cmake}) |
| 70 | + c.run("create") |
| 71 | + assert "mypkg/0.1: Hello World Release!" in c.out |
| 72 | + |
| 73 | + # Lets consume directly with CPS |
| 74 | + test_cmake = textwrap.dedent("""\ |
| 75 | + cmake_minimum_required(VERSION 4.2) |
| 76 | + project(PackageTest CXX) |
| 77 | +
|
| 78 | + set(CMAKE_EXPERIMENTAL_FIND_CPS_PACKAGES e82e467b-f997-4464-8ace-b00808fff261) |
| 79 | +
|
| 80 | + find_package(mypkg CONFIG REQUIRED) |
| 81 | +
|
| 82 | + add_executable(example src/example.cpp) |
| 83 | + target_link_libraries(example mypkg::mypkg) |
| 84 | + """) |
| 85 | + test_conanfile = textwrap.dedent("""\ |
| 86 | + import os |
| 87 | +
|
| 88 | + from conan import ConanFile |
| 89 | + from conan.tools.cmake import CMake, cmake_layout, CMakeToolchain, CMakeConfigDeps |
| 90 | + from conan.tools.build import can_run |
| 91 | +
|
| 92 | +
|
| 93 | + class TestConan(ConanFile): |
| 94 | + settings = "os", "compiler", "build_type", "arch" |
| 95 | +
|
| 96 | + def requirements(self): |
| 97 | + self.requires(self.tested_reference_str) |
| 98 | +
|
| 99 | + def generate(self): |
| 100 | + deps = CMakeConfigDeps(self) |
| 101 | + deps.set_property("mypkg", "cmake_find_mode", "none") |
| 102 | + deps.generate() |
| 103 | + tc = CMakeToolchain(self) |
| 104 | + tc.generate() |
| 105 | +
|
| 106 | + def build(self): |
| 107 | + cmake = CMake(self) |
| 108 | + cmake.configure() |
| 109 | + cmake.build() |
| 110 | +
|
| 111 | + def layout(self): |
| 112 | + cmake_layout(self) |
| 113 | +
|
| 114 | + def test(self): |
| 115 | + if can_run(self): |
| 116 | + cmd = os.path.join(self.cpp.build.bindir, "example") |
| 117 | + self.run(cmd, env="conanrun") |
| 118 | + """) |
| 119 | + shutil.rmtree(os.path.join(c.current_folder, "test_package", "build")) |
| 120 | + c.save({"test_package/conanfile.py": test_conanfile, |
| 121 | + "test_package/CMakeLists.txt": test_cmake}) |
| 122 | + c.run("create --build=missing -c tools.cmake.cmakedeps:new=will_break_next") |
| 123 | + assert "mypkg/0.1: Hello World Release!" in c.out |
0 commit comments