This document describes the CMake File API integration implemented for the Bazel Gazelle foreign-cc plugin.
The CMake File API integration replaces regex-based parsing of CMakeLists.txt files with a more robust and accurate approach using CMake's built-in File API. This provides better dependency resolution, target information, and build configuration details.
-
cmake_api.go - Complete CMake File API implementation
- JSON structures matching CMake File API v1 schema
- API query generation and response parsing
- Target, source, and dependency extraction
-
generate.go - Integration with Gazelle generation
- Primary File API usage with fallback to regex parsing
- Enhanced target processing and rule generation
-
resolve.go - Improved dependency resolution
- Better cross-target dependency handling using File API data
- Accurate Target Parsing: Extracts executables, libraries (static/shared), and their properties
- Source File Detection: Identifies C/C++ source files, headers, and their relationships
- Dependency Resolution: Maps target dependencies and linked libraries
- Include Directory Handling: Processes both global and target-specific include paths
- Subdirectory Support: Handles complex projects with multiple CMakeLists.txt files
- Error Handling: Graceful fallback to regex parsing when File API fails
The integration is automatically used when generating Bazel rules for CMake projects:
# Generate rules for a CMake project
bazel run //:gazelle
# Or use the standalone test program
bazel run //gazelle:gazelle-foreign-cc -- --source_dir=/path/to/cmake/projectThe implementation creates a File API query requesting:
- Codemodel information (targets, sources, dependencies)
- Cache variables
- CMake configuration details
Parses the JSON response to extract:
- Target definitions (name, type, sources)
- Compile information (include directories, definitions)
- Link information (libraries, dependencies)
- Source file groups and properties
- Executables: Converted to
cc_binaryrules - Static Libraries: Converted to
cc_libraryrules - Shared Libraries: Converted to
cc_libraryrules with appropriate linkage - Interface Libraries: Header-only libraries
The implementation includes comprehensive tests:
- Simple Project: Basic executable and library
- Complex Project: Multiple targets, subdirectories, dependencies
- Invalid Project: Tests fallback behavior
# Run all tests
./test_cmake_api.sh
# Test specific project
cd testdata/simple_cc_project
bazel run //gazelle:gazelle-foreign-ccSuccessfully parsed X targets using CMake File API:
Target: app (type: executable)
Sources: [main.cc]
Headers: []
Include dirs: []
Linked libs: [my_lib]
Target: my_lib (type: library)
Sources: [lib.cc]
Headers: [lib.h]
Include dirs: []
Linked libs: []
When CMake configuration fails (invalid syntax, missing dependencies), the system:
- Logs the CMake error
- Falls back to regex-based parsing
- Continues with best-effort rule generation
If CMake File API is not available (older CMake versions):
- Automatically detects the limitation
- Uses regex parsing as primary method
- Logs the fallback for debugging
- Accuracy: No parsing ambiguities or edge cases
- Completeness: Access to all CMake target information
- Maintainability: No complex regex patterns to maintain
- Robustness: Handles complex CMake constructs correctly
- Future-proof: Leverages CMake's official API
- CMake Version: Requires CMake 3.14+ for full File API support
- Configuration: Requires successful CMake configuration
- Build Directory: Creates temporary build directories for API queries
gazelle/
├── cmake_api.go # CMake File API implementation
├── generate.go # Rule generation with File API integration
├── resolve.go # Dependency resolution
├── main.go # Test program
└── BUILD.bazel # Build configuration
testdata/
├── simple_cc_project/ # Basic test case
├── complex_cc_project/ # Advanced test case
└── invalid_cmake_project/ # Error handling test
- Caching: Cache File API responses for performance
- Incremental Updates: Only re-query when CMakeLists.txt changes
- Advanced Features: Support for custom properties, generators
- Integration: Better integration with Bazel workspace rules