-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.py
More file actions
532 lines (432 loc) · 21.9 KB
/
plugin.py
File metadata and controls
532 lines (432 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""Conan Provider Plugin
This module implements the Conan provider plugin for CPPython. It handles
integration with the Conan package manager, including dependency resolution,
installation, and synchronization with other tools.
"""
import contextlib
import io
import os
from logging import Logger, getLogger
from pathlib import Path
from typing import Any
from conan.api.conan_api import ConanAPI
from conan.cli.cli import Cli
from cppython.core.plugin_schema.generator import SyncConsumer
from cppython.core.plugin_schema.provider import Provider, ProviderPluginGroupData, SupportedProviderFeatures
from cppython.core.schema import CorePluginData, Information, PluginReport, SupportedFeatures, SyncData
from cppython.plugins.cmake.plugin import CMakeGenerator
from cppython.plugins.cmake.schema import CMakeSyncData
from cppython.plugins.conan.builder import Builder
from cppython.plugins.conan.resolution import resolve_conan_data, resolve_conan_dependency
from cppython.plugins.conan.schema import ConanData, ConanfileGenerationData
from cppython.plugins.meson.plugin import MesonGenerator
from cppython.plugins.meson.schema import MesonSyncData
from cppython.utility.exception import InstallationVerificationError, NotSupportedError, ProviderInstallationError
from cppython.utility.utility import TypeName
class ConanProvider(Provider):
"""Conan Provider"""
def __init__(
self, group_data: ProviderPluginGroupData, core_data: CorePluginData, configuration_data: dict[str, Any]
) -> None:
"""Initializes the provider"""
self.group_data: ProviderPluginGroupData = group_data
self.core_data: CorePluginData = core_data
self.data: ConanData = resolve_conan_data(configuration_data, core_data)
self.builder = Builder()
# Initialize ConanAPI once and reuse it
self._conan_api = ConanAPI()
# Initialize CLI for command API to work properly
self._cli = Cli(self._conan_api)
self._cli.add_commands()
self._ensure_default_profiles()
self._cmake_binary: str | None = None
self._logger = getLogger('cppython.conan')
def _capture_conan_call(self, args: list[str]) -> None:
"""Run a Conan CLI command while capturing stdout/stderr to the logger.
Args:
args: Command arguments to pass to ``conan_api.command.run``.
Raises:
Exception: Re-raises any exception from the Conan API after logging.
"""
stdout_capture = io.StringIO()
stderr_capture = io.StringIO()
try:
with contextlib.redirect_stdout(stdout_capture), contextlib.redirect_stderr(stderr_capture):
self._conan_api.command.run(args)
except Exception:
# Log captured output before re-raising
captured_out = stdout_capture.getvalue()
captured_err = stderr_capture.getvalue()
if captured_out:
for line in captured_out.splitlines():
self._logger.error('%s', line)
if captured_err:
for line in captured_err.splitlines():
self._logger.error('%s', line)
raise
else:
captured_out = stdout_capture.getvalue()
captured_err = stderr_capture.getvalue()
if captured_out:
for line in captured_out.splitlines():
self._logger.debug('%s', line)
if captured_err:
for line in captured_err.splitlines():
self._logger.debug('%s', line)
@staticmethod
def features(directory: Path) -> SupportedFeatures:
"""Queries conan support
Args:
directory: The directory to query
Returns:
Supported features - `SupportedProviderFeatures`. Cast to this type to help us avoid generic typing
"""
return SupportedProviderFeatures()
@staticmethod
def information() -> Information:
"""Returns plugin information
Returns:
Plugin information
"""
return Information()
def _install_dependencies(self, *, update: bool = False, groups: list[str] | None = None) -> None:
"""Install/update dependencies using Conan CLI.
Args:
update: If True, check remotes for newer versions/revisions and install those.
If False, use cached versions when available.
groups: Optional list of dependency group names to include
"""
operation = 'update' if update else 'install'
logger = getLogger('cppython.conan')
try:
# Setup environment and generate conanfile
conanfile_path = self._prepare_installation(groups=groups)
except Exception as e:
raise ProviderInstallationError('conan', f'Failed to prepare {operation} environment: {e}', e) from e
try:
build_types = self.data.build_types
for build_type in build_types:
logger.info('Installing dependencies for build type: %s', build_type)
self._run_conan_install(conanfile_path, update, build_type, logger)
except Exception as e:
raise ProviderInstallationError('conan', f'Failed to install dependencies: {e}', e) from e
def _prepare_installation(self, groups: list[str] | None = None) -> Path:
"""Prepare the installation environment and generate conanfile.
Args:
groups: Optional list of dependency group names to include
Returns:
Path to conanfile.py
"""
# Resolve base dependencies
resolved_dependencies = [resolve_conan_dependency(req) for req in self.core_data.cppython_data.dependencies]
# Resolve only the requested dependency groups
resolved_dependency_groups = {}
if groups:
for group_name in groups:
if group_name in self.core_data.cppython_data.dependency_groups:
resolved_dependency_groups[group_name] = [
resolve_conan_dependency(req)
for req in self.core_data.cppython_data.dependency_groups[group_name]
]
generation_data = ConanfileGenerationData(
dependencies=resolved_dependencies,
dependency_groups=resolved_dependency_groups,
name=self.core_data.pep621_data.name,
version=self.core_data.pep621_data.version,
)
self.builder.generate_conanfile(
self.core_data.project_data.project_root,
generation_data,
)
# Ensure build directory exists
self.core_data.cppython_data.build_path.mkdir(parents=True, exist_ok=True)
# Setup paths
project_root = self.core_data.project_data.project_root
conanfile_path = project_root / 'conanfile.py'
if not conanfile_path.exists():
raise FileNotFoundError('Generated conanfile.py not found')
return conanfile_path
def _ensure_default_profiles(self) -> None:
"""Ensure default Conan profiles exist, creating them if necessary."""
try:
self._conan_api.profiles.get_default_host()
self._conan_api.profiles.get_default_build()
except Exception:
# If profiles don't exist, create them using profile detect
self._conan_api.command.run(['profile', 'detect'])
def _run_conan_install(self, conanfile_path: Path, update: bool, build_type: str, logger: Logger) -> None:
"""Run conan install command using Conan API with optional build type.
Args:
conanfile_path: Path to the conanfile.py
update: Whether to check for updates
build_type: Build type (Release, Debug, etc.) or None for default
logger: Logger instance
"""
# Build conan install command arguments
command_args = ['install', str(conanfile_path)]
# Use build_path as the output folder directly
output_folder = self.core_data.cppython_data.build_path
command_args.extend(['--output-folder', str(output_folder)])
# Add build missing flag
command_args.extend(['--build', 'missing'])
# Add update flag if needed
if update:
command_args.append('--update')
# Add build type setting if specified
if build_type:
command_args.extend(['-s', f'build_type={build_type}'])
# Enable CMakeConfigDeps (the modern CMake config-mode generator)
command_args.extend(['-c', 'tools.cmake.cmakedeps:new=will_break_next'])
# Enable 'import std;' support by providing the experimental UUID in the toolchain
# The UUID must be in the toolchain file (before try_compile block) so compiler
# detection can create __CMAKE::CXX23 for projects using 'import std;'
command_args.extend(
[
'-c',
'tools.cmake.cmaketoolchain:extra_variables={'
"'CMAKE_EXPERIMENTAL_CXX_IMPORT_STD': 'd0edc3af-4c50-42ea-a356-e2862fe7a444'"
'}',
]
)
# Add cmake binary configuration if specified
if self._cmake_binary:
# Quote the path if it contains spaces
cmake_path = f'"{self._cmake_binary}"' if ' ' in self._cmake_binary else self._cmake_binary
command_args.extend(['-c', f'tools.cmake:cmake_program={cmake_path}'])
try:
# Use reusable Conan API instance instead of subprocess
# Change to project directory since Conan API might not handle cwd like subprocess
original_cwd = os.getcwd()
try:
os.chdir(str(self.core_data.project_data.project_root))
self._capture_conan_call(command_args)
finally:
os.chdir(original_cwd)
except Exception as e:
error_msg = str(e)
logger.error('Conan install failed: %s', error_msg, exc_info=True)
raise ProviderInstallationError('conan', error_msg, e) from e
def verify_installed(self) -> None:
"""Verify that Conan-generated artifacts exist on disk.
Checks for the toolchain/native files that ``conan install`` produces
in the generators output directory.
Raises:
InstallationVerificationError: If expected artifacts are missing
"""
generators_path = self.core_data.cppython_data.build_path / 'generators'
missing: list[str] = []
if not generators_path.is_dir():
missing.append(f'generators directory ({generators_path})')
else:
# Check for at least one of the expected toolchain files
cmake_toolchain = generators_path / 'conan_toolchain.cmake'
meson_native = generators_path / 'conan_meson_native.ini'
if not cmake_toolchain.exists() and not meson_native.exists():
missing.append(
f'toolchain files in {generators_path} (expected conan_toolchain.cmake or conan_meson_native.ini)'
)
if missing:
raise InstallationVerificationError('conan', missing)
def install(self, groups: list[str] | None = None) -> None:
"""Installs the provider
Args:
groups: Optional list of dependency group names to install
"""
self._install_dependencies(update=False, groups=groups)
def update(self, groups: list[str] | None = None) -> None:
"""Updates the provider
Args:
groups: Optional list of dependency group names to update
"""
self._install_dependencies(update=True, groups=groups)
@staticmethod
def supported_sync_type(sync_type: type[SyncData]) -> bool:
"""Checks if the given sync type is supported by the Conan provider.
Args:
sync_type: The type of synchronization data to check.
Returns:
True if the sync type is supported, False otherwise.
"""
return sync_type in CMakeGenerator.sync_types() or sync_type in MesonGenerator.sync_types()
def sync_data(self, consumer: SyncConsumer) -> SyncData:
"""Generates synchronization data for the given consumer.
Args:
consumer: The input consumer for which synchronization data is generated.
Returns:
The synchronization data object.
Raises:
NotSupportedError: If the consumer's sync type is not supported.
"""
for sync_type in consumer.sync_types():
if sync_type == CMakeSyncData:
return self._sync_with_cmake(consumer)
if sync_type == MesonSyncData:
return self._create_meson_sync_data()
raise NotSupportedError(f'Unsupported sync types: {consumer.sync_types()}')
def _sync_with_cmake(self, consumer: SyncConsumer) -> CMakeSyncData:
"""Synchronize with CMake generator and create sync data.
Args:
consumer: The CMake generator consumer
Returns:
CMakeSyncData configured for Conan integration
"""
# Extract cmake_binary from CMakeGenerator if available
# The cmake_binary is already validated and resolved during CMake data resolution
if isinstance(consumer, CMakeGenerator) and consumer.data.cmake_binary:
self._cmake_binary = str(consumer.data.cmake_binary.resolve())
return self._create_cmake_sync_data()
def _create_cmake_sync_data(self) -> CMakeSyncData:
"""Creates CMake synchronization data with Conan toolchain configuration.
Returns:
CMakeSyncData configured for Conan integration
"""
# The generated conanfile uses explicit layout (self.folders.generators = "generators")
# Combined with --output-folder=build_path, generators are always at build_path/generators/
conan_toolchain_path = self.core_data.cppython_data.build_path / 'generators' / 'conan_toolchain.cmake'
return CMakeSyncData(
provider_name=TypeName('conan'),
toolchain_file=conan_toolchain_path,
)
def _create_meson_sync_data(self) -> MesonSyncData:
"""Creates Meson synchronization data with Conan toolchain configuration.
Conan's MesonToolchain generator produces ``conan_meson_native.ini``
and ``conan_meson_cross.ini`` files in the generators directory.
Returns:
MesonSyncData configured for Conan integration
"""
generators_path = self.core_data.cppython_data.build_path / 'generators'
native_file = generators_path / 'conan_meson_native.ini'
cross_file = generators_path / 'conan_meson_cross.ini'
return MesonSyncData(
provider_name=TypeName('conan'),
native_file=native_file if native_file.exists() else None,
cross_file=cross_file if cross_file.exists() else None,
)
@classmethod
async def download_tooling(cls, directory: Path) -> None:
"""Download external tooling required by the Conan provider.
Since we're using CMakeToolchain generator instead of cmake-conan provider,
no external tooling needs to be downloaded.
"""
# No external tooling required when using CMakeToolchain
pass
def publish(self) -> None:
"""Publishes the package using conan create workflow.
Creates packages for all configured build types (e.g., Release, Debug)
to support both single-config and multi-config generators.
"""
project_root = self.core_data.project_data.project_root
conanfile_path = project_root / 'conanfile.py'
logger = getLogger('cppython.conan')
if not conanfile_path.exists():
raise FileNotFoundError(f'conanfile.py not found at {conanfile_path}')
try:
# Create packages for each configured build type
build_types = self.data.build_types
for build_type in build_types:
logger.info('Creating package for build type: %s', build_type)
self._run_conan_create(conanfile_path, build_type, logger)
# Upload once after all configurations are built
if not self.data.skip_upload:
self._upload_package(logger)
except Exception as e:
error_msg = str(e)
logger.error('Conan create failed: %s', error_msg, exc_info=True)
raise ProviderInstallationError('conan', error_msg, e) from e
def _run_conan_create(self, conanfile_path: Path, build_type: str, logger: Logger) -> None:
"""Run conan create command for a specific build type.
Args:
conanfile_path: Path to the conanfile.py
build_type: Build type (Release, Debug, etc.)
logger: Logger instance
"""
# Build conan create command arguments
command_args = ['create', str(conanfile_path)]
# Add build mode (build everything for publishing)
command_args.extend(['--build', 'missing'])
# Skip test dependencies during publishing
command_args.extend(['-c', 'tools.graph:skip_test=True'])
command_args.extend(['-c', 'tools.build:skip_test=True'])
# Enable CMakeConfigDeps (the modern CMake config-mode generator)
command_args.extend(['-c', 'tools.cmake.cmakedeps:new=will_break_next'])
# Force Ninja Multi-Config generator for C++ module support
# The Visual Studio generator does not support BMI-only compilation
# needed for consuming C++ modules across package boundaries
command_args.extend(['-c', 'tools.cmake.cmaketoolchain:generator=Ninja Multi-Config'])
# Enable 'import std;' support in the CMake toolchain
# CMAKE_EXPERIMENTAL_CXX_IMPORT_STD must be in the toolchain file (before
# the try_compile block) so compiler detection can create __CMAKE::CXX23.
# Note: CMAKE_CXX_MODULE_STD must NOT be in the toolchain because it would
# cause ABI detection try_compile to fail (chicken-and-egg with __CMAKE::CXX23).
# The UUID is specific to the CMake version and will need updating
# when the CMake version changes until import std graduates from experimental.
command_args.extend(
[
'-c',
'tools.cmake.cmaketoolchain:extra_variables={'
"'CMAKE_EXPERIMENTAL_CXX_IMPORT_STD': 'd0edc3af-4c50-42ea-a356-e2862fe7a444'"
'}',
]
)
# Add build type setting
command_args.extend(['-s', f'build_type={build_type}'])
# Add cmake binary configuration if specified
if self._cmake_binary:
# Quote the path if it contains spaces
cmake_path = f'"{self._cmake_binary}"' if ' ' in self._cmake_binary else self._cmake_binary
command_args.extend(['-c', f'tools.cmake:cmake_program={cmake_path}'])
# Run conan create using reusable Conan API instance
# Change to project directory since Conan API might not handle cwd like subprocess
original_cwd = os.getcwd()
try:
os.chdir(str(self.core_data.project_data.project_root))
self._capture_conan_call(command_args)
finally:
os.chdir(original_cwd)
def _upload_package(self, logger) -> None:
"""Upload the package to configured remotes using Conan API."""
# If no remotes configured, upload to all remotes
if not self.data.remotes:
# Upload to all available remotes
command_args = ['upload', '*', '--all', '--confirm']
else:
# Upload only to specified remotes
for remote in self.data.remotes:
command_args = ['upload', '*', '--remote', remote, '--all', '--confirm']
# Log the command being executed
logger.info('Executing conan upload command: conan %s', ' '.join(command_args))
try:
self._capture_conan_call(command_args)
except Exception as e:
error_msg = str(e)
logger.error('Conan upload failed for remote %s: %s', remote, error_msg, exc_info=True)
raise ProviderInstallationError('conan', f'Upload to {remote} failed: {error_msg}', e) from e
return
# Log the command for uploading to all remotes
logger.info('Executing conan upload command: conan %s', ' '.join(command_args))
try:
self._capture_conan_call(command_args)
except Exception as e:
error_msg = str(e)
logger.error('Conan upload failed: %s', error_msg, exc_info=True)
raise ProviderInstallationError('conan', error_msg, e) from e
def plugin_info(self) -> PluginReport:
"""Return a report describing the Conan provider's configuration, managed files, and templates.
Returns:
A :class:`PluginReport` with Conan-specific details.
"""
project_root = self.core_data.project_data.project_root
template_content = Builder._conanfile_content(
self.core_data.pep621_data.name,
self.core_data.pep621_data.version,
)
return PluginReport(
configuration={
'build_types': self.data.build_types,
'remotes': self.data.remotes,
'profile_dir': str(self.data.profile_dir),
'skip_upload': self.data.skip_upload,
},
managed_files=[project_root / 'conanfile_base.py'],
template_files={'conanfile.py': template_content},
)