rs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. package cc
  15. import (
  16. "path/filepath"
  17. "runtime"
  18. "strings"
  19. "android/soong/android"
  20. "android/soong/bazel"
  21. "github.com/google/blueprint"
  22. )
  23. func init() {
  24. pctx.VariableFunc("rsCmd", func(ctx android.PackageVarContext) string {
  25. if ctx.Config().AlwaysUsePrebuiltSdks() {
  26. // Use RenderScript prebuilts for unbundled builds
  27. return filepath.Join("prebuilts/sdk/tools", runtime.GOOS, "bin/llvm-rs-cc")
  28. } else {
  29. return ctx.Config().HostToolPath(ctx, "llvm-rs-cc").String()
  30. }
  31. })
  32. }
  33. var rsCppCmdLine = strings.Replace(`
  34. ${rsCmd} -o ${outDir} -d ${outDir} -a ${out} -MD -reflect-c++ ${rsFlags} $in &&
  35. echo '${out}: \' > ${out}.d &&
  36. for f in ${depFiles}; do cat $${f} | awk 'start { sub(/( \\)?$$/, " \\"); print } /:/ { start=1 }' >> ${out}.d; done &&
  37. touch $out
  38. `, "\n", "", -1)
  39. var (
  40. rsCpp = pctx.AndroidStaticRule("rsCpp",
  41. blueprint.RuleParams{
  42. Command: rsCppCmdLine,
  43. CommandDeps: []string{"$rsCmd"},
  44. Depfile: "${out}.d",
  45. Deps: blueprint.DepsGCC,
  46. },
  47. "depFiles", "outDir", "rsFlags", "stampFile")
  48. )
  49. // Takes a path to a .rscript or .fs file, and returns a path to a generated ScriptC_*.cpp file
  50. // This has to match the logic in llvm-rs-cc in DetermineOutputFile.
  51. func rsGeneratedCppFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
  52. fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
  53. return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".cpp")
  54. }
  55. func rsGeneratedHFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
  56. fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
  57. return android.PathForModuleGen(ctx, "rs", "ScriptC_"+fileName+".h")
  58. }
  59. func rsGeneratedDepFile(ctx android.ModuleContext, rsFile android.Path) android.WritablePath {
  60. fileName := strings.TrimSuffix(rsFile.Base(), rsFile.Ext())
  61. return android.PathForModuleGen(ctx, "rs", fileName+".d")
  62. }
  63. func rsGenerateCpp(ctx android.ModuleContext, rsFiles android.Paths, rsFlags string) android.Paths {
  64. stampFile := android.PathForModuleGen(ctx, "rs", "rs.stamp")
  65. depFiles := make(android.WritablePaths, 0, len(rsFiles))
  66. genFiles := make(android.WritablePaths, 0, 2*len(rsFiles))
  67. headers := make(android.Paths, 0, len(rsFiles))
  68. for _, rsFile := range rsFiles {
  69. depFiles = append(depFiles, rsGeneratedDepFile(ctx, rsFile))
  70. headerFile := rsGeneratedHFile(ctx, rsFile)
  71. genFiles = append(genFiles, rsGeneratedCppFile(ctx, rsFile), headerFile)
  72. headers = append(headers, headerFile)
  73. }
  74. ctx.Build(pctx, android.BuildParams{
  75. Rule: rsCpp,
  76. Description: "llvm-rs-cc",
  77. Output: stampFile,
  78. ImplicitOutputs: genFiles,
  79. Inputs: rsFiles,
  80. Args: map[string]string{
  81. "rsFlags": rsFlags,
  82. "outDir": android.PathForModuleGen(ctx, "rs").String(),
  83. "depFiles": strings.Join(depFiles.Strings(), " "),
  84. },
  85. })
  86. return headers
  87. }
  88. func rsFlags(ctx ModuleContext, flags Flags, properties *BaseCompilerProperties) Flags {
  89. targetApi := String(properties.Renderscript.Target_api)
  90. if targetApi == "" && ctx.useSdk() {
  91. switch ctx.sdkVersion() {
  92. case "current", "system_current", "test_current":
  93. // Nothing
  94. default:
  95. targetApi = android.GetNumericSdkVersion(ctx.sdkVersion())
  96. }
  97. }
  98. if targetApi != "" {
  99. flags.rsFlags = append(flags.rsFlags, "-target-api "+targetApi)
  100. }
  101. flags.rsFlags = append(flags.rsFlags, "-Wall", "-Werror")
  102. flags.rsFlags = append(flags.rsFlags, properties.Renderscript.Flags...)
  103. if ctx.Arch().ArchType.Multilib == "lib64" {
  104. flags.rsFlags = append(flags.rsFlags, "-m64")
  105. } else {
  106. flags.rsFlags = append(flags.rsFlags, "-m32")
  107. }
  108. flags.rsFlags = append(flags.rsFlags, "${config.RsGlobalIncludes}")
  109. rootRsIncludeDirs := android.PathsForSource(ctx, properties.Renderscript.Include_dirs)
  110. flags.rsFlags = append(flags.rsFlags, includeDirsToFlags(rootRsIncludeDirs))
  111. flags.Local.CommonFlags = append(flags.Local.CommonFlags,
  112. "-I"+android.PathForModuleGen(ctx, "rs").String(),
  113. "-Iframeworks/rs",
  114. "-Iframeworks/rs/cpp",
  115. )
  116. return flags
  117. }
  118. type rscriptAttributes struct {
  119. // Renderscript source files
  120. Srcs bazel.LabelListAttribute
  121. }
  122. func bp2buildRScript(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) (bazel.LabelAttribute, bazel.StringListAttribute, bazel.StringListAttribute) {
  123. var rscriptAttrs rscriptAttributes
  124. var rsAbsIncludes bazel.StringListAttribute
  125. var localIncludes bazel.StringListAttribute
  126. var rsModuleName string
  127. var convertedRsSrcsLabel bazel.LabelAttribute
  128. if !ca.rscriptSrcs.IsEmpty() {
  129. rscriptAttrs.Srcs = ca.rscriptSrcs
  130. rsModuleName = m.Name() + "_renderscript"
  131. localIncludes.Value = []string{"."}
  132. rsAbsIncludes.Value = []string{"frameworks/rs", "frameworks/rs/cpp"}
  133. convertedRsSrcsLabel = bazel.LabelAttribute{Value: &bazel.Label{Label: rsModuleName}}
  134. ctx.CreateBazelTargetModule(
  135. bazel.BazelTargetModuleProperties{
  136. Rule_class: "rscript_to_cpp",
  137. Bzl_load_location: "//build/bazel/rules/cc:rscript_to_cpp.bzl",
  138. },
  139. android.CommonAttributes{Name: rsModuleName},
  140. &rscriptAttrs)
  141. }
  142. return convertedRsSrcsLabel, rsAbsIncludes, localIncludes
  143. }