Skip to content

Commit 79def81

Browse files
committed
mir/passes: lower vcs_tag into a CustomTarget`
Which allows normal lowering code to proceed as normal. This is the most basic implementation possible, it doesn't check any VCS values, only the version set in the project() call for a fallback.
1 parent 115a565 commit 79def81

File tree

7 files changed

+101
-0
lines changed

7 files changed

+101
-0
lines changed

src/mir/passes/free_functions.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,53 @@ std::optional<Instruction> lower_add_arguments(const FunctionCall & func, const
585585
return AddArguments{std::move(mapping), func.name.substr(0, 10) == "add_global"};
586586
};
587587

588+
std::optional<Instruction> lower_vcs_tag(const FunctionCall & f, const State::Persistant & p) {
589+
if (!f.pos_args.empty()) {
590+
throw Util::Exceptions::InvalidArguments(
591+
"vcs_tag: does not take any positional arguments.");
592+
}
593+
if (f.kw_args.find("input") == f.kw_args.end()) {
594+
throw Util::Exceptions::InvalidArguments(
595+
"vcs_tag: missing required keyword argument input.");
596+
}
597+
if (f.kw_args.find("output") == f.kw_args.end()) {
598+
throw Util::Exceptions::InvalidArguments(
599+
"vcs_tag: missing required keyword argument output.");
600+
}
601+
if (f.kw_args.find("command") != f.kw_args.end()) {
602+
throw Util::Exceptions::MesonException(
603+
"Not implemented: vcs_tag 'command' keyword argument");
604+
}
605+
606+
Instruction input = src_to_file(f.kw_args.find("input")->second, p, f.source_dir);
607+
auto output = extract_keyword_argument<MIR::String>(f.kw_args, "output",
608+
f.name + ": 'output' must be a string")
609+
.value();
610+
// TODO: get version from project() call
611+
auto fallback = extract_keyword_argument<MIR::String>(f.kw_args, "fallback",
612+
f.name + ": 'fallback' must be a string")
613+
.value_or(MIR::String{p.project_version});
614+
auto replace_string =
615+
extract_keyword_argument<MIR::String>(f.kw_args, "replace_string",
616+
f.name + ": 'replace_string' must be a string")
617+
.value_or(MIR::String{"@VCS_TAG@"});
618+
619+
File outfile{output.value, f.source_dir, true, p.source_root, p.build_root};
620+
const auto & src = std::get<File>(*src_to_file(input, p, f.source_dir).obj_ptr);
621+
622+
std::vector<std::string> command{
623+
p.mesonpp,
624+
"vcs_tag",
625+
src.relative_to_build_dir(),
626+
outfile.relative_to_build_dir(),
627+
fallback.value,
628+
replace_string.value,
629+
};
630+
631+
return CustomTarget{
632+
outfile.name, {std::move(input)}, {std::move(outfile)}, command, f.source_dir};
633+
}
634+
588635
bool holds_reduced(const Instruction & obj);
589636

590637
bool holds_reduced_array(const Instruction & obj) {
@@ -663,6 +710,8 @@ std::optional<Instruction> lower_free_funcs_impl(const Instruction & obj,
663710
i = lower_build_target<StaticLibrary>(f, pstate);
664711
} else if (f.name == "declare_dependency") {
665712
i = lower_declare_dependency(f, pstate);
713+
} else if (f.name == "vcs_tag") {
714+
i = lower_vcs_tag(f, pstate);
666715
} else if (f.name == "test") {
667716
i = lower_test(f, pstate);
668717
} else if (f.name == "add_project_arguments") {

tests/dsl/13 vcs_tag/basic.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a 1.0.0 and 1.0.0
2+
1.0.0 @1.0.0 @VCS_TAG

tests/dsl/13 vcs_tag/basic.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a @VCS_TAG@ and @VCS_TAG@
2+
@VCS_TAG@ @@VCS_TAG@ @VCS_TAG

tests/dsl/13 vcs_tag/compare.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# SPDX-License-Identifier: Apache-2.0
3+
# Copyright © 2024 Intel Corporation
4+
5+
from __future__ import annotations
6+
import argparse
7+
import filecmp
8+
import sys
9+
import typing as T
10+
11+
if T.TYPE_CHECKING:
12+
class Arguments(T.Protocol):
13+
result: str
14+
expected: str
15+
16+
17+
def main() -> int:
18+
parser = argparse.ArgumentParser()
19+
parser.add_argument('result')
20+
parser.add_argument('expected')
21+
args: Arguments = parser.parse_args()
22+
23+
same = filecmp.cmp(args.result, args.expected)
24+
return 0 if same else 1
25+
26+
27+
if __name__ == "__main__":
28+
sys.exit(main())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is a "fallback version" and "fallback version"
2+
"fallback version" @"fallback version" @VCS_TAG

tests/dsl/13 vcs_tag/meson.build

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright © 2024 Intel Corporation
3+
4+
project(
5+
'vcs_tag test',
6+
version: '1.0.0',
7+
)
8+
9+
python = find_program('python')
10+
tester = files('compare.py')
11+
12+
# TODO: this will not work correctly whenever we implement support for VCs.
13+
x = vcs_tag(input: 'basic.in', output: 'basic.out')
14+
test('basic', python, args: [tester, x, files('basic.expected')])
15+
16+
x = vcs_tag(input: 'basic.in', output: 'fallback.out', fallback : '"fallback version"')
17+
test('fallback', python, args: [tester, x, files('fallback.expected')])

tests/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ test_cases = {
1717
'10 dependency methods',
1818
'11 test types',
1919
'12 add arguments',
20+
'13 vcs_tag',
2021
],
2122
}
2223

0 commit comments

Comments
 (0)