Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Created September 2, 2013 04:24
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 Rapptz/6409201 to your computer and use it in GitHub Desktop.
Save Rapptz/6409201 to your computer and use it in GitHub Desktop.
Compilation helper. bootstrap
#!/usr/bin/env python
# Python 2.7
import os
import sys
import errno
import glob
# Meta data
project_name = 'untitled'
compiler = 'g++'
compiler_flags = ['-Wall', '-pedantic', '-pedantic-errors', '-Werror', '-Wextra', '-std=c++11']
include_paths = ['./']
library_paths = []
library_files = ['sfml-graphics', 'sfml-window', 'sfml-system']
ignored_warnings = ['switch']
debug = False
debug_flags = ['-g']
input_directory = ''
output_directory = 'bin/'
# Stored data
f = open('build.ninja', 'w')
include_flags = []
link_flags = []
lib_path = []
# Platform specific stuff
if sys.platform == 'win32':
library_files.append('sfml-main')
link_flags.append('-mwindows')
# Set up ignored warnings
compiler_flags = compiler_flags + ['-Wno-{}'.format(flag) for flag in ignored_warnings]
# Set up include paths
include_flags = ['-I"{}"'.format(path) for path in include_paths]
# Set up library stuff
lib_path = ['-L{}'.format(path) for path in library_paths]
link_flags = link_flags + ['-l{}'.format(lib) for lib in library_files]
# Utilities
def make_path(path):
if path:
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def stringify_list(the_list):
return ' '.join(map(str, the_list))
def make_rule(name, content, **kwargs):
rule = 'rule {}\n'.format(name)
for key in kwargs:
rule = rule + ' {} = {}\n'.format(key, kwargs[key])
rule = rule + ' command = {}\n\n'.format(content)
return rule
def make_variable(name, content):
return '{} = {}\n\n'.format(name, content)
def make_build(in_var, out_var, rule):
return 'build {}: {} {}\n\n'.format(out_var, rule, in_var)
# Some preliminary set up
input_files = glob.glob(os.path.join(input_directory, '*.cpp'))
out_files = []
make_path(input_directory)
make_path(output_directory)
# Start building build.ninja file
# cxxflags variable
f.write(make_variable('cxxflags', stringify_list(compiler_flags)))
# libpath variable
f.write(make_variable('libpath', stringify_list(library_paths)))
# lib variable
f.write(make_variable('lib', stringify_list(link_flags)))
# incflags variable
f.write(make_variable('incflags', stringify_list(include_flags)))
# cxx rule
f.write(make_rule('cxx', 'g++ ${cxxflags} -c ${in} -o ${out} ${incflags}'))
# link rule
f.write(make_rule('link', 'g++ ${in} -o ${out} ${libpath} ${lib}'))
# Generate build sequences
for input in input_files:
output_file = output_directory + input.replace('cpp', 'o')
f.write(make_build(input, output_file, 'cxx'))
out_files.append(output_file)
# Generate link sequence
f.write(make_build(stringify_list(out_files), output_directory + project_name, 'link'))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment