def _generate_resource_index(targets, sources, name, path): """Generate resource index description file for a cc resource library""" header, source = targets with open(header, 'w') as h, open(source, 'w') as c: full_name = blade_util.regular_variable_name(os.path.join(path, name)) guard_name = 'BLADE_RESOURCE_%s_H_' % full_name.upper() index_name = 'RESOURCE_INDEX_%s' % full_name h.write(textwrap.dedent('''\ // This file was automatically generated by blade #ifndef {0} #define {0} #ifdef __cplusplus extern "C" {{ #endif #ifndef BLADE_RESOURCE_TYPE_DEFINED #define BLADE_RESOURCE_TYPE_DEFINED struct BladeResourceEntry {{ const char* name; const char* data; unsigned int size; }}; #endif''').format(guard_name)) c.write(textwrap.dedent('''\ // This file was automatically generated by blade #include "{0}" const struct BladeResourceEntry {1}[] = {{''').format(header, index_name)) for s in sources: entry_var = blade_util.regular_variable_name(s) entry_name = os.path.relpath(s, path) entry_size = os.path.getsize(s) h.write('// %s\n' % entry_name) h.write('extern const char RESOURCE_%s[%d];\n' % (entry_var, entry_size)) h.write('extern const unsigned RESOURCE_%s_len;\n' % entry_var) c.write(' { "%s", RESOURCE_%s, %s },\n' % (entry_name, entry_var, entry_size)) c.write(textwrap.dedent('''\ }}; const unsigned {0}_len = {1};''').format(index_name, len(sources))) h.write(textwrap.dedent('''\ // Resource index extern const struct BladeResourceEntry {0}[]; extern const unsigned {0}_len; #ifdef __cplusplus }} // extern "C" #endif #endif // {1}''').format(index_name, guard_name))
def ninja_rules(self): rule = '%s__rule__' % regular_variable_name( self._source_file_path(self.name)) cmd = self.ninja_command() description = console.colored( '%s //%s' % (self.data['cmd_name'], self.fullname), 'dimpurple') self._write_rule('''rule %s command = %s && cd %s && ls ${out} > /dev/null description = %s ''' % (rule, cmd, self.blade.get_root_dir(), description)) outputs = [self._target_file_path(o) for o in self.data['outs']] inputs = self._expand_srcs() vars = {} if '${_in_1}' in cmd: vars['_in_1'] = inputs[0] if '${_out_1}' in cmd: vars['_out_1'] = outputs[0] if self.data['heavy']: vars['pool'] = 'heavy_pool' self.ninja_build(rule, outputs, inputs=inputs, implicit_deps=self.implicit_dependencies(), variables=vars) for i, out in enumerate(outputs): self._add_target_file(str(i), out)
def generate(self): """Generate code for backend build system.""" # NOTE: Here is something different with normal targets. # We have to generate each `rule` for a `gen_rule` target but not sharing a predefined rule. # Because the `command` variable is not lazy evaluated althrough it can be overridden in a # `build` statement, so any other build scoped variables are expanded to empty. rule = '%s__rule__' % regular_variable_name( self._source_file_path(self.name)) cmd = self._expand_command() description = console.colored( '%s %s' % (self.attr['cmd_name'], self.fullname), 'dimpurple') self._write_rule(_RULE_FORMAT % (rule, cmd, self.blade.get_root_dir(), description)) outputs = [self._target_file_path(o) for o in self.attr['outs']] inputs = self._expand_srcs() vars = {} if '${_in_1}' in cmd: vars['_in_1'] = inputs[0] if '${_out_1}' in cmd: vars['_out_1'] = outputs[0] if self.attr['heavy']: vars['pool'] = 'heavy_pool' self.generate_build(rule, outputs, inputs=inputs, implicit_deps=self.implicit_dependencies(), variables=vars) for i, out in enumerate(outputs): self._add_target_file(str(i), out)
def ninja_rules(self): self._check_deprecated_deps() if not self.srcs: return resources = [self._source_file_path(s) for s in self.srcs] index = [ self._target_file_path('%s.h' % self.name), self._target_file_path('%s.c' % self.name) ] self.ninja_build('resource_index', index, inputs=resources, variables={ 'name': regular_variable_name(self.name), 'path': self.path }) sources = ['%s.c' % self.name] for resource in self.srcs: generated_source = '%s.c' % resource self.ninja_build('resource', self._target_file_path(generated_source), inputs=self._source_file_path(resource)) sources.append(generated_source) objs = self._generated_cc_objects(sources) self._cc_library(objs)