Skip to main content

Portable C++ App

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 subprocess

class Cmd():

def __init__(self):
self.cwd = '.'

def cd(self, to):
self.cwd = to

def 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 = executable
self.out = out
self.app_out = out+'app/'
self.blacklist = []

def run(self):
executable = self.executable
out = self.out
app_out = self.app_out

cmd = Cmd()
lines = cmd.run(f'ldd {executable}').split('\n')

# collect the libs
cmd.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.sh
executable_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 Dockerfile
self.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 True
return False

def 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

Popular posts from this blog

Futureproof Software

From here:  https://dzone.com/articles/the-secrets-of-futureproof-software Self-healing Self-patching, or more broadly, self-updating Backward compatibility Dynamic adaptation/ability to evolve over time Intent-based Made up of reusable futureproof components

Choose Projects you do NOT do!

Inspiration from FluentC++ Choose which project NOT to work on. Be clear what you want and which projects are toxic. Keep the Hedgehog-concept in mind. Be sure that you are a) deeply passionate about the project b) the project can be the best in the world quality c) you can earn money with it If one of the three points is not met and not realistic soon don't do the project. Play around with it privately. Maybe you can pivot there so that it meets the criteria.