Skip to content

Instantly share code, notes, and snippets.

@prati0100
Last active August 17, 2022 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prati0100/64dd60ed3ca542397daee9058c56e826 to your computer and use it in GitHub Desktop.
Save prati0100/64dd60ed3ca542397daee9058c56e826 to your computer and use it in GitHub Desktop.
Script to store and run multiple build configurations.
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
#
# mk can be used to easily store and run multiple build profiles. For example, I
# work on U-Boot at $DAYJOB and I need to run builds for 3-4 different
# platforms. Each platform first needs a defconfig and then a build. And the
# build command is slightly different for each. So I wrote this small utility to
# store a set of build commands and the currently active set.
#
# The commands are stored in the "config" format. For example, create a .mkconf
# file in the project root containing:
#
# [foo]
# clean = make clean PLAT=foo
# build = make PLAT=foo
#
# [bar]
# clean = make clean PLAT=bar
# build = make PLAT=bar
#
# Now you can run `mk -p foo` to set the current platform to 'foo', stored in
# the '.mk' file. Next time you run mk clean it will run foo's clean command.
# The build command is considered the default. So just running mk runs the build
# command.
#
# The "common" section can be used to store commands common to all platforms.
# Naturally, a platform cannot be named "common". For example, in addition to
# the above config file, consider this:
#
# [common]
# menu = make menuconfig
#
# Now if you run `mk -p foo` and then `mk menu` it will run the menu command
# from the common section. Same for `mk -p bar`
import os
import configparser
import argparse
import subprocess
def get_prop(cfg, prop, sect):
if sect != None and prop in cfg[sect]:
return cfg[sect][prop]
elif prop in cfg["common"]:
return cfg["common"][prop]
else:
return None
def get_current_plat():
try:
file = open(".mk")
plat = file.read().strip()
file.close()
except:
return None
return plat
def set_platform(plat):
file = open(".mk", "w")
file.write(plat + "\n")
file.close()
def run_cmd_from_prop(cfg, prop):
plat = get_current_plat()
cmd = get_prop(cfg, prop, plat)
if cmd == None:
print("\"" + prop + "\" command not specified in platform or common section. Aborting.")
exit(1)
print(cmd)
try:
ret = subprocess.call(cmd, shell=True)
except:
ret = 1
return ret
def list_platforms(cfg):
curr = get_current_plat()
for plat in cfg.sections():
if plat == "common":
continue
if curr != None and plat == curr:
print("* ", end="")
else:
print(" ", end="")
print(plat)
arg = argparse.ArgumentParser()
group = arg.add_mutually_exclusive_group()
group.add_argument("-p", help="Set current platform", type=str, metavar="PLAT")
group.add_argument("-l", help="List platforms", action="store_true")
group.add_argument("command", help="Commad to run", metavar="CMD", nargs="?", default="build")
args = arg.parse_args()
cfg = configparser.ConfigParser()
files = cfg.read(".mkconf")
if not files:
print("No .mkconf file found. Aborting.")
exit(1)
if args.p != None:
plat = args.p
if plat in cfg:
set_platform(plat)
else:
print("Platform \"" + plat + "\" not found in config. Aborting.")
elif args.l:
list_platforms(cfg)
else:
ret = run_cmd_from_prop(cfg, args.command)
exit(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment