Skip to content

Instantly share code, notes, and snippets.

@dogukanarat
Created March 26, 2025 12:45
Show Gist options
  • Save dogukanarat/ebbde5095da8e62e0eb3da8edb521dc6 to your computer and use it in GitHub Desktop.
Save dogukanarat/ebbde5095da8e62e0eb3da8edb521dc6 to your computer and use it in GitHub Desktop.
# This script generates the config header file from .config
import os
import sys
import re
import argparse
def main():
parser = argparse.ArgumentParser(description='Generate config header file from .config')
parser.add_argument('config_file', help='Path to .config file')
parser.add_argument('header_file', help='Path to header file')
args = parser.parse_args()
config_file = args.config_file
header_file = args.header_file
if not os.path.exists(config_file):
print('Error: ' + config_file + ' does not exist')
sys.exit(1)
if not config_file.endswith('.config'):
print('Error: ' + config_file + ' is not a .config file')
sys.exit(1)
print('Generating config header file from ' + config_file + ' to ' + header_file)
# Read config file
with open(config_file, 'r') as f:
config = f.readlines()
# Parse config file
config_dict = {}
for line in config:
if line.startswith('#'):
continue
if not '=' in line:
continue
key, value = line.split('=')
key = key.strip()
value = value.strip()
config_dict[key] = value
# Generate header file
with open(header_file, 'w') as f:
f.write('/* This file is generated by ConfigHeaderGenerator.py */\n\n')
f.write('')
f.write('#ifndef CONFIG_H\n')
f.write('#define CONFIG_H\n\n')
for key, value in config_dict.items():
if (value == 'y'):
f.write("#define {:<30}\n".format(key))
elif (value == 'n'):
f.write("/* #define {:<30} */\n".format(key))
else:
f.write("#define {:<30} ({})\n".format(key, value))
f.write('')
f.write('\n#endif\n')
if __name__ == '__main__':
main()
menu "Project Configuration"
config CONFIG_ENABLE_FEATURE_X
bool "Enable feature X"
default y
config CONFIG_PARAM_FEATURE_X
string "Default installation path"
default "/usr/local/myapp"
endmenu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment