From: https://blog.csdn.net/weixin_30317869/article/details/111981053? Utm_medium=distribute. Pc_relevant. None - task - blog - baidujs_baidulandingword - 4 & amp; SPM=1001.2101.3001.4242
Preface
With Android version of the iteration, more and more mobile terminal using the savory Android, 9 and 10 system relative to the original version of the Android, Android 9 and 10. The use of more and more Android bp to replace previous Android. Mk compiled script, we know that Android. Mk USES the Makefile language, so in order to control the conditional compilation and more version adapter, we can directly in the different products in Android. Controlled by the Makefile language compilation in mk, although we can also through the Android tools of preset androidmk Android. Mk transformation for Android. The bp script, but for conditional compilation of macro control, androidmk is helpless, that is because Android. Bp is similar to the JSON text form, for Android. Mk flow control part inside, in the Android. In bp have to be controlled by means of using the language files to, good say so many, I think the reader should know what to write this chapter, it is led everyone holding hand rolled a Android. Bp add macro control compilation,
A. Practical start
We know the Android original compilation script Android. Mk add macro control has two scenarios, respectively is:
Add unconditionally control the macro, shape such as:
# unconditional control, translation of
LOCAL_CFLAGS +=- Wno - error=implicit function declaration - DPRINT
Conditional control macro added that satisfy a certain condition to add the corresponding macros, this scene, shape such as:
# conditional add macro control
Ifeq ($(ANDROIDBP_FUN), "YES")
LOCAL_CFLAGS +=- DXXX
Endif
The following chapters we will separately from the above two scenarios, to illustrate the Android. How to add macros in bp control,
1.1 the unconditional control macro to add
Here I am in real case to illustrate, step by step, how to add the unconditional macro control,
1.1.1 to write the source file. The main c
# include
Int main ()
{
# ifdef PRINT//macro control
Printf (" Hello world \ n ");
# endif
# ifdef XXX//macro control
Printf (" XXX \ n ");
# endif
Printf (" This is AndroidBp Test \ n ");
return 0;
}
Code is very simple, it is a simple print, before we officially in Android. Bp add macros before, let's look at Android. Mk how should write,
1.1.2 write Android. Mk compiled script
LOCAL_PATH:=$(call my - dir)
The include $(CLEAR_VARS)
The LOCAL_MODULE: AndroidBp=
C
LOCAL_SRC_FILES:=the main.
ANDROIDBP_FUN=YES
# conditional add macro control
Ifeq ($(ANDROIDBP_FUN), "YES")
LOCAL_CFLAGS +=- DXXX
Endif
# unconditional macro control
LOCAL_CFLAGS +=- Wno - error=implicit function declaration - DPRINT
LOCAL_SHARED_LIBRARIES:=libcutils liblog libutils
$(include BUILD_EXECUTABLE)
This is also very simple, is to compile a executable files, and conditional and unconditional add macro control,
1.1.3 Android. Mk conversion for Android. Bp
In front of the blog, we know that Android's built-in androidmk tools to Android. Mk is converted to the corresponding Android. Bp, but when the Android. Mk in macro control condition is powerless, let's look conversion if so:
[SPRD] XXX @ pd: ~/SSD/XXX/ap/idh code/xxxxroid/external/AndroidBp $androidmk Android. Mk & gt; Bp
Android.
[SPRD] XXX @ pd: ~/SSD/XXX/ap/idh code/xxxxroid/external/AndroidBp $
Check the converted generated Android. Bp file as follows, you can see androidmk conversion fails,
ANDROIDBP_FUN=(" YES ")
Cc_binary {
Name: "AndroidBp,"
SRCS: [". The main c "],
//ANDROIDMK TRANSLATION ERROR: unsupported conditional
//ifeq ($(ANDROIDBP_FUN), "YES")
Cflags: [" - DXXX "] + [//ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
//endif
"- Wno - error=implicit function - declaration",
"- the DPRINT",
],
Shared_libs: [
"Libcutils,"
"Liblog,"
"Libutils,"
],
}
1.1.4 Android. Bp directly add macro
We can directly through the previous chapter, Android. Bp added directly macro is very simple, just behind the cflags add corresponding macro with respect to OK,
Cflags: [" - DXXX "] + [//ANDROIDMK TRANSLATION ERROR: endif from unsupported contitional
//endif
"- Wno - error=implicit function - declaration",
"- the DPRINT",
],
1.2 the conditional control of macro add
According to the previous chapter we know that in the Android. Bp need to be in the condition of adding a macro is not directly do, must compile go script implementation of dynamic control, so let's take a look at how to dynamically add the following macro:
# conditional add macro control
Ifeq ($(ANDROIDBP_FUN), "YES")
LOCAL_CFLAGS +=- DXXX
Endif
1.2.1 add go script
We in Android. Bp directory add go script files at the same level xxxparser. Go, its content is as follows:
Package xxxparser
Import (
"Android/soong/android"
"Android/soong/cc"
)
Func init () {
//resister a module "xxxparser_defaults
"
Android. RegisterModuleType (" xxxparser_defaults xxxdroidDefaultsFactory)
}
Func xxxdroidDefaultsFactory () (android. The Module) {
The module:=cc DefaultsFactory ()
Android. AddLoadHook (module, xxxdroidDefaults)
Return the module
}
Func xxxdroidDefaults (CTX android. LoadHookContext) {
Type props struct {
Cflags [] string
}
P:=& amp; Props {}
P.C flags=globalDefaults (CTX)
CTX. AppendProperties (p)
}
Func globalDefaults (CTX android. BaseContext) (string []) {
Var cppflags [] string
If CTX. AConfig (). The Getenv (" ANDROIDBP_FUN ")=="YES" {
Cppflags=append (cppflags, "- DXXX")
}
Return cppflags
}
1.2.2 Android. Bp using the go script
In Android. Bp opening position is introduced into the go script file xxxparser. Go, is as follows:
//into the go script
Bootstrap_go_package {
Name: "soong - xxxparser,"
PkgPath: "android/soong/xxxparser",
Deps: [
"Blueprint",
"The blueprint - pathtools",
"Soong,"
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull