blob: 7dbb59d3e2dcc6c02335301cdce1c1a56f98e265 [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"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040021 "github.com/google/blueprint/pathtools"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022
23 "android/soong/android"
Ivan Lozanof3717ee2020-05-20 09:03:20 -040024 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
27var (
28 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
29 rustc = pctx.AndroidStaticRule("rustc",
30 blueprint.RuleParams{
31 Command: "$rustcCmd " +
32 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070033 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh885f1c62019-09-29 22:38:31 -070034 "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
Ivan Lozanoffee3342019-08-27 12:03:00 -070035 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080036 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
37 Deps: blueprint.DepsGCC,
38 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 },
Ivan Lozanof1c84332019-09-20 11:00:37 -070040 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040041
42 zip = pctx.AndroidStaticRule("zip",
43 blueprint.RuleParams{
44 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
45 CommandDeps: []string{"${SoongZipCmd}"},
46 Rspfile: "$out.rsp",
47 RspfileContent: "$in",
48 })
Ivan Lozanoffee3342019-08-27 12:03:00 -070049)
50
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040051type buildOutput struct {
52 outputFile android.Path
53 coverageFile android.Path
54}
Ivan Lozanoffee3342019-08-27 12:03:00 -070055
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040056func init() {
57 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070058}
59
Ivan Lozanob2df9f82019-11-05 12:16:46 -080060func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080062 flags.RustFlags = append(flags.RustFlags, "-C lto")
63
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
Ivan Lozanob2df9f82019-11-05 12:16:46 -080067func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040068 outputFile android.WritablePath, includeDirs []string) buildOutput {
69 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
Ivan Lozanob2df9f82019-11-05 12:16:46 -080072func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040073 outputFile android.WritablePath, includeDirs []string) buildOutput {
74 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
Ivan Lozanob2df9f82019-11-05 12:16:46 -080077func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040078 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080079 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040080 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070081}
82
Ivan Lozanob2df9f82019-11-05 12:16:46 -080083func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080085 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040086 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070087}
88
Ivan Lozanob2df9f82019-11-05 12:16:46 -080089func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040090 flags Flags, outputFile android.WritablePath, includeDirs []string) buildOutput {
91 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
94func rustLibsToPaths(libs RustLibraries) android.Paths {
95 var paths android.Paths
96 for _, lib := range libs {
97 paths = append(paths, lib.Path)
98 }
99 return paths
100}
101
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800102func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400103 outputFile android.WritablePath, crate_type string, includeDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104
105 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800106 var implicits android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400107 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700108 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400109 var implicitOutputs android.WritablePaths
110
111 output.outputFile = outputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700113 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114
115 inputs = append(inputs, main)
116
117 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700118 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119 rustcFlags = append(rustcFlags, flags.RustFlags...)
120 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700121 if crate_name != "" {
122 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
123 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124 if targetTriple != "" {
125 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700126 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127 }
Matthew Maurer99020b02019-10-31 10:44:40 -0700128 // TODO once we have static libraries in the host prebuilt .bp, this
129 // should be unconditionally added.
Ivan Lozano9d1df102020-04-28 10:10:23 -0400130 if !(ctx.Host() && ctx.TargetPrimary()) {
131 // If we're not targeting the host primary arch, do not use an implicit sysroot
Matthew Maurer99020b02019-10-31 10:44:40 -0700132 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
133 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700134 // Collect linker flags
135 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
136 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
138 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800139 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
141 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800142 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
144 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800145 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
147 }
148
149 for _, path := range includeDirs {
150 libFlags = append(libFlags, "-L "+path)
151 }
152
153 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800154 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
155 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
156 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
157 implicits = append(implicits, deps.StaticLibs...)
158 implicits = append(implicits, deps.SharedLibs...)
159 if deps.CrtBegin.Valid() {
160 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700161 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400163 if flags.Coverage {
164 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400165 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400166 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400167
168 if outputFile.Ext() != "" {
169 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400170 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
171 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400172 } else {
173 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400174 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
175 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400176 }
177
178 implicitOutputs = append(implicitOutputs, gcnoFile)
179 output.coverageFile = gcnoFile
180 }
181
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400183 Rule: rustc,
184 Description: "rustc " + main.Rel(),
185 Output: outputFile,
186 ImplicitOutputs: implicitOutputs,
187 Inputs: inputs,
188 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700189 Args: map[string]string{
190 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700191 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800193 "crtBegin": deps.CrtBegin.String(),
194 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195 },
196 })
197
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400198 return output
199}
200
201func TransformCoverageFilesToZip(ctx android.ModuleContext,
202 covFiles android.Paths, baseName string) android.OptionalPath {
203 if len(covFiles) > 0 {
204
205 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
206
207 ctx.Build(pctx, android.BuildParams{
208 Rule: zip,
209 Description: "zip " + outputFile.Base(),
210 Inputs: covFiles,
211 Output: outputFile,
212 })
213
214 return android.OptionalPathForPath(outputFile)
215 }
216 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217}