blob: 93acdc7dba2d23a16dc523d5bf8d4944cd5765c9 [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 (
Colin Cross2548b442018-11-18 20:57:21 -080018 "path/filepath"
19 "runtime"
Colin Cross2a252be2017-05-01 17:37:24 -070020 "strings"
21
Alixe2667872023-04-24 14:57:32 +000022 "android/soong/android"
23 "android/soong/bazel"
Colin Cross2a252be2017-05-01 17:37:24 -070024 "github.com/google/blueprint"
Colin Cross2a252be2017-05-01 17:37:24 -070025)
26
27func init() {
Colin Cross2548b442018-11-18 20:57:21 -080028 pctx.VariableFunc("rsCmd", func(ctx android.PackageVarContext) string {
Jeongik Cha816a23a2020-07-08 01:09:23 +090029 if ctx.Config().AlwaysUsePrebuiltSdks() {
Dan Willemsen9f435972020-05-28 15:28:00 -070030 // Use RenderScript prebuilts for unbundled builds
Colin Cross2548b442018-11-18 20:57:21 -080031 return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc")
32 } else {
Martin Stjernholm7260d062019-12-09 21:47:14 +000033 return ctx.Config().HostToolPath(ctx, "llvm-rs-cc").String()
Colin Cross2548b442018-11-18 20:57:21 -080034 }
35 })
Colin Cross2a252be2017-05-01 17:37:24 -070036}
37
38var rsCppCmdLine = strings.Replace(`
39${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in &&
Yuntao Xu56cc6582021-07-29 23:15:19 -070040echo '${out}: \' > ${out}.d &&
41for f in ${depFiles}; do cat $${f} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' >> ${out}.d; done &&
Colin Cross2a252be2017-05-01 17:37:24 -070042touch $out
43`, "\n", "", -1)
44
45var (
46 rsCpp = pctx.AndroidStaticRule("rsCpp",
47 blueprint.RuleParams{
48 Command: rsCppCmdLine,
49 CommandDeps: []string{"$rsCmd"},
50 Depfile: "${out}.d",
51 Deps: blueprint.DepsGCC,
Colin Cross2a252be2017-05-01 17:37:24 -070052 },
53 "depFiles", "outDir", "rsFlags", "stampFile")
54)
55
Jeff Vander Stoepd6126272019-07-15 19:08:37 -070056// 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 -070057// This has to match the logic in llvm-rs-cc in DetermineOutputFile.
58func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
59 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
60 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp")
61}
62
Colin Cross80e60542018-03-19 22:44:29 -070063func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
64 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
65 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h")
66}
67
Colin Cross2a252be2017-05-01 17:37:24 -070068func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
69 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
70 return android.PathForModuleGen(ctx, "rs", fileName+".d")
71}
72
73func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
74 stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
Colin Cross80e60542018-03-19 22:44:29 -070075 depFiles := make(android.WritablePaths, 0, len(rsFiles))
76 genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000077 headers := make(android.Paths, 0, len(rsFiles))
Colin Cross80e60542018-03-19 22:44:29 -070078 for _, rsFile := range rsFiles {
79 depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000080 headerFile := rsGeneratedHFile(ctx, rsFile)
81 genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile)
82 headers = append(headers, headerFile)
Colin Cross2a252be2017-05-01 17:37:24 -070083 }
84
Colin Crossae887032017-10-23 17:16:14 -070085 ctx.Build(pctx, android.BuildParams{
Colin Cross2a252be2017-05-01 17:37:24 -070086 Rule: rsCpp,
Colin Cross67a5c132017-05-09 13:45:28 -070087 Description: "llvm-rs-cc",
Colin Cross2a252be2017-05-01 17:37:24 -070088 Output: stampFile,
Colin Cross80e60542018-03-19 22:44:29 -070089 ImplicitOutputs: genFiles,
Colin Cross2a252be2017-05-01 17:37:24 -070090 Inputs: rsFiles,
91 Args: map[string]string{
92 "rsFlags": rsFlags,
93 "outDir": android.PathForModuleGen(ctx, "rs").String(),
94 "depFiles": strings.Join(depFiles.Strings(), " "),
95 },
96 })
97
Dan Willemsenb085b9b2019-06-13 04:55:09 +000098 return headers
Colin Cross2a252be2017-05-01 17:37:24 -070099}
100
101func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
Nan Zhang0007d812017-11-07 10:57:05 -0800102 targetApi := String(properties.Renderscript.Target_api)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700103 if targetApi == "" && ctx.useSdk() {
Prashanth Swaminathan30f7c792023-07-28 13:09:37 -0700104 targetApiLevel := android.ApiLevelOrPanic(ctx, ctx.sdkVersion())
105 if targetApiLevel.IsCurrent() || targetApiLevel.IsPreview() {
106 // If the target level is current or preview, leave the 'target-api' unset.
107 // This signals to llvm-rs-cc that the development API should be used.
108 } else {
109 targetApi = targetApiLevel.String()
Colin Cross2a252be2017-05-01 17:37:24 -0700110 }
111 }
112
113 if targetApi != "" {
114 flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
115 }
116
117 flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
118 flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
119 if ctx.Arch().ArchType.Multilib == "lib64" {
120 flags.rsFlags = append(flags.rsFlags, "-m64")
121 } else {
122 flags.rsFlags = append(flags.rsFlags, "-m32")
123 }
124 flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
125
126 rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
127 flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
128
Colin Cross4af21ed2019-11-04 09:37:55 -0800129 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Colin Cross2101f4a2017-05-08 09:16:34 -0700130 "-I"+android.PathForModuleGen(ctx, "rs").String(),
131 "-Iframeworks/rs",
132 "-Iframeworks/rs/cpp",
133 )
Colin Cross2a252be2017-05-01 17:37:24 -0700134
135 return flags
136}
Alixe2667872023-04-24 14:57:32 +0000137
138type rscriptAttributes struct {
139 // Renderscript source files
140 Srcs bazel.LabelListAttribute
141}
142
143func bp2buildRScript(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) (bazel.LabelAttribute, bazel.StringListAttribute, bazel.StringListAttribute) {
144 var rscriptAttrs rscriptAttributes
145 var rsAbsIncludes bazel.StringListAttribute
146 var localIncludes bazel.StringListAttribute
147 var rsModuleName string
148 var convertedRsSrcsLabel bazel.LabelAttribute
149
150 if !ca.rscriptSrcs.IsEmpty() {
151 rscriptAttrs.Srcs = ca.rscriptSrcs
152 rsModuleName = m.Name() + "_renderscript"
153
154 localIncludes.Value = []string{"."}
155 rsAbsIncludes.Value = []string{"frameworks/rs", "frameworks/rs/cpp"}
156 convertedRsSrcsLabel = bazel.LabelAttribute{Value: &bazel.Label{Label: rsModuleName}}
157
158 ctx.CreateBazelTargetModule(
159 bazel.BazelTargetModuleProperties{
160 Rule_class: "rscript_to_cpp",
161 Bzl_load_location: "//build/bazel/rules/cc:rscript_to_cpp.bzl",
162 },
163 android.CommonAttributes{Name: rsModuleName},
164 &rscriptAttrs)
165 }
166
167 return convertedRsSrcsLabel, rsAbsIncludes, localIncludes
168}