Skip to content

Instantly share code, notes, and snippets.

@ToniRV
Last active May 7, 2022 13:02
Show Gist options
  • Save ToniRV/5f906ce862718ba618a923717b54d6c3 to your computer and use it in GitHub Desktop.
Save ToniRV/5f906ce862718ba618a923717b54d6c3 to your computer and use it in GitHub Desktop.
Makes the annoying 'mkdir build, cd build, cmake .., make -j $(nproc)' in one call in the root directory as 'cbuild'. It also accepts the arg `--clean` which calls `make clean` before `make`. Simplest way to have it as a command line function is to add it to your .bashrc as an alias like: `alias cbuild="${HOME}/path/to/cbuild.py"`
#!/usr/bin/env python
import signal
import sys
import os
import subprocess
import argparse
# Group of Different functions for different styles
if sys.platform.lower() == "win32":
os.system('color')
RED = '\033[31m'
GREEN = '\033[32m'
RESET = '\033[0m'
def cmake():
print(GREEN + "Calling cmake:" + RESET)
if not os.path.exists('./CMakeLists.txt'):
raise Exception ('Missing CMakeLists.txt.')
try:
subprocess.call('cmake ..', shell = True, cwd="./build")
except:
raise Exception (RED + 'cmake failed.' + RESET)
return True
def make_clean():
print(GREEN + "Calling make clean." + RESET)
try:
subprocess.call('make clean', shell=True, cwd='./build')
except:
raise Exception (RED + 'make clean failed.' + RESET)
return True
def make_test():
print(GREEN + "Calling make test." + RESET)
try:
subprocess.call('make test', shell=True, cwd='./build')
except:
raise Exception (RED + 'make test failed.' + RESET)
return True
def make():
print(GREEN + "Calling make:" + RESET)
try:
subprocess.call('make -j $(nproc)', shell=True, cwd='./build')
except:
raise Exception (RED + 'make failed.' + RESET)
return True
def parser():
import argparse
basic_desc = "Automated build: cmake + make. Use `cbuild --clean` to call `make clean`. \
Or `cbuild --test` to call `make test`."
parser = argparse.ArgumentParser(add_help=True, description="{}".format(basic_desc))
parser.add_argument("-c", "--clean", action="store_true", help="Call make clean.",)
parser.add_argument("-t", "--test", action="store_true", help="Call make test.",)
return parser
if __name__ == "__main__":
# Parse command line flags
args = parser().parse_args()
if not os.path.isdir('build'):
print(GREEN + 'Creating build folder.' + RESET)
os.mkdir('build')
assert(cmake())
if args.clean:
assert(make_clean())
assert(make())
if args.test:
assert(make_test())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment