Improved build system generator for Python C/C++/Fortran/Cython extensions
—
Exception classes for error handling during CMake configuration, building, and installation processes. Provides specific error types for different failure scenarios in the scikit-build workflow.
Base exception class for all scikit-build related errors, providing a common interface for error handling in the build process.
class SKBuildError(RuntimeError):
"""
Base exception for all scikit-build errors.
Raised when an error occurs during project configuration, building,
or installation that is specific to scikit-build operations.
Inherits from RuntimeError to maintain compatibility with standard
Python exception handling patterns.
"""Exception for errors related to file installation and deployment during the build process.
class SKBuildInvalidFileInstallationError(SKBuildError):
"""
Exception raised when a file is being installed into an invalid location.
This error occurs when CMake or the build process attempts to install
files to locations that are not permitted or expected, such as:
- Installing outside the designated installation directory
- Overwriting system files
- Installing to read-only locations
- Path traversal attempts in installation paths
Inherits from SKBuildError.
"""Exception for errors related to CMake generator detection and selection.
class SKBuildGeneratorNotFoundError(SKBuildError):
"""
Exception raised when no suitable CMake generator is found for the platform.
This error occurs when scikit-build cannot find or select an appropriate
CMake generator for the current platform and build environment. Common
scenarios include:
- No compatible build tools installed (Make, Ninja, Visual Studio, etc.)
- Requested generator not available on the platform
- Generator compatibility test failures
- Missing dependencies for the selected generator
Inherits from SKBuildError.
"""from skbuild import setup
from skbuild.exceptions import SKBuildError, SKBuildGeneratorNotFoundError
try:
setup(
name="my-extension",
cmake_source_dir="src",
cmake_args=["-G", "Ninja"], # Force Ninja generator
)
except SKBuildGeneratorNotFoundError as e:
print(f"Generator error: {e}")
print("Please install Ninja build system or use a different generator")
sys.exit(1)
except SKBuildError as e:
print(f"Build error: {e}")
sys.exit(1)from skbuild.exceptions import (
SKBuildError,
SKBuildInvalidFileInstallationError,
SKBuildGeneratorNotFoundError
)
from skbuild.cmaker import CMaker
try:
cmaker = CMaker()
cmaker.configure(generator_name="Unix Makefiles")
cmaker.make()
installed_files = cmaker.install()
except SKBuildGeneratorNotFoundError:
print("No suitable build system found.")
print("Please install make, ninja, or other build tools.")
except SKBuildInvalidFileInstallationError as e:
print(f"Invalid file installation: {e}")
print("Check CMake install targets and file permissions.")
except SKBuildError as e:
print(f"General scikit-build error: {e}")
print("Check CMakeLists.txt and build configuration.")import traceback
from skbuild import setup
from skbuild.exceptions import SKBuildError
try:
setup(
name="problematic-extension",
cmake_source_dir="src",
cmake_args=[
"-DCMAKE_BUILD_TYPE=Debug",
"-DCMAKE_VERBOSE_MAKEFILE=ON" # Enable verbose output for debugging
]
)
except SKBuildError as e:
print(f"Error during build: {e}")
print("\nFull traceback:")
traceback.print_exc()
# Additional debugging information
print("\nDebugging tips:")
print("1. Check that CMakeLists.txt exists in the source directory")
print("2. Verify CMake version compatibility")
print("3. Check for missing dependencies or build tools")
print("4. Review CMake error messages in build output")from skbuild.exceptions import SKBuildError
from skbuild.constants import get_cmake_version, CMAKE_DEFAULT_EXECUTABLE
from packaging.version import Version
import sys
def safe_setup(**kwargs):
"""Setup with comprehensive error handling."""
try:
# Check CMake version before attempting build
cmake_version = get_cmake_version(CMAKE_DEFAULT_EXECUTABLE)
min_version = Version("3.12.0")
if cmake_version < min_version:
raise SKBuildError(
f"CMake {min_version} or later required, found {cmake_version}"
)
from skbuild import setup
return setup(**kwargs)
except FileNotFoundError:
print("Error: CMake not found. Please install CMake.")
sys.exit(1)
except SKBuildError as e:
print(f"Build failed: {e}")
# Provide helpful error messages based on error content
error_msg = str(e).lower()
if "generator" in error_msg:
print("Tip: Try installing build tools (make, ninja, visual studio)")
elif "cmake" in error_msg and "version" in error_msg:
print("Tip: Update CMake to a newer version")
elif "permission" in error_msg:
print("Tip: Check file permissions and installation directories")
sys.exit(1)
# Usage
safe_setup(
name="my-package",
cmake_source_dir="native",
packages=["my_package"]
)from skbuild.exceptions import SKBuildError
class CustomBuildError(SKBuildError):
"""Custom exception for project-specific build errors."""
pass
def validate_build_environment():
"""Validate build environment before setup."""
import os
import shutil
# Check for required tools
if not shutil.which("cmake"):
raise CustomBuildError("CMake not found in PATH")
if not os.path.exists("CMakeLists.txt"):
raise CustomBuildError("CMakeLists.txt not found in project root")
# Check for required dependencies
required_libs = ["libssl-dev", "libffi-dev"] # Example
# ... dependency checking logic ...
try:
validate_build_environment()
from skbuild import setup
setup(name="validated-package")
except CustomBuildError as e:
print(f"Environment validation failed: {e}")
sys.exit(1)
except SKBuildError as e:
print(f"Build error: {e}")
sys.exit(1)Install with Tessl CLI
npx tessl i tessl/pypi-scikit-build