summaryrefslogtreecommitdiffstats
path: root/build/go/rules.gni
blob: 9f9785493dead0ca7c39037e6885f64af4f0f6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

declare_args() {
  # By default, there is no go build tool, because go builds are not supported.
  go_build_tool = ""
}

# Declare a go test binary target.
#
# The target generates a go test executable, linking against other C code,
# which is compiled into a static library and linked against Go.
#
# Only works on linux. |go_build_tool| must be set to the absolute path
# of the go build tool.
#
# Variables (all required)
#   sources: list of .go files to compile
#   static_library_sources: list of C sources needed for the static library
#   deps: dependencies for the static library

template("go_test_binary") {
  # Only available on linux for now.
  assert(is_linux)
  assert(defined(invoker.sources))
  assert(go_build_tool != "")

  static_library_name = target_name + "_static_library"

  static_library(static_library_name) {
    sources = invoker.static_library_sources
    deps = invoker.deps
    complete_static_lib = true
  }

  action(target_name) {
    deps = [
      ":$static_library_name",
    ]
    script = "//build/go/go.py"
    outputs = [
      "${target_out_dir}/${target_name}",
    ]

    # Since go test does not permit specifying an output directory or output
    # binary name, we create a temporary build directory, and the python
    # script will later identify the output, copy it to the target location,
    # and clean up the temporary build directory.
    build_dir = "${target_out_dir}/${target_name}_build"
    args = [
             "--",
             "${go_build_tool}",
             rebase_path(build_dir, root_build_dir),
             rebase_path(target_out_dir, root_build_dir) + "/${target_name}",
             rebase_path("//", root_build_dir),
             "-I" + rebase_path("//"),
             " -L" + rebase_path(target_out_dir) + " -l" + static_library_name +
                 " -lstdc++ -lpthread -lm -lglib-2.0",
             "test",
             "-c",
           ] + rebase_path(invoker.sources, build_dir)
  }
}

template("go_shared_library") {
  # Only available on android for now.
  assert(is_android)
  assert(defined(invoker.sources))
  assert(go_build_tool != "")

  static_library_name = target_name + "_static_library"

  static_library(static_library_name) {
    complete_static_lib = true
    deps = invoker.deps
  }

  action(target_name) {
    deps = [
      ":$static_library_name",
    ]
    script = "//build/go/go.py"
    outputs = [
      "${target_out_dir}/${target_name}",
    ]

    # Since go test does not permit specifying an output directory or output
    # binary name, we create a temporary build directory, and the python
    # script will later identify the output, copy it to the target location,
    # and clean up the temporary build directory.
    build_dir = "${target_out_dir}/${target_name}_build"
    args = [
             "--",
             "CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=7 ${go_build_tool}",
             rebase_path(build_dir, root_build_dir),
             rebase_path(target_out_dir, root_build_dir) + "/${target_name}",
             rebase_path("//", root_build_dir),
             "-I" + rebase_path("//"),
             " -L" + rebase_path(target_out_dir) + " -l" + static_library_name +
                 "",
             "build -ldflags=-shared",
           ] + rebase_path(invoker.sources, build_dir)
  }
}