blob: c2335dd36800ecbf864b9fa13ddbb2a2caf4663d [file] [log] [blame]
Colin Cross2a252be2017-05-01 17:37:24 -07001// Copyright 2017 Google Inc. All rights reserved.
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 cc
16
17import (
18 "android/soong/android"
19 "strings"
20
21 "github.com/google/blueprint"
Colin Cross2a252be2017-05-01 17:37:24 -070022)
23
24func init() {
25 pctx.HostBinToolVariable("rsCmd", "llvm-rs-cc")
26}
27
28var rsCppCmdLine = strings.Replace(`
29${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in &&
30(echo '${out}: \' && cat ${depFiles} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }') > ${out}.d &&
31touch $out
32`, "\n", "", -1)
33
34var (
35 rsCpp = pctx.AndroidStaticRule("rsCpp",
36 blueprint.RuleParams{
37 Command: rsCppCmdLine,
38 CommandDeps: []string{"$rsCmd"},
39 Depfile: "${out}.d",
40 Deps: blueprint.DepsGCC,
Colin Cross2a252be2017-05-01 17:37:24 -070041 },
42 "depFiles", "outDir", "rsFlags", "stampFile")
43)
44
45// Takes a path to a .rs or .fs file, and returns a path to a generated ScriptC_*.cpp file
46// This has to match the logic in llvm-rs-cc in DetermineOutputFile.
47func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
48 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
49 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp")
50}
51
52func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
53 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
54 return android.PathForModuleGen(ctx, "rs", fileName+".d")
55}
56
57func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
58 stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
59 depFiles := make(android.WritablePaths, len(rsFiles))
60 cppFiles := make(android.WritablePaths, len(rsFiles))
61 for i, rsFile := range rsFiles {
62 depFiles[i] = rsGeneratedDepFile(ctx, rsFile)
63 cppFiles[i] = rsGeneratedCppFile(ctx, rsFile)
64 }
65
Colin Crossae887032017-10-23 17:16:14 -070066 ctx.Build(pctx, android.BuildParams{
Colin Cross2a252be2017-05-01 17:37:24 -070067 Rule: rsCpp,
Colin Cross67a5c132017-05-09 13:45:28 -070068 Description: "llvm-rs-cc",
Colin Cross2a252be2017-05-01 17:37:24 -070069 Output: stampFile,
70 ImplicitOutputs: cppFiles,
71 Inputs: rsFiles,
72 Args: map[string]string{
73 "rsFlags": rsFlags,
74 "outDir": android.PathForModuleGen(ctx, "rs").String(),
75 "depFiles": strings.Join(depFiles.Strings(), " "),
76 },
77 })
78
79 return android.Paths{stampFile}
80}
81
82func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
Nan Zhang0007d812017-11-07 10:57:05 -080083 targetApi := String(properties.Renderscript.Target_api)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070084 if targetApi == "" && ctx.useSdk() {
Colin Cross2a252be2017-05-01 17:37:24 -070085 switch ctx.sdkVersion() {
86 case "current", "system_current", "test_current":
87 // Nothing
88 default:
89 targetApi = ctx.sdkVersion()
90 }
91 }
92
93 if targetApi != "" {
94 flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
95 }
96
97 flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
98 flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
99 if ctx.Arch().ArchType.Multilib == "lib64" {
100 flags.rsFlags = append(flags.rsFlags, "-m64")
101 } else {
102 flags.rsFlags = append(flags.rsFlags, "-m32")
103 }
104 flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
105
106 rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
107 flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
108
109 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross2101f4a2017-05-08 09:16:34 -0700110 "-I"+android.PathForModuleGen(ctx, "rs").String(),
111 "-Iframeworks/rs",
112 "-Iframeworks/rs/cpp",
113 )
Colin Cross2a252be2017-05-01 17:37:24 -0700114
115 return flags
116}