blob: 104313f8aa0fd41870e8b816a7d5d414d880620a [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"},
34 Depfile: "$out.d",
35 Deps: blueprint.DepsGCC, // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
36 },
Ivan Lozanof1c84332019-09-20 11:00:37 -070037 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
Ivan Lozanoffee3342019-08-27 12:03:00 -070038)
39
40func init() {
41
42}
43
44func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070045 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 -070046}
47
48func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070049 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 -070050}
51
52func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070053 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 -070054}
55
56func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070057 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 -070058}
59
60func rustLibsToPaths(libs RustLibraries) android.Paths {
61 var paths android.Paths
62 for _, lib := range libs {
63 paths = append(paths, lib.Path)
64 }
65 return paths
66}
67
68func transformSrctoCrate(ctx android.ModuleContext, main android.Path,
Ivan Lozanof1c84332019-09-20 11:00:37 -070069 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 -070070
71 var inputs android.Paths
72 var deps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -070073 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoffee3342019-08-27 12:03:00 -070074 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070075 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070076
77 inputs = append(inputs, main)
78
79 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -070080 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070081 rustcFlags = append(rustcFlags, flags.RustFlags...)
82 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
83 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
84 if targetTriple != "" {
85 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -070086 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070088 // Collect linker flags
89 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
90 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
92 // Collect library/crate flags
93 for _, lib := range rlibs {
94 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
95 }
96 for _, lib := range dylibs {
97 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
98 }
99 for _, proc_macro := range proc_macros {
100 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
101 }
102
103 for _, path := range includeDirs {
104 libFlags = append(libFlags, "-L "+path)
105 }
106
107 // Collect dependencies
108 deps = append(deps, rustLibsToPaths(rlibs)...)
109 deps = append(deps, rustLibsToPaths(dylibs)...)
110 deps = append(deps, rustLibsToPaths(proc_macros)...)
111 deps = append(deps, static_libs...)
112 deps = append(deps, shared_libs...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700113 if crtBegin.Valid() {
114 deps = append(deps, crtBegin.Path(), crtEnd.Path())
115 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
117 ctx.Build(pctx, android.BuildParams{
118 Rule: rustc,
119 Description: "rustc " + main.Rel(),
120 Output: outputFile,
121 Inputs: inputs,
122 Implicits: deps,
123 Args: map[string]string{
124 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700125 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700127 "crtBegin": crtBegin.String(),
128 "crtEnd": crtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129 },
130 })
131
132}