-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtags.py
197 lines (157 loc) · 7.37 KB
/
gtags.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
SCons tool for GNU global `gtags` command from: https://www.gnu.org/software/global/.
Ex. gtags installation for Ubuntu: `apt install global`
Syntax:
GTAGS('dir', [ target, ... ])
GTAGS([ target, ... ])
GTAGS([ target, ... sources, ... ])
If omitted, the output tags directory is given in $GTAGSDBPATH, default is the current source directory.
The dependencies are usually other targets from your build script. Source dependencies of such targets will
be traversed and added to the list of input files for `gtags` command.
Each source suffix will be checked against the suffix list in env['GTAGSSUFFXIES'], and only the matching
files are processed. You can disable the check by using the string '*' as the first list entry.
To guarantee all tags are visible to the `gtags` command, and listed in the resulting GTAGS files, the
`gtags` tool should be used together with `gcc-dep`, to have full dependecy information (nested include
files) generated by gcc / g++ compiler. Otherwise only the source dependencies visible to the SCons internal
C/C++ scanner will be available to `gtags` command.
"""
import sys
import os
import tempfile
import subprocess
import SCons.Script
import source_browse_base as base
""" default name for `gtags` executable command """
gtags_bin = 'gtags'
def collect_source_dependencies(target, source, env):
""" emitter function for GTAGS() builder for listing sources of any target node included in the tags file """
ext = os.path.splitext(str(env.Dir(target[0]).path)) if len(target) else ''
if ext[1] == '.b7900ba9-4778-4214-82df-bb4e13689250':
if len(source) and ext[0] == os.path.splitext(str(env.File(source[0]).path))[0]:
target = [ ]
else:
target[0] = ext[0] # remove automatically added extension
getBool = base.BindCallArguments(base.getBool, target, source, env, None)
if not len(target):
target.append(env['GTAGSDBPATH'])
target = [ env.Dir(target[0]).File(tagfile) for tagfile in env['GTAGSOUTPUTS' ] ]
if 'GTAGSCONFIGLIST' in env and 'GTAGSCONFIG' not in env:
for tgt in target:
for config in env.Split(env['GTAGSCONFIGLIST']):
if os.path.exists(config):
env.Depends(tgt, config)
keepVariantDir = getBool('GTAGSKEEPVARIANTDIR')
return base.collect_source_dependencies(keepVariantDir, target, source, env, 'GTAGSSUFFIXES')
def run_gtags(target, source, env):
""" action function invoked by the GTAGS() Builder to run `gtags` command """
temp_config = None
work_dir = None
cmd_env = env['ENV']
gtagsroot_file = None
if 'GTAGSROOT' in env:
for k in env['GTAGSROOT']:
work_dir = env['GTAGSROOT'][k]
gtagsroot_file = os.path.join(str(target[0].dir), k)
break
command = \
env.Split(env['GTAGS']) \
+ \
env.Split(env['GTAGSFLAGS']) \
+ \
env.Split(env['GTAGSSTDINFLAGS'])
if 'GTAGSCONFIG' in env:
temp_config = tempfile.NamedTemporaryFile(delete = False)
try:
if temp_config is not None:
for config_line in env['GTAGSCONFIG']:
temp_config.write(config_line + '\n')
temp_config.close()
command += env.Split(env['GTAGSCONFIGFLAG']) + [ temp_config.name ]
command += \
env.Split(env['GTAGSOUTPUTFLAG']) \
+ \
[ str(target[0].dir) if work_dir is None else os.path.abspath(str(target[0].dir)) ]
if 'GTAGSCOMSTR' in env and env['GTAGSCOMSTR'] is not None:
print(env.subst("$GTAGSCOMSTR", True, target, source))
else:
# print("Targets: " + str([ str(tgt) for tgt in target ]))
if work_dir is not None:
print(' '.join([ '(' ] + base.shell_escape([ 'cd', work_dir ]) + [ '&&' ] + base.shell_escape(command) + [ ')' ]))
else:
print(' '.join(base.shell_escape(command)))
if 'GTAGSADDENV' in env:
for k in env['GTAGSADDENV']:
cmd_env[k] = env['GTAGSADDENV'][k]
# print("Environ: " + str(cmd_env))
gtags_process = subprocess.Popen(command, stdin = subprocess.PIPE, cwd = work_dir, env = cmd_env)
# source.sort()
for file in source:
# print("Generating tags for source file " + (str(file) if work_dir is None else os.path.abspath(str(file))))
gtags_process.stdin.write((str(file) if work_dir is None else os.path.abspath(str(file))) + "\n")
gtags_process.stdin.close()
if gtags_process.wait():
sys.stderr.write("gtags command exited with code: " + str(gtags_process.returncode) + '\n')
return gtags_process.returncode
if gtagsroot_file is not None:
root_file = open(gtagsroot_file, 'w')
try:
root_file.write(work_dir + '\n')
finally:
root_file.close()
finally:
if temp_config is not None:
# os.remove(temp_config.name)
pass
def exists(env):
""" Check if `gtags` module has been loaded in the environment """
return env['GTAGS'] if 'GTAGS' in env else None
def show_tags_generation_message(target, source, env):
pass
def generate(env, **kw):
"""
Populate environment with variables for the GTAGS() builder:
$GTAGS, $GTAGSDBPATH, $GTAGSFLAGS, $GTAGSSTDFLAGS, $GTAGSOUTPUTFLAG,
$GTAGSSUFFIXES
Attach the GTAGS() builder to the environment.
"""
env.SetDefault\
(
GTAGS = gtags_bin,
GTAGSDBPATH =
lambda target, source, env, for_signature:
env.Dir('.').srcnode(),
GTAGSFLAGS = [ '--statistics' ],
GTAGSSTDINFLAGS = [ '-f', '-' ],
GTAGSCONFIGFLAG = [ '--gtagsconf' ],
GTAGSOUTPUTFLAG = [ ],
GTAGSOUTPUTS = [ 'GTAGS', 'GRTAGS', 'GPATH', 'GTAGSROOT' ],
GTAGSROOT = { 'GTAGSROOT': '/' },
GTAGSADDENV = { 'GTAGSFORCECPP': '1' },
GTAGSCONFIGLIST = [ '/usr/local/etc/gtags.conf', '/etc/gtags.conf', os.path.join(os.environ['HOME'], '.globalrc'), '/gtags.conf' ],
GTAGSKEEPVARIANTDIR = False,
GTAGSCOMSTR = None,
GTAGSCONFIG = \
[
'default:\\',
' :langmap=c\\:.c.h,yacc\\:.y,asm\\:.s.S,java\\:.java,cpp\\:.c++.cc.hh.cpp.cxx.hxx.hpp.C.H.tcc,php\\:.php.php3.phtml,cpp\\:(*):'
],
GTAGSSUFFIXES =
[
''
'.c', '.h',
'.y',
'.s', '.S',
'.java',
'.c++', '.cc', '.hh', '.cpp', '.cxx', '.hxx', '.hpp', '.C', '.H', '.tcc', '',
'.php', '.php3', '.phtml'
]
)
env['BUILDERS']['GTAGS'] = env.Builder\
(
emitter = collect_source_dependencies,
action = SCons.Script.Action(run_gtags, show_tags_generation_message),
multi = True,
name = 'GTAGS',
suffix = 'b7900ba9-4778-4214-82df-bb4e13689250',
# source_scanner = SCons.Script.CScan
)