blob: ac6b8392b23b1cf1285772ee773683cf9754d523 [file] [log] [blame]
halcanarya4c26c02015-11-09 08:28:13 -08001#!/usr/bin/env python
halcanary135b7ec2015-03-27 12:11:49 -07002
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
halcanarya4c26c02015-11-09 08:28:13 -08008# This script will update Skia's dependencies as necessary and run
halcanary135b7ec2015-03-27 12:11:49 -07009# gyp if needed.
10
halcanary2362c472016-02-16 11:48:06 -080011# Depends on: Python, Git, and depot_tools.
halcanary135b7ec2015-03-27 12:11:49 -070012#
mtklein143fd552015-11-03 12:07:47 -080013# Example usage:
14#
halcanary2362c472016-02-16 11:48:06 -080015# git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
16# export PATH="${PWD}/depot_tools:${PATH}"
halcanary135b7ec2015-03-27 12:11:49 -070017# git clone https://skia.googlesource.com/skia
18# cd skia
halcanarya4c26c02015-11-09 08:28:13 -080019# python bin/sync-and-gyp
mtklein143fd552015-11-03 12:07:47 -080020# ninja -C out/Debug && out/Debug/dm
halcanary135b7ec2015-03-27 12:11:49 -070021#
mtklein143fd552015-11-03 12:07:47 -080022# Once changes are made to DEPS or gyp/ or the source, call:
halcanary135b7ec2015-03-27 12:11:49 -070023#
halcanarya4c26c02015-11-09 08:28:13 -080024# python bin/sync-and-gyp
mtklein143fd552015-11-03 12:07:47 -080025
halcanary117672d2016-02-17 08:39:23 -080026# To retreive and use all optional deps:
27#
28# python bin/sync-and-gyp --deps=all
29# ninja -C out/Debug && out/Debug/dm
30
halcanarya4c26c02015-11-09 08:28:13 -080031import fnmatch
32import hashlib
halcanarya4c26c02015-11-09 08:28:13 -080033import os
halcanary117672d2016-02-17 08:39:23 -080034import subprocess
mtkleindc469ad2016-02-17 06:37:51 -080035import sys
halcanary135b7ec2015-03-27 12:11:49 -070036
halcanarya4c26c02015-11-09 08:28:13 -080037skia_dir = os.path.join(os.path.dirname(__file__), os.pardir)
halcanary135b7ec2015-03-27 12:11:49 -070038
halcanary117672d2016-02-17 08:39:23 -080039skia_opt_deps = [arg for arg in sys.argv[1:] if arg.startswith('--deps=')]
40
halcanarya4c26c02015-11-09 08:28:13 -080041skia_out = os.environ.get("SKIA_OUT")
42if skia_out:
43 skia_out = os.path.abspath(skia_out)
44 hash_path = os.path.join(skia_out, 'gyp_hash')
45else:
46 hash_path = os.path.join('out', 'gyp_hash')
halcanary135b7ec2015-03-27 12:11:49 -070047
halcanarya4c26c02015-11-09 08:28:13 -080048os.chdir(skia_dir)
halcanary135b7ec2015-03-27 12:11:49 -070049
halcanarya4c26c02015-11-09 08:28:13 -080050if not os.path.isfile('DEPS'):
51 sys.stderr.write('DEPS file missing')
52 exit(1)
halcanary135b7ec2015-03-27 12:11:49 -070053
halcanary2362c472016-02-16 11:48:06 -080054deps_hasher = hashlib.sha1()
55with open('DEPS', 'r') as f:
56 deps_hasher.update(f.read())
halcanary117672d2016-02-17 08:39:23 -080057deps_hasher.update(repr(skia_opt_deps))
halcanary2362c472016-02-16 11:48:06 -080058deps_hash = deps_hasher.hexdigest()
59current_deps_hash = None
60if os.path.isfile('.deps_sha1'):
61 with open('.deps_sha1', 'r') as f:
62 current_deps_hash = f.read().strip()
63
64default_gclient_config = '''
65solutions = [
66 { "name" : ".",
67 "url" : "https://skia.googlesource.com/skia.git",
68 "deps_file" : "DEPS",
69 "managed" : False,
70 "custom_deps" : {
71 },
72 "safesync_url": "",
73 },
74]
75cache_dir = None
76'''
77if current_deps_hash != deps_hash:
78 # `gclient sync` is very slow, so skip whenever we can.
79 if not os.path.isfile('.gclient'):
80 with open('.gclient', 'w') as o:
81 o.write(default_gclient_config)
halcanary117672d2016-02-17 08:39:23 -080082 gclient_sync_command = ['gclient', 'sync'] + skia_opt_deps
halcanary2362c472016-02-16 11:48:06 -080083 try:
halcanary117672d2016-02-17 08:39:23 -080084 sys.stdout.write('%r\n' % gclient_sync_command)
85 subprocess.check_call(gclient_sync_command)
halcanary2362c472016-02-16 11:48:06 -080086 except:
87 sys.stderr.write('\n`gclient sync` failed.\n')
88 os.remove('.deps_sha1') # Unknown state.
89 exit(1)
90 # Only write hash after a successful sync.
91 with open('.deps_sha1', 'w') as o:
92 o.write(deps_hash)
halcanary135b7ec2015-03-27 12:11:49 -070093
halcanarya4c26c02015-11-09 08:28:13 -080094hasher = hashlib.sha1()
95
96for var in ['AR', 'AR_host', 'AR_target',
97 'CC', 'CC_host', 'CC_target',
98 'CFLAGS', 'CFLAGS_host',
99 'CPPFLAGS', 'CPPFLAGS_host',
100 'CXX', 'CXX_host', 'CXX_target',
101 'GYP_DEFINES', 'GYP_GENERATORS',
102 'NM', 'NM_host', 'NM_target',
103 'READELF', 'READELF_host', 'READELF_target']:
104 hasher.update(os.environ.get(var, '') + '\n')
105
106def listfiles(folder, matchfilter):
107 for root, folders, files in os.walk(folder):
108 for filename in files:
109 if fnmatch.fnmatch(filename, matchfilter):
110 yield os.path.join(root, filename)
111
112for filename in sorted(listfiles('gyp', '*')):
113 with open(filename, 'r') as f:
114 hasher.update(f.read())
115
116for dir in ['bench', 'gm', 'tests']:
117 for filename in sorted(listfiles(dir, '*.c*')):
118 hasher.update(filename + '\n')
119
120gyp_hash = hasher.hexdigest()
121
122def cat_if_exists(path):
123 if os.path.exists(path):
124 with open(path, 'r') as f:
125 return f.read()
126 return ''
127
128if cat_if_exists(hash_path).strip() != gyp_hash:
129 env = os.environ.copy()
130 if skia_out:
131 env['SKIA_OUT'] = skia_out
132 subprocess.call(['python', './gyp_skia'], env=env)
133 with open(hash_path, 'w') as o:
134 o.write(gyp_hash)