11
11
12
12
from __future__ import annotations
13
13
import dataclasses
14
- import importlib
15
- import json
16
14
import os
17
- import shutil
18
15
import collections
19
16
import urllib .parse
20
17
import itertools
21
18
import typing as T
22
19
23
20
from . import builder , version , cfg
24
- from ..mesonlib import MesonException , Popen_safe , MachineChoice
21
+ from .toml import load_toml , TomlImplementationMissing
22
+ from ..mesonlib import MesonException , MachineChoice
25
23
from .. import coredata , mlog
26
24
from ..wrap .wrap import PackageDefinition
27
25
28
26
if T .TYPE_CHECKING :
29
- from types import ModuleType
30
-
31
27
from typing_extensions import Protocol , Self
32
28
33
29
from . import manifest
@@ -45,25 +41,6 @@ class DataclassInstance(Protocol):
45
41
manifest .FixedBuildTarget )
46
42
47
43
48
- # tomllib is present in python 3.11, before that it is a pypi module called tomli,
49
- # we try to import tomllib, then tomli,
50
- # TODO: add a fallback to toml2json?
51
- tomllib : T .Optional [ModuleType ] = None
52
- toml2json : T .Optional [str ] = None
53
- for t in ['tomllib' , 'tomli' ]:
54
- try :
55
- tomllib = importlib .import_module (t )
56
- break
57
- except ImportError :
58
- pass
59
- else :
60
- # TODO: it would be better to use an Executable here, which could be looked
61
- # up in the cross file or provided by a wrap. However, that will have to be
62
- # passed in externally, since we don't have (and I don't think we should),
63
- # have access to the `Environment` for that in this module.
64
- toml2json = shutil .which ('toml2json' )
65
-
66
-
67
44
_EXTRA_KEYS_WARNING = (
68
45
"This may (unlikely) be an error in the cargo manifest, or may be a missing "
69
46
"implementation in Meson. If this issue can be reproduced with the latest "
@@ -72,29 +49,6 @@ class DataclassInstance(Protocol):
72
49
"version that is generating this warning if possible."
73
50
)
74
51
75
- class TomlImplementationMissing (MesonException ):
76
- pass
77
-
78
-
79
- def load_toml (filename : str ) -> T .Dict [object , object ]:
80
- if tomllib :
81
- with open (filename , 'rb' ) as f :
82
- raw = tomllib .load (f )
83
- else :
84
- if toml2json is None :
85
- raise TomlImplementationMissing ('Could not find an implementation of tomllib, nor toml2json' )
86
-
87
- p , out , err = Popen_safe ([toml2json , filename ])
88
- if p .returncode != 0 :
89
- raise MesonException ('toml2json failed to decode output\n ' , err )
90
-
91
- raw = json .loads (out )
92
-
93
- if not isinstance (raw , dict ):
94
- raise MesonException ("Cargo.toml isn't a dictionary? How did that happen?" )
95
-
96
- return raw
97
-
98
52
99
53
def fixup_meson_varname (name : str ) -> str :
100
54
"""Fixup a meson variable name
0 commit comments