Created
November 2, 2025 20:21
-
-
Save kammce/c39481a9662d50437d6a354896bdab73 to your computer and use it in GitHub Desktop.
I made a script to enable module support not realizing that CMake already had the support and that I've been using Conan's CMake libraries incorrectly. I don't need this but thought I'd keep it around.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # | |
| # Copyright 2024 - 2025 Khalil Estell and the libhal contributors | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| from conan import ConanFile | |
| import json | |
| import os | |
| from pathlib import Path | |
| from typing import Optional | |
| def install_cxx_modules_json(conan: ConanFile, | |
| target_name: str, | |
| bmi_destination: str = "bmi", | |
| cmake_files_destination: Optional[str] = None): | |
| """ | |
| Reads CXXModules.json from build directory and creates a package version | |
| with paths updated to match CMake's install destinations. | |
| Args: | |
| conan: The ConanFile instance | |
| target_name: The CMake target name (e.g., "hal") | |
| bmi_destination: Where CMake installed BMI files | |
| (CXX_MODULES_BMI DESTINATION) | |
| cmake_files_destination: Destination relative to the conan package | |
| directory to place the updated | |
| `CXXModules.json`. | |
| Defaults to "." (conan/package_folder) | |
| """ | |
| # Default cmake_files_destination if not provided | |
| if cmake_files_destination is None: | |
| cmake_files_destination = "." | |
| # Find the CXXModules.json file in build directory | |
| build_modules_dir = Path(conan.build_folder) / \ | |
| "CMakeFiles" / f"{target_name}.dir" | |
| cxx_modules_json_path = build_modules_dir / "CXXModules.json" | |
| if not cxx_modules_json_path.exists(): | |
| conan.output.warn( | |
| f"CXXModules.json not found at {cxx_modules_json_path}") | |
| return | |
| conan.output.info( | |
| f"Processing CXXModules.json from {cxx_modules_json_path}") | |
| # Read the original JSON | |
| modules_data = json.loads(cxx_modules_json_path.read_text()) | |
| # Create package directory for the updated JSON | |
| package_cmake_dir = Path(conan.package_folder) / cmake_files_destination | |
| package_cmake_dir.mkdir(parents=True, exist_ok=True) | |
| # Update BMI paths in modules section | |
| if "modules" in modules_data: | |
| for module_name, module_info in modules_data["modules"].items(): | |
| if "bmi" in module_info: | |
| bmi_build_path = Path(module_info["bmi"]) | |
| # Update to package install location (absolute path) | |
| bmi_filename = bmi_build_path.name | |
| package_bmi_path = Path( | |
| conan.package_folder) / bmi_destination / bmi_filename | |
| module_info["bmi"] = str(package_bmi_path.absolute()) | |
| conan.output.debug( | |
| f"Updated '{module_name}' BMI path: {bmi_filename}") | |
| # Update references section with package-relative paths | |
| if "references" in modules_data: | |
| for module_name, ref_info in modules_data["references"].items(): | |
| if "path" in ref_info: | |
| # Extract just the filename from the original path | |
| original_path = Path(ref_info["path"]) | |
| bmi_filename = original_path.name | |
| package_bmi_path = Path( | |
| conan.package_folder) / bmi_destination / bmi_filename | |
| # Update to package-relative path | |
| ref_info["path"] = str(package_bmi_path.absolute()) | |
| conan.output.debug( | |
| f"Updated '{module_name}' reference path: {ref_info['path']}") | |
| # Write the updated JSON to package | |
| package_json_path = package_cmake_dir / "CXXModules.json" | |
| package_json_path.write_text(json.dumps(modules_data, indent=2)) | |
| conan.output.info( | |
| f"Created package CXXModules.json at {package_json_path}") | |
| class ConanModuleSupportConan(ConanFile): | |
| name = "conan_module_support" | |
| description = "Conan utilities for packaging and consuming C++20 modules with CMake" | |
| license = "Apache-2.0" | |
| topics = ("conan", "modules", "cmake", "cpp20") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment