| # Copyright 2017 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/config/clang/clang.gni") |
| import("//build/config/sysroot.gni") |
| import("//build/host.gni") |
| import("//build/sdk/sdk_atom.gni") |
| import("//build/testing/test_spec.gni") |
| |
| declare_args() { |
| # gocache_dir |
| # Directory GOCACHE environment variable will be set to. This directory |
| # will have build and test results cached, and is safe to be written to |
| # concurrently. If overridden, this directory must be a full path. |
| gocache_dir = rebase_path("$root_out_dir/.gocache") |
| |
| # go_vet_enabled |
| # [bool] if false, go vet invocations are disabled for all builds. |
| go_vet_enabled = false |
| } |
| |
| # A template for an action that builds a Go binary. Users should instead use the |
| # go_binary or go_test rules. |
| # |
| # Parameters |
| # |
| # sdk_category (optional) |
| # Publication level of the library in SDKs. |
| # See //build/sdk/sdk_atom.gni. |
| # |
| # deps (optional) |
| # List of labels representing go_library targets this target depends on. |
| # |
| # non_go_deps (optional) |
| # List of labels this target depends on that are not Go libraries. |
| # |
| # skip_vet (optional) |
| # Whether to skip running go vet for this target. This flag should _only_ |
| # be used for packages in the Go source tree itself that otherwise match |
| # whitelist entries in go vet all. Go vet is only run if go_vet_enabled is |
| # true. |
| |
| template("go_build") { |
| assert(defined(invoker.gopackage), |
| "gopackage must be defined for $target_name") |
| |
| main_target_name = target_name |
| |
| output_name = target_name |
| if (defined(invoker.output_name)) { |
| output_name = invoker.output_name |
| } |
| output_path = "${root_out_dir}/${output_name}" |
| |
| # Test specs are used for linux and mac tests to record metadata for testing |
| # instruction; this happens within package.gni for fuchsia tests. |
| test_spec_target_name = "${target_name}_spec" |
| if (defined(invoker.test) && invoker.test && (is_linux || is_mac)) { |
| test_spec(test_spec_target_name) { |
| name = invoker.target_name |
| location = output_path |
| deps = [] |
| if (defined(invoker.deps)) { |
| deps += invoker.deps |
| } |
| if (defined(invoker.non_go_deps)) { |
| deps += invoker.non_go_deps |
| } |
| } |
| } else { |
| not_needed([ "test_spec_target_name" ]) |
| } |
| |
| action(main_target_name) { |
| deps = [] |
| if (defined(invoker.non_go_deps)) { |
| deps += invoker.non_go_deps |
| } |
| |
| use_strip = is_fuchsia |
| |
| outputs = [ |
| output_path, |
| ] |
| |
| if (use_strip) { |
| unstripped_output_path = "${root_out_dir}/exe.unstripped/${output_name}" |
| outputs += [ unstripped_output_path ] |
| } |
| |
| script = "//build/go/build.py" |
| depfile = "${output_path}.d" |
| |
| sources = [ |
| "//build/go/gen_library_metadata.py", |
| ] |
| |
| godepfile = "//buildtools/${host_platform}/godepfile" |
| inputs = [ |
| godepfile, |
| ] |
| |
| args = [ |
| "--godepfile", |
| rebase_path(godepfile, "", root_build_dir), |
| "--root-out-dir", |
| rebase_path(root_out_dir, root_build_dir), |
| "--depfile", |
| rebase_path(depfile), |
| "--current-cpu", |
| current_cpu, |
| "--current-os", |
| current_os, |
| "--binname", |
| output_name, |
| "--toolchain-prefix", |
| rebase_path(clang_prefix, "", root_build_dir), |
| "--shared-libs-root", |
| rebase_path( |
| get_label_info("//default($shlib_toolchain)", "root_out_dir")), |
| "--sysroot", |
| sysroot, |
| "--go-cache", |
| gocache_dir, |
| ] |
| |
| if (defined(invoker.skip_vet) && !invoker.skip_vet && go_vet_enabled) { |
| args += [ "--vet" ] |
| } |
| |
| if (is_fuchsia) { |
| deps += [ "//third_party/go:go_runtime" ] |
| |
| deps += [ |
| "//sdk:zircon_sysroot_export", |
| "//zircon/public/lib/fdio", |
| ] |
| |
| # GN provides no way to propagate include paths like this, so, this is brittle: |
| fdio_include = rebase_path("//zircon/system/ulib/fdio/include") |
| |
| args += [ |
| "--fdio-include", |
| fdio_include, |
| "--go-root", |
| rebase_path("$host_tools_dir/goroot"), |
| ] |
| } else { |
| args += [ |
| "--go-root", |
| rebase_path("//buildtools/${host_platform}/go"), |
| ] |
| } |
| |
| if (use_strip) { |
| args += [ |
| "--unstripped-binname", |
| "exe.unstripped/${output_name}", |
| ] |
| } |
| |
| if (defined(invoker.test) && invoker.test) { |
| args += [ "--is-test=true" ] |
| |
| if (is_linux || is_mac) { |
| deps += [ ":$test_spec_target_name" ] |
| } |
| } |
| |
| if (defined(invoker.deps)) { |
| deps += invoker.deps |
| args += [ "--go-dep-files" ] |
| foreach(dep, invoker.deps) { |
| gen_dir = get_label_info(dep, "target_gen_dir") |
| name = get_label_info(dep, "name") |
| args += [ rebase_path("$gen_dir/$name.go_deps") ] |
| } |
| } |
| |
| args += [ |
| "--package", |
| invoker.gopackage, |
| ] |
| } |
| |
| # Allow host binaries to be published in SDKs. |
| if (defined(invoker.sdk_category) && invoker.sdk_category != "excluded" && |
| !is_fuchsia && (!defined(invoker.test) || !invoker.test)) { |
| file_base = "tools/$output_name" |
| |
| sdk_atom("${target_name}_sdk") { |
| id = "sdk://tools/$output_name" |
| |
| category = invoker.sdk_category |
| |
| meta = { |
| dest = "$file_base-meta.json" |
| schema = "host_tool" |
| value = { |
| type = "host_tool" |
| name = output_name |
| root = "tools" |
| files = [ file_base ] |
| } |
| } |
| |
| files = [ |
| { |
| source = output_path |
| dest = file_base |
| }, |
| ] |
| |
| if (defined(invoker.sdk_deps)) { |
| deps = invoker.sdk_deps |
| } |
| |
| non_sdk_deps = [ ":$main_target_name" ] |
| } |
| } |
| } |