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