mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-07-27 04:13:53 -06:00
rust: build: restrict --cfg generation to only required symbols
Parse the Cargo.toml file, looking for the unexpected_cfgs configuration. When generating --cfg options from the config-host.h file, only use those that are included in the configuration. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
f3a6e9bc47
commit
1de82059aa
2 changed files with 46 additions and 17 deletions
|
@ -1,5 +1,5 @@
|
||||||
_qemu_api_cfg = run_command(rustc_args,
|
_qemu_api_cfg = run_command(rustc_args,
|
||||||
'--config-headers', config_host_h,
|
'--config-headers', config_host_h, files('Cargo.toml'),
|
||||||
capture: true, check: true).stdout().strip().split()
|
capture: true, check: true).stdout().strip().split()
|
||||||
|
|
||||||
# _qemu_api_cfg += ['--cfg', 'feature="allocator"']
|
# _qemu_api_cfg += ['--cfg', 'feature="allocator"']
|
||||||
|
|
|
@ -26,30 +26,51 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Iterable, Mapping, Optional, Set
|
||||||
|
|
||||||
from typing import List
|
try:
|
||||||
|
import tomllib
|
||||||
|
except ImportError:
|
||||||
|
import tomli as tomllib
|
||||||
|
|
||||||
|
|
||||||
def generate_cfg_flags(header: str) -> List[str]:
|
class CargoTOML:
|
||||||
|
tomldata: Mapping[Any, Any]
|
||||||
|
check_cfg: Set[str]
|
||||||
|
|
||||||
|
def __init__(self, path: str):
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
self.tomldata = tomllib.load(f)
|
||||||
|
|
||||||
|
self.check_cfg = set(self.find_check_cfg())
|
||||||
|
|
||||||
|
def find_check_cfg(self) -> Iterable[str]:
|
||||||
|
toml_lints = self.lints
|
||||||
|
rust_lints = toml_lints.get("rust", {})
|
||||||
|
cfg_lint = rust_lints.get("unexpected_cfgs", {})
|
||||||
|
return cfg_lint.get("check-cfg", [])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def lints(self) -> Mapping[Any, Any]:
|
||||||
|
return self.get_table("lints")
|
||||||
|
|
||||||
|
def get_table(self, key: str) -> Mapping[Any, Any]:
|
||||||
|
table = self.tomldata.get(key, {})
|
||||||
|
|
||||||
|
return table
|
||||||
|
|
||||||
|
|
||||||
|
def generate_cfg_flags(header: str, cargo_toml: CargoTOML) -> Iterable[str]:
|
||||||
"""Converts defines from config[..].h headers to rustc --cfg flags."""
|
"""Converts defines from config[..].h headers to rustc --cfg flags."""
|
||||||
|
|
||||||
def cfg_name(name: str) -> str:
|
|
||||||
"""Filter function for C #defines"""
|
|
||||||
if (
|
|
||||||
name.startswith("CONFIG_")
|
|
||||||
or name.startswith("TARGET_")
|
|
||||||
or name.startswith("HAVE_")
|
|
||||||
):
|
|
||||||
return name
|
|
||||||
return ""
|
|
||||||
|
|
||||||
with open(header, encoding="utf-8") as cfg:
|
with open(header, encoding="utf-8") as cfg:
|
||||||
config = [l.split()[1:] for l in cfg if l.startswith("#define")]
|
config = [l.split()[1:] for l in cfg if l.startswith("#define")]
|
||||||
|
|
||||||
cfg_list = []
|
cfg_list = []
|
||||||
for cfg in config:
|
for cfg in config:
|
||||||
name = cfg_name(cfg[0])
|
name = cfg[0]
|
||||||
if not name:
|
if f'cfg({name})' not in cargo_toml.check_cfg:
|
||||||
continue
|
continue
|
||||||
if len(cfg) >= 2 and cfg[1] != "1":
|
if len(cfg) >= 2 and cfg[1] != "1":
|
||||||
continue
|
continue
|
||||||
|
@ -59,7 +80,6 @@ def generate_cfg_flags(header: str) -> List[str]:
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
# pylint: disable=missing-function-docstring
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-v", "--verbose", action="store_true")
|
parser.add_argument("-v", "--verbose", action="store_true")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
@ -71,12 +91,21 @@ def main() -> None:
|
||||||
required=False,
|
required=False,
|
||||||
default=[],
|
default=[],
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
metavar="TOML_FILE",
|
||||||
|
action="store",
|
||||||
|
dest="cargo_toml",
|
||||||
|
help="path to Cargo.toml file",
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
if args.verbose:
|
if args.verbose:
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logging.debug("args: %s", args)
|
logging.debug("args: %s", args)
|
||||||
|
|
||||||
|
cargo_toml = CargoTOML(args.cargo_toml)
|
||||||
|
|
||||||
for header in args.config_headers:
|
for header in args.config_headers:
|
||||||
for tok in generate_cfg_flags(header):
|
for tok in generate_cfg_flags(header, cargo_toml):
|
||||||
print(tok)
|
print(tok)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue