blob: 11510e174be6b7567ca80b7ad47c9d81d0a2e61a [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 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700105 // Collect linker flags
106 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
107 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108
109 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800110 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
112 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800113 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700114 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
115 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800116 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
118 }
119
120 for _, path := range includeDirs {
121 libFlags = append(libFlags, "-L "+path)
122 }
123
124 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800125 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
126 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
127 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
128 implicits = append(implicits, deps.StaticLibs...)
129 implicits = append(implicits, deps.SharedLibs...)
130 if deps.CrtBegin.Valid() {
131 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700132 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133
134 ctx.Build(pctx, android.BuildParams{
135 Rule: rustc,
136 Description: "rustc " + main.Rel(),
137 Output: outputFile,
138 Inputs: inputs,
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800139 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 Args: map[string]string{
141 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700142 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800144 "crtBegin": deps.CrtBegin.String(),
145 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 },
147 })
148
149}