diff --git a/cuda_bindings/README.md b/cuda_bindings/README.md index b79b0febff..2a18f5a2df 100644 --- a/cuda_bindings/README.md +++ b/cuda_bindings/README.md @@ -10,6 +10,14 @@ Please refer to the [Installation page](https://nvidia.github.io/cuda-python/cud This subpackage adheres to the developing practices described in the parent metapackage [CONTRIBUTING.md](https://github.com/NVIDIA/cuda-python/blob/main/CONTRIBUTING.md). +## Debugging + +Pass the `pip` / `uv` configuration option `-C="debug=True"` or +`--config-settings="debug=True"` to explicitly to build debuggable binaries. +Debuggable binaries are built by default for editable builds. + +Debuggable builds are not supported on Windows. + ## Testing Testing dependencies can be installed using the `[test]` optional dependency identifier. For example, `pip install -v -e .[test]`. diff --git a/cuda_bindings/build_hooks.py b/cuda_bindings/build_hooks.py index b08b0d24d8..ce4745f7a0 100644 --- a/cuda_bindings/build_hooks.py +++ b/cuda_bindings/build_hooks.py @@ -311,7 +311,7 @@ def _prep_extensions(sources, libraries, include_dirs, library_dirs, extra_compi # Main build function -def _build_cuda_bindings(strip=False): +def _build_cuda_bindings(debug=False): """Build all cuda-bindings extensions. All CUDA-dependent logic (header parsing, code generation, cythonization) @@ -383,21 +383,23 @@ def _build_cuda_bindings(strip=False): extra_compile_args = [] extra_link_args = [] extra_cythonize_kwargs = {} - if sys.platform != "win32": + if sys.platform == "win32": + if debug: + raise RuntimeError("Debuggable builds are not supported on Windows.") + else: extra_compile_args += [ "-std=c++14", "-fpermissive", "-Wno-deprecated-declarations", "-fno-var-tracking-assignments", ] - if "--debug" in sys.argv: + if debug: extra_cythonize_kwargs["gdb_debug"] = True extra_compile_args += ["-g", "-O0"] extra_compile_args += ["-D _GLIBCXX_ASSERTIONS"] else: extra_compile_args += ["-O3"] - if strip and sys.platform == "linux": - extra_link_args += ["-Wl,--strip-all"] + extra_link_args += ["-Wl,--strip-all"] if compile_for_coverage: # CYTHON_TRACE_NOGIL indicates to trace nogil functions. It is not # related to free-threading builds. @@ -457,10 +459,13 @@ def _cleanup_dst_files(): def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): - _build_cuda_bindings(strip=True) + debug = config_settings.get("debug", False) if config_settings else False + _build_cuda_bindings(debug=debug) return _build_meta.build_wheel(wheel_directory, config_settings, metadata_directory) def build_editable(wheel_directory, config_settings=None, metadata_directory=None): - _build_cuda_bindings(strip=False) + debug_default = sys.platform != "win32" # Debug builds not supported on Windows + debug = config_settings.get("debug", debug_default) if config_settings else debug_default + _build_cuda_bindings(debug=debug) return _build_meta.build_editable(wheel_directory, config_settings, metadata_directory) diff --git a/cuda_core/README.md b/cuda_core/README.md index 7ea4196601..7959dfb00b 100644 --- a/cuda_core/README.md +++ b/cuda_core/README.md @@ -10,6 +10,14 @@ Please refer to the [Installation page](https://nvidia.github.io/cuda-python/cud This subpackage adheres to the developing practices described in the parent metapackage [CONTRIBUTING.md](https://github.com/NVIDIA/cuda-python/blob/main/CONTRIBUTING.md). +## Debugging + +Pass the `pip` / `uv` configuration option `-C="debug=True"` or +`--config-settings="debug=True"` to explicitly to build debuggable binaries. +Debuggable binaries are built by default for editable builds. + +Debuggable builds are not supported on Windows. + ## Testing To run these tests: diff --git a/cuda_core/build_hooks.py b/cuda_core/build_hooks.py index 491879c2ab..16d393344b 100644 --- a/cuda_core/build_hooks.py +++ b/cuda_core/build_hooks.py @@ -119,7 +119,7 @@ def _determine_cuda_major_version() -> str: _extensions = None -def _build_cuda_core(): +def _build_cuda_core(debug=False): # Customizing the build hooks is needed because we must defer cythonization until cuda-bindings, # now a required build-time dependency that's dynamically installed via the other hook below, # is installed. Otherwise, cimport any cuda.bindings modules would fail! @@ -164,6 +164,19 @@ def get_sources(mod_name): all_include_dirs = [os.path.join(_get_cuda_path(), "include")] extra_compile_args = [] + extra_link_args = [] + extra_cythonize_kwargs = {} + if sys.platform == "win32": + if debug: + raise RuntimeError("Debuggable builds are not supported on Windows.") + else: + if debug: + extra_cythonize_kwargs["gdb_debug"] = True + extra_compile_args += ["-g", "-O0"] + extra_compile_args += ["-D _GLIBCXX_ASSERTIONS"] + else: + extra_compile_args += ["-O3"] + extra_link_args += ["-Wl,--strip-all"] if COMPILE_FOR_COVERAGE: # CYTHON_TRACE_NOGIL indicates to trace nogil functions. It is not # related to free-threading builds. @@ -180,6 +193,7 @@ def get_sources(mod_name): + all_include_dirs, language="c++", extra_compile_args=extra_compile_args, + extra_link_args=extra_link_args, ) for mod in module_names() ) @@ -197,6 +211,7 @@ def get_sources(mod_name): nthreads=nthreads, compiler_directives=compiler_directives, compile_time_env=compile_time_env, + **extra_cythonize_kwargs, ) return @@ -282,7 +297,9 @@ def _add_cython_include_paths_to_pth(wheel_path: str) -> None: def build_editable(wheel_directory, config_settings=None, metadata_directory=None): - _build_cuda_core() + debug_default = sys.platform != "win32" # Debug builds not supported on Windows + debug = config_settings.get("debug", debug_default) if config_settings else debug_default + _build_cuda_core(debug=debug) wheel_name = _build_meta.build_editable(wheel_directory, config_settings, metadata_directory) # Patch the .pth file to add Cython include paths @@ -293,7 +310,8 @@ def build_editable(wheel_directory, config_settings=None, metadata_directory=Non def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): - _build_cuda_core() + debug = config_settings.get("debug", False) if config_settings else False + _build_cuda_core(debug=debug) return _build_meta.build_wheel(wheel_directory, config_settings, metadata_directory)