51839e29cb595 (Andy Shevchenko 2020-12-09 13:50:17 +0200 1) #!/usr/bin/env python3
b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 2) # SPDX-License-Identifier: GPL-2.0
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 3) #
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 4) # diffconfig - a tool to compare .config files.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 5) #
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 6) # originally written in 2006 by Matt Mackall
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 7) # (at least, this was in his bloatwatch source code)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 8) # last worked on 2008 by Tim Bird
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 9) #
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 10)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 11) import sys, os
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 12)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 13) def usage():
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 14) print("""Usage: diffconfig [-h] [-m] [<config1> <config2>]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 15)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 16) Diffconfig is a simple utility for comparing two .config files.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 17) Using standard diff to compare .config files often includes extraneous and
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 18) distracting information. This utility produces sorted output with only the
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 19) changes in configuration values between the two files.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 20)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 21) Added and removed items are shown with a leading plus or minus, respectively.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 22) Changed items show the old and new values on a single line.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 23)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 24) If -m is specified, then output will be in "merge" style, which has the
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 25) changed and new values in kernel config option format.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 26)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 27) If no config files are specified, .config and .config.old are used.
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 28)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 29) Example usage:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 30) $ diffconfig .config config-with-some-changes
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 31) -EXT2_FS_XATTR n
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 32) CRAMFS n -> y
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 33) EXT2_FS y -> n
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 34) LOG_BUF_SHIFT 14 -> 16
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 35) PRINTK_TIME n -> y
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 36) """)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 37) sys.exit(0)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 38)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 39) # returns a dictionary of name/value pairs for config items in the file
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 40) def readconfig(config_file):
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 41) d = {}
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 42) for line in config_file:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 43) line = line[:-1]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 44) if line[:7] == "CONFIG_":
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 45) name, val = line[7:].split("=", 1)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 46) d[name] = val
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 47) if line[-11:] == " is not set":
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 48) d[line[9:-11]] = "n"
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 49) return d
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 50)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 51) def print_config(op, config, value, new_value):
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 52) global merge_style
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 53)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 54) if merge_style:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 55) if new_value:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 56) if new_value=="n":
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 57) print("# CONFIG_%s is not set" % config)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 58) else:
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 59) print("CONFIG_%s=%s" % (config, new_value))
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 60) else:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 61) if op=="-":
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 62) print("-%s %s" % (config, value))
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 63) elif op=="+":
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 64) print("+%s %s" % (config, new_value))
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 65) else:
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 66) print(" %s %s -> %s" % (config, value, new_value))
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 67)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 68) def main():
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 69) global merge_style
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 70)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 71) # parse command line args
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 72) if ("-h" in sys.argv or "--help" in sys.argv):
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 73) usage()
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 74)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 75) merge_style = 0
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 76) if "-m" in sys.argv:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 77) merge_style = 1
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 78) sys.argv.remove("-m")
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 79)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 80) argc = len(sys.argv)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 81) if not (argc==1 or argc == 3):
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 82) print("Error: incorrect number of arguments or unrecognized option")
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 83) usage()
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 84)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 85) if argc == 1:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 86) # if no filenames given, assume .config and .config.old
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 87) build_dir=""
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 88) if "KBUILD_OUTPUT" in os.environ:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 89) build_dir = os.environ["KBUILD_OUTPUT"]+"/"
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 90) configa_filename = build_dir + ".config.old"
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 91) configb_filename = build_dir + ".config"
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 92) else:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 93) configa_filename = sys.argv[1]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 94) configb_filename = sys.argv[2]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 95)
6bf2e84b8cebc (Mike Pagano 2013-08-16 14:40:56 -0400 96) try:
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 97) a = readconfig(open(configa_filename))
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 98) b = readconfig(open(configb_filename))
6bf2e84b8cebc (Mike Pagano 2013-08-16 14:40:56 -0400 99) except (IOError):
6bf2e84b8cebc (Mike Pagano 2013-08-16 14:40:56 -0400 100) e = sys.exc_info()[1]
6bf2e84b8cebc (Mike Pagano 2013-08-16 14:40:56 -0400 101) print("I/O error[%s]: %s\n" % (e.args[0],e.args[1]))
6bf2e84b8cebc (Mike Pagano 2013-08-16 14:40:56 -0400 102) usage()
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 103)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 104) # print items in a but not b (accumulate, sort and print)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 105) old = []
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 106) for config in a:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 107) if config not in b:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 108) old.append(config)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 109) old.sort()
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 110) for config in old:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 111) print_config("-", config, a[config], None)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 112) del a[config]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 113)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 114) # print items that changed (accumulate, sort, and print)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 115) changed = []
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 116) for config in a:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 117) if a[config] != b[config]:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 118) changed.append(config)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 119) else:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 120) del b[config]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 121) changed.sort()
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 122) for config in changed:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 123) print_config("->", config, a[config], b[config])
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 124) del b[config]
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 125)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 126) # now print items in b but not in a
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 127) # (items from b that were in a were removed above)
c8272faf5e3f0 (Mike Pagano 2013-08-20 14:41:12 -0400 128) new = sorted(b.keys())
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 129) for config in new:
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 130) print_config("+", config, None, b[config])
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 131)
a717417e7f96a (Tim Bird 2008-06-24 10:56:06 -0700 132) main()