blob: 61fd1a84a54dcc2be4d6ced6ba3fd77e20c34945 [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"
Colin Cross2548b442018-11-18 20:57:21 -080019 "path/filepath"
20 "runtime"
Colin Cross2a252be2017-05-01 17:37:24 -070021 "strings"
22
23 "github.com/google/blueprint"
Colin Cross2a252be2017-05-01 17:37:24 -070024)
25
26func init() {
Colin Cross2548b442018-11-18 20:57:21 -080027 pctx.VariableFunc("rsCmd", func(ctx android.PackageVarContext) string {
28 if ctx.Config().UnbundledBuild() {
29 // Use RenderScript prebuilts for unbundled builds but not PDK builds
30 return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc")
31 } else {
32 return pctx.HostBinToolPath(ctx, "llvm-rs-cc").String()
33 }
34 })
Colin Cross2a252be2017-05-01 17:37:24 -070035}
36
37var rsCppCmdLine = strings.Replace(`
38${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in &&
39(echo '${out}: \' && cat ${depFiles} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }') > ${out}.d &&
40touch $out
41`, "\n", "", -1)
42
43var (
44 rsCpp = pctx.AndroidStaticRule("rsCpp",
45 blueprint.RuleParams{
46 Command: rsCppCmdLine,
47 CommandDeps: []string{"$rsCmd"},
48 Depfile: "${out}.d",
49 Deps: blueprint.DepsGCC,
Colin Cross2a252be2017-05-01 17:37:24 -070050 },
51 "depFiles", "outDir", "rsFlags", "stampFile")
52)
53
Jeff Vander Stoepd6126272019-07-15 19:08:37 -070054// Takes a path to a .rscript or .fs file, and returns a path to a generated ScriptC_*.cpp file
Colin Cross2a252be2017-05-01 17:37:24 -070055// This has to match the logic in llvm-rs-cc in DetermineOutputFile.
56func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
57 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
58 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp")
59}
60
Colin Cross80e60542018-03-19 22:44:29 -070061func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
62 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
63 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h")
64}
65
Colin Cross2a252be2017-05-01 17:37:24 -070066func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
67 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
68 return android.PathForModuleGen(ctx, "rs", fileName+".d")
69}
70
71func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
72 stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
Colin Cross80e60542018-03-19 22:44:29 -070073 depFiles := make(android.WritablePaths, 0, len(rsFiles))
74 genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000075 headers := make(android.Paths, 0, len(rsFiles))
Colin Cross80e60542018-03-19 22:44:29 -070076 for _, rsFile := range rsFiles {
77 depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000078 headerFile := rsGeneratedHFile(ctx, rsFile)
79 genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile)
80 headers = append(headers, headerFile)
Colin Cross2a252be2017-05-01 17:37:24 -070081 }
82
Colin Crossae887032017-10-23 17:16:14 -070083 ctx.Build(pctx, android.BuildParams{
Colin Cross2a252be2017-05-01 17:37:24 -070084 Rule: rsCpp,
Colin Cross67a5c132017-05-09 13:45:28 -070085 Description: "llvm-rs-cc",
Colin Cross2a252be2017-05-01 17:37:24 -070086 Output: stampFile,
Colin Cross80e60542018-03-19 22:44:29 -070087 ImplicitOutputs: genFiles,
Colin Cross2a252be2017-05-01 17:37:24 -070088 Inputs: rsFiles,
89 Args: map[string]string{
90 "rsFlags": rsFlags,
91 "outDir": android.PathForModuleGen(ctx, "rs").String(),
92 "depFiles": strings.Join(depFiles.Strings(), " "),
93 },
94 })
95
Dan Willemsenb085b9b2019-06-13 04:55:09 +000096 return headers
Colin Cross2a252be2017-05-01 17:37:24 -070097}
98
99func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
Nan Zhang0007d812017-11-07 10:57:05 -0800100 targetApi := String(properties.Renderscript.Target_api)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700101 if targetApi == "" && ctx.useSdk() {
Colin Cross2a252be2017-05-01 17:37:24 -0700102 switch ctx.sdkVersion() {
103 case "current", "system_current", "test_current":
104 // Nothing
105 default:
Sundong Ahn0926fae2017-10-17 16:34:51 +0900106 targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
Colin Cross2a252be2017-05-01 17:37:24 -0700107 }
108 }
109
110 if targetApi != "" {
111 flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
112 }
113
114 flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
115 flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
116 if ctx.Arch().ArchType.Multilib == "lib64" {
117 flags.rsFlags = append(flags.rsFlags, "-m64")
118 } else {
119 flags.rsFlags = append(flags.rsFlags, "-m32")
120 }
121 flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
122
123 rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
124 flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
125
Colin Cross4af21ed2019-11-04 09:37:55 -0800126 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Colin Cross2101f4a2017-05-08 09:16:34 -0700127 "-I"+android.PathForModuleGen(ctx, "rs").String(),
128 "-Iframeworks/rs",
129 "-Iframeworks/rs/cpp",
130 )
Colin Cross2a252be2017-05-01 17:37:24 -0700131
132 return flags
133}