Skip to content

Commit e6eac85

Browse files
bnoordhuisrefack
authored andcommitted
tools: add compile_commands.json gyp generator
this is a re-base of the gyp part of 3c46bb9 after bumping GYP version to https://chromium.googlesource.com/external/gyp/+/eb296f67da078ec01f5e3a9ea9cdc6d26d680161 Original-Review-By: James M Snell <[email protected]> Ref: #7986 PR-URL: #12450 Reviewed-By: João Reis <[email protected]>
1 parent d7a40a8 commit e6eac85

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright (c) 2016 Ben Noordhuis <[email protected]>. 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+
5+
import gyp.common
6+
import gyp.xcode_emulation
7+
import json
8+
import os
9+
10+
generator_additional_non_configuration_keys = []
11+
generator_additional_path_sections = []
12+
generator_extra_sources_for_rules = []
13+
generator_filelist_paths = None
14+
generator_supports_multiple_toolsets = True
15+
generator_wants_sorted_dependencies = False
16+
17+
# Lifted from make.py. The actual values don't matter much.
18+
generator_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+
36+
def IsMac(params):
37+
return 'mac' == gyp.common.GetFlavor(params)
38+
39+
40+
def CalculateVariables(default_variables, params):
41+
default_variables.setdefault('OS', gyp.common.GetFlavor(params))
42+
43+
44+
def AddCommandsForTarget(cwd, target, params, per_config_commands):
45+
output_dir = params['generator_flags']['output_dir']
46+
for configuration_name, configuration in target['configurations'].iteritems():
47+
builddir_name = os.path.join(output_dir, configuration_name)
48+
49+
if IsMac(params):
50+
xcode_settings = gyp.xcode_emulation.XcodeSettings(target)
51+
cflags = xcode_settings.GetCflags(configuration_name)
52+
cflags_c = xcode_settings.GetCflagsC(configuration_name)
53+
cflags_cc = xcode_settings.GetCflagsCC(configuration_name)
54+
else:
55+
cflags = configuration.get('cflags', [])
56+
cflags_c = configuration.get('cflags_c', [])
57+
cflags_cc = configuration.get('cflags_cc', [])
58+
59+
cflags_c = cflags + cflags_c
60+
cflags_cc = cflags + cflags_cc
61+
62+
defines = configuration.get('defines', [])
63+
defines = ['-D' + s for s in defines]
64+
65+
# TODO(bnoordhuis) Handle generated source files.
66+
sources = target.get('sources', [])
67+
sources = [s for s in sources if s.endswith('.c') or s.endswith('.cc')]
68+
69+
def resolve(filename):
70+
return os.path.abspath(os.path.join(cwd, filename))
71+
72+
# TODO(bnoordhuis) Handle generated header files.
73+
include_dirs = configuration.get('include_dirs', [])
74+
include_dirs = [s for s in include_dirs if not s.startswith('$(obj)')]
75+
includes = ['-I' + resolve(s) for s in include_dirs]
76+
77+
defines = gyp.common.EncodePOSIXShellList(defines)
78+
includes = gyp.common.EncodePOSIXShellList(includes)
79+
cflags_c = gyp.common.EncodePOSIXShellList(cflags_c)
80+
cflags_cc = gyp.common.EncodePOSIXShellList(cflags_cc)
81+
82+
commands = per_config_commands.setdefault(configuration_name, [])
83+
for source in sources:
84+
file = resolve(source)
85+
isc = source.endswith('.c')
86+
cc = 'cc' if isc else 'c++'
87+
cflags = cflags_c if isc else cflags_cc
88+
command = ' '.join((cc, defines, includes, cflags,
89+
'-c', gyp.common.EncodePOSIXShellArgument(file)))
90+
commands.append(dict(command=command, directory=output_dir, file=file))
91+
92+
93+
def GenerateOutput(target_list, target_dicts, data, params):
94+
per_config_commands = {}
95+
for qualified_target, target in target_dicts.iteritems():
96+
build_file, target_name, toolset = (
97+
gyp.common.ParseQualifiedTarget(qualified_target))
98+
if IsMac(params):
99+
settings = data[build_file]
100+
gyp.xcode_emulation.MergeGlobalXcodeSettingsToSpec(settings, target)
101+
cwd = os.path.dirname(build_file)
102+
AddCommandsForTarget(cwd, target, params, per_config_commands)
103+
104+
output_dir = params['generator_flags']['output_dir']
105+
for configuration_name, commands in per_config_commands.iteritems():
106+
filename = os.path.join(output_dir,
107+
configuration_name,
108+
'compile_commands.json')
109+
gyp.common.EnsureDirExists(filename)
110+
fp = open(filename, 'w')
111+
json.dump(commands, fp=fp, indent=0, check_circular=False)
112+
113+
114+
def PerformBuild(data, configurations, params):
115+
pass

0 commit comments

Comments
 (0)