unstable_kconfig.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright 2017, 2019 The Meson development team
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. # Unless required by applicable law or agreed to in writing, software
  7. # distributed under the License is distributed on an "AS IS" BASIS,
  8. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. # See the License for the specific language governing permissions and
  10. # limitations under the License.
  11. from . import ExtensionModule
  12. from .. import mesonlib
  13. from ..mesonlib import typeslistify
  14. from ..interpreterbase import FeatureNew, noKwargs
  15. from ..interpreter import InvalidCode
  16. import os
  17. class KconfigModule(ExtensionModule):
  18. @FeatureNew('Kconfig Module', '0.51.0')
  19. def __init__(self, *args, **kwargs):
  20. super().__init__(*args, **kwargs)
  21. self.snippets.add('load')
  22. def _load_file(self, path_to_config):
  23. result = dict()
  24. try:
  25. with open(path_to_config) as f:
  26. for line in f:
  27. if '#' in line:
  28. comment_idx = line.index('#')
  29. line = line[:comment_idx]
  30. line = line.strip()
  31. try:
  32. name, val = line.split('=', 1)
  33. except ValueError:
  34. continue
  35. result[name.strip()] = val.strip()
  36. except IOError as e:
  37. raise mesonlib.MesonException('Failed to load {}: {}'.format(path_to_config, e))
  38. return result
  39. @noKwargs
  40. def load(self, interpreter, state, args, kwargs):
  41. sources = typeslistify(args, (str, mesonlib.File))
  42. if len(sources) != 1:
  43. raise InvalidCode('load takes only one file input.')
  44. s = sources[0]
  45. is_built = False
  46. if isinstance(s, mesonlib.File):
  47. if s.is_built:
  48. FeatureNew('kconfig.load() of built files', '0.52.0').use(state.subproject)
  49. is_built = True
  50. s = s.absolute_path(interpreter.environment.source_dir, interpreter.environment.build_dir)
  51. else:
  52. s = os.path.join(interpreter.environment.source_dir, s)
  53. if s not in interpreter.build_def_files and not is_built:
  54. interpreter.build_def_files.append(s)
  55. return self._load_file(s)
  56. def initialize(*args, **kwargs):
  57. return KconfigModule(*args, **kwargs)