Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 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 cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | android.RegisterMakeVarsProvider(pctx, makeVarsProvider) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 29 | func makeVarsProvider(ctx android.MakeVarsContext) { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 30 | ctx.Strict("LLVM_PREBUILTS_VERSION", "${clangVersion}") |
| 31 | ctx.Strict("LLVM_PREBUILTS_BASE", "${clangBase}") |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 32 | ctx.Strict("LLVM_PREBUILTS_PATH", "${clangBin}") |
| 33 | ctx.Strict("CLANG", "${clangBin}/clang") |
| 34 | ctx.Strict("CLANG_CXX", "${clangBin}/clang++") |
| 35 | ctx.Strict("LLVM_AS", "${clangBin}/llvm-as") |
| 36 | ctx.Strict("LLVM_LINK", "${clangBin}/llvm-link") |
Dan Willemsen | e8c5237 | 2016-05-19 16:57:11 -0700 | [diff] [blame] | 37 | ctx.StrictSorted("CLANG_CONFIG_UNKNOWN_CFLAGS", strings.Join(clangUnknownCflags, " ")) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 38 | |
Dan Willemsen | 5efc706 | 2016-05-27 15:23:38 -0700 | [diff] [blame] | 39 | ctx.Strict("GLOBAL_CFLAGS_NO_OVERRIDE", "${noOverrideGlobalCflags}") |
| 40 | ctx.Strict("GLOBAL_CLANG_CFLAGS_NO_OVERRIDE", "${clangExtraNoOverrideCflags}") |
| 41 | ctx.Strict("GLOBAL_CPPFLAGS_NO_OVERRIDE", "") |
| 42 | ctx.Strict("GLOBAL_CLANG_CPPFLAGS_NO_OVERRIDE", "") |
| 43 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 44 | hostTargets := ctx.Config().Targets[android.Host] |
| 45 | makeVarsToolchain(ctx, "", hostTargets[0]) |
| 46 | if len(hostTargets) > 1 { |
| 47 | makeVarsToolchain(ctx, "2ND_", hostTargets[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 50 | crossTargets := ctx.Config().Targets[android.HostCross] |
| 51 | if len(crossTargets) > 0 { |
| 52 | makeVarsToolchain(ctx, "", crossTargets[0]) |
| 53 | if len(crossTargets) > 1 { |
| 54 | makeVarsToolchain(ctx, "2ND_", crossTargets[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 58 | deviceTargets := ctx.Config().Targets[android.Device] |
| 59 | makeVarsToolchain(ctx, "", deviceTargets[0]) |
| 60 | if len(deviceTargets) > 1 { |
| 61 | makeVarsToolchain(ctx, "2ND_", deviceTargets[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 65 | func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string, |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 66 | target android.Target) { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 67 | var typePrefix string |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 68 | switch target.Os.Class { |
| 69 | case android.Host: |
| 70 | typePrefix = "HOST_" |
| 71 | case android.HostCross: |
| 72 | typePrefix = "HOST_CROSS_" |
| 73 | case android.Device: |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 74 | typePrefix = "TARGET_" |
| 75 | } |
| 76 | makePrefix := secondPrefix + typePrefix |
| 77 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 78 | toolchain := toolchainFactories[target.Os][target.Arch.ArchType](target.Arch) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 79 | |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 80 | var productExtraCflags string |
| 81 | var productExtraLdflags string |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 82 | |
| 83 | hod := "host" |
| 84 | if target.Os.Class == android.Device { |
| 85 | hod = "device" |
| 86 | } |
| 87 | |
| 88 | if target.Os.Class == android.Device && Bool(ctx.Config().ProductVariables.Brillo) { |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 89 | productExtraCflags += "-D__BRILLO__" |
| 90 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 91 | if target.Os.Class == android.Host && Bool(ctx.Config().ProductVariables.HostStaticBinaries) { |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 92 | productExtraLdflags += "-static" |
Dan Willemsen | 06f4533 | 2016-05-16 19:32:33 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 95 | ctx.Strict(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{ |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 96 | toolchain.Cflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 97 | "${commonGlobalCflags}", |
| 98 | fmt.Sprintf("${%sGlobalCflags}", hod), |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 99 | toolchain.ToolchainCflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 100 | productExtraCflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 101 | }, " ")) |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 102 | ctx.Strict(makePrefix+"GLOBAL_CONLYFLAGS", "") |
| 103 | ctx.Strict(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{ |
Dan Willemsen | 216a459 | 2016-05-16 23:56:53 -0700 | [diff] [blame] | 104 | "${commonGlobalCppflags}", |
| 105 | toolchain.Cppflags(), |
| 106 | }, " ")) |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 107 | ctx.Strict(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{ |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 108 | toolchain.Ldflags(), |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 109 | toolchain.ToolchainLdflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 110 | productExtraLdflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 111 | }, " ")) |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 112 | |
| 113 | includeFlags, err := ctx.Eval(toolchain.IncludeFlags()) |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 114 | if err != nil { |
| 115 | panic(err) |
| 116 | } |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 117 | ctx.StrictRaw(makePrefix+"C_INCLUDES", strings.Replace(includeFlags, "-isystem ", "", -1)) |
| 118 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 119 | if target.Arch.ArchType == android.Arm { |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 120 | flags, err := toolchain.InstructionSetFlags("arm") |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 121 | if err != nil { |
| 122 | panic(err) |
| 123 | } |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 124 | ctx.Strict(makePrefix+"arm_CFLAGS", flags) |
| 125 | |
| 126 | flags, err = toolchain.InstructionSetFlags("thumb") |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 127 | if err != nil { |
| 128 | panic(err) |
| 129 | } |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 130 | ctx.Strict(makePrefix+"thumb_CFLAGS", flags) |
| 131 | } |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 132 | |
| 133 | if toolchain.ClangSupported() { |
| 134 | clangPrefix := secondPrefix + "CLANG_" + typePrefix |
Dan Willemsen | 3772da1 | 2016-05-16 18:01:46 -0700 | [diff] [blame] | 135 | clangExtras := "-target " + toolchain.ClangTriple() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 136 | if target.Os != android.Darwin { |
Dan Willemsen | 3772da1 | 2016-05-16 18:01:46 -0700 | [diff] [blame] | 137 | clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 138 | } |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 139 | |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 140 | ctx.Strict(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{ |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 141 | toolchain.ClangCflags(), |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 142 | "${commonClangGlobalCflags}", |
| 143 | fmt.Sprintf("${%sClangGlobalCflags}", hod), |
| 144 | toolchain.ToolchainClangCflags(), |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 145 | clangExtras, |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 146 | productExtraCflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 147 | }, " ")) |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 148 | ctx.Strict(clangPrefix+"GLOBAL_CONLYFLAGS", "${clangExtraConlyflags}") |
| 149 | ctx.Strict(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{ |
Dan Willemsen | 216a459 | 2016-05-16 23:56:53 -0700 | [diff] [blame] | 150 | "${commonClangGlobalCppflags}", |
| 151 | toolchain.ClangCppflags(), |
| 152 | }, " ")) |
Dan Willemsen | 9598e10 | 2016-05-19 16:59:04 -0700 | [diff] [blame] | 153 | ctx.Strict(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{ |
Dan Willemsen | d26a713 | 2016-05-18 23:00:57 -0700 | [diff] [blame] | 154 | toolchain.ClangLdflags(), |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 155 | toolchain.ToolchainClangLdflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 156 | productExtraLdflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 157 | clangExtras, |
| 158 | }, " ")) |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 159 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 160 | if target.Os.Class == android.Device { |
| 161 | ctx.Strict(secondPrefix+"ADDRESS_SANITIZER_RUNTIME_LIBRARY", strings.TrimSuffix(toolchain.AddressSanitizerRuntimeLibrary(), ".so")) |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // This is used by external/gentoo/... |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 165 | ctx.Strict("CLANG_CONFIG_"+target.Arch.ArchType.Name+"_"+typePrefix+"TRIPLE", |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 166 | toolchain.ClangTriple()) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc")) |
| 170 | ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++")) |
| 171 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 172 | if target.Os == android.Darwin { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 173 | ctx.Strict(makePrefix+"AR", "${macArPath}") |
| 174 | } else { |
| 175 | ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar")) |
| 176 | ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf")) |
| 177 | ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm")) |
| 178 | } |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 179 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 180 | if target.Os == android.Windows { |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 181 | ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump")) |
| 182 | } |
| 183 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 184 | if target.Os.Class == android.Device { |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 185 | ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy")) |
| 186 | ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld")) |
| 187 | ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip")) |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 188 | ctx.Strict(makePrefix+"GCC_VERSION", toolchain.GccVersion()) |
| 189 | ctx.Strict(makePrefix+"NDK_GCC_VERSION", toolchain.GccVersion()) |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dan Willemsen | 0bd5887 | 2016-05-26 15:23:07 -0700 | [diff] [blame] | 192 | ctx.Strict(makePrefix+"TOOLCHAIN_ROOT", toolchain.GccRoot()) |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 193 | ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, "")) |
Dan Willemsen | 558e517 | 2016-05-19 16:58:46 -0700 | [diff] [blame] | 194 | ctx.Strict(makePrefix+"SHLIB_SUFFIX", toolchain.ShlibSuffix()) |
| 195 | ctx.Strict(makePrefix+"EXECUTABLE_SUFFIX", toolchain.ExecutableSuffix()) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 196 | } |