2 """Toggle settings in `ftoption.h` file based on command-line arguments.
4 This script takes an `ftoption.h` file as input and rewrites
5 `#define`/`#undef` lines in it based on `--enable=CONFIG_VARNAME` or
6 `--disable=CONFIG_VARNAME` arguments passed to it, where `CONFIG_VARNAME` is
7 configuration variable name, such as `FT_CONFIG_OPTION_USE_LZW`, that may
10 Note that if one of `CONFIG_VARNAME` is not found in the input file, this
11 script exits with an error message listing the missing variable names.
21 parser = argparse.ArgumentParser(description=__doc__)
24 "input", metavar="FTOPTION_H", help="Path to input ftoption.h file."
27 parser.add_argument("--output", help="Output to file instead of stdout.")
33 help="Enable a given build option (e.g. FT_CONFIG_OPTION_USE_LZW).",
40 help="Disable a given build option.",
43 args = parser.parse_args()
45 common_options = set(args.enable) & set(args.disable)
48 "Options cannot be both enabled and disabled: %s"
49 % sorted(common_options)
53 with open(args.input) as f:
59 for line in input_file.splitlines():
61 # #define <CONFIG_VAR>
62 # /* #define <CONFIG_VAR> */
65 if line.startswith("/* #define ") and line.endswith(" */"):
66 option_name = line[11:-3].strip()
67 option_enabled = False
68 elif line.startswith("#define "):
69 option_name = line[8:].strip()
71 elif line.startswith("#undef "):
72 option_name = line[7:].strip()
73 option_enabled = False
75 new_lines.append(line)
78 options_seen.add(option_name)
79 if option_enabled and option_name in args.disable:
80 line = "#undef " + option_name
81 elif not option_enabled and option_name in args.enable:
82 line = "#define " + option_name
83 new_lines.append(line)
85 result = "\n".join(new_lines)
87 # Sanity check that all command-line options were actually processed.
88 cmdline_options = set(args.enable) | set(args.disable)
89 assert cmdline_options.issubset(
91 ), "Could not find options in input file: " + ", ".join(
92 sorted(cmdline_options - options_seen)
96 with open(args.output, "w") as f:
104 if __name__ == "__main__":