• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2016 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package art
16
17// This file implements the "codegen" property to apply different properties based on the currently
18// selected codegen arches, which defaults to all arches on the host and the primary and secondary
19// arches on the device.
20
21import (
22	"android/soong/android"
23	"sort"
24	"strings"
25)
26
27func codegen(ctx android.LoadHookContext, c *codegenProperties, library bool) {
28	var hostArches, deviceArches []string
29
30	e := envDefault(ctx, "ART_HOST_CODEGEN_ARCHS", "")
31	if e == "" {
32		hostArches = supportedArches
33	} else {
34		hostArches = strings.Split(e, " ")
35	}
36
37	e = envDefault(ctx, "ART_TARGET_CODEGEN_ARCHS", "")
38	if e == "" {
39		deviceArches = defaultDeviceCodegenArches(ctx)
40	} else {
41		deviceArches = strings.Split(e, " ")
42	}
43
44	addCodegenArchProperties := func(host bool, archName string) {
45		type props struct {
46			Target struct {
47				Android *CodegenCommonArchProperties
48				Host    *CodegenCommonArchProperties
49			}
50		}
51
52		type libraryProps struct {
53			Target struct {
54				Android *CodegenLibraryArchProperties
55				Host    *CodegenLibraryArchProperties
56			}
57		}
58
59		var arch *codegenArchProperties
60		switch archName {
61		case "arm":
62			arch = &c.Codegen.Arm
63		case "arm64":
64			arch = &c.Codegen.Arm64
65		case "mips":
66			arch = &c.Codegen.Mips
67		case "mips64":
68			arch = &c.Codegen.Mips64
69		case "x86":
70			arch = &c.Codegen.X86
71		case "x86_64":
72			arch = &c.Codegen.X86_64
73		default:
74			ctx.ModuleErrorf("Unknown codegen architecture %q", archName)
75			return
76		}
77
78		p := &props{}
79		l := &libraryProps{}
80		if host {
81			p.Target.Host = &arch.CodegenCommonArchProperties
82			l.Target.Host = &arch.CodegenLibraryArchProperties
83		} else {
84			p.Target.Android = &arch.CodegenCommonArchProperties
85			l.Target.Android = &arch.CodegenLibraryArchProperties
86		}
87
88		ctx.AppendProperties(p)
89		if library {
90			ctx.AppendProperties(l)
91		}
92	}
93
94	for _, arch := range deviceArches {
95		addCodegenArchProperties(false, arch)
96		if ctx.Failed() {
97			return
98		}
99	}
100
101	for _, arch := range hostArches {
102		addCodegenArchProperties(true, arch)
103		if ctx.Failed() {
104			return
105		}
106	}
107}
108
109type CodegenCommonArchProperties struct {
110	Srcs   []string
111	Cflags []string
112}
113
114type CodegenLibraryArchProperties struct {
115	Static struct {
116		Whole_static_libs []string
117	}
118	Shared struct {
119		Shared_libs []string
120	}
121}
122
123type codegenArchProperties struct {
124	CodegenCommonArchProperties
125	CodegenLibraryArchProperties
126}
127
128type codegenProperties struct {
129	Codegen struct {
130		Arm, Arm64, Mips, Mips64, X86, X86_64 codegenArchProperties
131	}
132}
133
134type codegenCustomizer struct {
135	library           bool
136	codegenProperties codegenProperties
137}
138
139func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string {
140	arches := make(map[string]bool)
141	for _, a := range ctx.DeviceConfig().Arches() {
142		s := a.ArchType.String()
143		arches[s] = true
144		if s == "arm64" {
145			arches["arm"] = true
146		} else if s == "mips64" {
147			arches["mips"] = true
148		} else if s == "x86_64" {
149			arches["x86"] = true
150		}
151	}
152	ret := make([]string, 0, len(arches))
153	for a := range arches {
154		ret = append(ret, a)
155	}
156	sort.Strings(ret)
157	return ret
158}
159
160func installCodegenCustomizer(module android.Module, library bool) {
161	c := &codegenProperties{}
162	android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, library) })
163	module.AddProperties(c)
164}
165