From edce3896254b32ccebfa37a2bdc71563df0a40b6 Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Sun, 6 Aug 2023 13:41:29 -0500 Subject: [PATCH] 2.4.0 - Better handling for linker flags --- .github/workflows/python-package.yml | 2 +- README.md | 4 +- local/variables/package.yaml | 4 +- pyproject.toml | 2 +- tests/commands/test_native.py | 2 +- .../valid/scenarios/native/src/apps/c/app3.c | 1 + .../valid/scenarios/native/src/apps/c/app4.c | 1 + .../scenarios/native/src/apps/c/test_app3.c | 1 + .../scenarios/native/src/apps/c/test_app4.c | 1 + .../scenarios/native/src/common/sample2.c | 1 + yambs/__init__.py | 4 +- yambs/data/native.yaml | 6 ++- yambs/data/schemas/Native.yaml | 4 +- yambs/data/schemas/Toolchain.yaml | 4 -- yambs/data/schemas/Variant.yaml | 6 +++ yambs/data/schemas/toolchain_common.yaml | 6 +++ yambs/data/templates/native_rules.ninja.j2 | 3 +- yambs/data/templates/variant.ninja.j2 | 6 ++- yambs/environment/native.py | 5 ++- yambs/generate/variants.py | 38 ++++++++++++------- 20 files changed, 70 insertions(+), 31 deletions(-) diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 05ce111..e190953 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -75,7 +75,7 @@ jobs: - run: | mk python-release owner=vkottler \ - repo=yambs version=2.3.3 + repo=yambs version=2.4.0 if: | matrix.python-version == '3.11' && matrix.system == 'ubuntu-latest' diff --git a/README.md b/README.md index 84613f9..ffa1c7e 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ===================================== generator=datazen version=3.1.3 - hash=605f8c7be702e2c1acaa6ff990953e10 + hash=56e5498dfc3e0d2dbd23a5dfbfe11761 ===================================== --> -# yambs ([2.3.3](https://pypi.org/project/yambs/)) +# yambs ([2.4.0](https://pypi.org/project/yambs/)) [![python](https://img.shields.io/pypi/pyversions/yambs.svg)](https://pypi.org/project/yambs/) ![Build Status](https://github.com/vkottler/yambs/workflows/Python%20Package/badge.svg) diff --git a/local/variables/package.yaml b/local/variables/package.yaml index d2d5ea9..5c10cdb 100644 --- a/local/variables/package.yaml +++ b/local/variables/package.yaml @@ -1,5 +1,5 @@ --- major: 2 -minor: 3 -patch: 3 +minor: 4 +patch: 0 entry: mbs diff --git a/pyproject.toml b/pyproject.toml index d329eb9..6d9c6b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__" [project] name = "yambs" -version = "2.3.3" +version = "2.4.0" description = "Yet another meta build-system." readme = "README.md" requires-python = ">=3.11" diff --git a/tests/commands/test_native.py b/tests/commands/test_native.py index 99cd0b1..b22103a 100644 --- a/tests/commands/test_native.py +++ b/tests/commands/test_native.py @@ -33,4 +33,4 @@ def test_native_command_basic(): # Try to build (if we can). # Re-enable this when we fix the third party script. if platform == "linux" and which("ninja"): - run("ninja", check=True) + run(["ninja", "all"], check=True) diff --git a/tests/data/valid/scenarios/native/src/apps/c/app3.c b/tests/data/valid/scenarios/native/src/apps/c/app3.c index 7ec677c..d3a95af 100644 --- a/tests/data/valid/scenarios/native/src/apps/c/app3.c +++ b/tests/data/valid/scenarios/native/src/apps/c/app3.c @@ -14,6 +14,7 @@ int main(void) { a *= 2.0f; a /= 2.0f; } + (void)a; return 0; } diff --git a/tests/data/valid/scenarios/native/src/apps/c/app4.c b/tests/data/valid/scenarios/native/src/apps/c/app4.c index 1358be5..ced6fd9 100644 --- a/tests/data/valid/scenarios/native/src/apps/c/app4.c +++ b/tests/data/valid/scenarios/native/src/apps/c/app4.c @@ -14,6 +14,7 @@ int main(void) { a *= 2.0f; a /= 2.0f; } + (void)a; return 0; } diff --git a/tests/data/valid/scenarios/native/src/apps/c/test_app3.c b/tests/data/valid/scenarios/native/src/apps/c/test_app3.c index 7ec677c..d3a95af 100644 --- a/tests/data/valid/scenarios/native/src/apps/c/test_app3.c +++ b/tests/data/valid/scenarios/native/src/apps/c/test_app3.c @@ -14,6 +14,7 @@ int main(void) { a *= 2.0f; a /= 2.0f; } + (void)a; return 0; } diff --git a/tests/data/valid/scenarios/native/src/apps/c/test_app4.c b/tests/data/valid/scenarios/native/src/apps/c/test_app4.c index 1358be5..ced6fd9 100644 --- a/tests/data/valid/scenarios/native/src/apps/c/test_app4.c +++ b/tests/data/valid/scenarios/native/src/apps/c/test_app4.c @@ -14,6 +14,7 @@ int main(void) { a *= 2.0f; a /= 2.0f; } + (void)a; return 0; } diff --git a/tests/data/valid/scenarios/native/src/common/sample2.c b/tests/data/valid/scenarios/native/src/common/sample2.c index 4db4069..4d6b2c8 100644 --- a/tests/data/valid/scenarios/native/src/common/sample2.c +++ b/tests/data/valid/scenarios/native/src/common/sample2.c @@ -5,4 +5,5 @@ void sample2_method(void) { for (int i = 0; i < 2000; i++) { a *= 4; } + (void)a; } diff --git a/yambs/__init__.py b/yambs/__init__.py index 576642c..cd76271 100644 --- a/yambs/__init__.py +++ b/yambs/__init__.py @@ -1,7 +1,7 @@ # ===================================== # generator=datazen # version=3.1.3 -# hash=f04ef9d4d6f5ee04feb84ec77c850330 +# hash=97870f506d5d2f9b5b8e3527f76efa07 # ===================================== """ @@ -10,4 +10,4 @@ DESCRIPTION = "Yet another meta build-system." PKG_NAME = "yambs" -VERSION = "2.3.3" +VERSION = "2.4.0" diff --git a/yambs/data/native.yaml b/yambs/data/native.yaml index 32daeec..6302895 100644 --- a/yambs/data/native.yaml +++ b/yambs/data/native.yaml @@ -8,10 +8,13 @@ cflag_groups: # Instrument builds for coverage and other analysis. debug: [-Og, -g, --coverage] - opt: [-O2, -s] + opt: [-O2] asan: [-fsanitize=address] msan: [-fsanitize=memory] +ldflag_groups: + opt: [-s] + variants: debug: @@ -27,6 +30,7 @@ variants: clang-opt: cflag_groups: [opt] + ldflag_groups: [opt] cc: clang cxx: clang++ ld: clang++ diff --git a/yambs/data/schemas/Native.yaml b/yambs/data/schemas/Native.yaml index fafa44c..916d78f 100644 --- a/yambs/data/schemas/Native.yaml +++ b/yambs/data/schemas/Native.yaml @@ -16,7 +16,7 @@ properties: items: type: string - cflag_groups: + cflag_groups: &flag_groups type: object additionalProperties: false patternProperties: @@ -25,6 +25,8 @@ properties: items: type: string + ldflag_groups: *flag_groups + variants: type: object additionalProperties: false diff --git a/yambs/data/schemas/Toolchain.yaml b/yambs/data/schemas/Toolchain.yaml index 6f094f7..9379929 100644 --- a/yambs/data/schemas/Toolchain.yaml +++ b/yambs/data/schemas/Toolchain.yaml @@ -15,10 +15,6 @@ properties: type: string default: "" - suffix: - type: string - default: "" - root: type: string default: "./toolchains/$toolchain/out/bin" diff --git a/yambs/data/schemas/Variant.yaml b/yambs/data/schemas/Variant.yaml index 0ec90e2..c5e77df 100644 --- a/yambs/data/schemas/Variant.yaml +++ b/yambs/data/schemas/Variant.yaml @@ -13,6 +13,12 @@ properties: items: type: string + ldflag_groups: + type: array + default: [] + items: + type: string + targets: type: array default: [apps, lib] diff --git a/yambs/data/schemas/toolchain_common.yaml b/yambs/data/schemas/toolchain_common.yaml index 6504281..bc1e1d5 100644 --- a/yambs/data/schemas/toolchain_common.yaml +++ b/yambs/data/schemas/toolchain_common.yaml @@ -13,3 +13,9 @@ properties: default: [] items: type: string + + extra_ldflags: + type: array + default: [] + items: + type: string diff --git a/yambs/data/templates/native_rules.ninja.j2 b/yambs/data/templates/native_rules.ninja.j2 index 1f40560..1b3a6af 100644 --- a/yambs/data/templates/native_rules.ninja.j2 +++ b/yambs/data/templates/native_rules.ninja.j2 @@ -11,9 +11,10 @@ rule cxx command = $cxx -MD -MF $out.d $cflags -std=$cxx_standard -c $in -o $out {% if common_ldflags %} -ldflags ={% for flag in common_ldflags %} {{flag}}{% endfor %} +common_ldflags ={% for flag in common_ldflags %} {{flag}}{% endfor %} {% endif %} +ldflags = $common_ldflags $variant_ldflags rule link command = $ld $cflags -Wl,-Map=$out.map $in $ldflags -o $out diff --git a/yambs/data/templates/variant.ninja.j2 b/yambs/data/templates/variant.ninja.j2 index 7dafd40..b4c8261 100644 --- a/yambs/data/templates/variant.ninja.j2 +++ b/yambs/data/templates/variant.ninja.j2 @@ -6,12 +6,16 @@ cxx_standard = {{cxx_standard}} cc = {{cc}}{{suffix}} cxx = {{cxx}}{{suffix}} ld = {{ld}}{{suffix}} - {% if extra_cflags %} + variant_cflags ={% for flag in extra_cflags %} {{flag}}{% endfor %} +{% endif %} +{% if extra_ldflags %} +variant_ldflags ={% for flag in extra_ldflags %} {{flag}}{% endfor %} {% endif %} + include $include_dir/rules.ninja include $include_dir/sources.ninja include $include_dir/apps.ninja diff --git a/yambs/environment/native.py b/yambs/environment/native.py index 5477d9b..e834064 100644 --- a/yambs/environment/native.py +++ b/yambs/environment/native.py @@ -202,7 +202,10 @@ def generate(self, sources_only: bool = False) -> None: # Render templates. generate_variants( - self.jinja, self.config, self.config.data["cflag_groups"] + self.jinja, + self.config, + self.config.data["cflag_groups"], + self.config.data["ldflag_groups"], ) self.render(self.config.root, "build.ninja") for template in ["all", "rules"]: diff --git a/yambs/generate/variants.py b/yambs/generate/variants.py index c24a48e..11cd69a 100644 --- a/yambs/generate/variants.py +++ b/yambs/generate/variants.py @@ -13,47 +13,59 @@ from yambs.config.common import CommonConfig from yambs.generate.common import render_template +FlagGroups = Dict[str, List[str]] + @contextmanager def modified_variant_data( - name: str, data: Dict[str, Any], cflag_groups: Dict[str, List[str]] + name: str, + data: Dict[str, Any], + cflag_groups: FlagGroups, + ldflag_groups: FlagGroups, ) -> Iterator[None]: """ Ensure that cflag groups are processed, and that the variant has access to its name while rendering. """ - orig = [] - if "extra_cflags" in data: - orig = data["extra_cflags"] + orig_c = data["extra_cflags"] + orig_ld = data["extra_ldflags"] - # Process cflag groups. - flags = set(orig + cflag_groups.get(name, [])) - for group in data["cflag_groups"]: - flags |= set(cflag_groups[group]) + # Process cflag groups. + flags = set(orig_c + cflag_groups.get(name, [])) + for group in data["cflag_groups"]: + flags |= set(cflag_groups[group]) + data["extra_cflags"] = flags - data["extra_cflags"] = flags + # Process ldflag groups. + flags = set(orig_ld + ldflag_groups.get(name, [])) + for group in data["ldflag_groups"]: + flags |= set(ldflag_groups[group]) + data["extra_ldflags"] = flags + # Add other useful data. data["name"] = name yield + # Restore original data. del data["name"] - if "extra_cflags" in data: - data["extra_cflags"] = orig + data["extra_cflags"] = orig_c + data["extra_ldflags"] = orig_ld def generate( jinja: Environment, config: CommonConfig, - cflag_groups: Dict[str, List[str]], + cflag_groups: FlagGroups, + ldflag_groups: FlagGroups, ) -> None: """Generate variant-related ninja files.""" for name, data in config.data["variants"].items(): variants_root = config.ninja_root.joinpath("variants", name) variants_root.mkdir(parents=True, exist_ok=True) - with modified_variant_data(name, data, cflag_groups): + with modified_variant_data(name, data, cflag_groups, ldflag_groups): render_template( jinja, variants_root,