blob: 7c9f5d38958409f12e79491dcd22b85c31a0dcb6 [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
Colin Cross80e60542018-03-19 22:44:29 -070052func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
53 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
54 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h")
55}
56
Colin Cross2a252be2017-05-01 17:37:24 -070057func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
58 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
59 return android.PathForModuleGen(ctx, "rs", fileName+".d")
60}
61
62func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
63 stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
Colin Cross80e60542018-03-19 22:44:29 -070064 depFiles := make(android.WritablePaths, 0, len(rsFiles))
65 genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
66 for _, rsFile := range rsFiles {
67 depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
68 genFiles = append(genFiles,
69 rsGeneratedCppFile(ctx, rsFile),
70 rsGeneratedHFile(ctx, rsFile))
Colin Cross2a252be2017-05-01 17:37:24 -070071 }
72
Colin Crossae887032017-10-23 17:16:14 -070073 ctx.Build(pctx, android.BuildParams{
Colin Cross2a252be2017-05-01 17:37:24 -070074 Rule: rsCpp,
Colin Cross67a5c132017-05-09 13:45:28 -070075 Description: "llvm-rs-cc",
Colin Cross2a252be2017-05-01 17:37:24 -070076 Output: stampFile,
Colin Cross80e60542018-03-19 22:44:29 -070077 ImplicitOutputs: genFiles,
Colin Cross2a252be2017-05-01 17:37:24 -070078 Inputs: rsFiles,
79 Args: map[string]string{
80 "rsFlags": rsFlags,
81 "outDir": android.PathForModuleGen(ctx, "rs").String(),
82 "depFiles": strings.Join(depFiles.Strings(), " "),
83 },
84 })
85
86 return android.Paths{stampFile}
87}
88
89func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
Nan Zhang0007d812017-11-07 10:57:05 -080090 targetApi := String(properties.Renderscript.Target_api)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070091 if targetApi == "" && ctx.useSdk() {
Colin Cross2a252be2017-05-01 17:37:24 -070092 switch ctx.sdkVersion() {
93 case "current", "system_current", "test_current":
94 // Nothing
95 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +090096 targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
Colin Cross2a252be2017-05-01 17:37:24 -070097 }
98 }
99
100 if targetApi != "" {
101 flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
102 }
103
104 flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
105 flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
106 if ctx.Arch().ArchType.Multilib == "lib64" {
107 flags.rsFlags = append(flags.rsFlags, "-m64")
108 } else {
109 flags.rsFlags = append(flags.rsFlags, "-m32")
110 }
111 flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
112
113 rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
114 flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
115
116 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross2101f4a2017-05-08 09:16:34 -0700117 "-I"+android.PathForModuleGen(ctx, "rs").String(),
118 "-Iframeworks/rs",
119 "-Iframeworks/rs/cpp",
120 )
Colin Cross2a252be2017-05-01 17:37:24 -0700121
122 return flags
123}