|
3 | 3 | import subprocess |
4 | 4 |
|
5 | 5 | import drake |
6 | | -import drake.cxx |
7 | 6 | import drake.git |
8 | 7 |
|
9 | | -def _default_make_binary(): |
10 | | - from drake.which import which |
11 | | - to_try = [ |
12 | | - 'make', |
13 | | - 'gmake', |
14 | | - 'mingw32-make', |
15 | | - 'mingw64-make', |
16 | | - ] |
17 | | - for binary in to_try: |
18 | | - path = which(binary) |
19 | | - if path is not None: |
20 | | - return path |
21 | | - |
22 | | -_DEFAULT_MAKE_BINARY = _default_make_binary() |
23 | | - |
24 | | -class GNUBuilder(drake.Builder): |
25 | | - |
26 | | - def __init__( |
27 | | - self, |
28 | | - cxx_toolkit, |
29 | | - targets = [], |
30 | | - configure: """Configure script path (or None if no configure |
31 | | - step is needed)""" = None, |
32 | | - working_directory: "Deduced from configure" = None, |
33 | | - configure_args: "Arguments of the configure script" = [], |
34 | | - sources = [], |
35 | | - make_binary: "Make binary" = _DEFAULT_MAKE_BINARY, |
36 | | - makefile: "Makefile filename, used if not None" = None, |
37 | | - build_args: "Additional arguments for the make command" = ['install'], |
38 | | - additional_env: "Additional environment variables" = {}, |
39 | | - configure_interpreter = None, |
40 | | - patch = None, |
41 | | - configure_stdout: 'Show configure standard output' = False, |
42 | | - build_stdout: 'Show build standard output' = False): |
43 | | - self.__toolkit = cxx_toolkit |
44 | | - self.__build_stdout = build_stdout |
45 | | - self.__configure = configure |
46 | | - self.__configure_args = configure_args |
47 | | - self.__configure_interpreter = configure_interpreter |
48 | | - self.__configure_stdout = configure_stdout |
49 | | - self.__targets = list(targets) |
50 | | - self.__make_binary = make_binary |
51 | | - self.__makefile = makefile |
52 | | - self.__build_args = build_args |
53 | | - self.__env = {} |
54 | | - self.__env.update(additional_env) |
55 | | - self.__patch = patch |
56 | | - if make_binary is not None: |
57 | | - self.__env.setdefault('MAKE', make_binary.replace('\\', '/')) |
58 | | - if working_directory is not None: |
59 | | - self.__working_directory = working_directory |
60 | | - if not self.__working_directory.exists(): |
61 | | - self.__working_directory.mkpath() |
62 | | - else: |
63 | | - if self.__configure is None: |
64 | | - raise Exception( |
65 | | - "Cannot deduce the working directory (no configure script)" |
66 | | - ) |
67 | | - self.__working_directory = self.__configure.path().dirname() |
68 | | - drake.Builder.__init__( |
69 | | - self, |
70 | | - (configure is not None and [configure] or []) + sources, |
71 | | - self.__targets) |
72 | | - if isinstance(cxx_toolkit.patchelf, drake.BaseNode): |
73 | | - self.add_src(cxx_toolkit.patchelf) |
74 | | - |
75 | | - def execute(self): |
76 | | - env = dict(self.__env) |
77 | | - import os |
78 | | - env.update(os.environ) |
79 | | - with drake.CWDPrinter(drake.path_root() / drake.path_build() / self.work_directory): |
80 | | - # Patch |
81 | | - if self.__patch is not None: |
82 | | - patch_path = str(drake.path_root() / self.__patch.path()) |
83 | | - patch_cmd = ['patch', '-N', '-p', '1', '-i', patch_path], |
84 | | - if not self.cmd('Patch %s' % self.work_directory, |
85 | | - patch_cmd, |
86 | | - cwd = self.work_directory): |
87 | | - return False |
88 | | - # Configure step |
89 | | - if self.__configure is not None: |
90 | | - if not self.cmd('Configure %s' % self.work_directory, |
91 | | - self.command_configure, |
92 | | - cwd = self.work_directory, |
93 | | - env = env, |
94 | | - leave_stdout = self.__configure_stdout): |
95 | | - return False |
96 | | - # Build step |
97 | | - if not self.cmd('Build %s' % self.work_directory, |
98 | | - self.command_build, |
99 | | - cwd = self.work_directory, |
100 | | - env = env, |
101 | | - leave_stdout = self.__build_stdout): |
102 | | - return False |
103 | | - for target in self.__targets: |
104 | | - path = target.path().without_prefix(self.work_directory) |
105 | | - if isinstance(target, drake.cxx.DynLib): |
106 | | - rpath = '.' |
107 | | - elif isinstance(target, drake.cxx.Executable): |
108 | | - rpath = '../lib' |
109 | | - else: |
110 | | - continue |
111 | | - with drake.WritePermissions(target): |
112 | | - cmd = self.__toolkit.rpath_set_command(target.path(), rpath) |
113 | | - if self.__toolkit.os is not drake.os.windows: |
114 | | - if not self.cmd('Fix rpath for %s' % target.path(), cmd): |
115 | | - return False |
116 | | - if self.__toolkit.os is drake.os.macos: |
117 | | - cmd = ['install_name_tool', |
118 | | - '-id', '@rpath/%s' % target.name().basename(), |
119 | | - str(target.path())] |
120 | | - if not self.cmd('Fix rpath for %s' % target.path(), cmd): |
121 | | - return False |
122 | | - lib_dependecies = self.parse_otool_libraries(target.path()) |
123 | | - for dep in lib_dependecies: |
124 | | - if dep.basename() in (t.path().basename() for t in self.__targets): |
125 | | - cmd = [ |
126 | | - 'install_name_tool', |
127 | | - '-change', |
128 | | - str(dep), |
129 | | - '@rpath/%s' % dep.basename(), |
130 | | - str(target.path()), |
131 | | - ] |
132 | | - if not self.cmd('Fix dependency name for %s' % target.path(), cmd): |
133 | | - return False |
134 | | - return True |
135 | | - |
136 | | - def parse_otool_libraries(self, path): |
137 | | - command = ['otool', '-L', str(path)] |
138 | | - return [drake.Path(line[1:].split(' ')[0]) |
139 | | - for line |
140 | | - in subprocess.check_output(command).decode().split('\n') |
141 | | - if line.startswith('\t')] |
142 | | - |
143 | | - @property |
144 | | - def command_configure(self): |
145 | | - if self.__configure is None: |
146 | | - return None |
147 | | - config = [str(drake.path_build(absolute = True) / self.__configure.path())] |
148 | | - if self.__configure_interpreter is not None: |
149 | | - config.insert(0, self.__configure_interpreter) |
150 | | - return config + self.__configure_args |
151 | | - |
152 | | - @property |
153 | | - def command_build(self): |
154 | | - if self.__makefile is not None: |
155 | | - return [self.__make_binary, '-f', self.__makefile, 'install'] + self.__build_args |
156 | | - return [self.__make_binary] + self.__build_args |
157 | | - |
158 | | - @property |
159 | | - def work_directory(self): |
160 | | - return str(self.__working_directory) |
161 | | - |
162 | | - |
163 | | - def hash(self): |
164 | | - env = {} |
165 | | - env.update(self.__env) |
166 | | - env.pop('DRAKE_RAW', '1') |
167 | | - return ''.join([ |
168 | | - str(self.command_configure), |
169 | | - str(self.command_build), |
170 | | - str(tuple(sorted(env))), |
171 | | - ]) |
172 | | - |
173 | | - def __str__(self): |
174 | | - return '%s(%s)' % (self.__class__.__name__, self.__working_directory) |
175 | | - |
176 | 8 | class FatLibraryGenerator(drake.Builder): |
177 | 9 |
|
178 | 10 | def __init__(self, |
|
0 commit comments