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

Posted alibli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一文看懂Android.bp和Androd.mk区别以及宏变量对应关系相关的知识,希望对你有一定的参考价值。

目录

一、区别

二、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"文件中找:


var moduleTypes = map[string]string
	"BUILD_SHARED_LIBRARY":        "cc_library_shared",
	"BUILD_STATIC_LIBRARY":        "cc_library_static",
	"BUILD_HOST_SHARED_LIBRARY":   "cc_library_host_shared",
	"BUILD_HOST_STATIC_LIBRARY":   "cc_library_host_static",
	"BUILD_HEADER_LIBRARY":        "cc_library_headers",
	"BUILD_EXECUTABLE":            "cc_binary",
	"BUILD_HOST_EXECUTABLE":       "cc_binary_host",
	"BUILD_NATIVE_TEST":           "cc_test",
	"BUILD_HOST_NATIVE_TEST":      "cc_test_host",
	"BUILD_NATIVE_BENCHMARK":      "cc_benchmark",
	"BUILD_HOST_NATIVE_BENCHMARK": "cc_benchmark_host",

	"BUILD_JAVA_LIBRARY":             "java_library_installable", // will be rewritten to java_library by bpfix
	"BUILD_STATIC_JAVA_LIBRARY":      "java_library",
	"BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
	"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
	"BUILD_PACKAGE":                  "android_app",
	"BUILD_RRO_PACKAGE":              "runtime_resource_overlay",

	"BUILD_CTS_EXECUTABLE":          "cc_binary",               // will be further massaged by bpfix depending on the output path
	"BUILD_CTS_SUPPORT_PACKAGE":     "cts_support_package",     // will be rewritten to android_test by bpfix
	"BUILD_CTS_PACKAGE":             "cts_package",             // will be rewritten to android_test by bpfix
	"BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix
	"BUILD_CTS_HOST_JAVA_LIBRARY":   "cts_host_java_library",   // will be rewritten to java_library_host by bpfix


var prebuiltTypes = map[string]string
	"SHARED_LIBRARIES": "cc_prebuilt_library_shared",
	"STATIC_LIBRARIES": "cc_prebuilt_library_static",
	"EXECUTABLES":      "cc_prebuilt_binary",
	"JAVA_LIBRARIES":   "java_import",
	"APPS":             "android_app_import",
	"ETC":              "prebuilt_etc",



var rewriteProperties = map[string](func(variableAssignmentContext) error)
	// custom functions
	"LOCAL_32_BIT_ONLY":                    local32BitOnly,
	"LOCAL_AIDL_INCLUDES":                  localAidlIncludes,
	"LOCAL_ASSET_DIR":                      localizePathList("asset_dirs"),
	"LOCAL_C_INCLUDES":                     localIncludeDirs,
	"LOCAL_EXPORT_C_INCLUDE_DIRS":          exportIncludeDirs,
	"LOCAL_JARJAR_RULES":                   localizePath("jarjar_rules"),
	"LOCAL_LDFLAGS":                        ldflags,
	"LOCAL_MODULE_CLASS":                   prebuiltClass,
	"LOCAL_MODULE_STEM":                    stem,
	"LOCAL_MODULE_HOST_OS":                 hostOs,
	"LOCAL_RESOURCE_DIR":                   localizePathList("resource_dirs"),
	"LOCAL_SANITIZE":                       sanitize(""),
	"LOCAL_SANITIZE_DIAG":                  sanitize("diag."),
	"LOCAL_STRIP_MODULE":                   strip(),
	"LOCAL_CFLAGS":                         cflags,
	"LOCAL_UNINSTALLABLE_MODULE":           invert("installable"),
	"LOCAL_PROGUARD_ENABLED":               proguardEnabled,
	"LOCAL_MODULE_PATH":                    prebuiltModulePath,
	"LOCAL_REPLACE_PREBUILT_APK_INSTALLED": prebuiltPreprocessed,

	// composite functions
	"LOCAL_MODULE_TAGS": includeVariableIf(bpVariable"tags", bpparser.ListType, not(valueDumpEquals("optional"))),

	// skip functions
	"LOCAL_ADDITIONAL_DEPENDENCIES": skip, // TODO: check for only .mk files?
	"LOCAL_CPP_EXTENSION":           skip,
	"LOCAL_MODULE_SUFFIX":           skip, // TODO
	"LOCAL_PATH":                    skip, // Nothing to do, except maybe avoid the "./" in paths?
	"LOCAL_PRELINK_MODULE":          skip, // Already phased out
	"LOCAL_BUILT_MODULE_STEM":       skip,
	"LOCAL_USE_AAPT2":               skip, // Always enabled in Soong
	"LOCAL_JAR_EXCLUDE_FILES":       skip, // Soong never excludes files from jars

	"LOCAL_ANNOTATION_PROCESSOR_CLASSES": skip, // Soong gets the processor classes from the plugin
	"LOCAL_CTS_TEST_PACKAGE":             skip, // Obsolete
	"LOCAL_JACK_ENABLED":                 skip, // Obselete
	"LOCAL_JACK_FLAGS":                   skip, // Obselete


func init() 
	addStandardProperties(bpparser.StringType,
		map[string]string
			"LOCAL_MODULE":                  "name",
			"LOCAL_CXX_STL":                 "stl",
			"LOCAL_MULTILIB":                "compile_multilib",
			"LOCAL_ARM_MODE_HACK":           "instruction_set",
			"LOCAL_SDK_VERSION":             "sdk_version",
			"LOCAL_MIN_SDK_VERSION":         "min_sdk_version",
			"LOCAL_NDK_STL_VARIANT":         "stl",
			"LOCAL_JAR_MANIFEST":            "manifest",
			"LOCAL_CERTIFICATE":             "certificate",
			"LOCAL_PACKAGE_NAME":            "name",
			"LOCAL_MODULE_RELATIVE_PATH":    "relative_install_path",
			"LOCAL_PROTOC_OPTIMIZE_TYPE":    "proto.type",
			"LOCAL_MODULE_OWNER":            "owner",
			"LOCAL_RENDERSCRIPT_TARGET_API": "renderscript.target_api",
			"LOCAL_NOTICE_FILE":             "notice",
			"LOCAL_JAVA_LANGUAGE_VERSION":   "java_version",
			"LOCAL_INSTRUMENTATION_FOR":     "instrumentation_for",
			"LOCAL_MANIFEST_FILE":           "manifest",

			"LOCAL_DEX_PREOPT_PROFILE_CLASS_LISTING": "dex_preopt.profile",
			"LOCAL_TEST_CONFIG":                      "test_config",
			"LOCAL_RRO_THEME":                        "theme",
		)
	addStandardProperties(bpparser.ListType,
		map[string]string
			"LOCAL_SRC_FILES":                     "srcs",
			"LOCAL_SRC_FILES_EXCLUDE":             "exclude_srcs",
			"LOCAL_HEADER_LIBRARIES":              "header_libs",
			"LOCAL_SHARED_LIBRARIES":              "shared_libs",
			"LOCAL_STATIC_LIBRARIES":              "static_libs",
			"LOCAL_WHOLE_STATIC_LIBRARIES":        "whole_static_libs",
			"LOCAL_SYSTEM_SHARED_LIBRARIES":       "system_shared_libs",
			"LOCAL_ASFLAGS":                       "asflags",
			"LOCAL_CLANG_ASFLAGS":                 "clang_asflags",
			"LOCAL_COMPATIBILITY_SUPPORT_FILES":   "data",
			"LOCAL_CONLYFLAGS":                    "conlyflags",
			"LOCAL_CPPFLAGS":                      "cppflags",
			"LOCAL_REQUIRED_MODULES":              "required",
			"LOCAL_HOST_REQUIRED_MODULES":         "host_required",
			"LOCAL_TARGET_REQUIRED_MODULES":       "target_required",
			"LOCAL_OVERRIDES_MODULES":             "overrides",
			"LOCAL_LDLIBS":                        "host_ldlibs",
			"LOCAL_CLANG_CFLAGS":                  "clang_cflags",
			"LOCAL_YACCFLAGS":                     "yacc.flags",
			"LOCAL_SANITIZE_RECOVER":              "sanitize.recover",
			"LOCAL_LOGTAGS_FILES":                 "logtags",
			"LOCAL_EXPORT_HEADER_LIBRARY_HEADERS": "export_header_lib_headers",
			"LOCAL_EXPORT_SHARED_LIBRARY_HEADERS": "export_shared_lib_headers",
			"LOCAL_EXPORT_STATIC_LIBRARY_HEADERS": "export_static_lib_headers",
			"LOCAL_INIT_RC":                       "init_rc",
			"LOCAL_VINTF_FRAGMENTS":               "vintf_fragments",
			"LOCAL_TIDY_FLAGS":                    "tidy_flags",
			// TODO: This is comma-separated, not space-separated
			"LOCAL_TIDY_CHECKS":           "tidy_checks",
			"LOCAL_RENDERSCRIPT_INCLUDES": "renderscript.include_dirs",
			"LOCAL_RENDERSCRIPT_FLAGS":    "renderscript.flags",

			"LOCAL_JAVA_RESOURCE_DIRS":    "java_resource_dirs",
			"LOCAL_JAVA_RESOURCE_FILES":   "java_resources",
			"LOCAL_JAVACFLAGS":            "javacflags",
			"LOCAL_ERROR_PRONE_FLAGS":     "errorprone.javacflags",
			"LOCAL_DX_FLAGS":              "dxflags",
			"LOCAL_JAVA_LIBRARIES":        "libs",
			"LOCAL_STATIC_JAVA_LIBRARIES": "static_libs",
			"LOCAL_JNI_SHARED_LIBRARIES":  "jni_libs",
			"LOCAL_AAPT_FLAGS":            "aaptflags",
			"LOCAL_PACKAGE_SPLITS":        "package_splits",
			"LOCAL_COMPATIBILITY_SUITE":   "test_suites",
			"LOCAL_OVERRIDES_PACKAGES":    "overrides",

			"LOCAL_ANNOTATION_PROCESSORS": "plugins",

			"LOCAL_PROGUARD_FLAGS":      "optimize.proguard_flags",
			"LOCAL_PROGUARD_FLAG_FILES": "optimize.proguard_flags_files",

			// These will be rewritten to libs/static_libs by bpfix, after their presence is used to convert
			// java_library_static to android_library.
			"LOCAL_SHARED_ANDROID_LIBRARIES": "android_libs",
			"LOCAL_STATIC_ANDROID_LIBRARIES": "android_static_libs",
			"LOCAL_ADDITIONAL_CERTIFICATES":  "additional_certificates",

			// Jacoco filters:
			"LOCAL_JACK_COVERAGE_INCLUDE_FILTER": "jacoco.include_filter",
			"LOCAL_JACK_COVERAGE_EXCLUDE_FILTER": "jacoco.exclude_filter",

			"LOCAL_FULL_LIBS_MANIFEST_FILES": "additional_manifests",
		)

	addStandardProperties(bpparser.BoolType,
		map[string]string
			// Bool properties
			"LOCAL_IS_HOST_MODULE":             "host",
			"LOCAL_CLANG":                      "clang",
			"LOCAL_FORCE_STATIC_EXECUTABLE":    "static_executable",
			"LOCAL_NATIVE_COVERAGE":            "native_coverage",
			"LOCAL_NO_CRT":                     "nocrt",
			"LOCAL_ALLOW_UNDEFINED_SYMBOLS":    "allow_undefined_symbols",
			"LOCAL_RTTI_FLAG":                  "rtti",
			"LOCAL_PACK_MODULE_RELOCATIONS":    "pack_relocations",
			"LOCAL_TIDY":                       "tidy",
			"LOCAL_USE_CLANG_LLD":              "use_clang_lld",
			"LOCAL_PROPRIETARY_MODULE":         "proprietary",
			"LOCAL_VENDOR_MODULE":              "vendor",
			"LOCAL_ODM_MODULE":                 "device_specific",
			"LOCAL_PRODUCT_MODULE":             "product_specific",
			"LOCAL_SYSTEM_EXT_MODULE":          "system_ext_specific",
			"LOCAL_EXPORT_PACKAGE_RESOURCES":   "export_package_resources",
			"LOCAL_PRIVILEGED_MODULE":          "privileged",
			"LOCAL_AAPT_INCLUDE_ALL_RESOURCES": "aapt_include_all_resources",
			"LOCAL_DONT_MERGE_MANIFESTS":       "dont_merge_manifests",
			"LOCAL_USE_EMBEDDED_NATIVE_LIBS":   "use_embedded_native_libs",
			"LOCAL_USE_EMBEDDED_DEX":           "use_embedded_dex",

			"LOCAL_DEX_PREOPT":                  "dex_preopt.enabled",
			"LOCAL_DEX_PREOPT_APP_IMAGE":        "dex_preopt.app_image",
			"LOCAL_DEX_PREOPT_GENERATE_PROFILE": "dex_preopt.profile_guided",

			"LOCAL_PRIVATE_PLATFORM_APIS": "platform_apis",
			"LOCAL_JETIFIER_ENABLED":      "jetifier",

			"LOCAL_IS_UNIT_TEST": "unit_test",

			"LOCAL_ENFORCE_USES_LIBRARIES": "enforce_uses_libs",
		)

四、常用宏变量作用

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

  • 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,-ELB(大小端字节序),那么就要加到这个上面,如:

LOCAL_LDFLAGS += -L$(LOCAL_PATH)/lib/ -lHWrecog –EBEL –On …

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

LOCAL_LDFLAGS += $(LOCAL_PATH)/lib/libHWrecog.a –EBEL –On

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

部分参考优秀博客: 

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

以上是关于一文看懂Android.bp和Androd.mk区别以及宏变量对应关系的主要内容,如果未能解决你的问题,请参考以下文章

一文看懂javaGC

Python字体反爬之乐居字体反爬,一文看懂,一文学会

一文看懂js中的clientX,clientY,pageX,pageY,screenX,screenY

一文看懂Modbus协议

一文看懂什么是HTTPS,及其安全传输机制和原理

一文看懂什么是HTTPS,及其安全传输机制和原理