1#!/usr/bin/env python 2# 3# Copyright 2016 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 8from __future__ import print_function 9 10import collections 11import json 12import os 13import subprocess 14import sys 15 16# Finds all public sources in include directories then write them to skia.h. 17 18# Also write skia.h.deps, which Ninja uses to track dependencies. It's the 19# very same mechanism Ninja uses to know which .h files affect which .cpp files. 20 21os.system("rm ../../.gn") 22os.system("ln -s third_party/skia/.gn ../../.gn") 23 24gn = sys.argv[1] 25absolute_source = sys.argv[2] 26skia_h = sys.argv[3] 27include_dirs = sys.argv[4:] 28 29absolute_source = os.path.normpath(absolute_source) 30 31include_dirs = [os.path.join(os.path.normpath(include_dir), '') 32 for include_dir in include_dirs] 33include_dirs.sort(key=len, reverse=True) 34 35gn_desc_cmd = [gn, 'desc', '.', '--root=%s' % absolute_source, '--format=json', 36 '*'] 37 38desc_json_txt = '' 39try: 40 desc_json_txt = subprocess.check_output(gn_desc_cmd).decode('utf-8') 41except subprocess.CalledProcessError as e: 42 print(e.output.decode('utf-8')) 43 raise 44 45begin_index = desc_json_txt.index("{") 46desc_json_txt = desc_json_txt[begin_index:] 47 48desc_json = {} 49try: 50 desc_json = json.loads(desc_json_txt) 51except ValueError: 52 print(desc_json_txt) 53 raise 54 55sources = set() 56 57for target in desc_json.values(): 58 # We'll use `public` headers if they're listed, or pull them from `sources` 59 # if not. GN sneaks in a default "public": "*" into the JSON if you don't 60 # set one explicitly. 61 search_list = target.get('public') 62 if search_list == '*': 63 search_list = target.get('sources', []) 64 65 for name in search_list: 66 sources.add(os.path.join(absolute_source, os.path.normpath(name[2:]))) 67 68Header = collections.namedtuple('Header', ['absolute', 'include']) 69headers = {} 70for source in sources: 71 source_as_include = [os.path.relpath(source, absolute_source) 72 for include_dir in include_dirs 73 if source.startswith(include_dir)] 74 if not source_as_include: 75 continue 76 statinfo = os.stat(source) 77 key = str(statinfo.st_ino) + ':' + str(statinfo.st_dev) 78 # On Windows os.stat st_ino is 0 until 3.3.4 and st_dev is 0 until 3.4.0. 79 if key == '0:0': 80 key = source 81 include_path = source_as_include[0] 82 if key not in headers or len(include_path) < len(headers[key].include): 83 headers[key] = Header(source, include_path) 84 85headers = sorted(headers.values(), key=lambda x: x.include) 86 87with open(skia_h, 'w') as f: 88 f.write('// skia.h generated by GN.\n') 89 f.write('#ifndef skia_h_DEFINED\n') 90 f.write('#define skia_h_DEFINED\n') 91 for header in headers: 92 f.write('#include "' + header.include + '"\n') 93 f.write('#endif//skia_h_DEFINED\n') 94 95with open(skia_h + '.deps', 'w') as f: 96 f.write(skia_h + ':') 97 for header in headers: 98 f.write(' ' + header.absolute) 99 f.write(' build.ninja.d') 100 f.write('\n') 101 102# Temporary: during development this file wrote skia.h.d, not skia.h.deps, 103# and I think we have some bad versions of those files laying around. 104if os.path.exists(skia_h + '.d'): 105 os.remove(skia_h + '.d') 106 107os.system("rm ../../.gn") 108os.system("ln -s build/core/gn/dotfile.gn ../../.gn") 109