ci_build.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import argparse
  9. import json
  10. import os
  11. import sys
  12. import subprocess
  13. def parse_args():
  14. cur_dir = os.path.dirname(os.path.abspath(__file__))
  15. parser = argparse.ArgumentParser()
  16. parser.add_argument('-p', '--platform', dest="build_platform", help="Platform to build")
  17. parser.add_argument('-t', '--type', dest="build_type", help="Target config type to build")
  18. parser.add_argument('-c', '--config', dest="build_config_filename",
  19. default="build_config.json",
  20. help="JSON filename in Platform/<platform> that defines build configurations for the platform")
  21. args = parser.parse_args()
  22. # Input validation
  23. if args.build_platform is None:
  24. print('[ci_build] No platform specified')
  25. sys.exit(-1)
  26. if args.build_type is None:
  27. print('[ci_build] No type specified')
  28. sys.exit(-1)
  29. return args
  30. def build(build_config_filename, build_platform, build_type):
  31. # Read build_config and locate build_type
  32. cwd_dir = os.getcwd()
  33. script_dir = os.path.dirname(os.path.abspath(__file__))
  34. engine_dir = os.path.abspath(os.path.join(script_dir, '../..'))
  35. config_dir = os.path.abspath(os.path.join(script_dir, 'Platform', build_platform))
  36. build_config_abspath = os.path.join(config_dir, build_config_filename)
  37. if not os.path.exists(build_config_abspath):
  38. config_dir = os.path.abspath(os.path.join(engine_dir, 'restricted', build_platform, os.path.relpath(script_dir, engine_dir)))
  39. build_config_abspath = os.path.join(config_dir, build_config_filename)
  40. if not os.path.exists(build_config_abspath):
  41. print('[ci_build] File: {} not found'.format(build_config_abspath))
  42. return -1
  43. with open(build_config_abspath) as f:
  44. build_config_json = json.load(f)
  45. build_type_config = build_config_json[build_type]
  46. if build_type_config is None:
  47. print('[ci_build] Build type {} was not found in {}'.format(build_type, build_config_abspath))
  48. # Load the command to execute
  49. build_cmd = build_type_config['COMMAND']
  50. if build_cmd is None:
  51. print('[ci_build] Build type {} in {} is missing required COMMAND entry'.format(build_type, build_config_abspath))
  52. return -1
  53. build_params = build_type_config['PARAMETERS']
  54. # Parameters are optional, so we could have none
  55. # build_cmd is relative to the folder where this file is
  56. build_cmd_path = os.path.join(script_dir, 'Platform/{}/{}'.format(build_platform, build_cmd))
  57. if not os.path.exists(build_cmd_path):
  58. config_dir = os.path.abspath(os.path.join(engine_dir, 'restricted', build_platform, os.path.relpath(script_dir, engine_dir)))
  59. build_cmd_path = os.path.join(config_dir, build_cmd)
  60. if not os.path.exists(build_cmd_path):
  61. print('[ci_build] File: {} not found'.format(build_cmd_path))
  62. return -1
  63. print('[ci_build] Executing \"{}\"'.format(build_cmd_path))
  64. print(' cwd = {}'.format(cwd_dir))
  65. print(' engine_dir = {}'.format(engine_dir))
  66. print(' parameters:')
  67. env_params = os.environ.copy()
  68. env_params['ENGINE_DIR'] = engine_dir
  69. for v in build_params:
  70. if v[:6] == "FORCE:":
  71. env_params[v[6:]] = build_params[v]
  72. print(' {} = {} (forced)'.format(v[6:], env_params[v[6:]]))
  73. else:
  74. existing_param = env_params.get(v)
  75. if not existing_param:
  76. env_params[v] = build_params[v]
  77. print(' {} = {} {}'.format(v, env_params[v], '(environment override)' if existing_param else ''))
  78. print('--------------------------------------------------------------------------------', flush=True)
  79. process_return = subprocess.run([build_cmd_path], cwd=cwd_dir, env=env_params)
  80. print('--------------------------------------------------------------------------------')
  81. if process_return.returncode != 0:
  82. print('[ci_build] FAIL: Command {} returned {}'.format(build_cmd_path, process_return.returncode), flush=True)
  83. return process_return.returncode
  84. else:
  85. print('[ci_build] OK', flush=True)
  86. return 0
  87. if __name__ == "__main__":
  88. args = parse_args()
  89. ret = build(args.build_config_filename, args.build_platform, args.build_type)
  90. sys.exit(ret)