blob: ef50701f83a94abdd73c8ba2fb8d851b333a559f [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
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
15package rust
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
25var (
26 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
27 rustc = pctx.AndroidStaticRule("rustc",
28 blueprint.RuleParams{
29 Command: "$rustcCmd " +
30 "-C linker=${config.RustLinker} " +
31 "-C link-args=\"${config.RustLinkerArgs} ${linkFlags}\" " +
32 "-o $out $in ${libFlags} $rustcFlags " +
33 "&& $rustcCmd --emit=dep-info -o $out.d $in ${libFlags} $rustcFlags",
34 CommandDeps: []string{"$rustcCmd"},
35 Depfile: "$out.d",
36 Deps: blueprint.DepsGCC, // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
37 },
38 "rustcFlags", "linkFlags", "libFlags")
39)
40
41func init() {
42
43}
44
45func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070046 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070050 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070051}
52
53func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070054 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
57func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070058 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61func rustLibsToPaths(libs RustLibraries) android.Paths {
62 var paths android.Paths
63 for _, lib := range libs {
64 paths = append(paths, lib.Path)
65 }
66 return paths
67}
68
69func transformSrctoCrate(ctx android.ModuleContext, main android.Path,
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070070 rlibs, dylibs, proc_macros RustLibraries, static_libs, shared_libs android.Paths, flags Flags, outputFile android.WritablePath, crate_type string, includeDirs []string) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070071
72 var inputs android.Paths
73 var deps android.Paths
74 var libFlags, rustcFlags []string
75 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070076 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070077
78 inputs = append(inputs, main)
79
80 // Collect rustc flags
81 rustcFlags = append(rustcFlags, flags.GlobalFlags...)
82 rustcFlags = append(rustcFlags, flags.RustFlags...)
83 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
84 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
85 if targetTriple != "" {
86 rustcFlags = append(rustcFlags, "--target="+targetTriple)
87 }
88
89 // Collect library/crate flags
90 for _, lib := range rlibs {
91 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
92 }
93 for _, lib := range dylibs {
94 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
95 }
96 for _, proc_macro := range proc_macros {
97 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
98 }
99
100 for _, path := range includeDirs {
101 libFlags = append(libFlags, "-L "+path)
102 }
103
104 // Collect dependencies
105 deps = append(deps, rustLibsToPaths(rlibs)...)
106 deps = append(deps, rustLibsToPaths(dylibs)...)
107 deps = append(deps, rustLibsToPaths(proc_macros)...)
108 deps = append(deps, static_libs...)
109 deps = append(deps, shared_libs...)
110
111 ctx.Build(pctx, android.BuildParams{
112 Rule: rustc,
113 Description: "rustc " + main.Rel(),
114 Output: outputFile,
115 Inputs: inputs,
116 Implicits: deps,
117 Args: map[string]string{
118 "rustcFlags": strings.Join(rustcFlags, " "),
119 "linkFlags": strings.Join(flags.LinkFlags, " "),
120 "libFlags": strings.Join(libFlags, " "),
121 },
122 })
123
124}