blob: 27eeec23db065fdce1c897782364cbedb72bf8fd [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} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070031 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh885f1c62019-09-29 22:38:31 -070032 "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080034 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
35 Deps: blueprint.DepsGCC,
36 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 },
Ivan Lozanof1c84332019-09-20 11:00:37 -070038 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
Ivan Lozanoffee3342019-08-27 12:03:00 -070039)
40
41func init() {
42
43}
44
Ivan Lozanob2df9f82019-11-05 12:16:46 -080045func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
46 outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano31b095d2019-11-20 10:14:33 -080047 flags.RustFlags = append(flags.RustFlags, "-C lto")
48
Ivan Lozanob2df9f82019-11-05 12:16:46 -080049 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
Ivan Lozanob2df9f82019-11-05 12:16:46 -080052func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
53 outputFile android.WritablePath, includeDirs []string) {
54 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
Ivan Lozanob2df9f82019-11-05 12:16:46 -080057func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
58 outputFile android.WritablePath, includeDirs []string) {
59 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070060}
61
Ivan Lozanob2df9f82019-11-05 12:16:46 -080062func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
63 outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano31b095d2019-11-20 10:14:33 -080064 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanob2df9f82019-11-05 12:16:46 -080065 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070066}
67
Ivan Lozanob2df9f82019-11-05 12:16:46 -080068func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
69 outputFile android.WritablePath, includeDirs []string) {
Ivan Lozano31b095d2019-11-20 10:14:33 -080070 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanob2df9f82019-11-05 12:16:46 -080071 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070072}
73
Ivan Lozanob2df9f82019-11-05 12:16:46 -080074func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
75 flags Flags, outputFile android.WritablePath, includeDirs []string) {
76 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070077}
78
79func rustLibsToPaths(libs RustLibraries) android.Paths {
80 var paths android.Paths
81 for _, lib := range libs {
82 paths = append(paths, lib.Path)
83 }
84 return paths
85}
86
Ivan Lozanob2df9f82019-11-05 12:16:46 -080087func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
88 outputFile android.WritablePath, crate_type string, includeDirs []string) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070089
90 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -080091 var implicits android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -070092 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoffee3342019-08-27 12:03:00 -070093 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070094 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070095
96 inputs = append(inputs, main)
97
98 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -070099 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 rustcFlags = append(rustcFlags, flags.RustFlags...)
101 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700102 if crate_name != "" {
103 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
104 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 if targetTriple != "" {
106 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700107 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108 }
Matthew Maurer99020b02019-10-31 10:44:40 -0700109 // TODO once we have static libraries in the host prebuilt .bp, this
110 // should be unconditionally added.
111 if !ctx.Host() {
112 // If we're on a device build, do not use an implicit sysroot
113 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
114 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700115 // Collect linker flags
116 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
117 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118
119 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800120 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
122 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800123 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
125 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800126 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
128 }
129
130 for _, path := range includeDirs {
131 libFlags = append(libFlags, "-L "+path)
132 }
133
134 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800135 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
136 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
137 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
138 implicits = append(implicits, deps.StaticLibs...)
139 implicits = append(implicits, deps.SharedLibs...)
140 if deps.CrtBegin.Valid() {
141 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700142 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
144 ctx.Build(pctx, android.BuildParams{
145 Rule: rustc,
146 Description: "rustc " + main.Rel(),
147 Output: outputFile,
148 Inputs: inputs,
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800149 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 Args: map[string]string{
151 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700152 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800154 "crtBegin": deps.CrtBegin.String(),
155 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 },
157 })
158
159}