Skip to content

Commit

Permalink
Collect all conan package flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Lefkowitz committed Apr 27, 2024
1 parent 412af8a commit 297ea01
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ Parse the `SConscript_conandeps` if you are using `conan`:
```py
from miniscons import conan

env, includes = conan()
env = conan()
```

Add the builds with their specific warning flags and libs to include:

```py
from miniscons import Build, flags
from miniscons import Build, flags, packages
from walkmate import tree

build = Build(
"build",
tree("src", r"\.cpp$", ["main.cpp"]),
tree("src", r"(?<!\.spec)\.cpp$"),
flags("c++11", ["shadow"]),
)

tests = Build(
"tests",
tree("src", r"\.cpp$", ["main.cpp"]),
flags("c++11"),
["gtest"],
packages(["gtest"]),
)
```

Expand All @@ -97,6 +97,8 @@ from walkmate import tree

cppclean = Script("cppclean", ["."])

includes = tests.packages["CPPPATH"]

tidy = Script(
"clang-tidy",
[tree("src", r"\.(cpp)$"), "--", [f"-I{i}" for i in includes]],
Expand Down
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .build import Build
from .compiler import flags
from .containers import flatten
from .environment import conan
from .environment import conan, packages
from .flag import Flag
from .routine import Routine
from .script import Script
Expand Down
22 changes: 11 additions & 11 deletions src/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Build:

files: list[str] = field(default_factory=list)
flags: list[str] = field(default_factory=list)
libs: list[str] = field(default_factory=list)

packages: dict[str, list[str]] = field(default_factory=dict)

output: str = "dist"
shared: bool = False
Expand All @@ -21,24 +22,23 @@ def __repr__(self) -> str:
def target(self) -> str:
return os.path.join(self.output, self.name)

@property
def merge(self) -> dict[str, list[str]]:
packages = self.packages.copy()
packages["CXXFLAGS"] = packages.get("CXXFLAGS", []) + self.flags
return packages

def path(self, file: str) -> str:
root = os.path.splitext(os.path.normpath(file))[0]
return f"{root.replace('.', '-')}-[{self.name}]"

def nodes(self, env: Environment) -> list[str]:
return [
env.Object(self.path(file), file, CXXFLAGS=self.flags)
for file in self.files
]
return [env.Object(self.path(file), file, **self.merge) for file in self.files]

def register(self, env: Environment) -> None:
# TODO: Include the CPPPATHS and LIBPATHS too:
# TODO: Make this conan-agnostic too:
libs = env["LIBS"] + self.libs

if self.shared:
outputs = env.Library(self.target, self.nodes(env), LIBS=libs)
outputs = env.Library(self.target, self.nodes(env), **self.merge)
env.Alias(self.name, outputs[0])
else:
env.Program(self.target, self.nodes(env), LIBS=libs)
env.Program(self.target, self.nodes(env), **self.merge)
env.Alias(self.name, self.target)
47 changes: 30 additions & 17 deletions src/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,43 @@


def conan(
path: str = "SConscript_conandeps", defines: list[str] | None = None
defines: list[str] | None = None,
source: str = "SConscript_conandeps",
) -> tuple[Environment, list[str]]:
if defines is None:
defines = []

exported = SConscript(path)

conandeps = exported["conandeps"]
conandeps["CPPDEFINES"].extend(defines)

env = Environment(
**conandeps,
num_jobs=psutil.cpu_count(),
ENV={"PATH": os.getenv("PATH", "")},
CXXCOMSTR=emojize(":wrench: Compiling $TARGET"),
LINKCOMSTR=emojize(":link: Linking $TARGET"),
ENV={"PATH": os.getenv("PATH", "")},
num_jobs=psutil.cpu_count(),
)

# TODO: Create a separate function to provide all the include and lib paths
includes = [
include
for dependency in exported.values()
if isinstance(dependency, dict)
for include in dependency["CPPPATH"]
]
conandeps = SConscript(source)["conandeps"]
conandeps["CPPDEFINES"] += defines

env.MergeFlags(conandeps)
return env


def packages(
names: list[str],
libs: list[str] | None = None,
explicit: bool = False,
source: str = "SConscript_conandeps",
) -> dict[str, list[str]]:
if libs is None:
libs = names.copy()

if not explicit:
names.append("conandeps")

reduced = {"LIBS": libs}

for name, package in SConscript(source).items():
if name in names:
for flag, exported in package.items():
reduced[flag] = reduced.get(flag, []) + exported

return (env, includes)
return reduced
14 changes: 5 additions & 9 deletions src/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ class Tasks:

@property
def cli(self) -> str:
fields = "\n\n".join(
[
":".join(
[k, "".join([f"\n {i}" for i in v] if len(v) > 0 else "\n -")]
)
for k, v in self.__dict__.items()
]
)
sections = [
":".join([k, "".join([f"\n {i}" for i in v] if len(v) > 0 else "\n -")])
for k, v in self.__dict__.items()
]

return f"\n{fields}\n"
return "".join(["\n", "\n\n".join(sections), "\n"])

def dump(self) -> None:
sys.stdout.write(f"{self.cli}\n")
Expand Down
2 changes: 1 addition & 1 deletion test/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ def test_action():
clang_format = Script("clang-format", ["-i", ["main.cpp", "main.hpp"]])
assert clang_format.action == "clang-format -i main.cpp main.hpp"

cspell = Script("cspell", [".", "--dot"], ["npx"])
cspell = Script("cspell", [".", "--dot", "--gitignore"], ["npx"])
assert cspell.action == "npx cspell . --dot --gitignore"

0 comments on commit 297ea01

Please sign in to comment.