-
-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathcxx.py
More file actions
186 lines (155 loc) · 6.92 KB
/
Copy pathcxx.py
File metadata and controls
186 lines (155 loc) · 6.92 KB
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
# MIT License
#
# Copyright The SCons Foundation
#
# 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.
"""Tool-specific initialization for generic Posix C++ compilers.
There normally shouldn't be any need to import this module directly.
It will usually be imported through the generic SCons.Tool.Tool()
selection method.
"""
import os.path
import re
import SCons.Defaults
import SCons.Util
import SCons.Scanner
compilers = ['CC', 'c++']
CXXSuffixes = ['.cpp', '.cc', '.cxx', '.c++', '.C++', '.mm']
if SCons.Util.case_sensitive_suffixes('.c', '.C'):
CXXSuffixes.append('.C')
def iscplusplus(source) -> bool:
if not source:
# Source might be None for unusual cases like SConf.
return False
for s in source:
if s.sources:
ext = os.path.splitext(str(s.sources[0]))[1]
if ext in CXXSuffixes:
return True
return False
def gen_module_map_file(root, module_map):
module_map_text = '$$root ' + str(root) + '\n'
for module, file in module_map.items():
module_map_text += str(module) + ' ' + str(file) + '\n'
return module_map_text
#TODO filter out C++ whitespace and comments
module_decl_re = re.compile("(module;)?(.|\n)*export module (.*);")
def module_emitter(target, source, env):
if("CXXMODULEPATH" in env):
env["__CXXMODULEINIT__"](env)
export = module_decl_re.match(source[0].get_text_contents())
if export:
modulename = export[3].strip()
if modulename not in env["CXXMODULEMAP"]:
env["CXXMODULEMAP"][modulename] = modulename + env["CXXMODULESUFFIX"]
target.append(env.File("$CXXMODULEPATH/" + env["CXXMODULEMAP"][modulename]))
return (target, source, env)
def module_emitter_static(target, source, env):
import SCons.Defaults
return SCons.Defaults.StaticObjectEmitter(*module_emitter(target, source, env))
def module_emitter_shared(target, source, env):
import SCons.Defaults
return SCons.Defaults.SharedObjectEmitter(*module_emitter(target, source, env))
#TODO filter out C++ whitespace and comments
module_import_re = re.compile(r"\s*(?:(?:export)?\s*import\s*(\S*)\s*;)|(?:(export)?\s*module\s*(\S*)\s*;)")
class CxxModuleScanner(SCons.Scanner.Current):
def scan(self, node, env, path):
result = self.c_scanner(node, env, path)
if not env.get("CXXMODULEPATH"):
return result
imports = module_import_re.findall(node.get_text_contents())
for module, export, impl in imports:
if not module:
if not export and impl: # module implementation unit depends on module interface
module = impl
else:
continue
is_header_unit = False
if(module[0] == "<" or module[0] == '"'):
module_id_prefix = "@system-header/" if module[0] == "<" else "@header/"
cmi = module_id_prefix + module[1:-1] + "$CXXMODULESUFFIX"
is_header_unit = True
else:
cmi = env["CXXMODULEMAP"].get(
module, module+"$CXXMODULESUFFIX")
cmi = env.File("$CXXMODULEPATH/" + cmi)
if(is_header_unit and not cmi.has_builder()):
source = self.c_scanner.find_include(
(module[0], module[1:-1]), node.dir, path)
if source[0]:
source = source[0]
else:
source = env.Value(module[1:-1])
env.CxxHeaderUnit(
cmi, source,
CXXCOMOUTPUTSPEC="$CXXSYSTEMHEADERFLAGS" if module[0] == "<" else "$CXXUSERHEADERFLAGS",
CPPPATH = [node.dir, "$CPPPATH"] if module[0] == '"' else "$CPPPATH",
CXXMODULEIDPREFIX=module_id_prefix
)
result.append(cmi)
return result
def __init__(self, *args, **kwargs):
from SCons.Scanner import FindPathDirs
super().__init__(self.scan, recursive = True, path_function = FindPathDirs("CPPPATH"), *args, **kwargs)
from SCons.Tool import CScanner
self.c_scanner = CScanner
header_unit = SCons.Builder.Builder(action="$CXXCOM",
source_scanner=CxxModuleScanner())
def generate(env) -> None:
"""
Add Builders and construction variables for Visual Age C++ compilers
to an Environment.
"""
import SCons.Tool
import SCons.Tool.cc
static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
from SCons.Tool import SourceFileScanner
for suffix in CXXSuffixes:
static_obj.add_action(suffix, SCons.Defaults.CXXAction)
shared_obj.add_action(suffix, SCons.Defaults.ShCXXAction)
static_obj.add_emitter(suffix, module_emitter_static)
shared_obj.add_emitter(suffix, module_emitter_shared)
SourceFileScanner.add_scanner(suffix, CxxModuleScanner())
env['BUILDERS']['CxxHeaderUnit'] = header_unit
SCons.Tool.cc.add_common_cc_variables(env)
if 'CXX' not in env:
env['CXX'] = env.Detect(compilers) or compilers[0]
env['CXXFLAGS'] = SCons.Util.CLVar('')
env['CXXCOMOUTPUTSPEC'] = '-o $TARGET'
env['CXXCOM'] = '$CXX $CXXCOMOUTPUTSPEC -c ${ CXXMODULEFLAGS if CXXMODULEPATH else "" } $CXXFLAGS $CCFLAGS $_CCCOMCOM $SOURCES'
env['CXXMODULEMAP'] = {}
env['SHCXX'] = '$CXX'
env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
env['SHCXXCOM'] = '$SHCXX -o $TARGET -c ${ CXXMODULEFLAGS if CXXMODULEPATH else "" } $SHCXXFLAGS $SHCCFLAGS $_CCCOMCOM $SOURCES'
env['CPPDEFPREFIX'] = '-D'
env['CPPDEFSUFFIX'] = ''
env['INCPREFIX'] = '-I'
env['INCSUFFIX'] = ''
env['SHOBJSUFFIX'] = '.os'
env['OBJSUFFIX'] = '.o'
env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 0
env['CXXFILESUFFIX'] = '.cc'
def exists(env):
return env.Detect(env.get('CXX', compilers))
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: