summaryrefslogtreecommitdiff
path: root/site_scons/site_tools/idl_tool.py
blob: 50687a3d6ca10d5bea58c3820357e8677f96be3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2020 MongoDB Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

"""IDL Compiler Scons Tool."""

import os.path
import subprocess
import sys

import SCons

# We lazily import this at generate time.
idlc = None

IDL_GLOBAL_DEPS = []


def idlc_emitter(target, source, env):
    """For each input IDL file, the tool produces a .cpp and .h file."""
    first_source = str(source[0])

    if not first_source.endswith(".idl"):
        raise ValueError(
            "Bad idl file name '%s', it must end with '.idl' " % (first_source)
        )

    base_file_name, _ = SCons.Util.splitext(str(target[0]))
    target_source = env.File(base_file_name + "_gen.cpp")
    target_header = env.File(base_file_name + "_gen.h")

    env.Alias("generated-sources", [target_source, target_header])

    return [target_source, target_header], source


IDLCAction = SCons.Action.Action("$IDLCCOM", "$IDLCCOMSTR")


def idl_scanner(node, env, path):

    # When generating ninja we only need to add the IDL_GLOBAL_DEPS
    # because the implicit dependencies will be picked up using the
    # deps=msvc method.
    if env.get("GENERATING_NINJA", False):
        return IDL_GLOBAL_DEPS

    nodes_deps_list = getattr(node.attributes, "IDL_NODE_DEPS", None)
    if nodes_deps_list is not None:
        return nodes_deps_list

    nodes_deps_list = IDL_GLOBAL_DEPS[:]

    with open(str(node), encoding="utf-8") as file_stream:
        parsed_doc = idlc.parser.parse(
            file_stream, str(node), idlc.CompilerImportResolver(["src"])
        )

    if not parsed_doc.errors and parsed_doc.spec.imports is not None:
        nodes_deps_list.extend(
            [env.File(d) for d in sorted(parsed_doc.spec.imports.dependencies)]
        )

    setattr(node.attributes, "IDL_NODE_DEPS", nodes_deps_list)
    return nodes_deps_list


idl_scanner = SCons.Scanner.Scanner(function=idl_scanner, skeys=[".idl"])

# TODO: create a scanner for imports when imports are implemented
IDLCBuilder = SCons.Builder.Builder(
    action=IDLCAction,
    emitter=idlc_emitter,
    src_suffix=".idl",
    suffix=".cpp",
    source_scanner=idl_scanner,
)


def generate(env):
    bld = IDLCBuilder

    env.Append(SCANNERS=idl_scanner)

    env["BUILDERS"]["Idlc"] = bld

    sys.path.append(env.Dir("#buildscripts").get_abspath())
    import buildscripts.idl.idl.compiler as idlc_mod

    global idlc
    idlc = idlc_mod

    env["IDLC"] = "$PYTHON buildscripts/idl/idlc.py"
    base_dir = env.Dir("$BUILD_DIR").path
    env["IDLCFLAGS"] = [
        "--include", "src",
        "--base_dir", base_dir,
        "--target_arch", "$TARGET_ARCH",
    ]
    env["IDLCCOM"] = "$IDLC $IDLCFLAGS --header ${TARGETS[1]} --output ${TARGETS[0]} $SOURCES"
    env["IDLCCOMSTR"] = ("Generating ${TARGETS[0]}"
        if not env.get("VERBOSE", "").lower() in ['true', '1']
        else None)
    env["IDLCSUFFIX"] = ".idl"

    global IDL_GLOBAL_DEPS
    IDL_GLOBAL_DEPS = env.Glob("#buildscripts/idl/*.py") + env.Glob(
        "#buildscripts/idl/idl/*.py"
    )
    env["IDL_HAS_INLINE_DEPENDENCIES"] = True


def exists(env):
    return True