blob: 66114928b7a03ed386afac6be3d63f2ca2aebf27 [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}\" " +
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "-o $out $in ${libFlags} $rustcFlags " +
33 "&& $rustcCmd --emit=dep-info -o $out.d $in ${libFlags} $rustcFlags",
34 CommandDeps: []string{"$rustcCmd"},
35 Depfile: "$out.d",
36 Deps: blueprint.DepsGCC, // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
37 },
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
45func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070046 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070047}
48
49func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070050 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070051}
52
53func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070054 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
57func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070058 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61func rustLibsToPaths(libs RustLibraries) android.Paths {
62 var paths android.Paths
63 for _, lib := range libs {
64 paths = append(paths, lib.Path)
65 }
66 return paths
67}
68
69func transformSrctoCrate(ctx android.ModuleContext, main android.Path,
Ivan Lozanof1c84332019-09-20 11:00:37 -070070 rlibs, dylibs, proc_macros RustLibraries, static_libs, shared_libs android.Paths, crtBegin, crtEnd android.OptionalPath, flags Flags, outputFile android.WritablePath, crate_type string, includeDirs []string) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070071
72 var inputs android.Paths
73 var deps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -070074 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoffee3342019-08-27 12:03:00 -070075 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070076 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070077
78 inputs = append(inputs, main)
79
80 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -070081 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070082 rustcFlags = append(rustcFlags, flags.RustFlags...)
83 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
84 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
85 if targetTriple != "" {
86 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -070087 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -070088 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070089 // Collect linker flags
90 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
91 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070092
93 // Collect library/crate flags
94 for _, lib := range rlibs {
95 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
96 }
97 for _, lib := range dylibs {
98 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
99 }
100 for _, proc_macro := range proc_macros {
101 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
102 }
103
104 for _, path := range includeDirs {
105 libFlags = append(libFlags, "-L "+path)
106 }
107
108 // Collect dependencies
109 deps = append(deps, rustLibsToPaths(rlibs)...)
110 deps = append(deps, rustLibsToPaths(dylibs)...)
111 deps = append(deps, rustLibsToPaths(proc_macros)...)
112 deps = append(deps, static_libs...)
113 deps = append(deps, shared_libs...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700114 if crtBegin.Valid() {
115 deps = append(deps, crtBegin.Path(), crtEnd.Path())
116 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117
118 ctx.Build(pctx, android.BuildParams{
119 Rule: rustc,
120 Description: "rustc " + main.Rel(),
121 Output: outputFile,
122 Inputs: inputs,
123 Implicits: deps,
124 Args: map[string]string{
125 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700126 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700128 "crtBegin": crtBegin.String(),
129 "crtEnd": crtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130 },
131 })
132
133}