blob: d9e36dbbeb1f3fc8fb6280b2c34346b15efa5ad3 [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
Ivan Lozano52767be2019-10-18 14:49:46 -070056func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
57 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "staticlib", includeDirs)
58}
59
60func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
61 transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, deps.CrtBegin, deps.CrtEnd, flags, outputFile, "cdylib", includeDirs)
62}
63
Ivan Lozanoffee3342019-08-27 12:03:00 -070064func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) {
Ivan Lozanof1c84332019-09-20 11:00:37 -070065 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 -070066}
67
68func rustLibsToPaths(libs RustLibraries) android.Paths {
69 var paths android.Paths
70 for _, lib := range libs {
71 paths = append(paths, lib.Path)
72 }
73 return paths
74}
75
76func transformSrctoCrate(ctx android.ModuleContext, main android.Path,
Ivan Lozanof1c84332019-09-20 11:00:37 -070077 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 -070078
79 var inputs android.Paths
80 var deps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -070081 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoffee3342019-08-27 12:03:00 -070082 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -070083 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -070084
85 inputs = append(inputs, main)
86
87 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -070088 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -070089 rustcFlags = append(rustcFlags, flags.RustFlags...)
90 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070091 if crate_name != "" {
92 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
93 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 if targetTriple != "" {
95 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -070096 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070098 // Collect linker flags
99 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
100 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101
102 // Collect library/crate flags
103 for _, lib := range rlibs {
104 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
105 }
106 for _, lib := range dylibs {
107 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
108 }
109 for _, proc_macro := range proc_macros {
110 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
111 }
112
113 for _, path := range includeDirs {
114 libFlags = append(libFlags, "-L "+path)
115 }
116
117 // Collect dependencies
118 deps = append(deps, rustLibsToPaths(rlibs)...)
119 deps = append(deps, rustLibsToPaths(dylibs)...)
120 deps = append(deps, rustLibsToPaths(proc_macros)...)
121 deps = append(deps, static_libs...)
122 deps = append(deps, shared_libs...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700123 if crtBegin.Valid() {
124 deps = append(deps, crtBegin.Path(), crtEnd.Path())
125 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126
127 ctx.Build(pctx, android.BuildParams{
128 Rule: rustc,
129 Description: "rustc " + main.Rel(),
130 Output: outputFile,
131 Inputs: inputs,
132 Implicits: deps,
133 Args: map[string]string{
134 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700135 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700137 "crtBegin": crtBegin.String(),
138 "crtEnd": crtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 },
140 })
141
142}