From e7c71c344d462cac998b37d551bd78baa892082d Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 31 Mar 2023 20:21:19 -0700 Subject: Split Rust crate builds into compile and link phases. Teach rustc to generate object files instead of a linked executable for binary and shared library crates. This lets us skip the lengthy Rust compilation phase when only the C++ dependencies of these crates have changed. This works using a Python script that replaces the linker invoked by rustc and converts the linker command line into a rspfile that can be used during the link phase. The script makes certain assumptions about how rustc invokes the linker, but since we control the version of Rust we use, we can update the script if those assumptions ever break. I've asked the Rust developers about adding an official way to do this so hopefully we won't need to rely on this hack in the future. The rustc rule for Kythe has been altered to pass linker=true, somewhat consistent with the main rustc rule, which now doesn't invoke the actual linker either. `m xref_rust` still succeeds, but hopefully we can find someone who knows more about the rest of the Kythe pipeline who can let us know whether it should still work. Otherwise, let's use this CL as an opportunity to find out whether the rest of the pipeline is happy with linker=true, since otherwise we will find it hard to maintain the Kythe rule in the future. Change-Id: Ifb9c8689c1bd78c0afcf6fdfd111140d20377536 --- rust/rust.go | 59 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'rust/rust.go') diff --git a/rust/rust.go b/rust/rust.go index 56b463160..7b520cdb0 100644 --- a/rust/rust.go +++ b/rust/rust.go @@ -420,13 +420,12 @@ type Deps struct { } type PathDeps struct { - DyLibs RustLibraries - RLibs RustLibraries - SharedLibs android.Paths - SharedLibDeps android.Paths - StaticLibs android.Paths - ProcMacros RustLibraries - AfdoProfiles android.Paths + DyLibs RustLibraries + RLibs RustLibraries + LibDeps android.Paths + WholeStaticLibs android.Paths + ProcMacros RustLibraries + AfdoProfiles android.Paths // depFlags and depLinkFlags are rustc and linker (clang) flags. depFlags []string @@ -435,7 +434,7 @@ type PathDeps struct { // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker. // Both of these are exported and propagate to dependencies. linkDirs []string - linkObjects []string + linkObjects android.Paths // Used by bindgen modules which call clang depClangFlags []string @@ -498,7 +497,7 @@ type compiler interface { type exportedFlagsProducer interface { exportLinkDirs(...string) - exportLinkObjects(...string) + exportLinkObjects(...android.Path) } type xref interface { @@ -507,21 +506,27 @@ type xref interface { type flagExporter struct { linkDirs []string - linkObjects []string + linkObjects android.Paths + libDeps android.Paths } func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) { flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...)) } -func (flagExporter *flagExporter) exportLinkObjects(flags ...string) { - flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...)) +func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) { + flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...)) +} + +func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) { + flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...)) } func (flagExporter *flagExporter) setProvider(ctx ModuleContext) { ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{ LinkDirs: flagExporter.linkDirs, LinkObjects: flagExporter.linkObjects, + LibDeps: flagExporter.libDeps, }) } @@ -534,7 +539,8 @@ func NewFlagExporter() *flagExporter { type FlagExporterInfo struct { Flags []string LinkDirs []string // TODO: this should be android.Paths - LinkObjects []string // TODO: this should be android.Paths + LinkObjects android.Paths + LibDeps android.Paths } var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{}) @@ -1250,6 +1256,7 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...) depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...) depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...) + depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...) } if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag { @@ -1293,6 +1300,7 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...) } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok { depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName) + depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path()) } else { ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName()) } @@ -1300,7 +1308,7 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { // Add this to linkObjects to pass the library directly to the linker as well. This propagates // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant. - depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String()) + depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...) depPaths.linkDirs = append(depPaths.linkDirs, linkPath) exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo) @@ -1326,7 +1334,7 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { linkPath = linkPathFromFilePath(linkObject.Path()) depPaths.linkDirs = append(depPaths.linkDirs, linkPath) - depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String()) + depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...) depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...) depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...) depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...) @@ -1352,7 +1360,9 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { // Make sure these dependencies are propagated if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep { lib.exportLinkDirs(linkPath) - lib.exportLinkObjects(linkObject.String()) + if linkObject.Valid() { + lib.exportLinkObjects(linkObject.Path()) + } } } else { switch { @@ -1384,19 +1394,16 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()}) } - var staticLibDepFiles android.Paths + var libDepFiles android.Paths for _, dep := range directStaticLibDeps { - staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path()) + libDepFiles = append(libDepFiles, dep.OutputFile().Path()) } - var sharedLibFiles android.Paths - var sharedLibDepFiles android.Paths for _, dep := range directSharedLibDeps { - sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary) if dep.TableOfContents.Valid() { - sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path()) + libDepFiles = append(libDepFiles, dep.TableOfContents.Path()) } else { - sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary) + libDepFiles = append(libDepFiles, dep.SharedLibrary) } } @@ -1412,15 +1419,13 @@ func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps { depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...) depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...) - depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...) - depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...) - depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...) + depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...) depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...) depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...) // Dedup exported flags from dependencies depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs) - depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects) + depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects) depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags) depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags) depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths) -- cgit v1.2.3-59-g8ed1b