2 # buildorder.py - script to generate a build order respecting package dependencies
10 if len(sys
.argv
) != 2: die("Supply path to packages directory as first and only argument")
11 packages_dir
= sys
.argv
[1]
12 if not os
.path
.isdir(packages_dir
): die(packages_dir
+ ' is not a directory')
15 def __init__(self
, name
):
17 self
.remaining_dependencies
= set() # String
18 self
.sub_packages
= set() # String
19 self
.prerequisite_for
= set() # Packages that needs this package
21 all_packages
= [] # List of all DebianPackage:s
22 packages_map
= {} # Mapping from package name to DebianPackage (if subpackage, mapping from subpackage name to parent package)
24 for subdir_name
in sorted(os
.listdir(packages_dir
)):
25 subdir_path
= packages_dir
+ '/' + subdir_name
26 if os
.path
.exists(subdir_path
+ '/BROKEN.txt'): continue
27 build_sh_path
= subdir_path
+ '/build.sh'
29 this_package
= DebianPackage(subdir_name
)
30 all_packages
.append(this_package
)
31 packages_map
[this_package
.name
] = this_package
33 if not os
.path
.isfile(build_sh_path
): die('The directory ' + subdir_name
+ ' does not contain build.sh')
34 with
open(build_sh_path
) as build_sh_file
:
35 for line
in build_sh_file
:
36 if line
.startswith('TERMUX_PKG_DEPENDS='):
37 deps_comma_separated
= line
[(line
.index('=')+2):(len(line
)-2)]
38 for dep
in deps_comma_separated
.split(','):
40 this_package
.remaining_dependencies
.add(dep
)
41 for file_in_subdir_name
in sorted(os
.listdir(subdir_path
)):
42 if file_in_subdir_name
.endswith('.subpackage.sh'):
43 subpackage_name
= file_in_subdir_name
[0:-len(".subpackage.sh"):]
44 this_package
.sub_packages
.add(subpackage_name
)
45 packages_map
[subpackage_name
] = this_package
46 with
open(subdir_path
+ '/' + file_in_subdir_name
) as subpackage_sh_file
:
47 for line
in subpackage_sh_file
:
48 if line
.startswith('TERMUX_SUBPKG_DEPENDS='):
49 deps_comma_separated
= line
[(line
.index('=')+2):(len(line
)-2)]
50 for dep
in deps_comma_separated
.split(','):
52 this_package
.remaining_dependencies
.add(dep
)
53 this_package
.remaining_dependencies
.discard(this_package
.name
) # Do not depend on itself
54 this_package
.remaining_dependencies
.difference_update(this_package
.sub_packages
) # Do not depend on any sub package
56 for package
in all_packages
:
57 for remaining
in package
.remaining_dependencies
:
58 if not remaining
in packages_map
: die('Package ' + package
.name
+ ' depends on non-existing package "' + remaining
+ '"')
59 packages_map
[remaining
].prerequisite_for
.add(package
)
61 # List of all DebianPackage:s without dependencies
62 packages_without_deps
= [p
for p
in all_packages
if not p
.remaining_dependencies
]
63 if not packages_without_deps
: die('No package without dependency - where to start?')
65 # Sort alphabetically, but with libandroid-support first (since dependency on libandroid-support
66 # does not need to be declared explicitly, so anything might in theory depend on it to build):
67 packages_without_deps
.sort(key
=lambda p
: 'aaaa' if p
.name
== 'libandroid-support' else p
.name
, reverse
=True)
71 while packages_without_deps
:
72 pkg
= packages_without_deps
.pop()
73 build_order
.append(pkg
)
74 for other_package
in pkg
.prerequisite_for
:
75 other_package
.remaining_dependencies
.discard(pkg
.name
) # Remove this package
76 other_package
.remaining_dependencies
.difference_update(pkg
.sub_packages
) # .. and all its subpackages
77 if not other_package
.remaining_dependencies
:
78 # Check if the other package is ready to build now
79 packages_without_deps
.append(other_package
)
81 if len(all_packages
) != len(build_order
):
82 print("ERROR: Cycle exists. Remaining: ");
83 for pkg
in all_packages
:
84 if pkg
not in build_order
: print(pkg
.name
)
87 for pkg
in build_order
: print(pkg
.name
)