blob: b0d759b9cd760fd4ee6db84e8068f19ba8a359fd [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"
Colin Cross2a252be2017-05-01 17:37:24 -070023 "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 {
Jeongik Cha816a23a2020-07-08 01:09:23 +090028 if ctx.Config().AlwaysUsePrebuiltSdks() {
Dan Willemsen9f435972020-05-28 15:28:00 -070029 // Use RenderScript prebuilts for unbundled builds
Colin Cross2548b442018-11-18 20:57:21 -080030 return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc")
31 } else {
Martin Stjernholm7260d062019-12-09 21:47:14 +000032 return ctx.Config().HostToolPath(ctx, "llvm-rs-cc").String()
Colin Cross2548b442018-11-18 20:57:21 -080033 }
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 &&
Yuntao Xu56cc6582021-07-29 23:15:19 -070039echo '${out}: \' > ${out}.d &&
40for f in ${depFiles}; do cat $${f} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' >> ${out}.d; done &&
Colin Cross2a252be2017-05-01 17:37:24 -070041touch $out
42`, "\n", "", -1)
43
44var (
45 rsCpp = pctx.AndroidStaticRule("rsCpp",
46 blueprint.RuleParams{
47 Command: rsCppCmdLine,
48 CommandDeps: []string{"$rsCmd"},
49 Depfile: "${out}.d",
50 Deps: blueprint.DepsGCC,
Colin Cross2a252be2017-05-01 17:37:24 -070051 },
52 "depFiles", "outDir", "rsFlags", "stampFile")
53)
54
Jeff Vander Stoepd6126272019-07-15 19:08:37 -070055// 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 -070056// This has to match the logic in llvm-rs-cc in DetermineOutputFile.
57func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
58 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
59 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp")
60}
61
Colin Cross80e60542018-03-19 22:44:29 -070062func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
63 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
64 return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h")
65}
66
Colin Cross2a252be2017-05-01 17:37:24 -070067func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
68 fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
69 return android.PathForModuleGen(ctx, "rs", fileName+".d")
70}
71
72func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
73 stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
Colin Cross80e60542018-03-19 22:44:29 -070074 depFiles := make(android.WritablePaths, 0, len(rsFiles))
75 genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000076 headers := make(android.Paths, 0, len(rsFiles))
Colin Cross80e60542018-03-19 22:44:29 -070077 for _, rsFile := range rsFiles {
78 depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
Dan Willemsenb085b9b2019-06-13 04:55:09 +000079 headerFile := rsGeneratedHFile(ctx, rsFile)
80 genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile)
81 headers = append(headers, headerFile)
Colin Cross2a252be2017-05-01 17:37:24 -070082 }
83
Colin Crossae887032017-10-23 17:16:14 -070084 ctx.Build(pctx, android.BuildParams{
Colin Cross2a252be2017-05-01 17:37:24 -070085 Rule: rsCpp,
Colin Cross67a5c132017-05-09 13:45:28 -070086 Description: "llvm-rs-cc",
Colin Cross2a252be2017-05-01 17:37:24 -070087 Output: stampFile,
Colin Cross80e60542018-03-19 22:44:29 -070088 ImplicitOutputs: genFiles,
Colin Cross2a252be2017-05-01 17:37:24 -070089 Inputs: rsFiles,
90 Args: map[string]string{
91 "rsFlags": rsFlags,
92 "outDir": android.PathForModuleGen(ctx, "rs").String(),
93 "depFiles": strings.Join(depFiles.Strings(), " "),
94 },
95 })
96
Dan Willemsenb085b9b2019-06-13 04:55:09 +000097 return headers
Colin Cross2a252be2017-05-01 17:37:24 -070098}
99
100func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
Nan Zhang0007d812017-11-07 10:57:05 -0800101 targetApi := String(properties.Renderscript.Target_api)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700102 if targetApi == "" && ctx.useSdk() {
Prashanth Swaminathan30f7c792023-07-28 13:09:37 -0700103 targetApiLevel := android.ApiLevelOrPanic(ctx, ctx.sdkVersion())
104 if targetApiLevel.IsCurrent() || targetApiLevel.IsPreview() {
105 // If the target level is current or preview, leave the 'target-api' unset.
106 // This signals to llvm-rs-cc that the development API should be used.
107 } else {
108 targetApi = targetApiLevel.String()
Colin Cross2a252be2017-05-01 17:37:24 -0700109 }
110 }
111
112 if targetApi != "" {
113 flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
114 }
115
116 flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
117 flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
118 if ctx.Arch().ArchType.Multilib == "lib64" {
119 flags.rsFlags = append(flags.rsFlags, "-m64")
120 } else {
121 flags.rsFlags = append(flags.rsFlags, "-m32")
122 }
123 flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
124
125 rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
126 flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
127
Colin Cross4af21ed2019-11-04 09:37:55 -0800128 flags.Local.CommonFlags = append(flags.Local.CommonFlags,
Colin Cross2101f4a2017-05-08 09:16:34 -0700129 "-I"+android.PathForModuleGen(ctx, "rs").String(),
130 "-Iframeworks/rs",
131 "-Iframeworks/rs/cpp",
132 )
Colin Cross2a252be2017-05-01 17:37:24 -0700133
134 return flags
135}