2015-06-13 01:03:31 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# buildorder.py - script to generate a build order respecting package dependencies
|
|
|
|
|
2015-12-24 04:43:35 +01:00
|
|
|
import os
|
2016-04-29 14:14:28 +02:00
|
|
|
import re
|
2015-12-24 04:43:35 +01:00
|
|
|
import sys
|
2015-06-13 01:03:31 +02:00
|
|
|
|
2015-12-24 07:52:06 +01:00
|
|
|
from itertools import filterfalse
|
|
|
|
|
|
|
|
|
|
|
|
# https://docs.python.org/3/library/itertools.html#itertools-recipes
|
|
|
|
|
|
|
|
def unique_everseen(iterable, key=None):
|
|
|
|
"List unique elements, preserving order. Remember all elements ever seen."
|
|
|
|
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
|
|
|
|
# unique_everseen('ABBCcAD', str.lower) --> A B C D
|
|
|
|
seen = set()
|
|
|
|
seen_add = seen.add
|
|
|
|
if key is None:
|
|
|
|
for element in filterfalse(seen.__contains__, iterable):
|
|
|
|
seen_add(element)
|
|
|
|
yield element
|
|
|
|
else:
|
|
|
|
for element in iterable:
|
|
|
|
k = key(element)
|
|
|
|
if k not in seen:
|
|
|
|
seen_add(k)
|
|
|
|
yield element
|
|
|
|
|
2015-06-13 01:03:31 +02:00
|
|
|
|
2015-12-24 04:43:35 +01:00
|
|
|
def die(msg):
|
|
|
|
sys.exit('ERROR: ' + msg)
|
2015-06-13 01:03:31 +02:00
|
|
|
|
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
class TermuxBuildFile(object):
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
|
|
|
|
def _get_dependencies(self):
|
|
|
|
pkg_dep_prefix = 'TERMUX_PKG_DEPENDS='
|
|
|
|
subpkg_dep_prefix = 'TERMUX_SUBPKG_DEPENDS='
|
|
|
|
|
2017-01-09 11:26:31 +01:00
|
|
|
with open(self.path, encoding="utf-8") as f:
|
2015-12-24 07:20:47 +01:00
|
|
|
prefix = None
|
|
|
|
for line in f:
|
|
|
|
if line.startswith(pkg_dep_prefix):
|
|
|
|
prefix = pkg_dep_prefix
|
|
|
|
elif line.startswith(subpkg_dep_prefix):
|
|
|
|
prefix = subpkg_dep_prefix
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2017-01-03 10:26:10 +01:00
|
|
|
comma_deps = line[len(prefix):].replace('"', '').replace("'", '')
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
return set([
|
2016-04-29 14:14:28 +02:00
|
|
|
# Replace parenthesis to handle version qualifiers, as in "gcc (>= 5.0)":
|
2017-01-03 23:46:59 +01:00
|
|
|
re.sub(r'\(.*?\)', '', dep).replace('-dev', '').strip() for dep in comma_deps.split(',')
|
2015-12-24 07:20:47 +01:00
|
|
|
])
|
|
|
|
|
|
|
|
# no deps found
|
|
|
|
return set()
|
|
|
|
|
|
|
|
|
2015-12-24 06:04:28 +01:00
|
|
|
class TermuxPackage(object):
|
2015-12-24 07:20:47 +01:00
|
|
|
PACKAGES_DIR = 'packages'
|
|
|
|
|
2015-12-24 04:43:35 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
2015-12-24 07:20:47 +01:00
|
|
|
self.dir = os.path.join(self.PACKAGES_DIR, name)
|
|
|
|
|
|
|
|
# search package build.sh
|
|
|
|
build_sh_path = os.path.join(self.dir, 'build.sh')
|
|
|
|
if not os.path.isfile(build_sh_path):
|
2016-06-02 17:13:53 +02:00
|
|
|
raise Exception("build.sh not found for package '" + name + "'")
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
self.buildfile = TermuxBuildFile(build_sh_path)
|
|
|
|
self.deps = self.buildfile._get_dependencies()
|
2016-09-16 11:48:02 +02:00
|
|
|
if 'libandroid-support' not in self.deps and self.name != 'libandroid-support':
|
|
|
|
# Every package may depend on libandroid-support without declaring it:
|
|
|
|
self.deps.add('libandroid-support')
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
# search subpackages
|
|
|
|
self.subpkgs = []
|
|
|
|
|
|
|
|
for filename in os.listdir(self.dir):
|
|
|
|
if not filename.endswith('.subpackage.sh'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
subpkg_name = filename.split('.subpackage.sh')[0]
|
|
|
|
subpkg = TermuxSubPackage(subpkg_name, parent=self)
|
|
|
|
|
|
|
|
self.subpkgs.append(subpkg)
|
|
|
|
self.deps |= subpkg.deps
|
|
|
|
|
|
|
|
# Do not depend on itself
|
|
|
|
self.deps.discard(self.name)
|
|
|
|
# Do not depend on any sub package
|
|
|
|
self.deps.difference_update([subpkg.name for subpkg in self.subpkgs])
|
|
|
|
|
|
|
|
self.needed_by = set() # to be completed outside, reverse of deps
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<{} '{}'>".format(self.__class__.__name__, self.name)
|
|
|
|
|
|
|
|
|
|
|
|
class TermuxSubPackage(TermuxPackage):
|
|
|
|
|
|
|
|
def __init__(self, name, parent=None):
|
|
|
|
if parent is None:
|
|
|
|
raise Exception("SubPackages should have a parent")
|
|
|
|
|
|
|
|
self.name = name
|
|
|
|
self.parent = parent
|
|
|
|
self.buildfile = TermuxBuildFile(os.path.join(self.parent.dir, name + '.subpackage.sh'))
|
|
|
|
self.deps = self.buildfile._get_dependencies()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<{} '{}' parent='{}'>".format(self.__class__.__name__, self.name, self.parent)
|
2015-12-24 04:43:35 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
# Tracks non-visited deps for each package
|
|
|
|
remaining_deps = {}
|
2015-12-24 04:43:35 +01:00
|
|
|
|
2015-12-24 06:04:28 +01:00
|
|
|
# Mapping from package name to TermuxPackage
|
2015-12-24 04:43:35 +01:00
|
|
|
# (if subpackage, mapping from subpackage name to parent package)
|
2015-12-24 07:20:47 +01:00
|
|
|
pkgs_map = {}
|
2015-06-13 01:03:31 +02:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
# Reverse dependencies
|
|
|
|
pkg_depends_on = {}
|
2015-12-24 04:43:35 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
PACKAGES_DIR = 'packages'
|
2015-12-24 04:43:35 +01:00
|
|
|
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
def populate():
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
for pkgdir_name in sorted(os.listdir(PACKAGES_DIR)):
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
pkg = TermuxPackage(pkgdir_name)
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
pkgs_map[pkg.name] = pkg
|
|
|
|
|
|
|
|
for subpkg in pkg.subpkgs:
|
|
|
|
pkgs_map[subpkg.name] = pkg
|
|
|
|
remaining_deps[subpkg.name] = set(subpkg.deps)
|
|
|
|
|
|
|
|
remaining_deps[pkg.name] = set(pkg.deps)
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
all_pkg_names = set(pkgs_map.keys())
|
|
|
|
|
|
|
|
for name, pkg in pkgs_map.items():
|
|
|
|
for dep_name in remaining_deps[name]:
|
|
|
|
if dep_name not in all_pkg_names:
|
2015-12-24 04:47:38 +01:00
|
|
|
die('Package %s depends on non-existing package "%s"' % (
|
2015-12-24 07:20:47 +01:00
|
|
|
name, dep_name
|
2015-12-24 04:47:38 +01:00
|
|
|
))
|
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
dep_pkg = pkgs_map[dep_name]
|
|
|
|
dep_pkg.needed_by.add(pkg)
|
|
|
|
|
|
|
|
|
2015-12-24 07:52:06 +01:00
|
|
|
def generate_full_buildorder():
|
|
|
|
build_order = []
|
|
|
|
|
2015-12-24 06:04:28 +01:00
|
|
|
# List of all TermuxPackages without dependencies
|
2015-12-24 07:20:47 +01:00
|
|
|
leaf_pkgs = [pkg for name, pkg in pkgs_map.items() if not pkg.deps]
|
|
|
|
|
|
|
|
if not leaf_pkgs:
|
|
|
|
die('No package without dependencies - where to start?')
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2016-09-16 11:48:02 +02:00
|
|
|
# Sort alphabetically:
|
|
|
|
pkg_queue = sorted(leaf_pkgs, key=lambda p: p.name)
|
2015-12-24 04:47:38 +01:00
|
|
|
|
|
|
|
# Topological sorting
|
2015-12-24 07:20:47 +01:00
|
|
|
visited = set()
|
|
|
|
|
|
|
|
while pkg_queue:
|
|
|
|
pkg = pkg_queue.pop(0)
|
|
|
|
if pkg.name in visited:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# print("Processing {}:".format(pkg.name), pkg.needed_by)
|
2015-12-24 07:34:27 +01:00
|
|
|
visited.add(pkg.name)
|
2015-12-24 04:47:38 +01:00
|
|
|
build_order.append(pkg)
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
for other_pkg in sorted(pkg.needed_by, key=lambda p: p.name):
|
2015-12-24 07:34:27 +01:00
|
|
|
# Remove this pkg from deps
|
2015-12-24 07:20:47 +01:00
|
|
|
remaining_deps[other_pkg.name].discard(pkg.name)
|
|
|
|
# ... and all its subpackages
|
|
|
|
remaining_deps[other_pkg.name].difference_update(
|
|
|
|
[subpkg.name for subpkg in pkg.subpkgs]
|
|
|
|
)
|
|
|
|
|
2015-12-24 07:34:27 +01:00
|
|
|
if not remaining_deps[other_pkg.name]: # all deps were already appended?
|
|
|
|
pkg_queue.append(other_pkg) # should be processed
|
2015-12-24 07:20:47 +01:00
|
|
|
|
|
|
|
if set(pkgs_map.values()) != set(build_order):
|
2015-12-24 04:47:38 +01:00
|
|
|
print("ERROR: Cycle exists. Remaining: ")
|
2015-12-24 07:20:47 +01:00
|
|
|
for name, pkg in pkgs_map.items():
|
2015-12-24 04:47:38 +01:00
|
|
|
if pkg not in build_order:
|
2015-12-24 07:20:47 +01:00
|
|
|
print(name, remaining_deps[name])
|
|
|
|
|
2015-12-24 04:47:38 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2015-12-24 07:52:06 +01:00
|
|
|
return build_order
|
2015-12-24 04:47:38 +01:00
|
|
|
|
2015-12-24 07:20:47 +01:00
|
|
|
|
2015-12-24 07:52:06 +01:00
|
|
|
def deps_then_me(pkg):
|
|
|
|
l = []
|
2015-12-24 07:29:34 +01:00
|
|
|
|
|
|
|
for dep in sorted(pkg.deps):
|
2015-12-24 07:52:06 +01:00
|
|
|
l += deps_then_me(pkgs_map[dep])
|
|
|
|
l += [pkg]
|
|
|
|
|
|
|
|
return l
|
|
|
|
|
|
|
|
|
|
|
|
def generate_targets_buildorder(targetnames):
|
|
|
|
buildorder = []
|
|
|
|
|
|
|
|
for pkgname in targetnames:
|
2017-01-08 13:37:43 +01:00
|
|
|
if not pkgname in pkgs_map:
|
|
|
|
die('Dependencies for ' + pkgname + ' could not be calculated (skip dependency check with -s)')
|
2015-12-24 07:52:06 +01:00
|
|
|
buildorder += deps_then_me(pkgs_map[pkgname])
|
|
|
|
|
|
|
|
return unique_everseen(buildorder)
|
2015-12-24 07:29:34 +01:00
|
|
|
|
2015-12-24 04:47:38 +01:00
|
|
|
if __name__ == '__main__':
|
2015-12-24 07:20:47 +01:00
|
|
|
populate()
|
2015-12-24 07:29:34 +01:00
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
2015-12-24 07:52:06 +01:00
|
|
|
bo = generate_full_buildorder()
|
|
|
|
else:
|
|
|
|
bo = generate_targets_buildorder(sys.argv[1:])
|
2015-12-24 07:29:34 +01:00
|
|
|
|
2015-12-24 07:52:06 +01:00
|
|
|
for pkg in bo:
|
|
|
|
print(pkg.name)
|