xref: /OK3568_Linux_fs/external/linux-rga/Android.go (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1package librga
2
3import (
4        "android/soong/android"
5        "android/soong/cc"
6        "fmt"
7        "strings"
8        "strconv"
9        "unsafe"
10)
11
12func init() {
13    fmt.Println("librga want to conditional Compile")
14    android.RegisterModuleType("cc_librga", DefaultsFactory)
15}
16
17func DefaultsFactory() (android.Module) {
18    module := cc.DefaultsFactory()
19    android.AddLoadHook(module, Defaults)
20    return module
21}
22
23func Defaults(ctx android.LoadHookContext) {
24    sdkVersion := ctx.AConfig().PlatformSdkVersion()
25    sdkVersionInt, err := strconv.Atoi(*(*string)(unsafe.Pointer(&sdkVersion)))
26    if err != nil {
27		fmt.Printf("librga cannot get ApiLevel, %q could not be parsed as an integer\n", sdkVersion)
28        panic(1)
29	}
30
31    if (sdkVersionInt >= 29 ) {
32        type props struct {
33            Srcs []string
34            Cflags []string
35            Shared_libs []string
36            Include_dirs []string
37            Header_libs []string
38            Export_header_lib_headers []string
39            Double_loadable *bool
40        }
41
42        p := &props{}
43        p.Srcs = getSrcs(ctx, sdkVersionInt)
44        p.Cflags = getCflags(ctx, sdkVersionInt)
45        p.Shared_libs = getSharedLibs(ctx, sdkVersionInt)
46        p.Include_dirs = getIncludeDirs(ctx, sdkVersionInt)
47        p.Header_libs = getHeaders(ctx, sdkVersionInt)
48        p.Export_header_lib_headers = getExportHeaders(ctx, sdkVersionInt)
49
50        double_loadable := true
51        p.Double_loadable = &double_loadable
52
53        ctx.AppendProperties(p)
54    } else {
55        type props struct {
56            Srcs []string
57            Cflags []string
58            Shared_libs []string
59            Include_dirs []string
60        }
61
62        p := &props{}
63        p.Srcs = getSrcs(ctx, sdkVersionInt)
64        p.Cflags = getCflags(ctx, sdkVersionInt)
65        p.Shared_libs = getSharedLibs(ctx, sdkVersionInt)
66        p.Include_dirs = getIncludeDirs(ctx, sdkVersionInt)
67
68        ctx.AppendProperties(p)
69    }
70}
71
72//条件编译主要修改函数
73func getSrcs(ctx android.BaseContext, sdkVersion int) ([]string) {
74    var src []string
75
76    if (strings.EqualFold(ctx.AConfig().Getenv("TARGET_RK_GRALLOC_VERSION"),"4") ) {
77        if (sdkVersion >= 30 ) {
78            src = append(src, "core/platform_gralloc4.cpp")
79        }
80    }
81
82    return src
83}
84
85func getCflags(ctx android.BaseContext, sdkVersion int) ([]string) {
86    var cppflags []string
87
88    //该打印输出为: TARGET_PRODUCT:rk3328 fmt.Println("TARGET_PRODUCT:",ctx.AConfig().Getenv("TARGET_PRODUCT")) //通过 strings.EqualFold 比较字符串,可参考go语言字符串对比
89    if (strings.EqualFold(ctx.AConfig().Getenv("TARGET_BOARD_PLATFORM"),"rk3368") ) {
90    //添加 DEBUG 宏定义
91        cppflags = append(cppflags,"-DRK3368=1")
92    }
93
94    if (strings.EqualFold(ctx.AConfig().Getenv("TARGET_RK_GRALLOC_VERSION"),"4") ) {
95        if (sdkVersion >= 30 ) {
96            cppflags = append(cppflags,"-DUSE_GRALLOC_4")
97        }
98    }
99
100    //将需要区分的环境变量在此区域添加 //....
101    return cppflags
102}
103
104func getSharedLibs(ctx android.BaseContext, sdkVersion int) ([]string) {
105    var libs []string
106
107    if (strings.EqualFold(ctx.AConfig().Getenv("TARGET_RK_GRALLOC_VERSION"),"4") ) {
108        if (sdkVersion >= 30 ) {
109            libs = append(libs, "libgralloctypes")
110            libs = append(libs, "libhidlbase")
111            libs = append(libs, "android.hardware.graphics.mapper@4.0")
112        }
113    }
114
115    if (sdkVersion < 29 ) {
116        libs = append(libs, "libdrm")
117    }
118
119    return libs
120}
121
122func getIncludeDirs(ctx android.BaseContext, sdkVersion int) ([]string) {
123    var dirs []string
124
125    if (strings.EqualFold(ctx.AConfig().Getenv("TARGET_RK_GRALLOC_VERSION"),"4") ) {
126        if (sdkVersion >= 30 ) {
127            dirs = append(dirs, "hardware/rockchip/libgralloc/bifrost")
128            dirs = append(dirs, "hardware/rockchip/libgralloc/bifrost/src")
129        }
130    }
131    // Add libion for RK3368
132    if (sdkVersion >= 29) {
133        if (sdkVersion >= 30) {
134            dirs = append(dirs, "system/memory/libion/original-kernel-headers")
135        } else {
136            dirs = append(dirs, "system/core/libion/original-kernel-headers")
137        }
138    }
139
140    return dirs
141}
142
143func getHeaders(ctx android.BaseContext, sdkVersion int) ([]string) {
144    var headers []string
145
146    if (sdkVersion >= 31 ) {
147        headers = append(headers, "libhardware_rockchip_headers")
148    }
149
150    return headers
151}
152
153func getExportHeaders(ctx android.BaseContext, sdkVersion int) ([]string) {
154    var headers []string
155
156    if (sdkVersion >= 31 ) {
157        headers = append(headers, "libhardware_rockchip_headers")
158    }
159
160    return headers
161}
162