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/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
49debug_target_name = "%s-shared/lib.unstripped/libvulkan_goldfish.so" % arch
50debug_dir = "libvulkan_goldfish/debug-symbols-%s" % arch
51debug_package_name = "fuchsia/lib/libvulkan/%s" % debug_dir
52
53repo_name = "goldfish-opengl"
54git_branch = subprocess.check_output([
55    "git", "-C", git_repo_location, "rev-parse", "--abbrev-ref", "HEAD"
56]).strip()
57if git_branch != "master":
58  print("Git repo %s on incorrect branch %s (should be master)" %
59        (repo_name, git_branch))
60  if args.ignore_branch:
61    print("Ignoring")
62  else:
63    print("Use --ignore-branch flag to upload anyway")
64    sys.exit(1)
65
66# Force ninja dry-run
67ninja_output = subprocess.check_output([
68    fx_path, "ninja", "-C", release_dir, "-v", "-n", target_name
69])
70
71if "ninja: no work to do." not in ninja_output:
72  print("Ninja reported work needed to be done for %s" % target_name)
73  if args.ignore_rebuild:
74    print("Ignoring")
75  else:
76    print("Use --ignore-rebuild flag to upload anyway")
77    sys.exit(1)
78
79gn_output = subprocess.check_output([
80    fx_path, "gn", "args", release_dir, "--list=is_debug", "--short"
81]).strip()
82if gn_output != "is_debug = false":
83  print("GN argument \"%s\" unexpected" % gn_output)
84  if args.ignore_buildtype:
85    print("Ignoring")
86  else:
87    print("Use --ignore-buildtype flag to upload anyway")
88    sys.exit(1)
89
90# Prepare libvulkan_goldfish binaries
91file_name = "libvulkan_goldfish.so"
92full_name = os.path.join(package_dir, file_name)
93
94source_file_name = os.path.join(release_dir, target_name)
95try:
96  os.remove(full_name)
97except:
98  pass
99try:
100  os.makedirs(package_dir)
101except:
102  pass
103shutil.copyfile(source_file_name, full_name)
104
105# Prepare libvulkan_goldfish debug binaries
106debug_source_file_name = os.path.join(release_dir, debug_target_name)
107elf_info = re.search(r'Build ID: ([a-f0-9]*)',
108  subprocess.check_output(['readelf', '-n', debug_source_file_name]).strip())
109if not elf_info:
110  print("Fatal: Cannot find build ID in elf binary")
111  sys.exit(1)
112
113build_id = elf_info.group(1)
114debug_output_dir = os.path.join(debug_dir, build_id[:2])
115
116try:
117  shutil.rmtree(debug_dir)
118except:
119  pass
120os.makedirs(debug_output_dir)
121shutil.copyfile(debug_source_file_name, os.path.join(debug_output_dir, build_id[2:] + '.debug'))
122
123# Create libvulkan_goldfish CIPD package
124git_rev = subprocess.check_output(
125    ["git", "-C", git_repo_location, "rev-parse", "HEAD"]).strip()
126
127cipd_command = ("%s cipd create -in %s -name %s -ref latest"
128                " -install-mode copy -tag git_revision:%s") % (
129                    fx_path, package_dir, package_name, git_rev)
130print cipd_command
131if not args.dry_run:
132  subprocess.check_call(cipd_command.split(" "))
133
134# Create libvulkan_goldfish/debug-symbols package
135cipd_command = ("%s cipd create -in %s -name %s -ref latest"
136                " -install-mode copy -tag git_revision:%s") % (
137                    fx_path, debug_dir, debug_package_name, git_rev)
138print cipd_command
139if not args.dry_run:
140  subprocess.check_call(cipd_command.split(" "))
141
142
143print ("""
144  <package name="%s"
145           version="git_revision:%s"
146           path="prebuilt/third_party/%s"/>
147""" % (package_name, git_rev, package_dir))[1:-1]
148print ("""
149  <package name="%s"
150           version="git_revision:%s"
151           path="prebuilt/.build-id"
152           attributes="debug-symbols,debug-symbols-%s"/>
153""" % (debug_package_name, git_rev, arch))[1:-1]
154