The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

builder.py in scripts

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of scripts/builder.py.
Build configuration
1 import os 2 import fnmatch 3 import glob 4 import types 5 from os import path 6 from scripts.run import run 7 8 def get_sources_path(directory, pattern): 9 """obtain path of all source files for a matching pattern""" 10 matches = [] 11 for root, dirnames, filenames in os.walk(directory): 12 for filename in filenames: 13 full_path = os.path.join(root, filename) 14 if fnmatch.filter([full_path], pattern): 15 matches.append(os.path.join(root, filename)) 16 return matches 17 18 class Builder(object): 19 """helper to generate tasks to compile vala code""" 20 21 def __init__(self, 22 source_directory, 23 valac_command, 24 cc_command, 25 linker_command, 26 target_binary, 27 link = None, 28 dependencies = None): 29 self.source_directory = source_directory 30 self.valac_command = valac_command 31 self.cc_command = cc_command 32 self.linker_command = linker_command 33 self.target_binary = target_binary 34 self.link = link 35 self.dependencies = dependencies 36 37 def build(self): 38 source_directory = self.source_directory 39 valac_command = self.valac_command 40 cc_command = self.cc_command 41 linker_command = self.linker_command 42 target_binary = self.target_binary 43 44 if not self.dependencies == None: 45 bindep = [path.join('build', 'bin', f) for f in self.dependencies] 46 else: 47 bindep = [] 48 49 copied_csource_paths = get_sources_path(source_directory, '*.c') 50 copied_cheader_paths = get_sources_path(source_directory, '*.h') 51 vala_source_paths = get_sources_path(source_directory, '*.vala') 52 build_directory = path.join('build', source_directory) 53 54 csource_files = [path.basename(f) for f in copied_csource_paths] 55 generated_csource_paths = [] 56 for vala_path in vala_source_paths: 57 vala_file = path.basename(vala_path) 58 cfile = vala_file.replace('.vala', '.c') 59 cpath = path.join(build_directory, cfile) 60 generated_csource_paths.append(cpath) 61 csource_files.append(cfile) 62 63 build_file = path.join(build_directory, 'placeholder') 64 yield { 65 'basename': 'mkdir ' + build_directory, 66 'actions': ['mkdir -p ' + path.join('build', 'bin'), 67 'mkdir -p ' + build_directory, 68 'touch ' + build_file], 69 'targets': [build_file], 70 } 71 72 copied_csources = [] 73 copied_cheader = [] 74 for csource in copied_cheader_paths + copied_csource_paths: 75 dest = path.join(build_directory, path.basename(csource)) 76 77 if not dest[-2:] == ".h": 78 copied_csources.append(dest) 79 else: 80 copied_cheader.append(dest) 81 82 yield { 83 'basename': 'copy ' + csource, 84 'file_dep': [build_file] + bindep, 85 'actions': ['cp ' + csource + ' ' + build_directory], 86 'targets': [dest] 87 } 88 89 yield { 90 'basename': 'valac ' + source_directory, 91 'file_dep': [build_file] + bindep, 92 'actions': [valac_command], 93 'targets': generated_csource_paths 94 } 95 96 csource_paths = generated_csource_paths + copied_csources 97 object_files = [] 98 99 for csource in csource_paths: 100 object_file = path.basename (csource.replace('.c', '.o')) 101 object_files.append(object_file); 102 103 command = cc_command.replace('C_SOURCE', csource) 104 object_path = path.join(build_directory, object_file) 105 command = command.replace('OBJECT_FILE', object_path) 106 yield { 107 'basename': 'compile ' + path.basename(csource), 108 'file_dep': [build_file, csource] + bindep + copied_cheader, 109 'actions': [command], 110 'targets': [path.join(build_directory, object_file)], 111 } 112 113 object_paths = [path.join(build_directory, f) for f in object_files] 114 yield { 115 'basename': source_directory, 116 'file_dep': object_paths + [build_file] + bindep, 117 'actions': [linker_command], 118 'targets': [path.join('build', 'bin', target_binary)] 119 } 120 121 if not self.link == None: 122 createlink = 'cd build/bin/ && ln -s -f ' + target_binary + ' ' + self.link 123 yield { 124 'basename': 'Create link ' + target_binary + ' ' + self.link, 125 'file_dep': [path.join('build', 'bin', target_binary)], 126 'actions': [createlink], 127 'targets': [path.join('build', 'bin', self.link)] 128 } 129 130 def execute_task(task): 131 for action in task['actions']: 132 print(action) 133 run(action) 134 135 def process_tasks(generator): 136 for task in generator: 137 if isinstance(task, types.GeneratorType): 138 process_tasks(task) 139 else: 140 execute_task(task) 141