pebble_sdk_lib.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. import json
  15. import sdk_paths
  16. from process_sdk_resources import generate_resources
  17. from sdk_helpers import (configure_libraries, configure_platform, get_target_platforms,
  18. validate_message_keys_object)
  19. def options(opt):
  20. """
  21. Specify the options available when invoking waf; uses OptParse
  22. :param opt: the OptionContext object
  23. :return: N/A
  24. """
  25. opt.load('pebble_sdk_common')
  26. opt.add_option('-t', '--timestamp', dest='timestamp',
  27. help="Use a specific timestamp to label this package (ie, your repository's last commit time), "
  28. "defaults to time of build")
  29. def configure(conf):
  30. """
  31. Configure the build using information obtained from the package.json file
  32. :param conf: the ConfigureContext object
  33. :return: N/A
  34. """
  35. conf.load('pebble_sdk_common')
  36. # This overrides the default config in pebble_sdk_common.py
  37. if conf.options.timestamp:
  38. conf.env.TIMESTAMP = conf.options.timestamp
  39. conf.env.BUNDLE_NAME = "dist.zip"
  40. package_json_node = conf.path.get_src().find_node('package.json')
  41. if package_json_node is None:
  42. conf.fatal('Could not find package.json')
  43. with open(package_json_node.abspath(), 'r') as f:
  44. package_json = json.load(f)
  45. # Extract project info from "pebble" object in package.json
  46. project_info = package_json['pebble']
  47. project_info['name'] = package_json['name']
  48. validate_message_keys_object(conf, project_info, 'package.json')
  49. conf.env.PROJECT_INFO = project_info
  50. conf.env.BUILD_TYPE = 'lib'
  51. conf.env.REQUESTED_PLATFORMS = project_info.get('targetPlatforms', [])
  52. conf.env.LIB_DIR = "node_modules"
  53. get_target_platforms(conf)
  54. # With new-style projects, check for libraries specified in package.json
  55. if 'dependencies' in package_json:
  56. configure_libraries(conf, package_json['dependencies'])
  57. conf.load('process_message_keys')
  58. if 'resources' in project_info and 'media' in project_info['resources']:
  59. conf.env.RESOURCES_JSON = package_json['pebble']['resources']['media']
  60. # base_env is set to a shallow copy of the current ConfigSet for this ConfigureContext
  61. base_env = conf.env
  62. for platform in conf.env.TARGET_PLATFORMS:
  63. # Create a deep copy of the `base_env` ConfigSet and set conf.env to a shallow copy of
  64. # the resultant ConfigSet
  65. conf.setenv(platform, base_env)
  66. configure_platform(conf, platform)
  67. # conf.env is set back to a shallow copy of the default ConfigSet stored in conf.all_envs['']
  68. conf.setenv('')
  69. def build(bld):
  70. """
  71. This method is invoked from a project's wscript with the `ctz.load('pebble_sdk_lib')` call
  72. and sets up all of the task generators for the SDK. After all of the build methods have run,
  73. the configured task generators will run, generating build tasks and managing dependencies. See
  74. https://waf.io/book/#_task_generators for more details on task generator setup.
  75. :param bld: the BuildContext object
  76. :return: N/A
  77. """
  78. bld.load('pebble_sdk_common')
  79. # cached_env is set to a shallow copy of the current ConfigSet for this BuildContext
  80. cached_env = bld.env
  81. for platform in bld.env.TARGET_PLATFORMS:
  82. # bld.env is set to a shallow copy of the ConfigSet labeled <platform>
  83. bld.env = bld.all_envs[platform]
  84. # Set the build group (set of TaskGens) to the group labeled <platform>
  85. if bld.env.USE_GROUPS:
  86. bld.set_group(bld.env.PLATFORM_NAME)
  87. # Generate resources specific to the current platform
  88. resource_path = None
  89. if bld.env.RESOURCES_JSON:
  90. try:
  91. resource_path = bld.path.find_node('src').find_node('resources').path_from(bld.path)
  92. except AttributeError:
  93. bld.fatal("Unable to locate resources at src/resources/")
  94. generate_resources(bld, resource_path)
  95. # bld.env is set back to a shallow copy of the original ConfigSet that was set when this `build`
  96. # method was invoked
  97. bld.env = cached_env