1# Copyright 2019 The Fuchsia Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6import sys
7import shutil
8import subprocess
9import argparse
10import re
11
12parser = argparse.ArgumentParser(description="Upload goldfish libvulkan to CIPD")
13
14parser.add_argument("--release-dir")
15parser.add_argument("--arch")
16parser.add_argument("--dry-run", action="store_true")
17parser.add_argument("--ignore-branch", action="store_true")
18parser.add_argument("--ignore-rebuild", action="store_true")
19parser.add_argument("--ignore-buildtype", action="store_true")
20
21args = parser.parse_args()
22
23dir_path = os.path.dirname(os.path.realpath(__file__))
24
25os.chdir(dir_path)
26
27fuchsia_root = os.path.abspath(os.path.join(dir_path, "../../../"))
28fx_path = os.path.join(fuchsia_root, "scripts/fx")
29
30if args.release_dir:
31  release_dir = os.path.abspath(args.release_dir)
32else:
33  release_dir = os.path.join(fuchsia_root, "out/default")
34
35if not os.path.exists(release_dir):
36  print "Release dir: %s doesn't exist" % release_dir
37  sys.exit(1)
38
39if args.arch:
40  arch = args.arch
41else:
42  arch = "x64"
43
44target_name = "%s-shared/lib.unstripped/libvulkan_goldfish.so" % arch
45git_repo_location = "%s/third_party/goldfish-opengl" % fuchsia_root
46package_dir = "libvulkan_goldfish/%s" % arch
47package_name = "fuchsia/lib/libvulkan/%s" % package_dir
48
49repo_name = "goldfish-opengl"
50git_branch = subprocess.check_output([
51    "git", "-C", git_repo_location, "rev-parse", "--abbrev-ref", "HEAD"
52]).strip()
53if git_branch != "master":
54  print("Git repo %s on incorrect branch %s (should be master)" %
55        (repo_name, git_branch))
56  if args.ignore_branch:
57    print("Ignoring")
58  else:
59    print("Use --ignore-branch flag to upload anyway")
60    sys.exit(1)
61
62# Force ninja dry-run
63ninja_output = subprocess.check_output([
64    fx_path, "ninja", "-C", release_dir, "-v", "-n", target_name
65])
66
67if "ninja: no work to do." not in ninja_output:
68  print("Ninja reported work needed to be done for %s" % target_name)
69  if args.ignore_rebuild:
70    print("Ignoring")
71  else:
72    print("Use --ignore-rebuild flag to upload anyway")
73    sys.exit(1)
74
75gn_output = subprocess.check_output([
76    fx_path, "gn", "args", release_dir, "--list=is_debug", "--short"
77]).strip()
78if gn_output != "is_debug = false":
79  print("GN argument \"%s\" unexpected" % gn_output)
80  if args.ignore_buildtype:
81    print("Ignoring")
82  else:
83    print("Use --ignore-buildtype flag to upload anyway")
84    sys.exit(1)
85
86file_name = "libvulkan_goldfish.so"
87full_name = os.path.join(package_dir, file_name)
88
89source_file_name = os.path.join(release_dir, target_name)
90try:
91  os.remove(full_name)
92except:
93  pass
94try:
95  os.makedirs(package_dir)
96except:
97  pass
98shutil.copyfile(source_file_name, full_name)
99
100git_rev = subprocess.check_output(
101    ["git", "-C", git_repo_location, "rev-parse", "HEAD"]).strip()
102
103cipd_command = ("%s cipd create -in %s -name %s -ref latest"
104                " -install-mode copy -tag git_revision:%s") % (
105                    fx_path, package_dir, package_name, git_rev)
106print cipd_command
107if not args.dry_run:
108  subprocess.check_call(cipd_command.split(" "))
109
110print ("""
111  <package name="%s"
112           version="git_revision:%s"
113           path="prebuild/third_party/%s"/>
114""" % (package_name, git_rev, package_dir))[1:-1]
115