xref: /openbmc/qemu/scripts/rust/rustc_args.py (revision 6fdc5bc1)
1*6fdc5bc1SManos Pitsidianakis#!/usr/bin/env python3
2*6fdc5bc1SManos Pitsidianakis
3*6fdc5bc1SManos Pitsidianakis"""Generate rustc arguments for meson rust builds.
4*6fdc5bc1SManos Pitsidianakis
5*6fdc5bc1SManos PitsidianakisThis program generates --cfg compile flags for the configuration headers passed
6*6fdc5bc1SManos Pitsidianakisas arguments.
7*6fdc5bc1SManos Pitsidianakis
8*6fdc5bc1SManos PitsidianakisCopyright (c) 2024 Linaro Ltd.
9*6fdc5bc1SManos Pitsidianakis
10*6fdc5bc1SManos PitsidianakisAuthors:
11*6fdc5bc1SManos Pitsidianakis Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
12*6fdc5bc1SManos Pitsidianakis
13*6fdc5bc1SManos PitsidianakisThis program is free software; you can redistribute it and/or modify
14*6fdc5bc1SManos Pitsidianakisit under the terms of the GNU General Public License as published by
15*6fdc5bc1SManos Pitsidianakisthe Free Software Foundation; either version 2 of the License, or
16*6fdc5bc1SManos Pitsidianakis(at your option) any later version.
17*6fdc5bc1SManos Pitsidianakis
18*6fdc5bc1SManos PitsidianakisThis program is distributed in the hope that it will be useful,
19*6fdc5bc1SManos Pitsidianakisbut WITHOUT ANY WARRANTY; without even the implied warranty of
20*6fdc5bc1SManos PitsidianakisMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*6fdc5bc1SManos PitsidianakisGNU General Public License for more details.
22*6fdc5bc1SManos Pitsidianakis
23*6fdc5bc1SManos PitsidianakisYou should have received a copy of the GNU General Public License
24*6fdc5bc1SManos Pitsidianakisalong with this program.  If not, see <http://www.gnu.org/licenses/>.
25*6fdc5bc1SManos Pitsidianakis"""
26*6fdc5bc1SManos Pitsidianakis
27*6fdc5bc1SManos Pitsidianakisimport argparse
28*6fdc5bc1SManos Pitsidianakisimport logging
29*6fdc5bc1SManos Pitsidianakis
30*6fdc5bc1SManos Pitsidianakisfrom typing import List
31*6fdc5bc1SManos Pitsidianakis
32*6fdc5bc1SManos Pitsidianakis
33*6fdc5bc1SManos Pitsidianakisdef generate_cfg_flags(header: str) -> List[str]:
34*6fdc5bc1SManos Pitsidianakis    """Converts defines from config[..].h headers to rustc --cfg flags."""
35*6fdc5bc1SManos Pitsidianakis
36*6fdc5bc1SManos Pitsidianakis    def cfg_name(name: str) -> str:
37*6fdc5bc1SManos Pitsidianakis        """Filter function for C #defines"""
38*6fdc5bc1SManos Pitsidianakis        if (
39*6fdc5bc1SManos Pitsidianakis            name.startswith("CONFIG_")
40*6fdc5bc1SManos Pitsidianakis            or name.startswith("TARGET_")
41*6fdc5bc1SManos Pitsidianakis            or name.startswith("HAVE_")
42*6fdc5bc1SManos Pitsidianakis        ):
43*6fdc5bc1SManos Pitsidianakis            return name
44*6fdc5bc1SManos Pitsidianakis        return ""
45*6fdc5bc1SManos Pitsidianakis
46*6fdc5bc1SManos Pitsidianakis    with open(header, encoding="utf-8") as cfg:
47*6fdc5bc1SManos Pitsidianakis        config = [l.split()[1:] for l in cfg if l.startswith("#define")]
48*6fdc5bc1SManos Pitsidianakis
49*6fdc5bc1SManos Pitsidianakis    cfg_list = []
50*6fdc5bc1SManos Pitsidianakis    for cfg in config:
51*6fdc5bc1SManos Pitsidianakis        name = cfg_name(cfg[0])
52*6fdc5bc1SManos Pitsidianakis        if not name:
53*6fdc5bc1SManos Pitsidianakis            continue
54*6fdc5bc1SManos Pitsidianakis        if len(cfg) >= 2 and cfg[1] != "1":
55*6fdc5bc1SManos Pitsidianakis            continue
56*6fdc5bc1SManos Pitsidianakis        cfg_list.append("--cfg")
57*6fdc5bc1SManos Pitsidianakis        cfg_list.append(name)
58*6fdc5bc1SManos Pitsidianakis    return cfg_list
59*6fdc5bc1SManos Pitsidianakis
60*6fdc5bc1SManos Pitsidianakis
61*6fdc5bc1SManos Pitsidianakisdef main() -> None:
62*6fdc5bc1SManos Pitsidianakis    # pylint: disable=missing-function-docstring
63*6fdc5bc1SManos Pitsidianakis    parser = argparse.ArgumentParser()
64*6fdc5bc1SManos Pitsidianakis    parser.add_argument("-v", "--verbose", action="store_true")
65*6fdc5bc1SManos Pitsidianakis    parser.add_argument(
66*6fdc5bc1SManos Pitsidianakis        "--config-headers",
67*6fdc5bc1SManos Pitsidianakis        metavar="CONFIG_HEADER",
68*6fdc5bc1SManos Pitsidianakis        action="append",
69*6fdc5bc1SManos Pitsidianakis        dest="config_headers",
70*6fdc5bc1SManos Pitsidianakis        help="paths to any configuration C headers (*.h files), if any",
71*6fdc5bc1SManos Pitsidianakis        required=False,
72*6fdc5bc1SManos Pitsidianakis        default=[],
73*6fdc5bc1SManos Pitsidianakis    )
74*6fdc5bc1SManos Pitsidianakis    args = parser.parse_args()
75*6fdc5bc1SManos Pitsidianakis    if args.verbose:
76*6fdc5bc1SManos Pitsidianakis        logging.basicConfig(level=logging.DEBUG)
77*6fdc5bc1SManos Pitsidianakis    logging.debug("args: %s", args)
78*6fdc5bc1SManos Pitsidianakis    for header in args.config_headers:
79*6fdc5bc1SManos Pitsidianakis        for tok in generate_cfg_flags(header):
80*6fdc5bc1SManos Pitsidianakis            print(tok)
81*6fdc5bc1SManos Pitsidianakis
82*6fdc5bc1SManos Pitsidianakis
83*6fdc5bc1SManos Pitsidianakisif __name__ == "__main__":
84*6fdc5bc1SManos Pitsidianakis    main()
85