2016-11-02 13:14:55 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
2019-12-17 16:32:21 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
import collections
|
|
|
|
import json
|
2016-11-02 13:14:55 +00:00
|
|
|
import os
|
2018-05-25 19:48:31 +00:00
|
|
|
import subprocess
|
2016-11-02 13:14:55 +00:00
|
|
|
import sys
|
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
# Finds all public sources in include directories then write them to skia.h.
|
2016-11-02 13:14:55 +00:00
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
# Also write skia.h.deps, which Ninja uses to track dependencies. It's the
|
2016-11-02 13:14:55 +00:00
|
|
|
# very same mechanism Ninja uses to know which .h files affect which .cpp files.
|
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
gn = sys.argv[1]
|
|
|
|
absolute_source = sys.argv[2]
|
|
|
|
skia_h = sys.argv[3]
|
|
|
|
include_dirs = sys.argv[4:]
|
2016-11-02 13:14:55 +00:00
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
absolute_source = os.path.normpath(absolute_source)
|
2016-11-02 13:14:55 +00:00
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
include_dirs = [os.path.join(os.path.normpath(include_dir), '')
|
|
|
|
for include_dir in include_dirs]
|
|
|
|
include_dirs.sort(key=len, reverse=True)
|
2016-11-02 13:14:55 +00:00
|
|
|
|
2018-05-30 17:17:49 +00:00
|
|
|
gn_desc_cmd = [gn, 'desc', '.', '--root=%s' % absolute_source, '--format=json',
|
|
|
|
'*']
|
2018-05-25 19:48:31 +00:00
|
|
|
|
2018-05-30 17:17:49 +00:00
|
|
|
desc_json_txt = ''
|
2018-05-25 19:48:31 +00:00
|
|
|
try:
|
2018-05-30 17:17:49 +00:00
|
|
|
desc_json_txt = subprocess.check_output(gn_desc_cmd)
|
2018-05-25 19:48:31 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2019-12-17 16:32:21 +00:00
|
|
|
print(e.output)
|
2018-05-25 19:48:31 +00:00
|
|
|
raise
|
|
|
|
|
2018-05-30 17:17:49 +00:00
|
|
|
desc_json = {}
|
2018-05-25 19:48:31 +00:00
|
|
|
try:
|
2018-05-30 17:17:49 +00:00
|
|
|
desc_json = json.loads(desc_json_txt)
|
2018-05-25 19:48:31 +00:00
|
|
|
except ValueError:
|
2019-12-17 16:32:21 +00:00
|
|
|
print(desc_json_txt)
|
2018-05-25 19:48:31 +00:00
|
|
|
raise
|
|
|
|
|
2018-05-30 17:17:49 +00:00
|
|
|
sources = set()
|
|
|
|
|
2019-12-17 16:32:21 +00:00
|
|
|
for target in desc_json.values():
|
2018-05-30 17:17:49 +00:00
|
|
|
# We'll use `public` headers if they're listed, or pull them from `sources`
|
|
|
|
# if not. GN sneaks in a default "public": "*" into the JSON if you don't
|
|
|
|
# set one explicitly.
|
|
|
|
search_list = target.get('public')
|
|
|
|
if search_list == '*':
|
|
|
|
search_list = target.get('sources', [])
|
|
|
|
|
|
|
|
for name in search_list:
|
|
|
|
sources.add(os.path.join(absolute_source, os.path.normpath(name[2:])))
|
2018-05-25 19:48:31 +00:00
|
|
|
|
|
|
|
Header = collections.namedtuple('Header', ['absolute', 'include'])
|
|
|
|
headers = {}
|
|
|
|
for source in sources:
|
2019-04-23 17:05:21 +00:00
|
|
|
source_as_include = [os.path.relpath(source, absolute_source)
|
2018-05-25 19:48:31 +00:00
|
|
|
for include_dir in include_dirs
|
|
|
|
if source.startswith(include_dir)]
|
|
|
|
if not source_as_include:
|
|
|
|
continue
|
|
|
|
statinfo = os.stat(source)
|
|
|
|
key = str(statinfo.st_ino) + ':' + str(statinfo.st_dev)
|
|
|
|
# On Windows os.stat st_ino is 0 until 3.3.4 and st_dev is 0 until 3.4.0.
|
|
|
|
if key == '0:0':
|
|
|
|
key = source
|
|
|
|
include_path = source_as_include[0]
|
|
|
|
if key not in headers or len(include_path) < len(headers[key].include):
|
|
|
|
headers[key] = Header(source, include_path)
|
|
|
|
|
2019-12-17 16:32:21 +00:00
|
|
|
headers = sorted(headers.values(), key=lambda x: x.include)
|
2018-05-25 19:48:31 +00:00
|
|
|
|
|
|
|
with open(skia_h, 'w') as f:
|
2016-11-02 13:14:55 +00:00
|
|
|
f.write('// skia.h generated by GN.\n')
|
|
|
|
f.write('#ifndef skia_h_DEFINED\n')
|
|
|
|
f.write('#define skia_h_DEFINED\n')
|
2018-05-25 19:48:31 +00:00
|
|
|
for header in headers:
|
|
|
|
f.write('#include "' + header.include + '"\n')
|
2016-11-02 13:14:55 +00:00
|
|
|
f.write('#endif//skia_h_DEFINED\n')
|
|
|
|
|
2018-05-25 19:48:31 +00:00
|
|
|
with open(skia_h + '.deps', 'w') as f:
|
2016-11-02 13:14:55 +00:00
|
|
|
f.write(skia_h + ':')
|
2018-05-25 19:48:31 +00:00
|
|
|
for header in headers:
|
|
|
|
f.write(' ' + header.absolute)
|
|
|
|
f.write(' build.ninja.d')
|
2016-11-02 13:14:55 +00:00
|
|
|
f.write('\n')
|
|
|
|
|
|
|
|
# Temporary: during development this file wrote skia.h.d, not skia.h.deps,
|
|
|
|
# and I think we have some bad versions of those files laying around.
|
|
|
|
if os.path.exists(skia_h + '.d'):
|
|
|
|
os.remove(skia_h + '.d')
|