import collections import itertools import json import os import os.path import re import shutil import string import subprocess import sys import cgi class BuildDesc: def __init__(self, prepend_envs=None, variables=None, build_type=None, generator=None): self.prepend_envs = prepend_envs or [] # [ { "var": "value" } ] self.variables = variables or [] self.build_type = build_type self.generator = generator def merged_with( self, build_desc ): """Returns a new BuildDesc by merging field content. Prefer build_desc fields to self fields for single valued field. """ return BuildDesc( self.prepend_envs + build_desc.prepend_envs, self.variables + build_desc.variables, build_desc.build_type or self.build_type, build_desc.generator or self.generator ) def env( self ): environ = os.environ.copy() for values_by_name in self.prepend_envs: for var, value in values_by_name.items(): var = var.upper() if type(value) is unicode: value = value.encode( sys.getdefaultencoding() ) if var in environ: environ[var] = value + os.pathsep + environ[var] else: environ[var] = value return environ def cmake_args( self ): args = ["-D%s" % var for var in self.variables] # skip build type for Visual Studio solution as it cause warning if self.build_type and 'Visual' not in self.generator: args.append( "-DCMAKE_BUILD_TYPE=%s" % self.build_type ) if self.generator: args.extend( ['-G', self.generator] ) return args def __repr__( self ): return "BuildDesc( %s, build_type=%s )" % (" ".join( self.cmake_args()), self.build_type) class BuildData: def __init__( self, desc, work_dir, source_dir ): self.desc = desc self.work_dir = work_dir self.source_dir = source_dir self.cmake_log_path = os.path.join( work_dir, 'batchbuild_cmake.log' ) self.build_log_path = os.path.join( work_dir, 'batchbuild_build.log' ) self.cmake_succeeded = False self.build_succeeded = False def execute_build(self): print 'Build %s' % self.desc self._make_new_work_dir( ) self.cmake_succeeded = self._generate_makefiles( ) if self.cmake_succeeded: self.build_succeeded = self._build_using_makefiles( ) return self.build_succeeded def _generate_makefiles(self): print ' Generating makefiles: ', cmd = ['cmake'] + self.desc.cmake_args( ) + [os.path.abspath( self.source_dir )] succeeded = self._execute_build_subprocess( cmd, self.desc.env(), self.cmake_log_path ) print 'done' if succeeded else 'FAILED' return succeeded def _build_using_makefiles(self): print ' Building:', cmd = ['cmake', '--build', self.work_dir] if self.desc.build_type: cmd += ['--config', self.desc.build_type] succeeded = self._execute_build_subprocess( cmd, self.desc.env(), self.build_log_path ) print 'done' if succeeded else 'FAILED' return succeeded def _execute_build_subprocess(self, cmd, env, log_path): process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=self.work_dir, env=env ) stdout, _ = process.communicate( ) succeeded = (process.returncode == 0) with open( log_path, 'wb' ) as flog: log = ' '.join( cmd ) + '\n' + stdout + '\nExit code: %r\n' % process.returncode flog.write( fix_eol( log ) ) return succeeded def _make_new_work_dir(self): if os.path.isdir( self.work_dir ): print ' Removing work directory', self.work_dir shutil.rmtree( self.work_dir, ignore_errors=True ) if not os.path.isdir( self.work_dir ): os.makedirs( self.work_dir ) def fix_eol( stdout ): """Fixes wrong EOL produced by cmake --build on Windows (\r\r\n instead of \r\n). """ return re.sub( '\r*\n', os.linesep, stdout ) def load_build_variants_from_config( config_path ): with open( config_path, 'rb' ) as fconfig: data = json.load( fconfig ) variants = data[ 'cmake_variants' ] build_descs_by_axis = collections.defaultdict( list ) for axis in variants: axis_name = axis["name"] build_descs = [] if "generators" in axis: for generator_data in axis["generators"]: for generator in generator_data["generator"]: build_desc = BuildDesc( generator=generator, prepend_envs=generator_data.get("env_prepend") ) build_descs.append( build_desc ) elif "variables" in axis: for variables in axis["variables"]: build_desc = BuildDesc( variables=variables ) build_descs.append( build_desc ) elif "build_types" in axis: for build_type in axis["build_types"]: build_desc = BuildDesc( build_type=build_type ) build_descs.append( build_desc ) build_descs_by_axis[axis_name].extend( build_descs ) return build_descs_by_axis def generate_build_variants( build_descs_by_axis ): """Returns a list of BuildDesc generated for the partial BuildDesc for each axis.""" axis_names = build_descs_by_axis.keys() build_descs = [] for axis_name, axis_build_descs in build_descs_by_axis.items(): if len(build_descs): # for each existing build_desc and each axis build desc, create a new build_desc new_build_descs = [] for prototype_build_desc, axis_build_desc in itertools.product( build_descs, axis_build_descs): new_build_descs.append( prototype_build_desc.merged_with( axis_build_desc ) ) build_descs = new_build_descs else: build_descs = axis_build_descs return build_descs HTML_TEMPLATE = string.Template('''
Variables | $th_vars
---|
Build type | $th_build_types