blob: fbe0e53722c61ece9cbd7934bd989a16c3d21d4c [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"
24)
25
26var (
27 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
28 rustc = pctx.AndroidStaticRule("rustc",
29 blueprint.RuleParams{
30 Command: "$rustcCmd " +
31 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070032 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh885f1c62019-09-29 22:38:31 -070033 "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
Ivan Lozanoffee3342019-08-27 12:03:00 -070034 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080035 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
36 Deps: blueprint.DepsGCC,
37 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 },
Ivan Lozanof1c84332019-09-20 11:00:37 -070039 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040040
41 zip = pctx.AndroidStaticRule("zip",
42 blueprint.RuleParams{
43 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
44 CommandDeps: []string{"${SoongZipCmd}"},
45 Rspfile: "$out.rsp",
46 RspfileContent: "$in",
47 })
Ivan Lozanoffee3342019-08-27 12:03:00 -070048)
49
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040050type buildOutput struct {
51 outputFile android.Path
52 coverageFile android.Path
53}
Ivan Lozanoffee3342019-08-27 12:03:00 -070054
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040055func init() {
56 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070057}
58
Ivan Lozanob2df9f82019-11-05 12:16:46 -080059func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080061 flags.RustFlags = append(flags.RustFlags, "-C lto")
62
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070064}
65
Ivan Lozanob2df9f82019-11-05 12:16:46 -080066func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040067 outputFile android.WritablePath, includeDirs []string) buildOutput {
68 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070069}
70
Ivan Lozanob2df9f82019-11-05 12:16:46 -080071func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040072 outputFile android.WritablePath, includeDirs []string) buildOutput {
73 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070074}
75
Ivan Lozanob2df9f82019-11-05 12:16:46 -080076func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040077 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080078 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040079 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070080}
81
Ivan Lozanob2df9f82019-11-05 12:16:46 -080082func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040083 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080084 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040085 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070086}
87
Ivan Lozanob2df9f82019-11-05 12:16:46 -080088func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040089 flags Flags, outputFile android.WritablePath, includeDirs []string) buildOutput {
90 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
93func rustLibsToPaths(libs RustLibraries) android.Paths {
94 var paths android.Paths
95 for _, lib := range libs {
96 paths = append(paths, lib.Path)
97 }
98 return paths
99}
100
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800101func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102 outputFile android.WritablePath, crate_type string, includeDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700103
104 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800105 var implicits android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400106 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700107 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400108 var implicitOutputs android.WritablePaths
109
110 output.outputFile = outputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700112 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113
114 inputs = append(inputs, main)
115
116 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700117 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118 rustcFlags = append(rustcFlags, flags.RustFlags...)
119 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700120 if crate_name != "" {
121 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
122 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123 if targetTriple != "" {
124 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700125 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126 }
Matthew Maurer99020b02019-10-31 10:44:40 -0700127 // TODO once we have static libraries in the host prebuilt .bp, this
128 // should be unconditionally added.
Ivan Lozano9d1df102020-04-28 10:10:23 -0400129 if !(ctx.Host() && ctx.TargetPrimary()) {
130 // If we're not targeting the host primary arch, do not use an implicit sysroot
Matthew Maurer99020b02019-10-31 10:44:40 -0700131 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
132 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700133 // Collect linker flags
134 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
135 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136
137 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800138 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
140 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800141 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
143 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800144 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
146 }
147
148 for _, path := range includeDirs {
149 libFlags = append(libFlags, "-L "+path)
150 }
151
152 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800153 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
154 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
155 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
156 implicits = append(implicits, deps.StaticLibs...)
157 implicits = append(implicits, deps.SharedLibs...)
158 if deps.CrtBegin.Valid() {
159 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700160 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400162 if flags.Coverage {
163 var gcnoFile android.WritablePath
164
165 if outputFile.Ext() != "" {
166 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
167 } else {
168 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
169 }
170
171 implicitOutputs = append(implicitOutputs, gcnoFile)
172 output.coverageFile = gcnoFile
173 }
174
Ivan Lozanoffee3342019-08-27 12:03:00 -0700175 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400176 Rule: rustc,
177 Description: "rustc " + main.Rel(),
178 Output: outputFile,
179 ImplicitOutputs: implicitOutputs,
180 Inputs: inputs,
181 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182 Args: map[string]string{
183 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700184 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800186 "crtBegin": deps.CrtBegin.String(),
187 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188 },
189 })
190
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400191 return output
192}
193
194func TransformCoverageFilesToZip(ctx android.ModuleContext,
195 covFiles android.Paths, baseName string) android.OptionalPath {
196 if len(covFiles) > 0 {
197
198 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
199
200 ctx.Build(pctx, android.BuildParams{
201 Rule: zip,
202 Description: "zip " + outputFile.Base(),
203 Inputs: covFiles,
204 Output: outputFile,
205 })
206
207 return android.OptionalPathForPath(outputFile)
208 }
209 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700210}