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