summaryrefslogtreecommitdiff
path: root/build_extra/rust/rust.gni
blob: 985fff5f6821556fa6bc681a0461f8c6a056a21f (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
stdlib_label = "//build_extra/rust:stdlib"

template("run_rustc") {
  action(target_name) {
    assert(defined(invoker.source_root), "Must specify source_root")
    forward_variables_from(invoker,
                           [
                             "cfg",
                             "crate_type",
                             "source_root",
                             "deps",
                             "extern",
                           ])
    if (defined(invoker.crate_name)) {
      crate_name = invoker.crate_name
    } else {
      crate_name = target_name
    }

    sources = [
      source_root,
    ]
    outputs = []
    depfile = "$target_out_dir/$target_name.d"
    script = "//third_party/v8/tools/run.py"

    args = [
      "rustc",
      rebase_path(source_root, root_build_dir),
      "--crate-name=$crate_name",
      "--crate-type=$crate_type",
      "--emit=dep-info=" + rebase_path(depfile, root_build_dir),
    ]

    # We only use staticlib for the special "empty" lib.
    if (crate_type == "staticlib") {
      staticlib = "$target_out_dir/$crate_name.a"
      outputs += [ staticlib ]
      args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ]
    }

    if (crate_type == "rlib" || crate_type == "bin") {
      obj = "$target_out_dir/$crate_name.o"
      outputs += [ obj ]
      args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ]
    }

    if (crate_type == "rlib") {
      rlib = "$target_out_dir/lib$crate_name.rlib"
      outputs += [ rlib ]
      args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ]
    }

    if (is_debug) {
      args += [ "-g" ]
    }

    if (is_official_build) {
      args += [ "-O" ]
    }

    if (defined(cfg)) {
      foreach(c, cfg) {
        args += [
          "--cfg",
          c,
        ]
      }
    }

    if (!defined(deps)) {
      deps = []
    }

    if (defined(extern)) {
      deps += extern
      foreach(label, extern) {
        name = get_label_info(label, "name")
        dir = get_label_info(label, "target_out_dir")
        rlib = "$dir/lib$name.rlib"
        args += [
          "--extern",
          "$name=" + rebase_path(rlib, root_build_dir),
        ]
      }
    }
  }
}

template("rust_component") {
  rustc_name = target_name + "_rustc"
  rustc_label = ":" + rustc_name
  crate_name = target_name
  run_rustc(rustc_name) {
    forward_variables_from(invoker,
                           [
                             "crate_name",
                             "crate_type",
                             "extern",
                             "cfg",
                             "source_root",
                           ])
    if (!defined(invoker.crate_type)) {
      crate_type = "rlib"
    }
  }

  crate_outputs = get_target_outputs(rustc_label)
  crate_obj = crate_outputs[0]

  component(target_name) {
    forward_variables_from(invoker,
                           [
                             "libs",
                             "deps",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(libs)) {
      libs = []
    }
    libs += [ crate_obj ]
    deps += [ rustc_label ]
  }
}

template("rust_executable") {
  bin_name = target_name + "_bin"
  bin_label = ":" + bin_name

  rust_component(bin_name) {
    crate_type = "bin"
    forward_variables_from(invoker, "*")
  }

  executable(target_name) {
    forward_variables_from(invoker, "*")

    if (!defined(deps)) {
      deps = []
    }

    deps += [
      bin_label,
      stdlib_label,
    ]

    if (defined(extern)) {
      deps += extern
    }
  }
}