make file 生成
需要批量写mk文件时 使用模板自动填充
useage :
./auto_generator.py vendor lib so
#!/usr/bin/env python
import sys
import os
import re
import zipfile
import shutil
templet = """include $(CLEAR_VARS)
LOCAL_MODULE := %s
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).so
LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/lib
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_SUFFIX := .so
include $(BUILD_PREBUILT)
"""
def main(argv):
preinstall_dir = os.path.join(argv[1],argv[2])
print "preinstall dir "+ preinstall_dir
if os.path.exists(preinstall_dir):
#Use to include modules
isfound = 'not_found_lib'
include_path = preinstall_dir + '/preinstall.mk'
android_path = preinstall_dir + '/Android.mk'
if os.path.exists(include_path):
os.remove(include_path)
if os.path.exists(android_path):
os.remove(android_path)
includefile = file(include_path, 'w')
androidfile = file(android_path, 'w')
androidfile.write("include $(call all-subdir-makefiles)\n\n")
MY_LOCAL_PREBUILT_JNI_LIBS = '\\' + '\n'
for root, dirs, files in os.walk(preinstall_dir):
for file_name in files:
p = re.compile(r'\S*(?=.so\b)')
found = p.search(file_name)
if found:
include_apk_path = preinstall_dir + '/' + found.group()
makefile_path = include_apk_path + '/Android.mk'
apk = preinstall_dir + '/' + found.group() + '.so'
apkpath = preinstall_dir + '/' + found.group() + '/'
if os.path.exists(include_apk_path):
shutil.rmtree(include_apk_path)
os.makedirs(include_apk_path)
shutil.move(apk,apkpath)
makefile = file(makefile_path,'w')
makefile.write("LOCAL_PATH := $(my-dir)\n\n")
makefile.write(templet % (found.group()))
makefile.close()
for root, dirs,files in os.walk(preinstall_dir):
for dir_file in dirs:
includefile.write('PRODUCT_PACKAGES += %s\n' %dir_file)
break
includefile.close()
if __name__=="__main__":
main(sys.argv)