• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2016 Ben Noordhuis <info@bnoordhuis.nl>. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import gyp.common
6import gyp.xcode_emulation
7import json
8import os
9
10generator_additional_non_configuration_keys = []
11generator_additional_path_sections = []
12generator_extra_sources_for_rules = []
13generator_filelist_paths = None
14generator_supports_multiple_toolsets = True
15generator_wants_sorted_dependencies = False
16
17# Lifted from make.py.  The actual values don't matter much.
18generator_default_variables = {
19    "CONFIGURATION_NAME": "$(BUILDTYPE)",
20    "EXECUTABLE_PREFIX": "",
21    "EXECUTABLE_SUFFIX": "",
22    "INTERMEDIATE_DIR": "$(obj).$(TOOLSET)/$(TARGET)/geni",
23    "PRODUCT_DIR": "$(builddir)",
24    "RULE_INPUT_DIRNAME": "%(INPUT_DIRNAME)s",
25    "RULE_INPUT_EXT": "$(suffix $<)",
26    "RULE_INPUT_NAME": "$(notdir $<)",
27    "RULE_INPUT_PATH": "$(abspath $<)",
28    "RULE_INPUT_ROOT": "%(INPUT_ROOT)s",
29    "SHARED_INTERMEDIATE_DIR": "$(obj)/gen",
30    "SHARED_LIB_PREFIX": "lib",
31    "STATIC_LIB_PREFIX": "lib",
32    "STATIC_LIB_SUFFIX": ".a",
33}
34
35
36def IsMac(params):
37    return "mac" == gyp.common.GetFlavor(params)
38
39
40def CalculateVariables(default_variables, params):
41    default_variables.setdefault("OS", gyp.common.GetFlavor(params))
42
43
44def AddCommandsForTarget(cwd, target, params, per_config_commands):
45    output_dir = params["generator_flags"].get("output_dir", "out")
46    for configuration_name, configuration in target["configurations"].items():
47        if IsMac(params):
48            xcode_settings = gyp.xcode_emulation.XcodeSettings(target)
49            cflags = xcode_settings.GetCflags(configuration_name)
50            cflags_c = xcode_settings.GetCflagsC(configuration_name)
51            cflags_cc = xcode_settings.GetCflagsCC(configuration_name)
52        else:
53            cflags = configuration.get("cflags", [])
54            cflags_c = configuration.get("cflags_c", [])
55            cflags_cc = configuration.get("cflags_cc", [])
56
57        cflags_c = cflags + cflags_c
58        cflags_cc = cflags + cflags_cc
59
60        defines = configuration.get("defines", [])
61        defines = ["-D" + s for s in defines]
62
63        # TODO(bnoordhuis) Handle generated source files.
64        extensions = (".c", ".cc", ".cpp", ".cxx")
65        sources = [s for s in target.get("sources", []) if s.endswith(extensions)]
66
67        def resolve(filename):
68            return os.path.abspath(os.path.join(cwd, filename))
69
70        # TODO(bnoordhuis) Handle generated header files.
71        include_dirs = configuration.get("include_dirs", [])
72        include_dirs = [s for s in include_dirs if not s.startswith("$(obj)")]
73        includes = ["-I" + resolve(s) for s in include_dirs]
74
75        defines = gyp.common.EncodePOSIXShellList(defines)
76        includes = gyp.common.EncodePOSIXShellList(includes)
77        cflags_c = gyp.common.EncodePOSIXShellList(cflags_c)
78        cflags_cc = gyp.common.EncodePOSIXShellList(cflags_cc)
79
80        commands = per_config_commands.setdefault(configuration_name, [])
81        for source in sources:
82            file = resolve(source)
83            isc = source.endswith(".c")
84            cc = "cc" if isc else "c++"
85            cflags = cflags_c if isc else cflags_cc
86            command = " ".join(
87                (
88                    cc,
89                    defines,
90                    includes,
91                    cflags,
92                    "-c",
93                    gyp.common.EncodePOSIXShellArgument(file),
94                )
95            )
96            commands.append(dict(command=command, directory=output_dir, file=file))
97
98
99def GenerateOutput(target_list, target_dicts, data, params):
100    per_config_commands = {}
101    for qualified_target, target in target_dicts.items():
102        build_file, target_name, toolset = gyp.common.ParseQualifiedTarget(
103            qualified_target
104        )
105        if IsMac(params):
106            settings = data[build_file]
107            gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(settings, target)
108        cwd = os.path.dirname(build_file)
109        AddCommandsForTarget(cwd, target, params, per_config_commands)
110
111    output_dir = params["generator_flags"].get("output_dir", "out")
112    for configuration_name, commands in per_config_commands.items():
113        filename = os.path.join(output_dir, configuration_name, "compile_commands.json")
114        gyp.common.EnsureDirExists(filename)
115        fp = open(filename, "w")
116        json.dump(commands, fp=fp, indent=0, check_circular=False)
117
118
119def PerformBuild(data, configurations, params):
120    pass
121