blob: 910965130076825063db7ba47d71ddb754a033a9 [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}\" " +
Chih-Hung Hsieh885f1c62019-09-29 22:38:31 -070032 "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080034 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
35 Deps: blueprint.DepsGCC,
36 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 },
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
Ivan Lozanob2df9f82019-11-05 12:16:46 -080045func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
46 outputFile android.WritablePath, includeDirs []string) {
47 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
Ivan Lozanob2df9f82019-11-05 12:16:46 -080050func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
51 outputFile android.WritablePath, includeDirs []string) {
52 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
Ivan Lozanob2df9f82019-11-05 12:16:46 -080055func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
56 outputFile android.WritablePath, includeDirs []string) {
57 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070058}
59
Ivan Lozanob2df9f82019-11-05 12:16:46 -080060func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
61 outputFile android.WritablePath, includeDirs []string) {
62 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070063}
64
Ivan Lozanob2df9f82019-11-05 12:16:46 -080065func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
66 outputFile android.WritablePath, includeDirs []string) {
67 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070068}
69
Ivan Lozanob2df9f82019-11-05 12:16:46 -080070func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
71 flags Flags, outputFile android.WritablePath, includeDirs []string) {
72 transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070073}
74
75func rustLibsToPaths(libs RustLibraries) android.Paths {
76 var paths android.Paths
77 for _, lib := range libs {
78 paths = append(paths, lib.Path)
79 }
80 return paths
81}
82
Ivan Lozanob2df9f82019-11-05 12:16:46 -080083func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
84 outputFile android.WritablePath, crate_type string, includeDirs []string) {
Ivan Lozanoffee3342019-08-27 12:03:00 -070085
86 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -080087 var implicits android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -070088 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070090 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
92 inputs = append(inputs, main)
93
94 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -070095 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070096 rustcFlags = append(rustcFlags, flags.RustFlags...)
97 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070098 if crate_name != "" {
99 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
100 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101 if targetTriple != "" {
102 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700103 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104 }
Matthew Maurer99020b02019-10-31 10:44:40 -0700105 // TODO once we have static libraries in the host prebuilt .bp, this
106 // should be unconditionally added.
107 if !ctx.Host() {
108 // If we're on a device build, do not use an implicit sysroot
109 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
110 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700111 // Collect linker flags
112 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
113 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114
115 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800116 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
118 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800119 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
121 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800122 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
124 }
125
126 for _, path := range includeDirs {
127 libFlags = append(libFlags, "-L "+path)
128 }
129
130 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800131 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
132 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
133 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
134 implicits = append(implicits, deps.StaticLibs...)
135 implicits = append(implicits, deps.SharedLibs...)
136 if deps.CrtBegin.Valid() {
137 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700138 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
140 ctx.Build(pctx, android.BuildParams{
141 Rule: rustc,
142 Description: "rustc " + main.Rel(),
143 Output: outputFile,
144 Inputs: inputs,
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800145 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 Args: map[string]string{
147 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700148 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800150 "crtBegin": deps.CrtBegin.String(),
151 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 },
153 })
154
155}