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