java_resources.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2015 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 java
  15. import (
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "github.com/google/blueprint/pathtools"
  20. "android/soong/android"
  21. )
  22. var resourceExcludes = []string{
  23. "**/*.java",
  24. "**/package.html",
  25. "**/overview.html",
  26. "**/.*.swp",
  27. "**/.DS_Store",
  28. "**/*~",
  29. }
  30. type resourceDeps struct {
  31. dir android.Path
  32. files android.Paths
  33. }
  34. func ResourceDirsToFiles(ctx android.BaseModuleContext,
  35. resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (deps []resourceDeps) {
  36. var excludeDirs []string
  37. var excludeFiles []string
  38. for _, exclude := range excludeResourceDirs {
  39. dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, exclude).String(), nil)
  40. for _, dir := range dirs {
  41. excludeDirs = append(excludeDirs, dir.String())
  42. excludeFiles = append(excludeFiles, dir.(android.SourcePath).Join(ctx, "**/*").String())
  43. }
  44. }
  45. excludeFiles = append(excludeFiles, android.PathsForModuleSrc(ctx, excludeResourceFiles).Strings()...)
  46. excludeFiles = append(excludeFiles, resourceExcludes...)
  47. for _, resourceDir := range resourceDirs {
  48. // resourceDir may be a glob, resolve it first
  49. dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs)
  50. for _, dir := range dirs {
  51. files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
  52. deps = append(deps, resourceDeps{
  53. dir: dir,
  54. files: files,
  55. })
  56. }
  57. }
  58. return deps
  59. }
  60. func ResourceDirsToJarArgs(ctx android.ModuleContext,
  61. resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
  62. resDeps := ResourceDirsToFiles(ctx, resourceDirs, excludeResourceDirs, excludeResourceFiles)
  63. for _, resDep := range resDeps {
  64. dir, files := resDep.dir, resDep.files
  65. if len(files) > 0 {
  66. args = append(args, "-C", dir.String())
  67. deps = append(deps, files...)
  68. for _, f := range files {
  69. path := f.String()
  70. if !strings.HasPrefix(path, dir.String()) {
  71. panic(fmt.Errorf("path %q does not start with %q", path, dir))
  72. }
  73. args = append(args, "-f", pathtools.MatchEscape(path))
  74. }
  75. }
  76. }
  77. return args, deps
  78. }
  79. // Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
  80. // that should not be treated as resources (including *.java).
  81. func ResourceFilesToJarArgs(ctx android.ModuleContext,
  82. res, exclude []string) (args []string, deps android.Paths) {
  83. exclude = append([]string(nil), exclude...)
  84. exclude = append(exclude, resourceExcludes...)
  85. return resourceFilesToJarArgs(ctx, res, exclude)
  86. }
  87. func resourceFilesToJarArgs(ctx android.ModuleContext,
  88. res, exclude []string) (args []string, deps android.Paths) {
  89. files := android.PathsForModuleSrcExcludes(ctx, res, exclude)
  90. args = resourcePathsToJarArgs(files)
  91. return args, files
  92. }
  93. func resourcePathsToJarArgs(files android.Paths) []string {
  94. var args []string
  95. lastDir := ""
  96. for i, f := range files {
  97. rel := f.Rel()
  98. path := f.String()
  99. if !strings.HasSuffix(path, rel) {
  100. panic(fmt.Errorf("path %q does not end with %q", path, rel))
  101. }
  102. dir := filepath.Clean(strings.TrimSuffix(path, rel))
  103. if i == 0 || dir != lastDir {
  104. args = append(args, "-C", dir)
  105. }
  106. args = append(args, "-f", pathtools.MatchEscape(path))
  107. lastDir = dir
  108. }
  109. return args
  110. }