|
1 | 1 | import os |
| 2 | +from dataclasses import dataclass |
2 | 3 |
|
3 | 4 | from conan import ConanFile |
4 | | -from conan.tools.cmake import CMakeToolchain |
| 5 | +from conan.tools.cmake import CMakeToolchain, cmake_layout, CMake |
| 6 | +from conan.tools.files import get, copy, download |
5 | 7 |
|
6 | 8 |
|
7 | | -class HelloConan(ConanFile): |
8 | | - settings = 'os', 'compiler', 'build_type', 'arch' |
9 | | - generators = 'CMakeDeps', 'CMakeToolchain' |
10 | | - default_options = {'fmt/*:header_only': True, 'spdlog/*:header_only': True, 'qt/*:with_fontconfig': False, 'open62541/*:cpp_compatible': True} |
| 9 | +@dataclass |
| 10 | +class Compiler: |
| 11 | + name: str |
| 12 | + version: str |
| 13 | + sha256: str |
| 14 | + extension: str |
11 | 15 |
|
| 16 | + |
| 17 | +class CppExampleProjectConan(ConanFile): |
| 18 | + version = "0.0.1" |
| 19 | + name = "cpp_example_project" |
| 20 | + settings = "os", "compiler", "build_type", "arch" |
| 21 | + generators = "CMakeDeps" |
| 22 | + |
| 23 | + options = { |
| 24 | + "use_sml": [True, False], |
| 25 | + "use_boost_beast": [True, False], |
| 26 | + "use_crow": [True, False], |
| 27 | + "use_cppzmq_proto": [True, False], |
| 28 | + "use_qt": [True, False], |
| 29 | + "use_open62541": [True, False], |
| 30 | + "use_open62541pp": [True, False], # fetched via FetchContent in CMake |
| 31 | + "use_slint": [True, False], # fetched via FetchContent in CMake |
| 32 | + "use_imgui": [True, False], |
| 33 | + } |
| 34 | + |
| 35 | + default_options = { |
| 36 | + "use_sml": True, |
| 37 | + "use_boost_beast": True, |
| 38 | + "use_crow": True, |
| 39 | + "use_cppzmq_proto": False, |
| 40 | + "use_qt": False, |
| 41 | + "use_open62541": True, |
| 42 | + "use_open62541pp": True, |
| 43 | + "use_slint": True, |
| 44 | + "use_imgui": True, |
| 45 | + # library-level options |
| 46 | + "fmt/*:header_only": True, |
| 47 | + "spdlog/*:header_only": True, |
| 48 | + "qt/*:with_fontconfig": False, |
| 49 | + "open62541/*:cpp_compatible": True, |
| 50 | + } |
| 51 | + |
| 52 | + def _get_toolchain(self): |
| 53 | + if self.settings.os == "Windows": |
| 54 | + return Compiler( |
| 55 | + name="arm-none-linux-gnueabihf", |
| 56 | + version="14.3.rel1", |
| 57 | + sha256="fd0801c9fcb0327978e5f7594f38225b7dec0fd515006c1608f74c0460111312", |
| 58 | + extension=".zip", |
| 59 | + ) |
| 60 | + return Compiler( |
| 61 | + name="arm-none-linux-gnueabihf", |
| 62 | + version="14.3.rel1", |
| 63 | + sha256="3ec0113af5154a2573b3851d74d9e9501a805abf9dfa0f82b04ef26fa0e6fc35", |
| 64 | + extension=".tar.xz", |
| 65 | + ) |
| 66 | + |
| 67 | + def source(self): |
| 68 | + if self.settings.arch == "armv7": |
| 69 | + download(self, "https://developer.arm.com/GetEula?Id=37988a7c-c40e-4b78-9fd1-62c20b507aa8", "LICENSE", verify=False) |
| 70 | + |
| 71 | + # 3. call for conan install |
12 | 72 | def requirements(self): |
13 | | - if self.settings.get_safe('arch') == 'armv7': |
14 | | - self.requires('fmt/11.1.3') |
15 | | - self.requires('sml/1.1.11') |
| 73 | + # ARM cross-compilation: minimal dependency set |
| 74 | + if self.settings.arch == "armv7": |
| 75 | + self.requires("fmt/11.2.0") |
| 76 | + if self.options.use_sml: |
| 77 | + self.requires("sml/1.1.12") |
16 | 78 | return |
17 | 79 |
|
18 | | - self.requires('catch2/3.8.0') |
19 | | - self.requires('gtest/1.15.0') |
20 | | - self.requires('docopt.cpp/0.6.3') |
21 | | - self.requires('spdlog/1.15.0') |
22 | | - if os.getenv("CONFIGURE_QT") == '1': |
23 | | - self.requires('qt/6.7.3') |
24 | | - else: |
25 | | - |
26 | | - self.requires('sml/1.1.11') |
27 | | - self.requires('nlohmann_json/3.11.3') |
28 | | - self.requires('boost/1.87.0') |
29 | | - self.requires('crowcpp-crow/1.2.0') |
30 | | - self.requires('cppzmq/4.10.0') |
31 | | - self.requires('protobuf/5.29.3') |
32 | | - self.requires('open62541/1.4.6') |
33 | | - |
34 | | - def configure(self): |
35 | | - cmake = CMakeToolchain(self) |
36 | | - cmake.user_presets_path = None |
| 80 | + # Qt-only build: only the Qt package is needed |
| 81 | + if self.options.use_qt: |
| 82 | + self.requires("qt/6.8.3") |
| 83 | + return |
| 84 | + |
| 85 | + # Standard desktop build |
| 86 | + self.requires("fmt/11.2.0") |
| 87 | + self.requires("spdlog/1.15.3") |
| 88 | + |
| 89 | + if self.options.use_sml: |
| 90 | + self.requires("sml/1.1.12") |
| 91 | + |
| 92 | + if self.options.use_boost_beast: |
| 93 | + self.requires("boost/1.88.0") |
| 94 | + self.requires("nlohmann_json/3.12.0") |
| 95 | + |
| 96 | + if self.options.use_crow: |
| 97 | + self.requires("crowcpp-crow/1.2.1") |
37 | 98 |
|
| 99 | + if self.options.use_cppzmq_proto: |
| 100 | + self.requires("cppzmq/4.11.0") |
| 101 | + self.requires("protobuf/6.30.1") |
| 102 | + |
| 103 | + if self.options.use_open62541 or self.options.use_open62541pp: |
| 104 | + self.requires("open62541/1.4.13") |
| 105 | + |
| 106 | + if self.options.use_imgui: |
| 107 | + self.requires("imgui/1.90.5") |
| 108 | + self.requires("implot/0.16") |
| 109 | + |
| 110 | + # 4. call for conan install |
| 111 | + def build_requirements(self): |
| 112 | + self.tool_requires("cmake/[>=4.0]") |
| 113 | + self.tool_requires("ninja/[>=1.11.0]") |
| 114 | + if self.settings.arch != "armv7": |
| 115 | + self.test_requires("catch2/3.10.0") |
| 116 | + self.test_requires("gtest/1.17.0") |
| 117 | + |
| 118 | + # 5. call for conan install |
| 119 | + def layout(self): |
| 120 | + cmake_layout(self) |
| 121 | + |
| 122 | + # 6. call for conan install |
| 123 | + def generate(self): |
| 124 | + tc = CMakeToolchain(self) |
| 125 | + tc.user_presets_path = "ConanPresets.json" |
| 126 | + tc.cache_variables["CMAKE_POLICY_VERSION_MINIMUM"] = "3.5" |
| 127 | + |
| 128 | + # Forward feature options to CMake cache variables for desktop builds |
| 129 | + if self.settings.arch != "armv7" and not self.options.use_qt: |
| 130 | + tc.cache_variables["CPP_STARTER_USE_SML"] = bool(self.options.use_sml) |
| 131 | + tc.cache_variables["CPP_STARTER_USE_BOOST_BEAST"] = bool(self.options.use_boost_beast) |
| 132 | + tc.cache_variables["CPP_STARTER_USE_CROW"] = bool(self.options.use_crow) |
| 133 | + tc.cache_variables["CPP_STARTER_USE_CPPZMQ_PROTO"] = bool(self.options.use_cppzmq_proto) |
| 134 | + tc.cache_variables["CPP_STARTER_USE_OPEN62541"] = bool(self.options.use_open62541) |
| 135 | + tc.cache_variables["CPP_STARTER_USE_OPEN62541PP"] = bool(self.options.use_open62541pp) |
| 136 | + tc.cache_variables["CPP_STARTER_USE_SLINT"] = bool(self.options.use_slint) |
| 137 | + tc.cache_variables["CPP_STARTER_USE_IMGUI"] = bool(self.options.use_imgui) |
| 138 | + |
| 139 | + if self.options.use_qt: |
| 140 | + tc.cache_variables["CPP_STARTER_USE_QT"] = True |
| 141 | + |
| 142 | + tc.generate() |
| 143 | + |
| 144 | + # Copy imgui backend bindings into the include path |
| 145 | + if self.options.use_imgui and "imgui" in self.dependencies: |
| 146 | + source_dir = os.path.join(self.dependencies["imgui"].package_folder, "res", "bindings") |
| 147 | + dest_dir = os.path.join(self.dependencies["imgui"].package_folder, "include", "backends") |
| 148 | + copy(self, "*", source_dir, dest_dir) |
| 149 | + |
| 150 | + # 1. call for conan build |
38 | 151 | def build(self): |
39 | | - cmake = CMakeToolchain(self) |
| 152 | + if self.settings.arch == "armv7": |
| 153 | + compiler_definition = self._get_toolchain() |
| 154 | + get( |
| 155 | + self, |
| 156 | + f"https://developer.arm.com/-/media/Files/downloads/gnu/{compiler_definition.version}/binrel/" |
| 157 | + f"arm-gnu-toolchain-{compiler_definition.version}-x86_64-{compiler_definition.name}{compiler_definition.extension}", |
| 158 | + sha256=compiler_definition.sha256, |
| 159 | + strip_root=True, |
| 160 | + ) |
| 161 | + |
| 162 | + cmake = CMake(self) |
40 | 163 | cmake.configure() |
41 | 164 | cmake.build() |
0 commit comments