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 | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 37 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 38 | hostType := android.CurrentHostType() |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 39 | arches := ctx.Config().HostArches[hostType] |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 40 | makeVarsToolchain(ctx, "", android.Host, hostType, arches[0]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 41 | if len(arches) > 1 { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 42 | makeVarsToolchain(ctx, "2ND_", android.Host, hostType, arches[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 45 | if winArches, ok := ctx.Config().HostArches[android.Windows]; ok { |
| 46 | makeVarsToolchain(ctx, "", android.Host, android.Windows, winArches[0]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 47 | if len(winArches) > 1 { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 48 | makeVarsToolchain(ctx, "2ND_", android.Host, android.Windows, winArches[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | arches = ctx.Config().DeviceArches |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 53 | makeVarsToolchain(ctx, "", android.Device, android.NoHostType, arches[0]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 54 | if len(arches) > 1 { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 55 | makeVarsToolchain(ctx, "2ND_", android.Device, android.NoHostType, arches[1]) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 59 | func makeVarsToolchain(ctx android.MakeVarsContext, secondPrefix string, |
| 60 | hod android.HostOrDevice, ht android.HostType, arch android.Arch) { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 61 | var typePrefix string |
| 62 | if hod.Host() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 63 | if ht == android.Windows { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 64 | typePrefix = "HOST_CROSS_" |
| 65 | } else { |
| 66 | typePrefix = "HOST_" |
| 67 | } |
| 68 | } else { |
| 69 | typePrefix = "TARGET_" |
| 70 | } |
| 71 | makePrefix := secondPrefix + typePrefix |
| 72 | |
| 73 | toolchain := toolchainFactories[hod][ht][arch.ArchType](arch) |
| 74 | |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 75 | var productExtraCflags string |
| 76 | var productExtraLdflags string |
Dan Willemsen | 06f4533 | 2016-05-16 19:32:33 -0700 | [diff] [blame] | 77 | if hod.Device() && Bool(ctx.Config().ProductVariables.Brillo) { |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 78 | productExtraCflags += "-D__BRILLO__" |
| 79 | } |
| 80 | if hod.Host() && Bool(ctx.Config().ProductVariables.HostStaticBinaries) { |
| 81 | productExtraLdflags += "-static" |
Dan Willemsen | 06f4533 | 2016-05-16 19:32:33 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Dan Willemsen | b436fde | 2016-05-16 18:04:45 -0700 | [diff] [blame] | 84 | ctx.StrictSorted(makePrefix+"GLOBAL_CFLAGS", strings.Join([]string{ |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 85 | toolchain.ToolchainCflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 86 | "${commonGlobalCflags}", |
| 87 | fmt.Sprintf("${%sGlobalCflags}", hod), |
| 88 | productExtraCflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 89 | toolchain.Cflags(), |
| 90 | }, " ")) |
Dan Willemsen | 216a459 | 2016-05-16 23:56:53 -0700 | [diff] [blame] | 91 | ctx.StrictSorted(makePrefix+"GLOBAL_CONLYFLAGS", "") |
| 92 | ctx.StrictSorted(makePrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{ |
| 93 | "${commonGlobalCppflags}", |
| 94 | toolchain.Cppflags(), |
| 95 | }, " ")) |
Dan Willemsen | b436fde | 2016-05-16 18:04:45 -0700 | [diff] [blame] | 96 | ctx.StrictSorted(makePrefix+"GLOBAL_LDFLAGS", strings.Join([]string{ |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 97 | toolchain.ToolchainLdflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 98 | productExtraLdflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 99 | toolchain.Ldflags(), |
| 100 | }, " ")) |
| 101 | |
| 102 | if toolchain.ClangSupported() { |
| 103 | clangPrefix := secondPrefix + "CLANG_" + typePrefix |
Dan Willemsen | 3772da1 | 2016-05-16 18:01:46 -0700 | [diff] [blame] | 104 | clangExtras := "-target " + toolchain.ClangTriple() |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 105 | if ht != android.Darwin { |
Dan Willemsen | 3772da1 | 2016-05-16 18:01:46 -0700 | [diff] [blame] | 106 | clangExtras += " -B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 107 | } |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 108 | |
Dan Willemsen | b436fde | 2016-05-16 18:04:45 -0700 | [diff] [blame] | 109 | ctx.StrictSorted(clangPrefix+"GLOBAL_CFLAGS", strings.Join([]string{ |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 110 | toolchain.ToolchainClangCflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 111 | "${commonClangGlobalCflags}", |
| 112 | "${clangExtraCflags}", |
| 113 | fmt.Sprintf("${%sClangGlobalCflags}", hod), |
| 114 | productExtraCflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 115 | toolchain.ClangCflags(), |
| 116 | clangExtras, |
| 117 | }, " ")) |
Dan Willemsen | 216a459 | 2016-05-16 23:56:53 -0700 | [diff] [blame] | 118 | ctx.StrictSorted(clangPrefix+"GLOBAL_CONLYFLAGS", "${clangExtraConlyflags}") |
| 119 | ctx.StrictSorted(clangPrefix+"GLOBAL_CPPFLAGS", strings.Join([]string{ |
| 120 | "${commonClangGlobalCppflags}", |
| 121 | toolchain.ClangCppflags(), |
| 122 | }, " ")) |
Dan Willemsen | b436fde | 2016-05-16 18:04:45 -0700 | [diff] [blame] | 123 | ctx.StrictSorted(clangPrefix+"GLOBAL_LDFLAGS", strings.Join([]string{ |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 124 | toolchain.ToolchainClangLdflags(), |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 125 | productExtraLdflags, |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 126 | toolchain.ClangLdflags(), |
| 127 | clangExtras, |
| 128 | }, " ")) |
| 129 | } |
| 130 | |
| 131 | ctx.Strict(makePrefix+"CC", gccCmd(toolchain, "gcc")) |
| 132 | ctx.Strict(makePrefix+"CXX", gccCmd(toolchain, "g++")) |
| 133 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 134 | if ht == android.Darwin { |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 135 | ctx.Strict(makePrefix+"AR", "${macArPath}") |
| 136 | } else { |
| 137 | ctx.Strict(makePrefix+"AR", gccCmd(toolchain, "ar")) |
| 138 | ctx.Strict(makePrefix+"READELF", gccCmd(toolchain, "readelf")) |
| 139 | ctx.Strict(makePrefix+"NM", gccCmd(toolchain, "nm")) |
| 140 | } |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 141 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame^] | 142 | if ht == android.Windows { |
Dan Willemsen | 73d21f2 | 2016-05-16 14:22:56 -0700 | [diff] [blame] | 143 | ctx.Strict(makePrefix+"OBJDUMP", gccCmd(toolchain, "objdump")) |
| 144 | } |
| 145 | |
| 146 | if hod.Device() { |
| 147 | ctx.Strict(makePrefix+"OBJCOPY", gccCmd(toolchain, "objcopy")) |
| 148 | ctx.Strict(makePrefix+"LD", gccCmd(toolchain, "ld")) |
| 149 | ctx.Strict(makePrefix+"STRIP", gccCmd(toolchain, "strip")) |
| 150 | } |
| 151 | |
| 152 | ctx.Strict(makePrefix+"TOOLS_PREFIX", gccCmd(toolchain, "")) |
Dan Willemsen | 4b7d5de | 2016-01-12 23:20:28 -0800 | [diff] [blame] | 153 | } |