Skip to content

Instantly share code, notes, and snippets.

@brettdh
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettdh/ab71855542fb42cc7dc0 to your computer and use it in GitHub Desktop.
Save brettdh/ab71855542fb42cc7dc0 to your computer and use it in GitHub Desktop.
Script to search paths for importable Android NDK modules and symlink their build files to $NDK_MODULE_PATH.
#!/usr/bin/env python
import os, sys, re
import argparse
from itertools import combinations
module_re = re.compile("LOCAL_MODULE[^a-z_]+([a-z_]+)")
build_re = re.compile("BUILD_SHARED_LIBRARY")
def getModules(android_makefile):
cur_module = None
modules = []
with open(android_makefile) as f:
for line in f.readlines():
module_match = re.search(module_re, line)
if module_match:
cur_module = module_match.group(1)
continue
build_match = re.search(build_re, line)
if build_match:
modules.append(cur_module)
return modules
def main():
if "NDK_MODULE_PATH" not in os.environ:
print "Please define NDK_MODULE_PATH first."
sys.exit(1)
parser = argparse.ArgumentParser(description="Searches a directory tree for NDK modules and " +
"adds symlinks to NDK_MODULE_PATH for them.")
parser.add_argument("search_dirs", nargs="+",
help="one or more directories to search for NDK modules.")
parser.add_argument("--namespace", default="edu.umich.mobility", help="Directory where symlinks will be placed.")
args = parser.parse_args()
for dir_pair in combinations(args.search_dirs, 2):
if dir_pair[0] in dir_pair[1] or dir_pair[1] in dir_pair[0]:
print "Error: search dirs mustn't overlap"
parser.error("%s and %s have a common path element" % dir_pair)
module_dirs = {}
ndk_module_path = os.environ["NDK_MODULE_PATH"].strip()
if ":" in ndk_module_path:
ndk_module_path = ndk_module_path.split(":")[0]
if not os.path.exists(ndk_module_path):
os.mkdir(ndk_module_path)
namespace_path = "%s/%s" % (ndk_module_path, args.namespace)
if not os.path.exists(namespace_path):
os.mkdir(namespace_path)
for root_dir in args.search_dirs:
if not os.path.exists(root_dir):
print "Skipping %s; doesn't exist" % root_dir
continue
for dirpath, dirs, files in os.walk(root_dir):
if "Android.mk" in files:
mk_path = "%s/Android.mk" % dirpath
print "Found %s" % mk_path
cur_modules = getModules(mk_path)
for module_name in cur_modules:
module_dirs[module_name] = dirpath
print "About to symlink modules into %s" % namespace_path
for entry in module_dirs.items():
module_name, module_path = entry
print " %-10s => %s" % (module_name, module_path)
confirm = raw_input("Proceed? [y/n] ")
if not confirm.lower().startswith("y"):
print "Making no symlinks."
sys.exit(1)
for entry in module_dirs.items():
module_name, module_path = entry
link_path = "%s/%s" % (namespace_path, module_name)
if os.path.exists(link_path):
print "%s already exists" % link_path
else:
print "Symlink: %s => %s" % (link_path, module_path)
os.symlink(module_path, link_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment