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