Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "runtime" |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame^] | 22 | "android/soong/android" |
| 23 | "android/soong/bazel" |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 28 | pctx.VariableFunc("rsCmd", func(ctx android.PackageVarContext) string { |
Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 29 | if ctx.Config().AlwaysUsePrebuiltSdks() { |
Dan Willemsen | 9f43597 | 2020-05-28 15:28:00 -0700 | [diff] [blame] | 30 | // Use RenderScript prebuilts for unbundled builds |
Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 31 | return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc") |
| 32 | } else { |
Martin Stjernholm | 7260d06 | 2019-12-09 21:47:14 +0000 | [diff] [blame] | 33 | return ctx.Config().HostToolPath(ctx, "llvm-rs-cc").String() |
Colin Cross | 2548b44 | 2018-11-18 20:57:21 -0800 | [diff] [blame] | 34 | } |
| 35 | }) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var rsCppCmdLine = strings.Replace(` |
| 39 | ${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in && |
Yuntao Xu | 56cc658 | 2021-07-29 23:15:19 -0700 | [diff] [blame] | 40 | echo '${out}: \' > ${out}.d && |
| 41 | for f in ${depFiles}; do cat $${f} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' >> ${out}.d; done && |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 42 | touch $out |
| 43 | `, "\n", "", -1) |
| 44 | |
| 45 | var ( |
| 46 | rsCpp = pctx.AndroidStaticRule("rsCpp", |
| 47 | blueprint.RuleParams{ |
| 48 | Command: rsCppCmdLine, |
| 49 | CommandDeps: []string{"$rsCmd"}, |
| 50 | Depfile: "${out}.d", |
| 51 | Deps: blueprint.DepsGCC, |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 52 | }, |
| 53 | "depFiles", "outDir", "rsFlags", "stampFile") |
| 54 | ) |
| 55 | |
Jeff Vander Stoep | d612627 | 2019-07-15 19:08:37 -0700 | [diff] [blame] | 56 | // Takes a path to a .rscript or .fs file, and returns a path to a generated ScriptC_*.cpp file |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 57 | // This has to match the logic in llvm-rs-cc in DetermineOutputFile. |
| 58 | func 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 Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 63 | func 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 Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 68 | func 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 | |
| 73 | func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths { |
| 74 | stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp") |
Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 75 | depFiles := make(android.WritablePaths, 0, len(rsFiles)) |
| 76 | genFiles := make(android.WritablePaths, 0, 2*len(rsFiles)) |
Dan Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 77 | headers := make(android.Paths, 0, len(rsFiles)) |
Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 78 | for _, rsFile := range rsFiles { |
| 79 | depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile)) |
Dan Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 80 | headerFile := rsGeneratedHFile(ctx, rsFile) |
| 81 | genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile) |
| 82 | headers = append(headers, headerFile) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 85 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 86 | Rule: rsCpp, |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 87 | Description: "llvm-rs-cc", |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 88 | Output: stampFile, |
Colin Cross | 80e6054 | 2018-03-19 22:44:29 -0700 | [diff] [blame] | 89 | ImplicitOutputs: genFiles, |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 90 | 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 Willemsen | b085b9b | 2019-06-13 04:55:09 +0000 | [diff] [blame] | 98 | return headers |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 102 | targetApi := String(properties.Renderscript.Target_api) |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 103 | if targetApi == "" && ctx.useSdk() { |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 104 | switch ctx.sdkVersion() { |
| 105 | case "current", "system_current", "test_current": |
| 106 | // Nothing |
| 107 | default: |
Sundong Ahn | 0926fae | 2017-10-17 16:34:51 +0900 | [diff] [blame] | 108 | targetApi = android.GetNumericSdkVersion(ctx.sdkVersion()) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 109 | } |
| 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 Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 128 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, |
Colin Cross | 2101f4a | 2017-05-08 09:16:34 -0700 | [diff] [blame] | 129 | "-I"+android.PathForModuleGen(ctx, "rs").String(), |
| 130 | "-Iframeworks/rs", |
| 131 | "-Iframeworks/rs/cpp", |
| 132 | ) |
Colin Cross | 2a252be | 2017-05-01 17:37:24 -0700 | [diff] [blame] | 133 | |
| 134 | return flags |
| 135 | } |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame^] | 136 | |
| 137 | type rscriptAttributes struct { |
| 138 | // Renderscript source files |
| 139 | Srcs bazel.LabelListAttribute |
| 140 | } |
| 141 | |
| 142 | func bp2buildRScript(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) (bazel.LabelAttribute, bazel.StringListAttribute, bazel.StringListAttribute) { |
| 143 | var rscriptAttrs rscriptAttributes |
| 144 | var rsAbsIncludes bazel.StringListAttribute |
| 145 | var localIncludes bazel.StringListAttribute |
| 146 | var rsModuleName string |
| 147 | var convertedRsSrcsLabel bazel.LabelAttribute |
| 148 | |
| 149 | if !ca.rscriptSrcs.IsEmpty() { |
| 150 | rscriptAttrs.Srcs = ca.rscriptSrcs |
| 151 | rsModuleName = m.Name() + "_renderscript" |
| 152 | |
| 153 | localIncludes.Value = []string{"."} |
| 154 | rsAbsIncludes.Value = []string{"frameworks/rs", "frameworks/rs/cpp"} |
| 155 | convertedRsSrcsLabel = bazel.LabelAttribute{Value: &bazel.Label{Label: rsModuleName}} |
| 156 | |
| 157 | ctx.CreateBazelTargetModule( |
| 158 | bazel.BazelTargetModuleProperties{ |
| 159 | Rule_class: "rscript_to_cpp", |
| 160 | Bzl_load_location: "//build/bazel/rules/cc:rscript_to_cpp.bzl", |
| 161 | }, |
| 162 | android.CommonAttributes{Name: rsModuleName}, |
| 163 | &rscriptAttrs) |
| 164 | } |
| 165 | |
| 166 | return convertedRsSrcsLabel, rsAbsIncludes, localIncludes |
| 167 | } |