当前位置:   article > 正文

一文看懂Android.bp和Androd.mk区别以及宏变量对应关系_android.bp 宏

android.bp 宏

目录

一、区别

二、mk转换为bp

三、宏变量对应关系

四、常用宏变量作用


一、区别

  • Android.bp --> Soong --> Ninja
  • Makefile or Android.mk --> kati --> Ninja

最终都是生成Ninja格式文件进行编译。

相关概念:

  • ninja是一个编译框架,会根据相应的ninja格式的配置文件进行编译,但是ninja文件一般不会手动修改,而是通过将 Android.bp文件转换成ninja格式文件来编译
  • Soong类似于之前的Makefile编译系统的核心,负责提供Android.bp语义解析,并将之转换成Ninja文件。
  • Blueprint是生成、解析Android.bp的工具,是Soong的一部分。Soong负责Android编译而设计的工具,而Blueprint只是解析文件格式,Soong解析内容的具体含义。Blueprint和Soong都是由Golang写的项目,从Android 7.0,prebuilts/go/目录下新增Golang所需的运行环境,在编译时使用
  • kati是专为Android开发的一个基于Golang和C++的工具,主要功能是把Android.mk文件转换成Ninja文件。代码路径是build/kati/,编译后的产物是ckati

二、mk转换为bp

Android.bp的出现就是为了替换Android.mk文件。bp跟mk文件不同,它是纯粹的配置,没有分支、循环等流程控制,不能做算数逻辑运算。如果需要控制逻辑,那么只能通过Go语言编写。旧的mk可以转换为bp,Soong会编译生成一个androidmk命令,用于将Android.mk文件转换为Android.bp文件。只针对简单的mk文件转换,涉及分支,循环等控制转换并不准确。即:

Android.mk --> Soong中androidmk --> Android.bp

三、宏变量对应关系

直接在"build/soong/androidmk/androidmk/android.go"文件中找:

  1. var moduleTypes = map[string]string{
  2. "BUILD_SHARED_LIBRARY": "cc_library_shared",
  3. "BUILD_STATIC_LIBRARY": "cc_library_static",
  4. "BUILD_HOST_SHARED_LIBRARY": "cc_library_host_shared",
  5. "BUILD_HOST_STATIC_LIBRARY": "cc_library_host_static",
  6. "BUILD_HEADER_LIBRARY": "cc_library_headers",
  7. "BUILD_EXECUTABLE": "cc_binary",
  8. "BUILD_HOST_EXECUTABLE": "cc_binary_host",
  9. "BUILD_NATIVE_TEST": "cc_test",
  10. "BUILD_HOST_NATIVE_TEST": "cc_test_host",
  11. "BUILD_NATIVE_BENCHMARK": "cc_benchmark",
  12. "BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",
  13. "BUILD_JAVA_LIBRARY": "java_library_installable", // will be rewritten to java_library by bpfix
  14. "BUILD_STATIC_JAVA_LIBRARY": "java_library",
  15. "BUILD_HOST_JAVA_LIBRARY": "java_library_host",
  16. "BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
  17. "BUILD_PACKAGE": "android_app",
  18. "BUILD_RRO_PACKAGE": "runtime_resource_overlay",
  19. "BUILD_CTS_EXECUTABLE": "cc_binary", // will be further massaged by bpfix depending on the output path
  20. "BUILD_CTS_SUPPORT_PACKAGE": "cts_support_package", // will be rewritten to android_test by bpfix
  21. "BUILD_CTS_PACKAGE": "cts_package", // will be rewritten to android_test by bpfix
  22. "BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix
  23. "BUILD_CTS_HOST_JAVA_LIBRARY": "cts_host_java_library", // will be rewritten to java_library_host by bpfix
  24. }
  25. var prebuiltTypes = map[string]string{
  26. "SHARED_LIBRARIES": "cc_prebuilt_library_shared",
  27. "STATIC_LIBRARIES": "cc_prebuilt_library_static",
  28. "EXECUTABLES": "cc_prebuilt_binary",
  29. "JAVA_LIBRARIES": "java_import",
  30. "APPS": "android_app_import",
  31. "ETC": "prebuilt_etc",
  32. }
  33. var rewriteProperties = map[string](func(variableAssignmentContext) error){
  34. // custom functions
  35. "LOCAL_32_BIT_ONLY": local32BitOnly,
  36. "LOCAL_AIDL_INCLUDES": localAidlIncludes,
  37. "LOCAL_ASSET_DIR": localizePathList("asset_dirs"),
  38. "LOCAL_C_INCLUDES": localIncludeDirs,
  39. "LOCAL_EXPORT_C_INCLUDE_DIRS": exportIncludeDirs,
  40. "LOCAL_JARJAR_RULES": localizePath("jarjar_rules"),
  41. "LOCAL_LDFLAGS": ldflags,
  42. "LOCAL_MODULE_CLASS": prebuiltClass,
  43. "LOCAL_MODULE_STEM": stem,
  44. "LOCAL_MODULE_HOST_OS": hostOs,
  45. "LOCAL_RESOURCE_DIR": localizePathList("resource_dirs"),
  46. "LOCAL_SANITIZE": sanitize(""),
  47. "LOCAL_SANITIZE_DIAG": sanitize("diag."),
  48. "LOCAL_STRIP_MODULE": strip(),
  49. "LOCAL_CFLAGS": cflags,
  50. "LOCAL_UNINSTALLABLE_MODULE": invert("installable"),
  51. "LOCAL_PROGUARD_ENABLED": proguardEnabled,
  52. "LOCAL_MODULE_PATH": prebuiltModulePath,
  53. "LOCAL_REPLACE_PREBUILT_APK_INSTALLED": prebuiltPreprocessed,
  54. // composite functions
  55. "LOCAL_MODULE_TAGS": includeVariableIf(bpVariable{"tags", bpparser.ListType}, not(valueDumpEquals("optional"))),
  56. // skip functions
  57. "LOCAL_ADDITIONAL_DEPENDENCIES": skip, // TODO: check for only .mk files?
  58. "LOCAL_CPP_EXTENSION": skip,
  59. "LOCAL_MODULE_SUFFIX": skip, // TODO
  60. "LOCAL_PATH": skip, // Nothing to do, except maybe avoid the "./" in paths?
  61. "LOCAL_PRELINK_MODULE": skip, // Already phased out
  62. "LOCAL_BUILT_MODULE_STEM": skip,
  63. "LOCAL_USE_AAPT2": skip, // Always enabled in Soong
  64. "LOCAL_JAR_EXCLUDE_FILES": skip, // Soong never excludes files from jars
  65. "LOCAL_ANNOTATION_PROCESSOR_CLASSES": skip, // Soong gets the processor classes from the plugin
  66. "LOCAL_CTS_TEST_PACKAGE": skip, // Obsolete
  67. "LOCAL_JACK_ENABLED": skip, // Obselete
  68. "LOCAL_JACK_FLAGS": skip, // Obselete
  69. }
  70. func init() {
  71. addStandardProperties(bpparser.StringType,
  72. map[string]string{
  73. "LOCAL_MODULE": "name",
  74. "LOCAL_CXX_STL": "stl",
  75. "LOCAL_MULTILIB": "compile_multilib",
  76. "LOCAL_ARM_MODE_HACK": "instruction_set",
  77. "LOCAL_SDK_VERSION": "sdk_version",
  78. "LOCAL_MIN_SDK_VERSION": "min_sdk_version",
  79. "LOCAL_NDK_STL_VARIANT": "stl",
  80. "LOCAL_JAR_MANIFEST": "manifest",
  81. "LOCAL_CERTIFICATE": "certificate",
  82. "LOCAL_PACKAGE_NAME": "name",
  83. "LOCAL_MODULE_RELATIVE_PATH": "relative_install_path",
  84. "LOCAL_PROTOC_OPTIMIZE_TYPE": "proto.type",
  85. "LOCAL_MODULE_OWNER": "owner",
  86. "LOCAL_RENDERSCRIPT_TARGET_API": "renderscript.target_api",
  87. "LOCAL_NOTICE_FILE": "notice",
  88. "LOCAL_JAVA_LANGUAGE_VERSION": "java_version",
  89. "LOCAL_INSTRUMENTATION_FOR": "instrumentation_for",
  90. "LOCAL_MANIFEST_FILE": "manifest",
  91. "LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING": "dex_preopt.profile",
  92. "LOCAL_TEST_CONFIG": "test_config",
  93. "LOCAL_RRO_THEME": "theme",
  94. })
  95. addStandardProperties(bpparser.ListType,
  96. map[string]string{
  97. "LOCAL_SRC_FILES": "srcs",
  98. "LOCAL_SRC_FILES_EXCLUDE": "exclude_srcs",
  99. "LOCAL_HEADER_LIBRARIES": "header_libs",
  100. "LOCAL_SHARED_LIBRARIES": "shared_libs",
  101. "LOCAL_STATIC_LIBRARIES": "static_libs",
  102. "LOCAL_WHOLE_STATIC_LIBRARIES": "whole_static_libs",
  103. "LOCAL_SYSTEM_SHARED_LIBRARIES": "system_shared_libs",
  104. "LOCAL_ASFLAGS": "asflags",
  105. "LOCAL_CLANG_ASFLAGS": "clang_asflags",
  106. "LOCAL_COMPATIBILITY_SUPPORT_FILES": "data",
  107. "LOCAL_CONLYFLAGS": "conlyflags",
  108. "LOCAL_CPPFLAGS": "cppflags",
  109. "LOCAL_REQUIRED_MODULES": "required",
  110. "LOCAL_HOST_REQUIRED_MODULES": "host_required",
  111. "LOCAL_TARGET_REQUIRED_MODULES": "target_required",
  112. "LOCAL_OVERRIDES_MODULES": "overrides",
  113. "LOCAL_LDLIBS": "host_ldlibs",
  114. "LOCAL_CLANG_CFLAGS": "clang_cflags",
  115. "LOCAL_YACCFLAGS": "yacc.flags",
  116. "LOCAL_SANITIZE_RECOVER": "sanitize.recover",
  117. "LOCAL_LOGTAGS_FILES": "logtags",
  118. "LOCAL_EXPORT_HEADER_LIBRARY_HEADERS": "export_header_lib_headers",
  119. "LOCAL_EXPORT_SHARED_LIBRARY_HEADERS": "export_shared_lib_headers",
  120. "LOCAL_EXPORT_STATIC_LIBRARY_HEADERS": "export_static_lib_headers",
  121. "LOCAL_INIT_RC": "init_rc",
  122. "LOCAL_VINTF_FRAGMENTS": "vintf_fragments",
  123. "LOCAL_TIDY_FLAGS": "tidy_flags",
  124. // TODO: This is comma-separated, not space-separated
  125. "LOCAL_TIDY_CHECKS": "tidy_checks",
  126. "LOCAL_RENDERSCRIPT_INCLUDES": "renderscript.include_dirs",
  127. "LOCAL_RENDERSCRIPT_FLAGS": "renderscript.flags",
  128. "LOCAL_JAVA_RESOURCE_DIRS": "java_resource_dirs",
  129. "LOCAL_JAVA_RESOURCE_FILES": "java_resources",
  130. "LOCAL_JAVACFLAGS": "javacflags",
  131. "LOCAL_ERROR_PRONE_FLAGS": "errorprone.javacflags",
  132. "LOCAL_DX_FLAGS": "dxflags",
  133. "LOCAL_JAVA_LIBRARIES": "libs",
  134. "LOCAL_STATIC_JAVA_LIBRARIES": "static_libs",
  135. "LOCAL_JNI_SHARED_LIBRARIES": "jni_libs",
  136. "LOCAL_AAPT_FLAGS": "aaptflags",
  137. "LOCAL_PACKAGE_SPLITS": "package_splits",
  138. "LOCAL_COMPATIBILITY_SUITE": "test_suites",
  139. "LOCAL_OVERRIDES_PACKAGES": "overrides",
  140. "LOCAL_ANNOTATION_PROCESSORS": "plugins",
  141. "LOCAL_PROGUARD_FLAGS": "optimize.proguard_flags",
  142. "LOCAL_PROGUARD_FLAG_FILES": "optimize.proguard_flags_files",
  143. // These will be rewritten to libs/static_libs by bpfix, after their presence is used to convert
  144. // java_library_static to android_library.
  145. "LOCAL_SHARED_ANDROID_LIBRARIES": "android_libs",
  146. "LOCAL_STATIC_ANDROID_LIBRARIES": "android_static_libs",
  147. "LOCAL_ADDITIONAL_CERTIFICATES": "additional_certificates",
  148. // Jacoco filters:
  149. "LOCAL_JACK_COVERAGE_INCLUDE_FILTER": "jacoco.include_filter",
  150. "LOCAL_JACK_COVERAGE_EXCLUDE_FILTER": "jacoco.exclude_filter",
  151. "LOCAL_FULL_LIBS_MANIFEST_FILES": "additional_manifests",
  152. })
  153. addStandardProperties(bpparser.BoolType,
  154. map[string]string{
  155. // Bool properties
  156. "LOCAL_IS_HOST_MODULE": "host",
  157. "LOCAL_CLANG": "clang",
  158. "LOCAL_FORCE_STATIC_EXECUTABLE": "static_executable",
  159. "LOCAL_NATIVE_COVERAGE": "native_coverage",
  160. "LOCAL_NO_CRT": "nocrt",
  161. "LOCAL_ALLOW_UNDEFINED_SYMBOLS": "allow_undefined_symbols",
  162. "LOCAL_RTTI_FLAG": "rtti",
  163. "LOCAL_PACK_MODULE_RELOCATIONS": "pack_relocations",
  164. "LOCAL_TIDY": "tidy",
  165. "LOCAL_USE_CLANG_LLD": "use_clang_lld",
  166. "LOCAL_PROPRIETARY_MODULE": "proprietary",
  167. "LOCAL_VENDOR_MODULE": "vendor",
  168. "LOCAL_ODM_MODULE": "device_specific",
  169. "LOCAL_PRODUCT_MODULE": "product_specific",
  170. "LOCAL_SYSTEM_EXT_MODULE": "system_ext_specific",
  171. "LOCAL_EXPORT_PACKAGE_RESOURCES": "export_package_resources",
  172. "LOCAL_PRIVILEGED_MODULE": "privileged",
  173. "LOCAL_AAPT_INCLUDE_ALL_RESOURCES": "aapt_include_all_resources",
  174. "LOCAL_DONT_MERGE_MANIFESTS": "dont_merge_manifests",
  175. "LOCAL_USE_EMBEDDED_NATIVE_LIBS": "use_embedded_native_libs",
  176. "LOCAL_USE_EMBEDDED_DEX": "use_embedded_dex",
  177. "LOCAL_DEX_PREOPT": "dex_preopt.enabled",
  178. "LOCAL_DEX_PREOPT_APP_IMAGE": "dex_preopt.app_image",
  179. "LOCAL_DEX_PREOPT_GENERATE_PROFILE": "dex_preopt.profile_guided",
  180. "LOCAL_PRIVATE_PLATFORM_APIS": "platform_apis",
  181. "LOCAL_JETIFIER_ENABLED": "jetifier",
  182. "LOCAL_IS_UNIT_TEST": "unit_test",
  183. "LOCAL_ENFORCE_USES_LIBRARIES": "enforce_uses_libs",
  184. })
  185. }

四、常用宏变量作用

不断更新...记录常用但不好理解的

  • LOCAL_PROPRIETARY_MODULE,proprietary:true控制生成路径到vendor/lib,false就是system/lib;
  • LOCAL_CLANG,clang:true来指定默认编译器为Clang,Android 8.0后不需要指定,默认是Clang;
  • -----------------------------------------------------alibli分割符-------------------------------------------------------
  • LOCAL_SRC_FILES:当前模块包含的所有源代码文件
  • LOCAL_MODULE:当前模块的名称,这个名称应当是唯一的,模块间的依赖关系就是通过这个名称来引用的
  • LOCAL_C_INCLUDES:C 或 C++ 语言需要的头文件的路径
  • LOCAL_STATIC_LIBRARIES:当前模块在静态链接时需要的库的名称
  • LOCAL_SHARED_LIBRARIES:当前模块在运行时依赖的动态库的名称
  • LOCAL_CFLAGS:提供给 C/C++ 编译器的额外编译参数
  • LOCAL_JAVA_LIBRARIES:当前模块依赖的 Java 共享库
  • LOCAL_STATIC_JAVA_LIBRARIES:当前模块依赖的 Java 静态库
  • LOCAL_PACKAGE_NAME:当前 APK 应用的名称
  • LOCAL_CERTIFICATE:签署当前应用的证书名称
  • LOCAL_MODULE_PATH:指定输出路径
  • LOCAL_PRIVILEGED_MODULE:指定生成的apk是否在system/priv-app/下,等同于设置LOCAL_MODULE_PATH := $(TARGET_OUT)/system/priv-app
  • LOCAL_MODULE_TAGS:user/debug/eng/optional 指定该模块在什么版本下编译 optional 表示所有版本都编译
  • -----------------------------------------------------alibli分割符-------------------------------------------------------
  • BUILD_HOST_STATIC_LIBRARY:编译静态库(适用于主机)
  • BUILD_HOST_SHARED_LIBRARY:编译动态库(适用于主机)
  • BUILD_HOST_EXECUTABLE:编译可执行程序(适用于主机)
  • BUILD_HOST_PREBUILT:预编译(适用于主机)
  • BUILD_HOST_JAVA_LIBRARY:编译java包(适用于主机)
  • BUILD_JAVA_LIBRARY:编译java包
  • BUILD_STATIC_JAVA_LIBRARY:编译java静态包
  • BUILD_STATIC_LIBRARY:编译静态库
  • BUILD_SHARED_LIBRARY:编译动态库
  • BUILD_EXECUTABLE:编译可执行程序
  • BUILD_PACKAGE:编译apk
  • BUILD_PREBUILT:预编译(针对单个预编译文件)
  • BUILD_MULTI_PREBUILT:预编译(针对多个预编译文件)
  • -----------------------------------------------------alibli分割符-------------------------------------------------------

LOCAL_SHARED_LIBRARIES、LOCAL_LDLIBS、LOCAL_LDFLAGS的区别:

LOCAL_SHARED_LIBRARIES 会生成依赖关系,当库不存在时会去编译这个库。

LOCAL_LDLIBS :链接的库不产生依赖关系,一般用于不需要重新编译的库,如库不存在,则会报错找不到。且貌似只能链接那些存在于系统目录下本模块需要连接的库。如果某一个库既有动态库又有静态库,那么在默认情况下是链接的动态库而非静态库。

如:LOCAL_LDLIBS += -lm –lz –lc -lcutils –lutils –llog …

如果你的Android.mk文件中只有这么一行,那么将会采用动态链接。

LOCAL_LDFLAGS:这个编译变量传递给链接器一个一些额外的参数,比如想传递额外的库和库路径给ld,或者传递给ld linker的一些链接参数,-On,-EL{B}(大小端字节序),那么就要加到这个上面,如:

LOCAL_LDFLAGS += -L$(LOCAL_PATH)/lib/ -lHWrecog –EB{EL} –O{n} …

或者直接加上绝对路径库的全名:

LOCAL_LDFLAGS += $(LOCAL_PATH)/lib/libHWrecog.a –EB{EL} –O{n}

如果是非系统的第三方库,貌似只能用LOCAL_LDFLAGS方式,LOCAL_LDLIBS方式不行。
 

部分参考优秀博客: 

Android.mk常用宏定义 - 简书 (jianshu.com)


---------------------
作者:alibli
来源:CSDN
原文:https://blog.csdn.net/weixin_36389889/article/details/128469488
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/255790
推荐阅读
相关标签
  

闽ICP备14008679号