From e99c59f4cf0a7874a29187ed84b2e773efa1856d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Jul 2025 17:41:41 +0200 Subject: [PATCH 1/3] Add lesson18 files --- lesson18/hello/CMakeLists.txt | 12 +++++ lesson18/hello/conanfile.py | 52 +++++++++++++++++++++ lesson18/hello/include/hello.h | 9 ++++ lesson18/hello/src/hello.cpp | 9 ++++ lesson18/hello/test_package/CMakeLists.txt | 7 +++ lesson18/hello/test_package/conanfile.py | 26 +++++++++++ lesson18/hello/test_package/src/example.cpp | 6 +++ lesson18/say/CMakeLists.txt | 9 ++++ lesson18/say/conanfile.py | 39 ++++++++++++++++ lesson18/say/src/say.cpp | 8 ++++ lesson18/say/test_package/conanfile.py | 16 +++++++ 11 files changed, 193 insertions(+) create mode 100644 lesson18/hello/CMakeLists.txt create mode 100644 lesson18/hello/conanfile.py create mode 100644 lesson18/hello/include/hello.h create mode 100644 lesson18/hello/src/hello.cpp create mode 100644 lesson18/hello/test_package/CMakeLists.txt create mode 100644 lesson18/hello/test_package/conanfile.py create mode 100644 lesson18/hello/test_package/src/example.cpp create mode 100644 lesson18/say/CMakeLists.txt create mode 100644 lesson18/say/conanfile.py create mode 100644 lesson18/say/src/say.cpp create mode 100644 lesson18/say/test_package/conanfile.py diff --git a/lesson18/hello/CMakeLists.txt b/lesson18/hello/CMakeLists.txt new file mode 100644 index 0000000..661a7e5 --- /dev/null +++ b/lesson18/hello/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.15) +project(hello CXX) + +add_library(hello src/hello.cpp) +target_include_directories(hello PUBLIC include) + +set_target_properties(hello PROPERTIES PUBLIC_HEADER "include/hello.h") + +find_package(fmt) +target_link_libraries(hello fmt::fmt) + +install(TARGETS hello) diff --git a/lesson18/hello/conanfile.py b/lesson18/hello/conanfile.py new file mode 100644 index 0000000..7b1c7f7 --- /dev/null +++ b/lesson18/hello/conanfile.py @@ -0,0 +1,52 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps + + +class helloRecipe(ConanFile): + name = "hello" + version = "1.0" + package_type = "library" + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + + options = {"shared": [True, False], + "fPIC": [True, False]} + + default_options = {"shared": False, + "fPIC": True} + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*", "include/*" + + def config_options(self): + if self.settings.os == "Windows": + self.options.rm_safe("fPIC") + + def configure(self): + if self.options.shared: + self.options.rm_safe("fPIC") + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires("fmt/11.2.0") + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = ["hello"] diff --git a/lesson18/hello/include/hello.h b/lesson18/hello/include/hello.h new file mode 100644 index 0000000..cdc81d4 --- /dev/null +++ b/lesson18/hello/include/hello.h @@ -0,0 +1,9 @@ +#pragma once + +#ifdef _WIN32 + #define HELLO_EXPORT __declspec(dllexport) +#else + #define HELLO_EXPORT +#endif + +HELLO_EXPORT void hello(); diff --git a/lesson18/hello/src/hello.cpp b/lesson18/hello/src/hello.cpp new file mode 100644 index 0000000..91b0e35 --- /dev/null +++ b/lesson18/hello/src/hello.cpp @@ -0,0 +1,9 @@ +#include +#include "hello.h" + +#include + +void hello() { + fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, + "hello/1.0: Hello World! (with color!)\n"); +} diff --git a/lesson18/hello/test_package/CMakeLists.txt b/lesson18/hello/test_package/CMakeLists.txt new file mode 100644 index 0000000..c33c2a7 --- /dev/null +++ b/lesson18/hello/test_package/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.15) +project(PackageTest CXX) + +find_package(hello CONFIG REQUIRED) + +add_executable(example src/example.cpp) +target_link_libraries(example hello::hello) diff --git a/lesson18/hello/test_package/conanfile.py b/lesson18/hello/test_package/conanfile.py new file mode 100644 index 0000000..e073113 --- /dev/null +++ b/lesson18/hello/test_package/conanfile.py @@ -0,0 +1,26 @@ +import os + +from conan import ConanFile +from conan.tools.cmake import CMake, cmake_layout +from conan.tools.build import can_run + + +class helloTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + generators = "CMakeDeps", "CMakeToolchain" + + def requirements(self): + self.requires(self.tested_reference_str) + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def layout(self): + cmake_layout(self) + + def test(self): + if can_run(self): + cmd = os.path.join(self.cpp.build.bindir, "example") + self.run(cmd, env="conanrun") diff --git a/lesson18/hello/test_package/src/example.cpp b/lesson18/hello/test_package/src/example.cpp new file mode 100644 index 0000000..1b2f1e3 --- /dev/null +++ b/lesson18/hello/test_package/src/example.cpp @@ -0,0 +1,6 @@ +#include "hello.h" + + +int main() { + hello(); +} diff --git a/lesson18/say/CMakeLists.txt b/lesson18/say/CMakeLists.txt new file mode 100644 index 0000000..808d193 --- /dev/null +++ b/lesson18/say/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.15) +project(say CXX) + +add_executable(say src/say.cpp) + +find_package(hello REQUIRED CONFIG) +target_link_libraries(say hello::hello) + +install(TARGETS say) diff --git a/lesson18/say/conanfile.py b/lesson18/say/conanfile.py new file mode 100644 index 0000000..5132473 --- /dev/null +++ b/lesson18/say/conanfile.py @@ -0,0 +1,39 @@ +from conan import ConanFile +from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps + + +class sayRecipe(ConanFile): + name = "say" + version = "1.0" + package_type = "application" + + # Binary configuration + settings = "os", "compiler", "build_type", "arch" + + # Sources are located in the same place as this recipe, copy them to the recipe + exports_sources = "CMakeLists.txt", "src/*", "include/*" + + def layout(self): + cmake_layout(self) + + def requirements(self): + self.requires("hello/1.0") + + def generate(self): + deps = CMakeDeps(self) + deps.generate() + tc = CMakeToolchain(self) + tc.generate() + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() + + def package(self): + cmake = CMake(self) + cmake.install() + + def package_info(self): + self.cpp_info.libs = [] + self.cpp_info.libdirs = [] diff --git a/lesson18/say/src/say.cpp b/lesson18/say/src/say.cpp new file mode 100644 index 0000000..3b5a4b2 --- /dev/null +++ b/lesson18/say/src/say.cpp @@ -0,0 +1,8 @@ +#include + +#include "hello.h" + +int main() { + std::cout << "We say: "; + hello(); +} diff --git a/lesson18/say/test_package/conanfile.py b/lesson18/say/test_package/conanfile.py new file mode 100644 index 0000000..031bed2 --- /dev/null +++ b/lesson18/say/test_package/conanfile.py @@ -0,0 +1,16 @@ +from conan import ConanFile +from conan.tools.build import can_run +from conan.tools.layout import basic_layout + +class helloTestConan(ConanFile): + settings = "os", "compiler", "build_type", "arch" + + def requirements(self): + self.requires(self.tested_reference_str) + + def layout(self): + basic_layout(self) + + def test(self): + if can_run(self): + self.run("say", env="conanrun") From 07ccfa357468ba092aa93b2029bff741bed7ebb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Jul 2025 17:54:14 +0200 Subject: [PATCH 2/3] test for lesson 18 --- lesson18/say/test_package/conanfile.py | 3 ++- lesson18/test.ps1 | 21 +++++++++++++++++++++ lesson18/test.sh | 23 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lesson18/test.ps1 create mode 100755 lesson18/test.sh diff --git a/lesson18/say/test_package/conanfile.py b/lesson18/say/test_package/conanfile.py index 031bed2..570999d 100644 --- a/lesson18/say/test_package/conanfile.py +++ b/lesson18/say/test_package/conanfile.py @@ -13,4 +13,5 @@ def layout(self): def test(self): if can_run(self): - self.run("say", env="conanrun") + extension = ".exe" if self.settings.os == "Windows" else "" + self.run(f"say{extension}", env="conanrun") diff --git a/lesson18/test.ps1 b/lesson18/test.ps1 new file mode 100644 index 0000000..0709cc2 --- /dev/null +++ b/lesson18/test.ps1 @@ -0,0 +1,21 @@ +$ErrorActionPreference = 'Stop' + +# Add hello package to editable mode +conan editable add hello + +cd say + +# Try to build src +conan build . --build=missing --build=editable + +# It built successfully and we can run it +./build/Release/say.exe + +cd .. + +# And now ensure the usual workflow still works +conan editable remove hello + +conan create hello --build=missing + +conan create say diff --git a/lesson18/test.sh b/lesson18/test.sh new file mode 100755 index 0000000..ad06410 --- /dev/null +++ b/lesson18/test.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +set -e + +# Add hello package to editable mode +conan editable add hello + +cd say + +# Try to build src +conan build . --build=missing --build=editable + +# It built successfully and we can run it +./build/Release/say + +cd .. + +# And now ensure the usual workflow still works +conan editable remove hello + +conan create hello --build=missing + +conan create say From 34515025409d8ca9db46e9e7c9b91e036e6b0601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Jul 2025 17:55:01 +0200 Subject: [PATCH 3/3] test for lesson 18 --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 026196b..a707304 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -24,7 +24,7 @@ jobs: options: --user 0:0 strategy: matrix: - lesson: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16] + lesson: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18] env: CONAN_HOME: ${{ github.workspace }}/.conan_home_${{ matrix.lesson }} @@ -46,7 +46,7 @@ jobs: runs-on: macos-latest strategy: matrix: - lesson: [1, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16] + lesson: [1, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18] env: CONAN_HOME: ${{ github.workspace }}/.conan_home_${{ matrix.lesson }} @@ -68,7 +68,7 @@ jobs: runs-on: windows-latest strategy: matrix: - lesson: [1, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16] + lesson: [1, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18] env: CONAN_HOME: ${{ github.workspace }}/.conan_home_${{ matrix.lesson }}