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 from os import path 5 from doit.tools import run_once 6 7 def get_sources_path(directory, pattern): 8 """obtain path of all source files for a matching pattern""" 9 matches = [] 10 for root, dirnames, filenames in os.walk(directory): 11 for filename in filenames: 12 full_path = os.path.join(root, filename) 13 if fnmatch.filter([full_path], pattern): 14 matches.append(os.path.join(root, filename)) 15 return matches 16 17 class Builder(object): 18 """helper to generate tasks to compile vala code""" 19 20 def __init__(self, 21 source_directory, 22 valac_command, 23 cc_command, 24 linker_command, 25 target_binary, 26 link = None, 27 dependencies = None): 28 self.source_directory = source_directory 29 self.valac_command = valac_command 30 self.cc_command = cc_command 31 self.linker_command = linker_command 32 self.target_binary = target_binary 33 self.link = link 34 self.dependencies = dependencies 35 36 def build(self): 37 source_directory = self.source_directory 38 valac_command = self.valac_command 39 cc_command = self.cc_command 40 linker_command = self.linker_command 41 target_binary = self.target_binary 42 43 if not self.dependencies == None: 44 bindep = [path.join('build', 'bin', f) for f in self.dependencies] 45 else: 46 bindep = [] 47 48 copied_csource_paths = get_sources_path(source_directory, '*.c') 49 copied_cheader_paths = get_sources_path(source_directory, '*.h') 50 vala_source_paths = get_sources_path(source_directory, '*.vala') 51 build_directory = path.join('build', source_directory) 52 53 csource_files = [path.basename(f) for f in copied_csource_paths] 54 generated_csource_paths = [] 55 for vala_path in vala_source_paths: 56 vala_file = path.basename(vala_path) 57 cfile = vala_file.replace('.vala', '.c') 58 cpath = path.join(build_directory, cfile) 59 generated_csource_paths.append(cpath) 60 csource_files.append(cfile) 61 62 build_file = path.join(build_directory, 'placeholder') 63 yield { 64 'basename': 'mkdir ' + build_directory, 65 'actions': ['mkdir -p ' + path.join('build', 'bin'), 66 'mkdir -p ' + build_directory, 67 'touch ' + build_file], 68 'targets': [build_file], 69 'uptodate': [run_once] 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 ' + build_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_directory, '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