1# Copyright 2021 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://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, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15import("//build_overrides/pigweed.gni") 16 17import("$dir_pw_android_toolchain/android.gni") 18import("$dir_pw_toolchain/generate_toolchain.gni") 19 20# Creates an Android toolchain target. 21# 22# Arguments are forwarded to $generate_toolchain. 23template("pw_generate_android_toolchain") { 24 assert(pw_android_toolchain_NDK_PATH != "", 25 "pw_android_toolchain_NDK_PATH is not set") 26 assert(defined(invoker.defaults), "toolchain is missing 'defaults'") 27 invoker_toolchain_args = invoker.defaults 28 29 # Build _clang_prefix from "host_os" and "host_cpu". 30 _host_os = "" 31 if (host_os == "linux") { 32 _host_os = "linux" 33 } else if (host_os == "mac") { 34 _host_os = "darwin" 35 } else if (host_os == "win") { 36 _host_os = "windows" 37 } 38 39 _host_cpu = "" 40 if (host_cpu == "x64") { 41 _host_cpu = "-x86_64" 42 } 43 44 _clang_prefix = "${pw_android_toolchain_NDK_PATH}/toolchains/llvm/prebuilt/${_host_os}${_host_cpu}/bin/" 45 46 # Build _tool_name_root from "_ndk_cpu" and "api_level". 47 if (defined(invoker_toolchain_args.api_level)) { 48 _api_level = invoker_toolchain_args.api_level 49 } else { 50 _api_level = pw_android_toolchain_API_LEVEL 51 } 52 53 assert(defined(invoker_toolchain_args.current_cpu), 54 "toolchain.defaults is missing 'current_cpu'") 55 if (invoker_toolchain_args.current_cpu == "arm") { 56 _tool_name_root = "armv7a-linux-androideabi${_api_level}-" 57 } else if (invoker_toolchain_args.current_cpu == "arm64") { 58 _tool_name_root = "aarch64-linux-android${_api_level}-" 59 } else if (invoker_toolchain_args.current_cpu == "x86") { 60 _tool_name_root = "i686-linux-android${_api_level}-" 61 } else if (invoker_toolchain_args.current_cpu == "x64") { 62 _tool_name_root = "x86_64-linux-android${_api_level}-" 63 } else { 64 assert(false, "toolchain.defaults.current_cpu unknown or invalid") 65 } 66 67 generate_toolchain(target_name) { 68 ar = _clang_prefix + "llvm-ar" 69 cc = _clang_prefix + _tool_name_root + "clang" 70 cxx = _clang_prefix + _tool_name_root + "clang++" 71 72 forward_variables_from(invoker, 73 "*", 74 [ 75 "defaults", 76 "name", 77 ]) 78 defaults = { 79 current_os = "android" 80 forward_variables_from(invoker_toolchain_args, "*") 81 } 82 } 83} 84 85# Creates a series of Android toolchain targets with common compiler options. 86# 87# Args: 88# toolchains: List of scopes defining each of the desired tolchains. 89# The scope must contain a "name" variable; other variables are forwared to 90# $generate_toolchain. 91template("pw_generate_android_toolchains") { 92 not_needed([ "target_name" ]) 93 assert( 94 defined(invoker.toolchains), 95 "pw_generate_android_toolchains must be called with a list of toolchains") 96 97 # Create a target for each of the desired toolchains. 98 foreach(_toolchain, invoker.toolchains) { 99 # If the toolchain defines a CPU, use that, otherwise expand to all of the 100 # CPU targets an NDK may contain and prepend the CPU name. 101 _current_cpu = "" 102 if (defined(_toolchain.defaults)) { 103 invoker_toolchain_args = { 104 } 105 invoker_toolchain_args = _toolchain.defaults 106 if (defined(invoker_toolchain_args.current_cpu)) { 107 _current_cpu = invoker_toolchain_args.current_cpu 108 } 109 } 110 111 if (_current_cpu != "") { 112 pw_generate_android_toolchain(_toolchain.name) { 113 forward_variables_from(_toolchain, "*", [ "name" ]) 114 } 115 } else { 116 foreach(_current_cpu, pw_android_toolchain_cpu_targets) { 117 pw_generate_android_toolchain("${_current_cpu}_${_toolchain.name}") { 118 forward_variables_from(_toolchain, 119 "*", 120 [ 121 "defaults", 122 "name", 123 ]) 124 defaults = { 125 current_cpu = _current_cpu 126 if (defined(_toolchain.defaults)) { 127 forward_variables_from(_toolchain.defaults, "*") 128 } 129 } 130 } 131 } 132 } 133 } 134} 135