Skip to content

Instantly share code, notes, and snippets.

@ambroff
Created October 30, 2011 04:01
Show Gist options
  • Save ambroff/1325459 to your computer and use it in GitHub Desktop.
Save ambroff/1325459 to your computer and use it in GitHub Desktop.
Configure script for CMake projects. ./configure.py; make; make install; make distclean.
#!/usr/bin/env python
# Copyright (C) 2011 by Kyle Ambroff <kyle@ambroff.com>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Run cmake to configure build and generate build directory. This script also
creates a GNU Makefile in the top level of the source tree, which allows you
to build without knowing where the build directory is.
"make distclean" will delete the build directory and the Makefile."""
import errno
import os
import optparse
import shutil
import subprocess
import sys
def configure():
options = parse_args()
src_root = get_src_root()
build_dir = os.path.join(src_root, 'build')
try:
os.makedirs(build_dir)
except OSError, e:
if e.errno != errno.EEXIST:
raise
args = ['cmake', '..']
if options.debug:
args.append('-DCMAKE_BUILD_TYPE:STRING=Debug')
else:
args.append('-DCMAKE_BUILD_TYPE:STRING=Release')
if options.prefix:
prefix = os.path.realpath(options.prefix)
args.append('-DCMAKE_INSTALL_PREFIX:STRING=%s' % prefix)
try:
subprocess.check_call(args, cwd=build_dir)
write_root_makefile(src_root)
except OSError, e:
if e.errno == errno.ENOENT:
print >> sys.stderr, 'ERROR: You must have cmake installed. ' \
'Please install with your package manager, or download from ' \
'http://www.cmake.org/.'
sys.exit(1)
else:
raise
except subprocess.CalledProcessError, e:
print >> sys.stderr, 'ERROR: Failed to run cmake. Deleting build ' \
'directory.'
shutil.rmtree(build_dir)
sys.exit(1)
MAKEFILE = """\
# Automatically generated by configure.py
all:
+$(MAKE) -C $(CURDIR)/build
%:
+$(MAKE) -C $(CURDIR)/build $@
distclean:
-rm -rvf $(CURDIR)/build $(CURDIR)/Makefile
"""
def write_root_makefile(srcroot):
f = open(os.path.join(srcroot, 'Makefile'), 'w')
try:
f.write(MAKEFILE)
finally:
f.close()
def get_src_root():
"""Returns absolute path to the root of the source tree."""
return os.path.realpath(os.path.dirname(__file__))
def parse_args():
p = optparse.OptionParser(usage='%prog [OPTIONS]', description=__doc__)
p.add_option(
'--enable-debug', dest='debug', default=False, action='store_true',
help='Unoptimized builds with debug symbols.')
p.add_option(
'--prefix', dest='prefix', default=None,
help='Optional install prefix. Default (probably): /usr/local')
options, args = p.parse_args()
if args:
p.error('Unexpected arguments: %s' % ', '.join(args))
return options
if __name__ == '__main__':
configure()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment