1// 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	"path/filepath"
19	"strings"
20
21	"github.com/google/blueprint"
22
23	"android/soong/android"
24	"android/soong/rust/config"
25)
26
27var (
28	_     = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
29	rustc = pctx.AndroidStaticRule("rustc",
30		blueprint.RuleParams{
31			Command: "$envVars $rustcCmd " +
32				"-C linker=${config.RustLinker} " +
33				"-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
34				"--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
35				" && grep \"^$out:\" $out.d.raw > $out.d",
36			CommandDeps: []string{"$rustcCmd"},
37			// Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
38			// Rustc emits unneeded dependency lines for the .d and input .rs files.
39			// Those extra lines cause ninja warning:
40			//     "warning: depfile has multiple output paths"
41			// For ninja, we keep/grep only the dependency rule for the rust $out file.
42			Deps:    blueprint.DepsGCC,
43			Depfile: "$out.d",
44		},
45		"rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
46
47	_       = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
48	rustdoc = pctx.AndroidStaticRule("rustdoc",
49		blueprint.RuleParams{
50			Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
51				"touch $out",
52			CommandDeps: []string{"$rustdocCmd"},
53		},
54		"rustdocFlags", "outDir", "envVars")
55
56	_            = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
57	clippyDriver = pctx.AndroidStaticRule("clippy",
58		blueprint.RuleParams{
59			Command: "$envVars $clippyCmd " +
60				// Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
61				// Use the metadata output as it has the smallest footprint.
62				"--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
63				"$rustcFlags $clippyFlags" +
64				" && grep \"^$out:\" $out.d.raw > $out.d",
65			CommandDeps: []string{"$clippyCmd"},
66			Deps:        blueprint.DepsGCC,
67			Depfile:     "$out.d",
68		},
69		"rustcFlags", "libFlags", "clippyFlags", "envVars")
70
71	zip = pctx.AndroidStaticRule("zip",
72		blueprint.RuleParams{
73			Command:        "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
74			CommandDeps:    []string{"${SoongZipCmd}"},
75			Rspfile:        "$out.rsp",
76			RspfileContent: "$in",
77		})
78
79	cp = pctx.AndroidStaticRule("cp",
80		blueprint.RuleParams{
81			Command:        "cp `cat $outDir.rsp` $outDir",
82			Rspfile:        "${outDir}.rsp",
83			RspfileContent: "$in",
84		},
85		"outDir")
86)
87
88type buildOutput struct {
89	outputFile android.Path
90}
91
92func init() {
93	pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
94}
95
96func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
97	outputFile android.WritablePath) buildOutput {
98	flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
99
100	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin")
101}
102
103func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
104	outputFile android.WritablePath) buildOutput {
105	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib")
106}
107
108func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
109	outputFile android.WritablePath) buildOutput {
110	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib")
111}
112
113func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
114	outputFile android.WritablePath) buildOutput {
115	flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
116	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib")
117}
118
119func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
120	outputFile android.WritablePath) buildOutput {
121	flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
122	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib")
123}
124
125func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
126	flags Flags, outputFile android.WritablePath) buildOutput {
127	return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro")
128}
129
130func rustLibsToPaths(libs RustLibraries) android.Paths {
131	var paths android.Paths
132	for _, lib := range libs {
133		paths = append(paths, lib.Path)
134	}
135	return paths
136}
137
138func makeLibFlags(deps PathDeps) []string {
139	var libFlags []string
140
141	// Collect library/crate flags
142	for _, lib := range deps.RLibs {
143		libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
144	}
145	for _, lib := range deps.DyLibs {
146		libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
147	}
148	for _, proc_macro := range deps.ProcMacros {
149		libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
150	}
151
152	for _, path := range deps.linkDirs {
153		libFlags = append(libFlags, "-L "+path)
154	}
155
156	return libFlags
157}
158
159func rustEnvVars(ctx ModuleContext, deps PathDeps) []string {
160	var envVars []string
161
162	// libstd requires a specific environment variable to be set. This is
163	// not officially documented and may be removed in the future. See
164	// https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
165	if ctx.RustModule().CrateName() == "std" {
166		envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
167	}
168
169	if len(deps.SrcDeps) > 0 {
170		moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
171		// We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
172		// assumes that paths are relative to the source file.
173		var outDirPrefix string
174		if !filepath.IsAbs(moduleGenDir.String()) {
175			// If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
176			outDirPrefix = "$$PWD/"
177		} else {
178			// If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
179			outDirPrefix = ""
180		}
181		envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
182	}
183
184	return envVars
185}
186
187func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
188	outputFile android.WritablePath, crate_type string) buildOutput {
189
190	var inputs android.Paths
191	var implicits android.Paths
192	var output buildOutput
193	var rustcFlags, linkFlags []string
194	var implicitOutputs android.WritablePaths
195
196	output.outputFile = outputFile
197	crateName := ctx.RustModule().CrateName()
198	targetTriple := ctx.toolchain().RustTriple()
199
200	envVars := rustEnvVars(ctx, deps)
201
202	inputs = append(inputs, main)
203
204	// Collect rustc flags
205	rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
206	rustcFlags = append(rustcFlags, flags.RustFlags...)
207	rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
208	if crateName != "" {
209		rustcFlags = append(rustcFlags, "--crate-name="+crateName)
210	}
211	if targetTriple != "" {
212		rustcFlags = append(rustcFlags, "--target="+targetTriple)
213		linkFlags = append(linkFlags, "-target "+targetTriple)
214	}
215
216	// Suppress an implicit sysroot
217	rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
218
219	// Collect linker flags
220	linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
221	linkFlags = append(linkFlags, flags.LinkFlags...)
222
223	libFlags := makeLibFlags(deps)
224
225	// Collect dependencies
226	implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
227	implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
228	implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
229	implicits = append(implicits, deps.StaticLibs...)
230	implicits = append(implicits, deps.SharedLibDeps...)
231	implicits = append(implicits, deps.srcProviderFiles...)
232
233	if deps.CrtBegin.Valid() {
234		implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
235	}
236
237	if len(deps.SrcDeps) > 0 {
238		moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
239		var outputs android.WritablePaths
240
241		for _, genSrc := range deps.SrcDeps {
242			if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
243				ctx.PropertyErrorf("srcs",
244					"multiple source providers generate the same filename output: "+genSrc.Base())
245			}
246			outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
247		}
248
249		ctx.Build(pctx, android.BuildParams{
250			Rule:        cp,
251			Description: "cp " + moduleGenDir.Path().Rel(),
252			Outputs:     outputs,
253			Inputs:      deps.SrcDeps,
254			Args: map[string]string{
255				"outDir": moduleGenDir.String(),
256			},
257		})
258		implicits = append(implicits, outputs.Paths()...)
259	}
260
261	envVars = append(envVars, "ANDROID_RUST_VERSION="+config.RustDefaultVersion)
262
263	if flags.Clippy {
264		clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
265		ctx.Build(pctx, android.BuildParams{
266			Rule:            clippyDriver,
267			Description:     "clippy " + main.Rel(),
268			Output:          clippyFile,
269			ImplicitOutputs: nil,
270			Inputs:          inputs,
271			Implicits:       implicits,
272			Args: map[string]string{
273				"rustcFlags":  strings.Join(rustcFlags, " "),
274				"libFlags":    strings.Join(libFlags, " "),
275				"clippyFlags": strings.Join(flags.ClippyFlags, " "),
276				"envVars":     strings.Join(envVars, " "),
277			},
278		})
279		// Declare the clippy build as an implicit dependency of the original crate.
280		implicits = append(implicits, clippyFile)
281	}
282
283	ctx.Build(pctx, android.BuildParams{
284		Rule:            rustc,
285		Description:     "rustc " + main.Rel(),
286		Output:          outputFile,
287		ImplicitOutputs: implicitOutputs,
288		Inputs:          inputs,
289		Implicits:       implicits,
290		Args: map[string]string{
291			"rustcFlags": strings.Join(rustcFlags, " "),
292			"linkFlags":  strings.Join(linkFlags, " "),
293			"libFlags":   strings.Join(libFlags, " "),
294			"crtBegin":   deps.CrtBegin.String(),
295			"crtEnd":     deps.CrtEnd.String(),
296			"envVars":    strings.Join(envVars, " "),
297		},
298	})
299
300	return output
301}
302
303func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
304	flags Flags) android.ModuleOutPath {
305
306	rustdocFlags := append([]string{}, flags.RustdocFlags...)
307	rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
308
309	// Build an index for all our crates. -Z unstable options is required to use
310	// this flag.
311	rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
312
313	targetTriple := ctx.toolchain().RustTriple()
314
315	// Collect rustc flags
316	if targetTriple != "" {
317		rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
318	}
319
320	crateName := ctx.RustModule().CrateName()
321	rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
322
323	rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
324	docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
325
326	// Yes, the same out directory is used simultaneously by all rustdoc builds.
327	// This is what cargo does. The docs for individual crates get generated to
328	// a subdirectory named for the crate, and rustdoc synchronizes writes to
329	// shared pieces like the index and search data itself.
330	// https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
331	docDir := android.PathForOutput(ctx, "rustdoc")
332
333	ctx.Build(pctx, android.BuildParams{
334		Rule:        rustdoc,
335		Description: "rustdoc " + main.Rel(),
336		Output:      docTimestampFile,
337		Input:       main,
338		Implicit:    ctx.RustModule().unstrippedOutputFile.Path(),
339		Args: map[string]string{
340			"rustdocFlags": strings.Join(rustdocFlags, " "),
341			"outDir":       docDir.String(),
342			"envVars":      strings.Join(rustEnvVars(ctx, deps), " "),
343		},
344	})
345
346	return docTimestampFile
347}
348