Skip to content

Instantly share code, notes, and snippets.

@jaasonw
Last active March 25, 2019 21:37
Show Gist options
  • Save jaasonw/d0ae2f3865d7443913a5e25ea55e929b to your computer and use it in GitHub Desktop.
Save jaasonw/d0ae2f3865d7443913a5e25ea55e929b to your computer and use it in GitHub Desktop.
A short and simple build script that I use for compiling small c++ projects
# a simple build script suitable for compiling small projects
# written by jason wong
import os
import time
COMPILER = "g++"
MAIN = "main.cpp"
EXECUTABLE = "a.exe"
# includes and sources should search recursively
INCLUDES = [
"include"
]
# these are too
SOURCES = [
"src"
]
# these are not recursive (dont include -l)
LIB = [
]
LINK = [
]
# compiler flags here
FLAGS = [
]
# add clang specific flags here
if COMPILER == "clang++":
FLAGS.append("");
pass
# the rest is ignorable
flag_str = ""
for f in FLAGS:
flag_str += f + " "
include_str = ""
for top_level_directory in INCLUDES:
include_str += f"-I\"{top_level_directory}/\" "
for root, directories, filenames in os.walk(top_level_directory):
for directory in directories:
include_str += f"-I\"{os.path.join(root, directory)}/\" "
lib_str = ""
for directory in LIB:
lib_str += f"-L\"{directory}/\" "
sources_str = ""
for top_level_directory in SOURCES:
for root, directories, filenames in os.walk(top_level_directory):
for filename in filenames:
sources_str += f" \"{os.path.join(root,filename)}\" "
link_str = ""
for l in LINK:
link_str += f"-l{l} "
try:
os.remove(EXECUTABLE)
except:
pass
cmd = f"{COMPILER} {flag_str} {include_str} {lib_str} -o {EXECUTABLE} {MAIN} {sources_str} {link_str}"
print(cmd)
t1 = time.time()
os.system(cmd)
t2 = time.time()
print("Operation time Elapsed:", f'{t2 - t1:.2f}', "seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment