check-config.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #! /usr/bin/env python
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # Copyright (c) 2015, 2018 The Linux Foundation. All rights reserved.
  4. """
  5. Android kernel configuration validator.
  6. The Android kernel reference trees contain some config stubs of
  7. configuration options that are required for Android to function
  8. correctly, and additional ones that are recommended.
  9. This script can help compare these base configs with the ".config"
  10. output of the compiler to determine if the proper configs are defined.
  11. """
  12. from collections import namedtuple
  13. from optparse import OptionParser
  14. import re
  15. import sys
  16. version = "check-config.py, version 0.0.1"
  17. req_re = re.compile(r'''^CONFIG_(.*)=(.*)$''')
  18. forb_re = re.compile(r'''^# CONFIG_(.*) is not set$''')
  19. comment_re = re.compile(r'''^(#.*|)$''')
  20. Enabled = namedtuple('Enabled', ['name', 'value'])
  21. Disabled = namedtuple('Disabled', ['name'])
  22. def walk_config(name):
  23. with open(name, 'r') as fd:
  24. for line in fd:
  25. line = line.rstrip()
  26. m = req_re.match(line)
  27. if m:
  28. yield Enabled(m.group(1), m.group(2))
  29. continue
  30. m = forb_re.match(line)
  31. if m:
  32. yield Disabled(m.group(1))
  33. continue
  34. m = comment_re.match(line)
  35. if m:
  36. continue
  37. print "WARNING: Unknown .config line: ", line
  38. class Checker():
  39. def __init__(self):
  40. self.required = {}
  41. self.exempted = set()
  42. self.forbidden = set()
  43. def add_required(self, fname):
  44. for ent in walk_config(fname):
  45. if type(ent) is Enabled:
  46. self.required[ent.name] = ent.value
  47. elif type(ent) is Disabled:
  48. if ent.name in self.required:
  49. del self.required[ent.name]
  50. self.forbidden.add(ent.name)
  51. def add_exempted(self, fname):
  52. with open(fname, 'r') as fd:
  53. for line in fd:
  54. line = line.rstrip()
  55. self.exempted.add(line)
  56. def check(self, path):
  57. failure = False
  58. # Don't run this for mdm targets
  59. if re.search('mdm', path):
  60. print "Not applicable to mdm targets... bypassing"
  61. else:
  62. for ent in walk_config(path):
  63. # Go to the next iteration if this config is exempt
  64. if ent.name in self.exempted:
  65. continue
  66. if type(ent) is Enabled:
  67. if ent.name in self.forbidden:
  68. print "error: Config should not be present: %s" %ent.name
  69. failure = True
  70. if ent.name in self.required and ent.value != self.required[ent.name]:
  71. print "error: Config has wrong value: %s %s expecting: %s" \
  72. %(ent.name, ent.value, self.required[ent.name])
  73. failure = True
  74. elif type(ent) is Disabled:
  75. if ent.name in self.required:
  76. print "error: Config should be present, but is disabled: %s" %ent.name
  77. failure = True
  78. if failure:
  79. sys.exit(1)
  80. def main():
  81. usage = """%prog [options] path/to/.config"""
  82. parser = OptionParser(usage=usage, version=version)
  83. parser.add_option('-r', '--required', dest="required",
  84. action="append")
  85. parser.add_option('-e', '--exempted', dest="exempted",
  86. action="append")
  87. (options, args) = parser.parse_args()
  88. if len(args) != 1:
  89. parser.error("Expecting a single path argument to .config")
  90. elif options.required is None or options.exempted is None:
  91. parser.error("Expecting a file containing required configurations")
  92. ch = Checker()
  93. for r in options.required:
  94. ch.add_required(r)
  95. for e in options.exempted:
  96. ch.add_exempted(e)
  97. ch.check(args[0])
  98. if __name__ == '__main__':
  99. main()