Conditional judgment in Android.bp

In the Android.mk of the android compilation system, there are still conditional judgments, but after Android.bp, android.bp is a plain text form similar to JSON. For the conditional judgment part in Android.mk, use go in Android.bp Language files to control.

Add Demo for conditional judgment macro switch

Macro switches added in Android.mk:

ifeq ($(ENABLE_USER2ENG),true)
LOCAL_CFLAGS += -DALLOW_ADBD_DISABLE_VERITY=1
LOCAL_CFLAGS += -DENABLE_USER2ENG=1
endif

If you want to add the above macro switch to Android.bp, you need to write a new file in go language:

For example, my modification is in system/core/fs_mgr/Android.bp, so I need to add system/core/fs_mgr/fs_mgr.go:

package fs_mgr
 
import (
        "android/soong/android"
        "android/soong/cc"
        "fmt"
)
 
func init() {
    
    
    // for DEBUG
    fmt.Println("init start")
    android.RegisterModuleType("AAA", fs_mgrDefaultsFactory)
}
 
func fs_mgrDefaultsFactory() (android.Module) {
    
    
    module := cc.DefaultsFactory()
    android.AddLoadHook(module, fs_mgrDefaults)
    return module
}
 
func fs_mgrDefaults(ctx android.LoadHookContext) {
    
    
    type props struct {
    
    
        Cflags []string
    }
    p := &props{
    
    }
    p.Cflags = globalDefaults(ctx)
    ctx.AppendProperties(p)
}
 
func globalDefaults(ctx android.BaseContext) ([]string) {
    
    
    var cppflags []string
 
    fmt.Println("ENABLE_USER2ENG:",
        ctx.AConfig().IsEnvTrue("ENABLE_USER2ENG"))
    if ctx.AConfig().IsEnvTrue("ENABLE_USER2ENG") {
    
    
          cppflags = append(cppflags,
                         "-DALLOW_ADBD_DISABLE_VERITY=1",
                         "-DENABLE_USER2ENG=1")
    }
 
    return cppflags
}

Android.bpNeed to modify:

/// add start
bootstrap_go_package {
    
    
    // name and pkgPath need to  according to your module
    name: "soong-fs_mgr",
    pkgPath: "android/soong/fs_mgr",
    deps: [
        "blueprint",
        "blueprint-pathtools",
        "soong",
        "soong-android",
        "soong-cc",
        "soong-genrule",
    ],
    srcs: [
          // include new add .go file
          "fs_mgr.go",
    ],
    pluginFor: ["soong_build"],
}
 
// AAA is a module
AAA {
    
    
    name: "BBB",
}
/// add end
 
cc_defaults {
    
    
    name: "fs_mgr_defaults",
    defaults: ["BBB"],// new add
    sanitize: {
    
    
        misc_undefined: ["integer"],
    },
    local_include_dirs: ["include/"],
    cppflags: ["-Werror", "-DMTK_FSTAB_FLAGS"],
}

When modifying with reference to this example, pay attention AAAto BBBthe corresponding relationship.

refers:
https://blog.csdn.net/weixin_33807284/article/details/86032658

Guess you like

Origin blog.csdn.net/yao_zhuang/article/details/113487116