Commit | Line | Data |
---|---|---|
de9b991b JR |
1 | import os |
2 | import shutil | |
3 | import git | |
4 | import subprocess | |
5 | import logging | |
cb87ff89 JR |
6 | import lttng_ivc.settings as Settings |
7 | ||
8 | from lttng_ivc.utils.utils import sha256_checksum | |
de9b991b | 9 | |
fe7b987e | 10 | _logger = logging.getLogger('project') |
de9b991b JR |
11 | |
12 | class Project(object): | |
13 | ||
14 | def __init__(self, label, git_path, sha1, tmpdir): | |
15 | self.label = label | |
16 | self.git_path = git_path | |
17 | self.sha1 = sha1 | |
18 | ||
19 | """ Custom configure flags in the for of ['-x', 'arg']""" | |
20 | self.custom_configure_flags = [] | |
21 | ccache = shutil.which("ccache") | |
22 | if ccache is not None: | |
23 | self.custom_configure_flags.append("CC={} gcc".format(ccache)) | |
24 | self.custom_configure_flags.append("CXX={} g++".format(ccache)) | |
97d494b0 | 25 | self.custom_configure_flags.append("CFLAGS=-g -O0".format(ccache)) |
de9b991b JR |
26 | |
27 | """ A collection of Project dependencies """ | |
fe7b987e | 28 | self.dependencies = {} |
cb87ff89 | 29 | # used for project cache and pickle validation |
fe7b987e | 30 | self._immutable = False |
cb87ff89 | 31 | self._py_file_checksum = sha256_checksum(Settings.project_py_file_location) |
de9b991b JR |
32 | |
33 | # State | |
de9b991b JR |
34 | self.isBuilt = False |
35 | self.isConfigured = False | |
36 | self.isInstalled = False | |
37 | ||
fe7b987e JR |
38 | self.basedir = tmpdir |
39 | self.log_path = os.path.join(tmpdir, "log") | |
40 | self.source_path = os.path.join(tmpdir, "source") | |
41 | self.installation_path = os.path.join(tmpdir, "install") | |
42 | ||
97d494b0 JR |
43 | if os.path.isdir(self.basedir): |
44 | # Perform cleanup since it should not happen | |
45 | shutil.rmtree(self.basedir) | |
46 | ||
fe7b987e | 47 | os.makedirs(self.log_path) |
de9b991b JR |
48 | os.makedirs(self.source_path) |
49 | os.makedirs(self.installation_path) | |
de9b991b JR |
50 | |
51 | self.special_env_variables = {} | |
52 | ||
53 | # Init the repo for work | |
54 | self.checkout() | |
55 | self.bootstrap() | |
56 | ||
18aedaf9 JR |
57 | def add_special_env_variable(self, key, value): |
58 | if key in self.special_env_variables: | |
59 | _logger.warning("{} Special var {} is already defined".format( | |
60 | self.label, key)) | |
61 | raise Exception("Multiple definition of a special environment variable") | |
62 | self.special_env_variables[key] = value | |
63 | ||
de9b991b | 64 | def get_cppflags(self): |
18aedaf9 JR |
65 | cppflags = ["-I{}/include".format(self.installation_path)] |
66 | for key, dep in self.dependencies.items(): | |
67 | cppflags.append(dep.get_cppflags()) | |
68 | ||
69 | return " ".join(cppflags) | |
de9b991b JR |
70 | |
71 | def get_ldflags(self): | |
18aedaf9 JR |
72 | ldflags = ["-L{}/lib".format(self.installation_path)] |
73 | for key, dep in self.dependencies.items(): | |
74 | ldflags.append(dep.get_ldflags()) | |
75 | return " ".join(ldflags) | |
de9b991b JR |
76 | |
77 | def get_ld_library_path(self): | |
18aedaf9 JR |
78 | library_path = ["{}/lib".format(self.installation_path)] |
79 | for key, dep in self.dependencies.items(): | |
80 | library_path.append(dep.get_ld_library_path()) | |
81 | return ":".join(library_path) | |
82 | ||
83 | def get_bin_path(self): | |
84 | bin_path = ["{}/bin".format(self.installation_path)] | |
85 | for key, dep in self.dependencies.items(): | |
86 | bin_path.append(dep.get_bin_path()) | |
87 | return ":".join(bin_path) | |
de9b991b JR |
88 | |
89 | def get_env(self): | |
90 | """Modify environment to reflect dependency""" | |
18aedaf9 JR |
91 | env_var = {"CPPFLAGS": (self.get_cppflags(), " "), |
92 | "LDFLAGS": (self.get_ldflags(), " "), | |
93 | "LD_LIBRARY_PATH": (self.get_ld_library_path(), ":"), | |
94 | } | |
de9b991b JR |
95 | |
96 | env = os.environ.copy() | |
97 | ||
98 | for var, value in self.special_env_variables.items(): | |
99 | if var in env: | |
de9b991b | 100 | # Raise for now since no special cases is known |
18aedaf9 JR |
101 | _logger.warning("{} Special var {} is already defined".format( |
102 | self.label, var)) | |
de9b991b JR |
103 | raise Exception("Multiple definition of a special environment variable") |
104 | else: | |
105 | env[var] = value | |
106 | ||
fe7b987e | 107 | for key, dep in self.dependencies.items(): |
de9b991b | 108 | # Extra space just in case |
de9b991b JR |
109 | for var, value in dep.special_env_variables.items(): |
110 | if var in env: | |
de9b991b | 111 | # Raise for now since no special cases is known |
18aedaf9 JR |
112 | _logger.warning("{} Special var {} is already defined".format( |
113 | self.label, var)) | |
de9b991b JR |
114 | raise Exception("Multiple definition of a special environment variable") |
115 | else: | |
116 | env[var] = value | |
117 | ||
18aedaf9 JR |
118 | for var, (value, delimiter) in env_var.items(): |
119 | tmp = [value] | |
120 | if var in env: | |
121 | tmp.append(env[var]) | |
122 | env[var] = delimiter.join(tmp) | |
123 | ||
de9b991b JR |
124 | return env |
125 | ||
126 | def autobuild(self): | |
127 | """ | |
128 | Perform the bootstrap, configuration, build and install the | |
129 | project. Build dependencies if not already built | |
130 | """ | |
fe7b987e JR |
131 | if (self.isConfigured and self.isBuilt and self.isInstalled): |
132 | return | |
133 | ||
134 | if self._immutable: | |
135 | raise Exception("Object is immutable. Illegal autobuild") | |
136 | ||
137 | for key, dep in self.dependencies.items(): | |
de9b991b JR |
138 | dep.autobuild() |
139 | ||
fe7b987e | 140 | if self.isConfigured ^ self.isBuilt ^ self.isInstalled: |
de9b991b JR |
141 | raise Exception("Project steps where manually triggered. Can't autobuild") |
142 | ||
97d494b0 JR |
143 | _logger.debug("{} Autobuild configure".format(self.label)) |
144 | try: | |
145 | self.configure() | |
146 | except subprocess.CalledProcessError as e: | |
147 | _logger.error("{} Configure failed. See {} for more details.".format(self.label, self.log_path)) | |
148 | raise e | |
149 | ||
150 | _logger.debug("{} Autobuild build".format(self.label)) | |
151 | try: | |
152 | self.build() | |
153 | except subprocess.CalledProcessError as e: | |
154 | _logger.error("{} Build failed. See {} for more details.".format(self.label, self.log_path)) | |
155 | raise e | |
156 | ||
157 | _logger.debug("{} Autobuild install".format(self.label)) | |
158 | try: | |
159 | self.install() | |
160 | except subprocess.CalledProcessError as e: | |
161 | _logger.error("{} Install failed. See {} for more details.".format(self.label, self.log_path)) | |
162 | raise e | |
de9b991b JR |
163 | |
164 | def checkout(self): | |
fe7b987e JR |
165 | if self._immutable: |
166 | raise Exception("Object is immutable. Illegal checkout") | |
167 | ||
de9b991b JR |
168 | repo = git.Repo.clone_from(self.git_path, self.source_path) |
169 | commit = repo.commit(self.sha1) | |
170 | repo.head.reference = commit | |
171 | assert repo.head.is_detached | |
172 | repo.head.reset(index=True, working_tree=True) | |
173 | ||
174 | def bootstrap(self): | |
175 | """ | |
176 | Bootstap the project. Raise subprocess.CalledProcessError on | |
177 | bootstrap error. | |
178 | """ | |
fe7b987e JR |
179 | if self._immutable: |
180 | raise Exception("Object is immutable. Illegal bootstrap") | |
181 | ||
182 | out = os.path.join(self.log_path, "bootstrap.out") | |
183 | err = os.path.join(self.log_path, "bootstrap.err") | |
184 | ||
de9b991b | 185 | os.chdir(self.source_path) |
fe7b987e JR |
186 | with open(out, 'w') as stdout, open(err, 'w') as stderr: |
187 | p = subprocess.run(['./bootstrap'], stdout=stdout, stderr=stderr) | |
de9b991b JR |
188 | p.check_returncode() |
189 | return p | |
190 | ||
191 | def configure(self): | |
192 | """ | |
193 | Configure the project. | |
194 | Raises subprocess.CalledProcessError on configure error | |
195 | """ | |
fe7b987e JR |
196 | if self._immutable: |
197 | raise Exception("Object is immutable. Illegal configure") | |
198 | ||
de9b991b | 199 | # Check that all our dependencies were actually installed |
fe7b987e | 200 | for key, dep in self.dependencies.items(): |
de9b991b JR |
201 | if not dep.isInstalled: |
202 | # TODO: Custom exception here Dependency Error | |
203 | raise Exception("Dependency project flagged as not installed") | |
204 | ||
fe7b987e JR |
205 | out = os.path.join(self.log_path, "configure.out") |
206 | err = os.path.join(self.log_path, "configure.err") | |
207 | ||
de9b991b JR |
208 | os.chdir(self.source_path) |
209 | args = ['./configure'] | |
210 | prefix = '--prefix={}'.format(self.installation_path) | |
211 | args.append(prefix) | |
212 | args.extend(self.custom_configure_flags) | |
213 | ||
214 | # TODO: log output and add INFO log point | |
fe7b987e JR |
215 | with open(out, 'w') as stdout, open(err, 'w') as stderr: |
216 | p = subprocess.run(args, env=self.get_env(), stdout=stdout, | |
217 | stderr=stderr) | |
de9b991b JR |
218 | p.check_returncode() |
219 | self.isConfigured = True | |
220 | return p | |
221 | ||
222 | def build(self): | |
223 | """ | |
224 | Build the project. Raise subprocess.CalledProcessError on build | |
225 | error. | |
226 | """ | |
fe7b987e JR |
227 | if self._immutable: |
228 | raise Exception("Object is immutable. Illegal build") | |
229 | ||
230 | out = os.path.join(self.log_path, "build.out") | |
231 | err = os.path.join(self.log_path, "build.err") | |
232 | ||
de9b991b JR |
233 | os.chdir(self.source_path) |
234 | args = ['make'] | |
235 | env = self.get_env() | |
de9b991b JR |
236 | |
237 | # Number of usable cpu | |
238 | # https://docs.python.org/3/library/os.html#os.cpu_count | |
239 | num_cpu = str(len(os.sched_getaffinity(0))) | |
240 | args.append('-j') | |
241 | args.append(num_cpu) | |
97d494b0 | 242 | args.append('V=1') |
de9b991b JR |
243 | |
244 | # TODO: log output and add INFO log point with args | |
fe7b987e JR |
245 | with open(out, 'w') as stdout, open(err, 'w') as stderr: |
246 | p = subprocess.run(args, env=env, stdout=stdout, | |
247 | stderr=stderr) | |
de9b991b JR |
248 | p.check_returncode() |
249 | self.isBuilt = True | |
250 | return p | |
251 | ||
252 | def install(self): | |
253 | """ | |
254 | Install the project. Raise subprocess.CalledProcessError on | |
255 | bootstrap error | |
256 | """ | |
fe7b987e JR |
257 | if self._immutable: |
258 | raise Exception("Object is immutable. Illegal install") | |
259 | ||
97d494b0 JR |
260 | out = os.path.join(self.log_path, "install.out") |
261 | err = os.path.join(self.log_path, "install.err") | |
fe7b987e | 262 | |
de9b991b JR |
263 | os.chdir(self.source_path) |
264 | args = ['make', 'install'] | |
265 | ||
266 | # TODO: log output and add INFO log point | |
fe7b987e JR |
267 | with open(out, 'w') as stdout, open(err, 'w') as stderr: |
268 | p = subprocess.run(args, env=self.get_env(), stdout=stdout, | |
269 | stderr=stderr) | |
de9b991b JR |
270 | p.check_returncode() |
271 | self.isInstalled = True | |
272 | return p | |
273 | ||
274 | def cleanup(self): | |
275 | if os.path.exists(self.source_path): | |
276 | shutil.rmtree(self.source_path) | |
277 | if os.path.exists(self.installation_path): | |
278 | shutil.rmtree(self.installation_path) | |
279 | ||
280 | ||
281 | class Lttng_modules(Project): | |
97d494b0 JR |
282 | def __init__(self, label, git_path, sha1, tmpdir): |
283 | super(Lttng_modules, self).__init__(label=label, git_path=git_path, | |
284 | sha1=sha1, tmpdir=tmpdir) | |
285 | self.add_special_env_variable("MODPROBE_OPTIONS","-b {}".format(self.installation_path)) | |
286 | ||
de9b991b JR |
287 | def bootstrap(self): |
288 | pass | |
289 | ||
290 | def configure(self): | |
291 | pass | |
292 | ||
293 | def install(self): | |
fe7b987e JR |
294 | if self._immutable: |
295 | raise Exception("Object is immutable. Illegal install") | |
de9b991b JR |
296 | os.chdir(self.source_path) |
297 | args = ['make', 'INSTALL_MOD_PATH={}'.format(self.installation_path), | |
298 | 'modules_install'] | |
299 | p = subprocess.run(args, env=self.get_env(), stdout=subprocess.PIPE, | |
300 | stderr=subprocess.PIPE) | |
301 | p.check_returncode() | |
302 | ||
303 | # Perform a local depmod | |
304 | args = ['depmod', '-b', self.installation_path] | |
305 | p = subprocess.run(args, env=self.get_env()) | |
306 | p.check_returncode() | |
307 | self.isInstalled = True | |
308 | ||
309 | ||
310 | class Lttng_ust(Project): | |
311 | def __init__(self, label, git_path, sha1, tmpdir): | |
312 | super(Lttng_ust, self).__init__(label=label, git_path=git_path, | |
313 | sha1=sha1, tmpdir=tmpdir) | |
314 | self.custom_configure_flags.extend(['--disable-man-pages']) | |
315 | ||
316 | ||
317 | class Lttng_tools(Project): | |
18aedaf9 JR |
318 | def __init__(self, label, git_path, sha1, tmpdir): |
319 | super(Lttng_tools, self).__init__(label=label, git_path=git_path, | |
320 | sha1=sha1, tmpdir=tmpdir) | |
321 | self.add_special_env_variable("LTTNG_SESSION_CONFIG_XSD_PATH", | |
322 | os.path.join(self.installation_path, "share/xml/lttng/")) | |
de9b991b JR |
323 | |
324 | ||
325 | class Babeltrace(Project): | |
326 | pass |