Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/aedifix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CMakeList,
CMakePath,
CMaker,
CMakeSemiColonList,
CMakeString,
)
from .main import basic_configure
Expand All @@ -28,6 +29,7 @@
"CMakeInt",
"CMakeList",
"CMakePath",
"CMakeSemiColonList",
"CMakeString",
"CMaker",
"ConfigArgument",
Expand Down
2 changes: 2 additions & 0 deletions src/aedifix/cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
CMakeInt,
CMakeList,
CMakePath,
CMakeSemiColonList,
CMakeString,
)
from .cmaker import CMaker
Expand All @@ -21,6 +22,7 @@
"CMakeInt",
"CMakeList",
"CMakePath",
"CMakeSemiColonList",
"CMakeString",
"CMaker",
)
12 changes: 10 additions & 2 deletions src/aedifix/cmake/cmake_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,22 @@ def _canonicalize_cb(self) -> tuple[bool, list[str] | None]:
val = [v for v in (str(x).strip() for x in val) if v]
return bool(val), val

def to_command_line(self, *, quote: bool = False) -> str:
def _cmake_list_to_command_line(self, *, quote: bool, sep: str) -> str:
if (val := self.value) is None:
val = []
val = " ".join(map(str, val))
val = sep.join(map(str, val))
if quote:
val = shlex_quote(val)
return f"{self.prefix}{self.name}:{self.type}={val}"

def to_command_line(self, *, quote: bool = False) -> str:
return self._cmake_list_to_command_line(quote=quote, sep=" ")


class CMakeSemiColonList(CMakeList):
def to_command_line(self, *, quote: bool = False) -> str:
return self._cmake_list_to_command_line(quote=quote, sep=";")


class CMakeBool(CMakeFlagBase):
def __init__(
Expand Down
12 changes: 10 additions & 2 deletions src/aedifix/packages/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from pathlib import Path
from typing import TYPE_CHECKING, Final

from ..cmake import CMAKE_VARIABLE, CMakeExecutable, CMakeList, CMakePath
from ..cmake import (
CMAKE_VARIABLE,
CMakeExecutable,
CMakeList,
CMakePath,
CMakeSemiColonList,
)
from ..package import Package
from ..util.argument_parser import ArgSpec, ConfigArgument

Expand Down Expand Up @@ -143,7 +149,9 @@ class CUDA(Package):
"'ampere' or 'hopper, blackwell'"
),
),
cmake_var=CMAKE_VARIABLE("CMAKE_CUDA_ARCHITECTURES", CMakeList),
cmake_var=CMAKE_VARIABLE(
"CMAKE_CUDA_ARCHITECTURES", CMakeSemiColonList
),
)

def configure(self) -> None:
Expand Down
85 changes: 85 additions & 0 deletions tests/cmake/test_cmake_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CMakeInt,
CMakeList,
CMakePath,
CMakeSemiColonList,
CMakeString,
)

Expand Down Expand Up @@ -106,6 +107,90 @@ def test_neq(self) -> None:
assert lhs != rhs_b


class TestCMakeSemiColonList:
def test_create(self) -> None:
var = CMakeSemiColonList("foo")
assert var.name == "foo"
assert var.value is None
assert var.prefix == "-D"
assert var.type == "STRING"

var = CMakeSemiColonList("foo", value=[1, 2, 3])
assert var.name == "foo"
assert var.value == [1, 2, 3]
assert var.prefix == "-D"
assert var.type == "STRING"

var = CMakeSemiColonList("foo", value=(1, 2, 3), prefix="bar")
assert var.name == "foo"
assert var.value == [1, 2, 3]
assert var.prefix == "bar"
assert var.type == "STRING"

var = CMakeSemiColonList("foo", value="foo;bar")
assert var.name == "foo"
assert var.value == ["foo", "bar"]
assert var.prefix == "-D"
assert var.type == "STRING"

def test_create_bad(self) -> None:
# looking for "<class 'int'>" here
with pytest.raises(TypeError, match=re.escape(rf"{int}")):
CMakeSemiColonList("foo", value=1) # type: ignore[arg-type]

def test_canonicalize(self) -> None:
var = CMakeSemiColonList("foo")
canon = var.canonicalize()
assert canon is None
assert id(canon) != id(var) # must be distinct
assert var.name == "foo"
assert var.value is None
assert var.prefix == "-D"
assert var.type == "STRING"

var.value = [4, 5, 6]
canon = var.canonicalize()
assert isinstance(canon, CMakeSemiColonList)
assert id(canon) != id(var) # must be distinct
assert canon.name == var.name
assert canon.value == list(map(str, var.value))
assert id(canon.value) != id(var.value) # must be distinct
assert canon.prefix == var.prefix
assert canon.type == var.type

@pytest.mark.parametrize(
"value", (None, [], ["1"], ["1", "2"], [34, 99, 999])
)
def test_to_command_line(self, value: list[str]) -> None:
val_copy = copy.deepcopy(value)
var = CMakeSemiColonList("foo", value=value)
cmd = var.to_command_line()
assert isinstance(cmd, str)
expected_str = "" if val_copy is None else ";".join(map(str, val_copy))
assert cmd == f"-Dfoo:STRING={expected_str}"
assert var.value == val_copy

def test_eq(self) -> None:
lhs = CMakeSemiColonList("foo", value=(1, 2, 3), prefix="bar")
rhs = CMakeSemiColonList("foo", value=(1, 2, 3), prefix="bar")
assert lhs == rhs

def test_neq(self) -> None:
lhs = CMakeSemiColonList("foo", value=(1, 2, 3), prefix="bar")

rhs = CMakeSemiColonList("bar", value=(1, 2, 3), prefix="bar")
assert lhs != rhs

rhs = CMakeSemiColonList("foo", value=(1, 2, 3, 4), prefix="bar")
assert lhs != rhs

rhs = CMakeSemiColonList("foo", value=(1, 2, 3), prefix="asdasd")
assert lhs != rhs

rhs_b = CMakeBool("foo", value=None, prefix="asdasd")
assert lhs != rhs_b
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd split these tests up, test_neq_name, test_neq_value, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #19 for it



class TestCMakeBool:
def test_create(self) -> None:
var = CMakeBool("foo")
Expand Down
Loading