Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 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 | |
| 15 | package 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 | |
| 21 | import ( |
| 22 | "android/soong/android" |
| 23 | "sort" |
| 24 | "strings" |
| 25 | ) |
| 26 | |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 27 | type moduleType struct { |
| 28 | library bool |
| 29 | static bool |
| 30 | shared bool |
| 31 | } |
| 32 | |
| 33 | var ( |
| 34 | staticLibrary = moduleType{true, true, false} |
| 35 | sharedLibrary = moduleType{true, false, true} |
| 36 | staticAndSharedLibrary = moduleType{true, true, true} |
| 37 | binary = moduleType{false, false, false} |
| 38 | ) |
| 39 | |
| 40 | func codegen(ctx android.LoadHookContext, c *codegenProperties, t moduleType) { |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 41 | var hostArches, deviceArches []string |
| 42 | |
| 43 | e := envDefault(ctx, "ART_HOST_CODEGEN_ARCHS", "") |
| 44 | if e == "" { |
| 45 | hostArches = supportedArches |
| 46 | } else { |
| 47 | hostArches = strings.Split(e, " ") |
| 48 | } |
| 49 | |
| 50 | e = envDefault(ctx, "ART_TARGET_CODEGEN_ARCHS", "") |
| 51 | if e == "" { |
| 52 | deviceArches = defaultDeviceCodegenArches(ctx) |
| 53 | } else { |
| 54 | deviceArches = strings.Split(e, " ") |
| 55 | } |
| 56 | |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 57 | getCodegenArchProperties := func(archName string) *codegenArchProperties { |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 58 | var arch *codegenArchProperties |
| 59 | switch archName { |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 60 | case "arm": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 61 | arch = &c.Codegen.Arm |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 62 | case "arm64": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 63 | arch = &c.Codegen.Arm64 |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 64 | case "mips": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 65 | arch = &c.Codegen.Mips |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 66 | case "mips64": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 67 | arch = &c.Codegen.Mips64 |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 68 | case "x86": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 69 | arch = &c.Codegen.X86 |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 70 | case "x86_64": |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 71 | arch = &c.Codegen.X86_64 |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 72 | default: |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 73 | ctx.ModuleErrorf("Unknown codegen architecture %q", archName) |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 74 | } |
| 75 | return arch |
| 76 | } |
| 77 | |
| 78 | appendCodegenSourceArchProperties := func(p *CodegenSourceArchProperties, archName string) { |
| 79 | arch := getCodegenArchProperties(archName) |
| 80 | p.Srcs = append(p.Srcs, arch.CodegenSourceArchProperties.Srcs...) |
| 81 | } |
| 82 | |
| 83 | addCodegenSourceArchProperties := func(host bool, p *CodegenSourceArchProperties) { |
| 84 | type sourceProps struct { |
| 85 | Target struct { |
| 86 | Android *CodegenSourceArchProperties |
| 87 | Host *CodegenSourceArchProperties |
| 88 | } |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 89 | } |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 90 | |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 91 | sp := &sourceProps{} |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 92 | if host { |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 93 | sp.Target.Host = p |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 94 | } else { |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 95 | sp.Target.Android = p |
| 96 | } |
| 97 | ctx.AppendProperties(sp) |
| 98 | } |
| 99 | |
| 100 | addCodegenArchProperties := func(host bool, archName string) { |
| 101 | type commonProps struct { |
| 102 | Target struct { |
| 103 | Android *CodegenCommonArchProperties |
| 104 | Host *CodegenCommonArchProperties |
| 105 | } |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 108 | type sharedLibraryProps struct { |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 109 | Target struct { |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 110 | Android *CodegenLibraryArchSharedProperties |
| 111 | Host *CodegenLibraryArchSharedProperties |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | type staticLibraryProps struct { |
| 116 | Target struct { |
| 117 | Android *CodegenLibraryArchStaticProperties |
| 118 | Host *CodegenLibraryArchStaticProperties |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | arch := getCodegenArchProperties(archName) |
| 123 | |
| 124 | cp := &commonProps{} |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 125 | sharedLP := &sharedLibraryProps{} |
| 126 | staticLP := &staticLibraryProps{} |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 127 | if host { |
| 128 | cp.Target.Host = &arch.CodegenCommonArchProperties |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 129 | sharedLP.Target.Host = &arch.CodegenLibraryArchSharedProperties |
| 130 | staticLP.Target.Host = &arch.CodegenLibraryArchStaticProperties |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 131 | } else { |
| 132 | cp.Target.Android = &arch.CodegenCommonArchProperties |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 133 | sharedLP.Target.Android = &arch.CodegenLibraryArchSharedProperties |
| 134 | staticLP.Target.Android = &arch.CodegenLibraryArchStaticProperties |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ctx.AppendProperties(cp) |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 138 | if t.library { |
| 139 | if t.static { |
| 140 | ctx.AppendProperties(staticLP) |
| 141 | } |
| 142 | if t.shared { |
| 143 | ctx.AppendProperties(sharedLP) |
| 144 | } |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 145 | } |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 148 | addCodegenProperties := func(host bool, arches []string) { |
| 149 | sourceProps := &CodegenSourceArchProperties{} |
| 150 | for _, arch := range arches { |
| 151 | appendCodegenSourceArchProperties(sourceProps, arch) |
| 152 | addCodegenArchProperties(host, arch) |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 153 | } |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 154 | sourceProps.Srcs = android.FirstUniqueStrings(sourceProps.Srcs) |
| 155 | addCodegenSourceArchProperties(host, sourceProps) |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 158 | addCodegenProperties(false /* host */, deviceArches) |
| 159 | addCodegenProperties(true /* host */, hostArches) |
| 160 | } |
| 161 | |
| 162 | // These properties are allowed to contain the same source file name in different architectures. |
| 163 | // They we will be deduplicated automatically. |
| 164 | type CodegenSourceArchProperties struct { |
| 165 | Srcs []string |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 168 | type CodegenCommonArchProperties struct { |
Roland Levillain | 12dd9ae | 2018-11-06 13:32:06 +0000 | [diff] [blame] | 169 | Cflags []string |
| 170 | Cppflags []string |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 173 | type CodegenLibraryArchStaticProperties struct { |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 174 | Static struct { |
Paul Duffin | 74f89af | 2019-07-12 15:27:50 +0100 | [diff] [blame] | 175 | Whole_static_libs []string |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 176 | } |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 177 | } |
| 178 | type CodegenLibraryArchSharedProperties struct { |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 179 | Shared struct { |
Paul Duffin | 1533243 | 2019-07-12 15:27:50 +0100 | [diff] [blame] | 180 | Shared_libs []string |
| 181 | Export_shared_lib_headers []string |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 185 | type codegenArchProperties struct { |
Roland Levillain | 118ce36 | 2019-08-05 18:06:00 +0100 | [diff] [blame] | 186 | CodegenSourceArchProperties |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 187 | CodegenCommonArchProperties |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 188 | CodegenLibraryArchStaticProperties |
| 189 | CodegenLibraryArchSharedProperties |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 192 | type codegenProperties struct { |
| 193 | Codegen struct { |
| 194 | Arm, Arm64, Mips, Mips64, X86, X86_64 codegenArchProperties |
| 195 | } |
| 196 | } |
| 197 | |
Colin Cross | 6e51178 | 2016-09-13 13:41:03 -0700 | [diff] [blame] | 198 | func defaultDeviceCodegenArches(ctx android.LoadHookContext) []string { |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 199 | arches := make(map[string]bool) |
| 200 | for _, a := range ctx.DeviceConfig().Arches() { |
Nicolas Geoffray | e52ac15 | 2016-09-09 15:48:19 +0100 | [diff] [blame] | 201 | s := a.ArchType.String() |
| 202 | arches[s] = true |
| 203 | if s == "arm64" { |
| 204 | arches["arm"] = true |
| 205 | } else if s == "mips64" { |
| 206 | arches["mips"] = true |
| 207 | } else if s == "x86_64" { |
| 208 | arches["x86"] = true |
| 209 | } |
Colin Cross | 1f7f3bd | 2016-07-27 10:12:38 -0700 | [diff] [blame] | 210 | } |
| 211 | ret := make([]string, 0, len(arches)) |
| 212 | for a := range arches { |
| 213 | ret = append(ret, a) |
| 214 | } |
| 215 | sort.Strings(ret) |
| 216 | return ret |
| 217 | } |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 218 | |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 219 | func installCodegenCustomizer(module android.Module, t moduleType) { |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 220 | c := &codegenProperties{} |
Colin Cross | f383ed8 | 2019-09-24 15:02:23 -0700 | [diff] [blame^] | 221 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { codegen(ctx, c, t) }) |
Colin Cross | ca06ea3 | 2017-06-27 10:38:55 -0700 | [diff] [blame] | 222 | module.AddProperties(c) |
Colin Cross | 6e95dd5 | 2016-09-12 15:37:10 -0700 | [diff] [blame] | 223 | } |