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