Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/tools/test_generate_cmake_presets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json

from tools.generate_cmake_presets import generate_presets


def test_generate_presets_accepts_explicit_cuda_compiler(tmp_path):
output = tmp_path / "CMakeUserPresets.json"

generate_presets(
output_path=str(output),
force_overwrite=True,
cuda_compiler="/opt/maca/bin/cucc",
)

data = json.loads(output.read_text(encoding="utf-8"))
cache = data["configurePresets"][0]["cacheVariables"]
assert cache["CMAKE_CUDA_COMPILER"] == "/opt/maca/bin/cucc"


def test_generate_presets_resolves_compiler_command(monkeypatch, tmp_path):
output = tmp_path / "CMakeUserPresets.json"
compiler = tmp_path / "bin" / "cucc"
compiler.parent.mkdir()
compiler.write_text("", encoding="utf-8")

monkeypatch.setattr(
"tools.generate_cmake_presets.which",
lambda name: str(compiler) if name == "cucc" else None,
)

generate_presets(
output_path=str(output),
force_overwrite=True,
cuda_compiler="cucc",
)

data = json.loads(output.read_text(encoding="utf-8"))
cache = data["configurePresets"][0]["cacheVariables"]
assert cache["CMAKE_CUDA_COMPILER"] == str(compiler)
43 changes: 39 additions & 4 deletions tools/generate_cmake_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,32 @@ def get_cpu_cores():
return multiprocessing.cpu_count()


def generate_presets(output_path="CMakeUserPresets.json", force_overwrite=False):
def _resolve_compiler_path(compiler):
if not compiler:
return compiler
if os.path.isabs(compiler):
return compiler
resolved = which(compiler)
if resolved:
return os.path.abspath(resolved)
return os.path.abspath(compiler)


def generate_presets(
output_path="CMakeUserPresets.json",
force_overwrite=False,
cuda_compiler=None,
):
"""Generates the CMakeUserPresets.json file."""

print("Attempting to detect your system configuration...")

# Detect NVCC
nvcc_path = None
if CUDA_HOME:
nvcc_path = _resolve_compiler_path(cuda_compiler)
if nvcc_path:
print(f"Using CUDA-compatible compiler from argument: {nvcc_path}")

if not nvcc_path and CUDA_HOME:
prospective_path = os.path.join(CUDA_HOME, "bin", "nvcc")
if os.path.exists(prospective_path):
nvcc_path = prospective_path
Expand Down Expand Up @@ -176,6 +194,23 @@ def generate_presets(output_path="CMakeUserPresets.json", force_overwrite=False)
action="store_true",
help="Force overwrite existing CMakeUserPresets.json without prompting",
)
parser.add_argument(
"--output",
default="CMakeUserPresets.json",
help="Output path for the generated presets file",
)
parser.add_argument(
"--cuda-compiler",
help=(
"Path to the CUDA-compatible compiler. Use this in non-interactive "
"MACA containers when the compiler is exposed as cucc/mxcc rather "
"than nvcc."
),
)

args = parser.parse_args()
generate_presets(force_overwrite=args.force_overwrite)
generate_presets(
output_path=args.output,
force_overwrite=args.force_overwrite,
cuda_compiler=args.cuda_compiler,
)