generate_resource_ball.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2024 Google LLC
  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. from waflib import Node, Task, TaskGen
  15. from resources.resource_map import resource_generator
  16. from resources.resource_map.resource_generator_js import JsResourceGenerator
  17. from resources.types.resource_definition import StorageType
  18. from resources.types.resource_object import ResourceObject
  19. from resources.types.resource_ball import ResourceBall
  20. class reso(Task.Task):
  21. def run(self):
  22. reso = resource_generator.generate_object(self, self.definition)
  23. reso.dump(self.outputs[0])
  24. class resource_ball(Task.Task):
  25. def run(self):
  26. resos = [ResourceObject.load(r.abspath()) for r in self.inputs]
  27. resource_id_mapping = getattr(self.env, 'RESOURCE_ID_MAPPING', {})
  28. ordered_resos = []
  29. # Sort the resources into an ordered list by storage. Don't just use sorted
  30. # because we want to preserve the ordering within the storage type. If a
  31. # resource id mapping exists, use that to sort the resources instead.
  32. if not resource_id_mapping:
  33. for s in [StorageType.pbpack, StorageType.builtin, StorageType.pfs]:
  34. ordered_resos.extend((o for o in resos if o.definition.storage == s))
  35. else:
  36. resos_dict = {resource_id_mapping[reso.definition.name]: reso for reso in resos}
  37. ordered_resos = [resos_dict[x] for x in range(1, len(resos) + 1)]
  38. res_ball = ResourceBall(ordered_resos, getattr(self, 'resource_declarations', []))
  39. res_ball.dump(self.outputs[0])
  40. def process_resource_definition(task_gen, resource_definition):
  41. """
  42. Create a task that generates a .reso and returns the node pointing to
  43. the output
  44. """
  45. sources = []
  46. for s in resource_definition.sources:
  47. source_node = task_gen.path.make_node(s)
  48. if source_node is None:
  49. task_gen.bld.fatal("Could not find resource at %s" %
  50. task_gen.bld.path.find_node(s).abspath())
  51. sources.append(source_node)
  52. output_name = '%s.%s.%s' % (sources[0].relpath(), str(resource_definition.name), 'reso')
  53. # Build our outputs in a directory relative to where our final pbpack is going to go
  54. output = task_gen.resource_ball.parent.make_node(output_name)
  55. task = task_gen.create_task('reso', sources, output)
  56. task.definition = resource_definition
  57. task.dep_nodes = getattr(task_gen, 'resource_dependencies', [])
  58. # Save JS bytecode filename for dependency calculation for SDK memory report
  59. if resource_definition.type == 'js' and 'PEBBLE_SDK_ROOT' in task_gen.env:
  60. task_gen.bld.all_envs[task_gen.env.PLATFORM_NAME].JS_RESO = output
  61. return output
  62. @TaskGen.feature('generate_resource_ball')
  63. @TaskGen.before_method('process_source', 'process_rule')
  64. def process_resource_ball(task_gen):
  65. """
  66. resources: a list of ResourceDefinitions objects and nodes pointing to
  67. .reso files
  68. resource_dependencies: node list that all our generated resources depend on
  69. resource_ball: a node to where the ball should be generated
  70. vars: a list of environment variables that generated resources should depend on as a source
  71. """
  72. resource_objects = []
  73. bundled_resos = []
  74. for r in task_gen.resources:
  75. if isinstance(r, Node.Node):
  76. # It's already a node, presumably pointing to a .reso file
  77. resource_objects.append(r)
  78. else:
  79. # It's a resource definition, we need to process it into a .reso
  80. # file. Note this is where the task that does the data conversion
  81. # gets created.
  82. processed_resource = process_resource_definition(task_gen, r)
  83. resource_objects.append(processed_resource)
  84. bundled_resos.append(processed_resource)
  85. if getattr(task_gen, 'project_resource_ball', None):
  86. prb_task = task_gen.create_task('resource_ball',
  87. bundled_resos,
  88. task_gen.project_resource_ball)
  89. prb_task.dep_node = getattr(task_gen, 'resource_dependencies', [])
  90. prb_task.dep_vars = getattr(task_gen, 'vars', [])
  91. task = task_gen.create_task('resource_ball', resource_objects, task_gen.resource_ball)
  92. task.resource_declarations = getattr(task_gen, 'resource_declarations', [])
  93. task.dep_nodes = getattr(task_gen, 'resource_dependencies', [])
  94. task.dep_vars = getattr(task_gen, 'vars', [])