C++ Apps are not portable because of the dynamic libs. This (rough) script creates a portable version together with a Dockerfile to evaluate it:
import subprocessclass Cmd():def __init__(self):self.cwd = '.'def cd(self, to):self.cwd = todef run(self, command):print(command)cammand_splitted = [x for x in command.split(' ') if x]result = subprocess.run(cammand_splitted, stdout=subprocess.PIPE, cwd=self.cwd)return result.stdout.decode('utf-8')class Bundler():def __init__(self, executable, out, blacklist=[]):self.executable = executableself.out = outself.app_out = out+'app/'self.blacklist = []def run(self):executable = self.executableout = self.outapp_out = self.app_outcmd = Cmd()lines = cmd.run(f'ldd {executable}').split('\n')# collect the libscmd.run(f'mkdir -p {app_out}libs/')cmd.run(f'mkdir -p {app_out}bin/')cmd.run(f'cp {executable} {app_out}bin/')for line in lines:e = self.extract_from_line(line, 2)if e and not self.in_blacklist(e, self.blacklist):cmd.run(f'cp {e} {app_out}libs/')# make run.shexecutable_name = executable.split('/')[-1]t = "${@:2}"self.write_lines(app_out + 'run.sh', ['#!/bin/bash','export LD_LIBRARY_PATH=$(pwd)/libs',f'./bin/{executable_name} {t}'])cmd.run(f'chmod +x {app_out}run.sh')# create Dockerfileself.write_lines(out + 'Dockerfile', ['FROM ubuntu:18.04','ADD app /app','WORKDIR /app','','CMD ["/app/run.sh"]'])self.write_lines(out + 'run.sh', ['docker rmi out -f','docker build . -t out',f'docker run out {t}',])cmd.run(f'chmod +x {out}run.sh')def extract_from_line(self, line, n):line = line.strip()elements = line.split(' ')return elements[n] if len(elements) > n else ''def in_blacklist(self, element, blacklist):for b in blacklist:if b in element:return Truereturn Falsedef write_lines(self, filename, lines):with open(filename, 'w') as the_file:for line in lines:the_file.write(line+'\n')Bundler('build/run_video_slam_headless', 'out/').run()
Comments
Post a Comment