• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright 2018 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7Script to run wayland-scaner for wayland_protocol.gni.
8"""
9
10from __future__ import print_function
11
12import argparse
13import os.path
14import subprocess
15import sys
16
17def generate_code(wayland_scanner_cmd, code_type, path_in, path_out):
18    """
19        [generate code]
20        Args:
21            wayland_scanner_cmd : wayland_scanner_cmd
22            code_type : type
23            path_in : path_in
24            path_out : path_out
25        Returns:
26            none: none
27        Raises:
28            IOError: wayland-scanner returned an error
29    """
30    ret = subprocess.call([wayland_scanner_cmd, code_type, path_in, path_out])
31    if ret != 0:
32        raise RuntimeError("wayland-scanner returned an error: %d" % ret)
33
34
35def main():
36    """
37        [main]
38        Args: NA
39        Returns: NA
40        Raises: NA
41    """
42    parser = argparse.ArgumentParser()
43    parser.add_argument("--cmd", help="wayland-scanner command to execute")
44    parser.add_argument("--src-root", help="Root source directory")
45    parser.add_argument("--root-gen-dir", help="Directory for generated files")
46    parser.add_argument("protocols", nargs="+",
47                        help="Input protocol file paths relative to src root.")
48
49    options = parser.parse_args()
50    cmd = os.path.realpath(options.cmd)
51    src_root = options.src_root
52    root_gen_dir = options.root_gen_dir
53    protocols = options.protocols
54
55    version = subprocess.check_output([cmd, '--version'], \
56            stderr=subprocess.STDOUT)
57    # The version is of the form "wayland-scanner 1.18.0\n"
58    ver = version.decode().strip().split(' ')[1].split('.')
59    version = tuple([int(x) for x in ver])
60
61    for protocol in protocols:
62        protocol_path = os.path.join(src_root, protocol)
63        protocol_without_extension = protocol.rsplit(".", 1)[0]
64        out_base_name = os.path.join(root_gen_dir, protocol_without_extension)
65        code_cmd = 'private-code' if version > (1, 14, 90) else 'code'
66        generate_code(cmd, code_cmd, protocol_path,
67                      out_base_name + "-protocol.c")
68        generate_code(cmd, "client-header", protocol_path,
69                      out_base_name + "-client-protocol.h")
70        generate_code(cmd, "server-header", protocol_path,
71                      out_base_name + "-server-protocol.h")
72
73if __name__ == "__main__":
74    try:
75        main()
76    except RuntimeError as error:
77        print(error, file=sys.stderr)
78        sys.exit(1)
79