FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeql/unified/swift-syntax-rs/build.rs at codeql-cli-2.27.1 · github/codeql · GitHub
github
/
codeql
Public
Notifications
You must be signed in to change notification settings
Fork
2.1k
Star
10.1k
Code
Issues
1k
Pull requests
470
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codeql
/
unified
/
swift-syntax-rs
/
build.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
106 lines (97 loc) · 4.58 KB
Breadcrumbs
codeql
/
unified
/
swift-syntax-rs
/
build.rs
Copy path
File metadata and controls
106 lines (97 loc) · 4.58 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
use
std
::
env
;
use
std
::
path
::
PathBuf
;
use
std
::
process
::
Command
;
fn
main
(
)
{
let
manifest_dir =
PathBuf
::
from
(
env
::
var
(
"CARGO_MANIFEST_DIR"
)
.
unwrap
(
)
)
;
let
swift_dir = manifest_dir
.
join
(
"swift"
)
;
// Emitting any `rerun-if-changed` disables Cargo's default whole-package
// scan, so we must list every input to the `swift build` below. Watch the
// package manifests, the pinned dependency lockfile, the pinned compiler
// version, and the entire Swift sources tree (a directory is scanned
// recursively). `.build/` is intentionally *not* watched (it is this
// build's output; watching it would cause perpetual rebuilds).
for
input
in
[
swift_dir
.
join
(
"Package.swift"
)
,
swift_dir
.
join
(
"Package.resolved"
)
,
swift_dir
.
join
(
"Sources"
)
,
manifest_dir
.
join
(
".swift-version"
)
,
]
{
println
!
(
"cargo:rerun-if-changed={}"
,
input
.
display
(
)
)
;
}
// Build the Swift FFI package as a release dynamic library.
//
// Degrade gracefully when there is no runnable Swift toolchain. This crate
// is a workspace member, so a plain `cargo check`/`fmt`/`clippy` at the repo
// root runs this build script; if `swift build` cannot even be spawned we
// emit a warning and skip the link directives rather than panicking, so
// those Swift-free workflows keep working. Only `cargo build`/`cargo test`
// then fail — at link time, which is fair: they genuinely need Swift (and CI
// builds go through Bazel anyway). A Swift toolchain that *is* present but
// whose build fails is still surfaced as a hard error below.
let
mut
command =
Command
::
new
(
"swift"
)
;
command
.
args
(
[
"build"
,
"-c"
,
"release"
]
)
.
current_dir
(
&
swift_dir
)
;
apply_bare_repository_workaround
(
&
mut
command
)
;
let
status =
match
command
.
status
(
)
{
Ok
(
status
)
=> status
,
Err
(
e
)
=>
{
println
!
(
"cargo:warning=skipping the Swift FFI build: failed to run `swift build`: {e}.
\
Install a Swift toolchain (see https://www.swift.org/install/, e.g. via
\
swiftly) and ensure `swift` is on PATH to build or test this crate.
\
`cargo check`/`fmt`/`clippy` work without it. The pinned version is in
\
`.swift-version`."
)
;
return
;
}
}
;
assert
!
(
status
.
success
(
)
,
"`swift build` failed"
)
;
// Link against the freshly built dynamic library.
let
build_dir = swift_dir
.
join
(
".build/release"
)
;
println
!
(
"cargo:rustc-link-search=native={}"
,
build_dir
.
display
(
)
)
;
println
!
(
"cargo:rustc-link-lib=dylib=SwiftSyntaxFFI"
)
;
println
!
(
"cargo:rustc-link-arg=-Wl,-rpath,{}"
,
build_dir
.
display
(
)
)
;
println
!
(
"cargo:libdir={}"
,
build_dir
.
display
(
)
)
;
// The executable also needs to find the Swift runtime libraries at run time.
if
let
Some
(
runtime
)
=
swift_runtime_dir
(
)
{
println
!
(
"cargo:rustc-link-search=native={}"
,
runtime
.
display
(
)
)
;
println
!
(
"cargo:rustc-link-arg=-Wl,-rpath,{}"
,
runtime
.
display
(
)
)
;
println
!
(
"cargo:runtimedir={}"
,
runtime
.
display
(
)
)
;
}
}
/// Query the active Swift toolchain for the directory containing its runtime
/// shared libraries (e.g. `libswiftCore.so`).
fn
swift_runtime_dir
(
)
->
Option
<
PathBuf
>
{
let
output =
Command
::
new
(
"swiftc"
)
.
arg
(
"-print-target-info"
)
.
output
(
)
.
ok
(
)
?
;
if
!output
.
status
.
success
(
)
{
return
None
;
}
let
info =
String
::
from_utf8_lossy
(
&
output
.
stdout
)
;
// Extract the value of `"runtimeResourcePath": "..."` without pulling in a
// JSON dependency.
let
key =
"
\"
runtimeResourcePath
\"
"
;
let
start = info
.
find
(
key
)
?
;
let
rest =
&
info
[
start + key
.
len
(
)
..
]
;
let
colon = rest
.
find
(
':'
)
?
;
let
after =
&
rest
[
colon +
1
..
]
;
let
open = after
.
find
(
'"'
)
?
;
let
value_start =
&
after
[
open +
1
..
]
;
let
close = value_start
.
find
(
'"'
)
?
;
let
resource_path =
&
value_start
[
..close
]
;
Some
(
PathBuf
::
from
(
resource_path
)
.
join
(
if
cfg
!
(
target_os =
"macos"
)
{
"macosx"
}
else
{
"linux"
}
)
)
}
/// Some environments (notably GitHub Codespaces) inject
/// `GIT_CONFIG_KEY_0=safe.bareRepository` / `GIT_CONFIG_VALUE_0=explicit`, which
/// breaks the cached bare git repositories `swift build` uses. When exactly that
/// key is present, relax it to `all` for the `swift build` subprocess only
/// (rather than unconditionally, which could clobber an unrelated
/// `GIT_CONFIG_VALUE_0`).
fn
apply_bare_repository_workaround
(
command
:
&
mut
Command
)
{
if
env
::
var
(
"GIT_CONFIG_KEY_0"
)
.
as_deref
(
)
==
Ok
(
"safe.bareRepository"
)
{
command
.
env
(
"GIT_CONFIG_VALUE_0"
,
"all"
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL