FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hatch-cpp/hatch_cpp/toolchains/common.py at main · python-project-templates/hatch-cpp · GitHub
python-project-templates
hatch-cpp
Repository navigation
Code
Issues
4
(4)
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
hatch-cpp
/
hatch_cpp
/
toolchains
/
common.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
414 lines (364 loc) · 19.8 KB
Breadcrumbs
hatch-cpp
/
hatch_cpp
/
toolchains
/
common.py
Copy path
File metadata and controls
414 lines (364 loc) · 19.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from
__future__
import
annotations
from
os
import
environ
from
pathlib
import
Path
from
re
import
match
from
shutil
import
which
from
sys
import
base_exec_prefix
,
exec_prefix
,
executable
,
platform
as
sys_platform
,
version_info
from
sysconfig
import
get_config_var
,
get_path
from
typing
import
Any
,
Literal
from
pydantic
import
AliasChoices
,
BaseModel
,
Field
,
field_validator
,
model_validator
__all__
=
(
"Binding"
,
"BuildType"
,
"CompilerToolchain"
,
"HatchCppLibrary"
,
"HatchCppPlatform"
,
"Language"
,
"Platform"
,
"PlatformDefaults"
,
"Toolchain"
,
"_normalize_rpath"
,
)
BuildType
=
Literal
[
"debug"
,
"release"
]
CompilerToolchain
=
Literal
[
"gcc"
,
"clang"
,
"msvc"
]
Toolchain
=
Literal
[
"vcpkg"
,
"cmake"
,
"vanilla"
]
Language
=
Literal
[
"c"
,
"c++"
]
Binding
=
Literal
[
"cpython"
,
"pybind11"
,
"nanobind"
,
"generic"
]
Platform
=
Literal
[
"linux"
,
"darwin"
,
"win32"
,
"emscripten"
]
PlatformDefaults
=
{
"linux"
: {
"CC"
:
"gcc"
,
"CXX"
:
"g++"
,
"LD"
:
"ld"
},
"darwin"
: {
"CC"
:
"clang"
,
"CXX"
:
"clang++"
,
"LD"
:
"ld"
},
"win32"
: {
"CC"
:
"cl"
,
"CXX"
:
"cl"
,
"LD"
:
"link"
},
"emscripten"
: {
"CC"
:
"emcc"
,
"CXX"
:
"em++"
,
"LD"
:
"wasm-ld"
},
}
class
HatchCppLibrary
(
BaseModel
,
validate_assignment
=
True
):
"""A C++ library."""
name
:
str
sources
:
list
[
str
]
language
:
Language
=
"c++"
binding
:
Binding
=
"cpython"
std
:
str
|
None
=
None
include_dirs
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"include_dirs"
,
"include-dirs"
))
include_dirs_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"include_dirs_linux"
,
"include-dirs-linux"
))
include_dirs_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"include_dirs_darwin"
,
"include-dirs-darwin"
))
include_dirs_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"include_dirs_win32"
,
"include-dirs-win32"
))
library_dirs
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"library_dirs"
,
"library-dirs"
))
library_dirs_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"library_dirs_linux"
,
"library-dirs-linux"
))
library_dirs_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"library_dirs_darwin"
,
"library-dirs-darwin"
))
library_dirs_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"library_dirs_win32"
,
"library-dirs-win32"
))
libraries
:
list
[
str
]
=
Field
(
default_factory
=
list
)
libraries_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"libraries_linux"
,
"libraries-linux"
))
libraries_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"libraries_darwin"
,
"libraries-darwin"
))
libraries_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"libraries_win32"
,
"libraries-win32"
))
extra_compile_args
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_compile_args"
,
"extra-compile-args"
))
extra_compile_args_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_compile_args_linux"
,
"extra-compile-args-linux"
))
extra_compile_args_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_compile_args_darwin"
,
"extra-compile-args-darwin"
))
extra_compile_args_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_compile_args_win32"
,
"extra-compile-args-win32"
))
extra_link_args
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_link_args"
,
"extra-link-args"
))
extra_link_args_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_link_args_linux"
,
"extra-link-args-linux"
))
extra_link_args_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_link_args_darwin"
,
"extra-link-args-darwin"
))
extra_link_args_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_link_args_win32"
,
"extra-link-args-win32"
))
extra_objects
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_objects"
,
"extra-objects"
))
extra_objects_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_objects_linux"
,
"extra-objects-linux"
))
extra_objects_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_objects_darwin"
,
"extra-objects-darwin"
))
extra_objects_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"extra_objects_win32"
,
"extra-objects-win32"
))
define_macros
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"define_macros"
,
"define-macros"
))
define_macros_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"define_macros_linux"
,
"define-macros-linux"
))
define_macros_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"define_macros_darwin"
,
"define-macros-darwin"
))
define_macros_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"define_macros_win32"
,
"define-macros-win32"
))
undef_macros
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"undef_macros"
,
"undef-macros"
))
undef_macros_linux
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"undef_macros_linux"
,
"undef-macros-linux"
))
undef_macros_darwin
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"undef_macros_darwin"
,
"undef-macros-darwin"
))
undef_macros_win32
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"undef_macros_win32"
,
"undef-macros-win32"
))
export_symbols
:
list
[
str
]
=
Field
(
default_factory
=
list
,
alias
=
AliasChoices
(
"export_symbols"
,
"export-symbols"
))
depends
:
list
[
str
]
=
Field
(
default_factory
=
list
)
py_limited_api
:
str
|
None
=
Field
(
default
=
""
,
alias
=
AliasChoices
(
"py_limited_api"
,
"py-limited-api"
))
@
field_validator
(
"py_limited_api"
,
mode
=
"before"
)
@
classmethod
def
check_py_limited_api
(
cls
,
value
:
Any
)
->
Any
:
if
value
and
not
match
(
r"cp3\d"
,
value
):
raise
ValueError
(
"py-limited-api must be in the form of cp3X"
)
return
value
def
get_qualified_name
(
self
,
platform
):
if
platform
==
"emscripten"
:
if
self
.
binding
==
"generic"
:
return
f"
{
self
.
name
}
.wasm"
return
f"
{
self
.
name
}
.cpython-
{
version_info
.
major
}
{
version_info
.
minor
}
-wasm32-emscripten.so"
if
self
.
binding
==
"cpython"
and
not
self
.
py_limited_api
:
suffix
=
get_config_var
(
"EXT_SUFFIX"
)
return
f"
{
self
.
name
}
{
suffix
}
"
if
platform
==
"win32"
:
suffix
=
"dll"
if
self
.
binding
==
"generic"
else
"pyd"
elif
platform
==
"darwin"
:
suffix
=
"dylib"
if
self
.
binding
==
"generic"
else
"so"
else
:
suffix
=
"so"
if
self
.
py_limited_api
and
platform
!=
"win32"
:
return
f"
{
self
.
name
}
.abi3.
{
suffix
}
"
return
f"
{
self
.
name
}
.
{
suffix
}
"
@
model_validator
(
mode
=
"after"
)
def
check_binding_and_py_limited_api
(
self
):
if
self
.
binding
==
"pybind11"
and
self
.
py_limited_api
:
raise
ValueError
(
"pybind11 does not support Py_LIMITED_API"
)
if
self
.
binding
==
"generic"
and
self
.
py_limited_api
:
raise
ValueError
(
"Generic binding can not support Py_LIMITED_API"
)
return
self
def
get_effective_link_args
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get link args merged with platform-specific link args."""
args
=
list
(
self
.
extra_link_args
)
if
platform
==
"linux"
:
args
.
extend
(
self
.
extra_link_args_linux
)
elif
platform
==
"darwin"
:
args
.
extend
(
self
.
extra_link_args_darwin
)
elif
platform
==
"win32"
:
args
.
extend
(
self
.
extra_link_args_win32
)
return
args
def
get_effective_include_dirs
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get include dirs merged with platform-specific include dirs."""
dirs
=
list
(
self
.
include_dirs
)
if
platform
==
"linux"
:
dirs
.
extend
(
self
.
include_dirs_linux
)
elif
platform
==
"darwin"
:
dirs
.
extend
(
self
.
include_dirs_darwin
)
elif
platform
==
"win32"
:
dirs
.
extend
(
self
.
include_dirs_win32
)
return
dirs
def
get_effective_library_dirs
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get library dirs merged with platform-specific library dirs."""
dirs
=
list
(
self
.
library_dirs
)
if
platform
==
"linux"
:
dirs
.
extend
(
self
.
library_dirs_linux
)
elif
platform
==
"darwin"
:
dirs
.
extend
(
self
.
library_dirs_darwin
)
elif
platform
==
"win32"
:
dirs
.
extend
(
self
.
library_dirs_win32
)
return
dirs
def
get_effective_libraries
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get libraries merged with platform-specific libraries."""
libs
=
list
(
self
.
libraries
)
if
platform
==
"linux"
:
libs
.
extend
(
self
.
libraries_linux
)
elif
platform
==
"darwin"
:
libs
.
extend
(
self
.
libraries_darwin
)
elif
platform
==
"win32"
:
libs
.
extend
(
self
.
libraries_win32
)
return
libs
def
get_effective_compile_args
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get compile args merged with platform-specific compile args."""
args
=
list
(
self
.
extra_compile_args
)
if
platform
==
"linux"
:
args
.
extend
(
self
.
extra_compile_args_linux
)
elif
platform
==
"darwin"
:
args
.
extend
(
self
.
extra_compile_args_darwin
)
elif
platform
==
"win32"
:
args
.
extend
(
self
.
extra_compile_args_win32
)
return
args
def
get_effective_extra_objects
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get extra objects merged with platform-specific extra objects."""
objs
=
list
(
self
.
extra_objects
)
if
platform
==
"linux"
:
objs
.
extend
(
self
.
extra_objects_linux
)
elif
platform
==
"darwin"
:
objs
.
extend
(
self
.
extra_objects_darwin
)
elif
platform
==
"win32"
:
objs
.
extend
(
self
.
extra_objects_win32
)
return
objs
def
get_effective_define_macros
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get define macros merged with platform-specific define macros."""
macros
=
list
(
self
.
define_macros
)
if
platform
==
"linux"
:
macros
.
extend
(
self
.
define_macros_linux
)
elif
platform
==
"darwin"
:
macros
.
extend
(
self
.
define_macros_darwin
)
elif
platform
==
"win32"
:
macros
.
extend
(
self
.
define_macros_win32
)
return
macros
def
get_effective_undef_macros
(
self
,
platform
:
Platform
)
->
list
[
str
]:
"""Get undef macros merged with platform-specific undef macros."""
macros
=
list
(
self
.
undef_macros
)
if
platform
==
"linux"
:
macros
.
extend
(
self
.
undef_macros_linux
)
elif
platform
==
"darwin"
:
macros
.
extend
(
self
.
undef_macros_darwin
)
elif
platform
==
"win32"
:
macros
.
extend
(
self
.
undef_macros_win32
)
return
macros
def
_normalize_rpath
(
value
:
str
,
platform
:
Platform
)
->
str
:
r"""Translate and escape rpath values for the target platform.
- On macOS (darwin): ``$ORIGIN`` is replaced with ``@loader_path``.
- On Linux: ``@loader_path`` is replaced with ``$ORIGIN``, and
``$ORIGIN`` is escaped as ``\$ORIGIN`` so that ``os.system()``
(which invokes a shell) passes it through literally.
- On Windows: no transformation is applied (Windows does not use
rpath).
"""
if
platform
==
"darwin"
:
# Handle already-escaped \$ORIGIN first, then plain $ORIGIN
value
=
value
.
replace
(
r"\$ORIGIN"
,
"@loader_path"
)
value
=
value
.
replace
(
"$ORIGIN"
,
"@loader_path"
)
elif
platform
==
"linux"
:
# Translate macOS rpath to Linux equivalent
value
=
value
.
replace
(
"@loader_path"
,
"$ORIGIN"
)
# Escape $ORIGIN for shell safety (os.system runs through bash)
value
=
value
.
replace
(
"$ORIGIN"
,
r"\$ORIGIN"
)
return
value
class
HatchCppPlatform
(
BaseModel
):
cc
:
str
cxx
:
str
ld
:
str
platform
:
Platform
toolchain
:
CompilerToolchain
disable_ccache
:
bool
=
False
@
staticmethod
def
default
()
->
HatchCppPlatform
:
platform
=
"emscripten"
if
environ
.
get
(
"PYODIDE_ABI_VERSION"
)
else
sys_platform
CC
=
environ
.
get
(
"CC"
,
PlatformDefaults
[
platform
][
"CC"
])
CXX
=
environ
.
get
(
"CXX"
,
PlatformDefaults
[
platform
][
"CXX"
])
LD
=
environ
.
get
(
"LD"
,
PlatformDefaults
[
platform
][
"LD"
])
if
"gcc"
in
CC
and
"g++"
in
CXX
:
toolchain
=
"gcc"
elif
"clang"
in
CC
and
"clang++"
in
CXX
:
toolchain
=
"clang"
elif
"cl"
in
CC
and
"cl"
in
CXX
:
toolchain
=
"msvc"
# Fallback to platform defaults
elif
platform
==
"linux"
:
toolchain
=
"gcc"
elif
platform
in
(
"darwin"
,
"emscripten"
):
toolchain
=
"clang"
elif
platform
==
"win32"
:
toolchain
=
"msvc"
else
:
toolchain
=
"gcc"
# TODO:
# https://github.com/rui314/mold/issues/647
# if which("ld.mold"):
# LD = which("ld.mold")
# elif which("ld.lld"):
# LD = which("ld.lld")
return
HatchCppPlatform
(
cc
=
CC
,
cxx
=
CXX
,
ld
=
LD
,
platform
=
platform
,
toolchain
=
toolchain
)
@
model_validator
(
mode
=
"wrap"
)
@
classmethod
def
validate_model
(
cls
,
data
,
handler
):
model
=
handler
(
data
)
if
which
(
"ccache"
)
and
model
.
platform
!=
"emscripten"
and
not
model
.
disable_ccache
and
model
.
toolchain
in
[
"gcc"
,
"clang"
]:
if
not
model
.
cc
.
startswith
(
"ccache "
):
model
.
cc
=
f"ccache
{
model
.
cc
}
"
if
not
model
.
cxx
.
startswith
(
"ccache "
):
model
.
cxx
=
f"ccache
{
model
.
cxx
}
"
return
model
@
staticmethod
def
platform_for_toolchain
(
toolchain
:
CompilerToolchain
)
->
HatchCppPlatform
:
platform
=
HatchCppPlatform
.
default
()
platform
.
toolchain
=
toolchain
return
platform
def
get_compile_flags
(
self
,
library
:
HatchCppLibrary
,
build_type
:
BuildType
=
"release"
)
->
str
:
flags
=
""
# Get effective platform-specific values
effective_include_dirs
=
library
.
get_effective_include_dirs
(
self
.
platform
)
effective_compile_args
=
library
.
get_effective_compile_args
(
self
.
platform
)
effective_define_macros
=
library
.
get_effective_define_macros
(
self
.
platform
)
effective_undef_macros
=
library
.
get_effective_undef_macros
(
self
.
platform
)
effective_extra_objects
=
library
.
get_effective_extra_objects
(
self
.
platform
)
effective_link_args
=
library
.
get_effective_link_args
(
self
.
platform
)
# Python.h
if
library
.
binding
!=
"generic"
:
effective_include_dirs
.
append
(
get_path
(
"include"
))
if
library
.
binding
==
"pybind11"
:
import
pybind11
effective_include_dirs
.
append
(
pybind11
.
get_include
())
if
not
library
.
std
:
library
.
std
=
"c++11"
elif
library
.
binding
==
"nanobind"
:
import
nanobind
effective_include_dirs
.
append
(
nanobind
.
include_dir
())
if
not
library
.
std
:
library
.
std
=
"c++17"
library
.
sources
.
append
(
str
(
Path
(
nanobind
.
include_dir
()).
parent
/
"src"
/
"nb_combined.cpp"
))
effective_include_dirs
.
append
(
str
(
Path
(
nanobind
.
include_dir
()).
parent
/
"ext"
/
"robin_map"
/
"include"
))
if
library
.
py_limited_api
:
if
library
.
binding
==
"pybind11"
:
raise
ValueError
(
"pybind11 does not support Py_LIMITED_API"
)
effective_define_macros
.
append
(
f"Py_LIMITED_API=0x0
{
library
.
py_limited_api
[
2
]
}
0
{
hex
(
int
(
library
.
py_limited_api
[
3
:]))[
2
:]
}
00f0"
)
# Toolchain-specific flags
if
self
.
toolchain
==
"gcc"
:
flags
+=
" "
+
" "
.
join
(
f"-I
{
d
}
"
for
d
in
effective_include_dirs
)
if
self
.
platform
!=
"emscripten"
:
flags
+=
" -fPIC"
flags
+=
" "
+
" "
.
join
(
effective_compile_args
)
flags
+=
" "
+
" "
.
join
(
f"-D
{
macro
}
"
for
macro
in
effective_define_macros
)
flags
+=
" "
+
" "
.
join
(
f"-U
{
macro
}
"
for
macro
in
effective_undef_macros
)
if
library
.
std
:
flags
+=
f" -std=
{
library
.
std
}
"
elif
self
.
toolchain
==
"clang"
:
flags
+=
" "
.
join
(
f"-I
{
d
}
"
for
d
in
effective_include_dirs
)
if
self
.
platform
!=
"emscripten"
:
flags
+=
" -fPIC"
flags
+=
" "
+
" "
.
join
(
effective_compile_args
)
flags
+=
" "
+
" "
.
join
(
f"-D
{
macro
}
"
for
macro
in
effective_define_macros
)
flags
+=
" "
+
" "
.
join
(
f"-U
{
macro
}
"
for
macro
in
effective_undef_macros
)
if
library
.
std
:
flags
+=
f" -std=
{
library
.
std
}
"
elif
self
.
toolchain
==
"msvc"
:
flags
+=
" "
.
join
(
f"/I
{
d
}
"
for
d
in
effective_include_dirs
)
flags
+=
" "
+
" "
.
join
(
effective_compile_args
)
flags
+=
" "
+
" "
.
join
(
effective_link_args
)
flags
+=
" "
+
" "
.
join
(
effective_extra_objects
)
flags
+=
" "
+
" "
.
join
(
f"/D
{
macro
}
"
for
macro
in
effective_define_macros
)
flags
+=
" "
+
" "
.
join
(
f"/U
{
macro
}
"
for
macro
in
effective_undef_macros
)
flags
+=
" /EHsc /DWIN32"
if
library
.
std
:
# MSVC minimum is c++14; clamp older standards
std
=
library
.
std
if
library
.
std
not
in
(
"c++11"
,
"c++0x"
)
else
"c++14"
flags
+=
f" /std:
{
std
}
"
# clean
while
flags
.
count
(
" "
):
flags
=
flags
.
replace
(
" "
,
" "
)
return
flags
def
get_link_flags
(
self
,
library
:
HatchCppLibrary
,
build_type
:
BuildType
=
"release"
)
->
str
:
flags
=
""
effective_link_args
=
library
.
get_effective_link_args
(
self
.
platform
)
effective_extra_objects
=
library
.
get_effective_extra_objects
(
self
.
platform
)
effective_libraries
=
library
.
get_effective_libraries
(
self
.
platform
)
effective_library_dirs
=
library
.
get_effective_library_dirs
(
self
.
platform
)
# Normalize rpath values ($ORIGIN <-> @loader_path) and escape for shell
effective_link_args
=
[
_normalize_rpath
(
arg
,
self
.
platform
)
for
arg
in
effective_link_args
]
if
self
.
toolchain
==
"gcc"
or
self
.
toolchain
==
"clang"
:
flags
+=
" -shared"
flags
+=
" "
+
" "
.
join
(
effective_link_args
)
flags
+=
" "
+
" "
.
join
(
effective_extra_objects
)
flags
+=
" "
+
" "
.
join
(
f"-l
{
lib
}
"
for
lib
in
effective_libraries
)
flags
+=
" "
+
" "
.
join
(
f"-L
{
lib
}
"
for
lib
in
effective_library_dirs
)
flags
+=
f" -o
{
library
.
get_qualified_name
(
self
.
platform
)
}
"
if
self
.
platform
==
"darwin"
:
flags
+=
" -undefined dynamic_lookup"
if
"mold"
in
self
.
ld
:
flags
+=
f" -fuse-ld=
{
self
.
ld
}
"
elif
"lld"
in
self
.
ld
:
flags
+=
" -fuse-ld=lld"
elif
self
.
toolchain
==
"msvc"
:
flags
+=
" "
+
" "
.
join
(
effective_link_args
)
flags
+=
" "
+
" "
.
join
(
effective_extra_objects
)
flags
+=
" /LD"
flags
+=
f" /Fe:
{
library
.
get_qualified_name
(
self
.
platform
)
}
"
flags
+=
" /link /DLL"
# Add Python libs directory - check multiple possible locations
# In virtual environments, sys.executable is in the venv, but pythonXX.lib
# lives under the base Python installation's 'libs' directory.
python_libs_paths
=
[
Path
(
executable
).
parent
/
"libs"
,
# Standard Python install
Path
(
executable
).
parent
.
parent
/
"libs"
,
# Some virtualenv layouts
Path
(
get_config_var
(
"installed_base"
)
or
""
)
/
"libs"
,
# sysconfig approach
Path
(
exec_prefix
)
/
"libs"
,
# exec_prefix approach
Path
(
base_exec_prefix
)
/
"libs"
,
# base_exec_prefix approach
]
for
libs_path
in
python_libs_paths
:
if
libs_path
.
exists
():
flags
+=
f" /LIBPATH:
{
libs_path
!s
}
"
break
flags
+=
" "
+
" "
.
join
(
f"
{
lib
}
.lib"
for
lib
in
effective_libraries
)
flags
+=
" "
+
" "
.
join
(
f"/LIBPATH:
{
lib
}
"
for
lib
in
effective_library_dirs
)
# clean
while
flags
.
count
(
" "
):
flags
=
flags
.
replace
(
" "
,
" "
)
return
flags
Back
|
FazBrowse Home
|
New Git URL