-
-
Notifications
You must be signed in to change notification settings - Fork 0
197 lines (193 loc) · 7.34 KB
/
docs.yml
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
permissions:
contents: read
on:
workflow_call:
inputs:
rust:
required: false
type: string
default: nightly
target:
required: false
type: string
args:
required: false
type: string
env:
CARGO_INCREMENTAL: 0
CARGO_NET_GIT_FETCH_WITH_CLI: true
CARGO_NET_RETRY: 10
CARGO_TERM_COLOR: always
RUST_BACKTRACE: 1
RUSTFLAGS: -D warnings
RUSTDOCFLAGS: -D warnings
RUSTUP_MAX_RETRIES: 10
# Reusable workflows cannot inherit environment variables.
ATOMIC_MAYBE_UNINIT_DENY_WARNINGS: 1
CARGO_HACK_DENY_WARNINGS: 1
CARGO_LLVM_COV_DENY_WARNINGS: 1
CARGO_MINIMAL_VERSIONS_DENY_WARNINGS: 1
CARGO_NO_DEV_DEPS_DENY_WARNINGS: 1
CONST_FN_DENY_WARNINGS: 1
PORTABLE_ATOMIC_DENY_WARNINGS: 1
SEMIHOSTING_DENY_WARNINGS: 1
defaults:
run:
shell: bash --noprofile --norc -CeEuo pipefail {0}
jobs:
docs:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: taiki-e/checkout-action@v1
- uses: taiki-e/github-actions/install-rust@main
with:
toolchain: ${{ inputs.rust }}
# Refs:
# - https://github.com/rust-lang/docs.rs/blob/HEAD/crates/metadata/lib.rs
# - https://github.com/rust-lang/docs.rs/blob/HEAD/src/docbuilder/rustwide_builder.rs
# NB: sync with check-external-types.yml
- name: Run cargo rustdoc
run: |
trap -- 's=$?; printf >&2 "%s\n" "${0##*/}:${LINENO}: \`${BASH_COMMAND}\` exit with ${s}"; exit ${s}' ERR
retry() {
for i in {1..10}; do
if "$@"; then
return 0
else
sleep "${i}"
fi
done
"$@"
}
# docs.rs uses -Z rustdoc-scrape-examples since https://github.com/rust-lang/docs.rs/pull/1954
# shellcheck disable=SC2206
base_args=(
--lib
-Z unstable-options
-Z rustdoc-map
-Z rustdoc-scrape-examples
--config "doc.extern-map.registries.crates-io=\"https://docs.rs\""
${{ inputs.args }}
)
IFS=$'\n\t'
base_rustflags=()
base_rustdocflags=()
if [[ -n "${RUSTFLAGS:-}" ]]; then
while read -rd' '; do
t="${REPLY# *}"
base_rustflags+=("${t%* }")
done <<<"${RUSTFLAGS} "
fi
if [[ -n "${RUSTDOCFLAGS:-}" ]]; then
while read -rd' '; do
t="${REPLY# *}"
base_rustdocflags+=("${t%* }")
done <<<"${RUSTDOCFLAGS} "
fi
base_rustdocflags+=(
-Z unstable-options
# https://github.com/rust-lang/docs.rs/issues/2389
--cfg docsrs
--document-hidden-items
--document-private-items
--extern-html-root-takes-precedence
)
unset RUSTFLAGS
unset RUSTDOCFLAGS
export DOCS_RS=1
metadata=$(cargo metadata --format-version=1 --no-deps)
# Handle target inputs.
input_target="${{ inputs.target }}"
base_targets=()
if [[ -n "${input_target}" ]]; then
while read -rd,; do
base_targets+=(--target="${REPLY}")
retry rustup target add "${REPLY}" || true # tier 3 targets fail to install rustup target
done <<<"${input_target},"
fi
# Run cargo rustdoc for public crates with features and rustc/rustdoc/cargo args specified in docs.rs metadata in Cargo.toml.
# Publishing is unrestricted if null, and forbidden if an empty array.
for pkg in $(jq -c '. as $metadata | .workspace_members[] as $id | $metadata.packages[] | select(.id == $id and .publish != [])' <<<"${metadata}"); do
args=("${base_args[@]}")
pkg_rustflags=("${base_rustflags[@]}")
pkg_rustdocflags=("${base_rustdocflags[@]}")
eval "$(jq -r '@sh "manifest_path=\(.manifest_path)"' <<<"${pkg}")"
args+=(--manifest-path="${manifest_path}")
docs_rs_metadata=$(jq '.metadata.docs.rs' <<<"${pkg}")
if [[ "${docs_rs_metadata}" == "null" ]]; then
docs_rs_metadata=$(jq '.metadata."docs.rs"' <<<"${pkg}")
if [[ "${docs_rs_metadata}" == "null" ]]; then
docs_rs_metadata='{}'
fi
fi
if ! jq '.targets[] | .kind[]' <<<"${pkg}" | grep -Fq '"proc-macro"'; then
# Respect target-related fields specified in docs.rs metadata if inputs.target is empty.
if [[ -z "${input_target}" ]]; then
default_target=$(jq -r '."default-target"' <<<"${docs_rs_metadata}")
if [[ "${default_target}" != null ]]; then
args+=(--target="${default_target}")
retry rustup target add "${default_target}" || true # tier 3 targets fail to install rustup target
fi
if [[ "$(jq '.targets' <<<"${docs_rs_metadata}")" != null ]]; then
for target in $(jq -r '.targets[]' <<<"${docs_rs_metadata}"); do
args+=(--target="${target}")
retry rustup target add "${target}" || true # tier 3 targets fail to install rustup target
done
fi
else
args+=("${base_targets[@]}")
fi
fi
if [[ "$(jq '.features' <<<"${docs_rs_metadata}")" != null ]]; then
for feature in $(jq -r '.features[]' <<<"${docs_rs_metadata}"); do
args+=(--features="${feature}")
done
fi
if [[ "$(jq '."all-features"' <<<"${docs_rs_metadata}")" == 'true' ]]; then
args+=(--all-features)
fi
if [[ "$(jq '."no-default-features"' <<<"${docs_rs_metadata}")" == 'true' ]]; then
args+=(--no-default-features)
fi
if [[ "$(jq '."rustc-args"' <<<"${docs_rs_metadata}")" != null ]]; then
for arg in $(jq -r '."rustc-args"[]' <<<"${docs_rs_metadata}"); do
pkg_rustflags+=("${arg}")
done
fi
if [[ "$(jq '."rustdoc-args"' <<<"${docs_rs_metadata}")" != null ]]; then
for arg in $(jq -r '."rustdoc-args"[]' <<<"${docs_rs_metadata}"); do
pkg_rustdocflags+=("${arg}")
done
fi
if [[ -n "${pkg_rustflags[*]}" ]]; then
f=''
for flag in "${pkg_rustflags[@]}"; do
f+="\"${flag}\","
done
args+=(
--config "build.rustflags=[${f}]"
-Z host-config
-Z target-applies-to-host
--config "host.rustflags=[${f}]"
)
fi
if [[ -n "${pkg_rustdocflags[*]}" ]]; then
f=''
for flag in "${pkg_rustdocflags[@]}"; do
f+="\"${flag}\","
done
args+=(
--config "build.rustdocflags=[${f}]"
)
fi
if [[ "$(jq '."cargo-args"' <<<"${docs_rs_metadata}")" != null ]]; then
for arg in $(jq -r '."cargo-args"[]' <<<"${docs_rs_metadata}"); do
args+=("${arg}")
done
fi
(
set -x
cargo rustdoc "${args[@]}"
)
done