906b7671ba6850eb48ae7723c804c70eeb32065a
[platform/upstream/libSkiaSharp.git] / bin / sync-and-gyp
1 #!/usr/bin/env python
2
3 # Copyright 2015 Google Inc.
4 #
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8 # This script will update Skia's dependencies as necessary and run
9 # gyp if needed.
10
11 # Depends on: Python, Git, and depot_tools.
12 #
13 # Example usage:
14 #
15 #   git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
16 #   export PATH="${PWD}/depot_tools:${PATH}"
17 #   git clone https://skia.googlesource.com/skia
18 #   cd skia
19 #   python bin/sync-and-gyp
20 #   ninja -C out/Debug && out/Debug/dm
21 #
22 # Once changes are made to DEPS or gyp/ or the source, call:
23 #
24 #   python bin/sync-and-gyp
25
26 import fnmatch
27 import hashlib
28 import subprocess
29 import os
30 import sys
31
32 skia_dir = os.path.join(os.path.dirname(__file__), os.pardir)
33
34 skia_out = os.environ.get("SKIA_OUT")
35 if skia_out:
36   skia_out = os.path.abspath(skia_out)
37   hash_path = os.path.join(skia_out, 'gyp_hash')
38 else:
39   hash_path = os.path.join('out', 'gyp_hash')
40
41 os.chdir(skia_dir)
42
43 if not os.path.isfile('DEPS'):
44   sys.stderr.write('DEPS file missing')
45   exit(1)
46
47 deps_hasher = hashlib.sha1()
48 with open('DEPS', 'r') as f:
49   deps_hasher.update(f.read())
50 deps_hash = deps_hasher.hexdigest()
51 current_deps_hash = None
52 if os.path.isfile('.deps_sha1'):
53   with open('.deps_sha1', 'r') as f:
54     current_deps_hash = f.read().strip()
55
56 default_gclient_config = '''
57 solutions = [
58   { "name"        : ".",
59     "url"         : "https://skia.googlesource.com/skia.git",
60     "deps_file"   : "DEPS",
61     "managed"     : False,
62     "custom_deps" : {
63     },
64     "safesync_url": "",
65   },
66 ]
67 cache_dir = None
68 '''
69 if current_deps_hash != deps_hash:
70   # `gclient sync` is very slow, so skip whenever we can.
71   if not os.path.isfile('.gclient'):
72     with open('.gclient', 'w') as o:
73       o.write(default_gclient_config)
74   try:
75     subprocess.check_call(['gclient', 'sync'])
76   except:
77     sys.stderr.write('\n`gclient sync` failed.\n')
78     os.remove('.deps_sha1')  # Unknown state.
79     exit(1)
80   # Only write hash after a successful sync.
81   with open('.deps_sha1', 'w') as o:
82     o.write(deps_hash)
83
84 hasher = hashlib.sha1()
85
86 for var in ['AR', 'AR_host', 'AR_target',
87             'CC', 'CC_host', 'CC_target',
88             'CFLAGS', 'CFLAGS_host',
89             'CPPFLAGS', 'CPPFLAGS_host',
90             'CXX', 'CXX_host', 'CXX_target',
91             'GYP_DEFINES', 'GYP_GENERATORS',
92             'NM', 'NM_host', 'NM_target',
93             'READELF', 'READELF_host', 'READELF_target']:
94   hasher.update(os.environ.get(var, '') + '\n')
95
96 def listfiles(folder, matchfilter):
97   for root, folders, files in os.walk(folder):
98     for filename in files:
99       if fnmatch.fnmatch(filename, matchfilter):
100         yield os.path.join(root, filename)
101
102 for filename in sorted(listfiles('gyp', '*')):
103   with open(filename, 'r') as f:
104     hasher.update(f.read())
105
106 for dir in ['bench', 'gm', 'tests']:
107   for filename in sorted(listfiles(dir, '*.c*')):
108     hasher.update(filename + '\n')
109
110 gyp_hash = hasher.hexdigest()
111
112 def cat_if_exists(path):
113   if os.path.exists(path):
114     with open(path, 'r') as f:
115       return f.read()
116   return ''
117
118 if cat_if_exists(hash_path).strip() != gyp_hash:
119   env = os.environ.copy()
120   if skia_out:
121     env['SKIA_OUT'] = skia_out
122   subprocess.call(['python', './gyp_skia'], env=env)
123   with open(hash_path, 'w') as o:
124     o.write(gyp_hash)