1#!/usr/bin/env sh
2
3# Builds documentation for all target triples that we have a registered URL for
4# in liblibc. This scrapes the list of triples to document from `src/lib.rs`
5# which has a bunch of `html_root_url` directives we pick up.
6
7set -ex
8
9TARGET_DOC_DIR="target/doc"
10README="README.md"
11PLATFORM_SUPPORT="platform-support.md"
12
13rm -rf "$TARGET_DOC_DIR"
14mkdir -p "$TARGET_DOC_DIR"
15
16if ! rustc --version | grep -E "nightly" ; then
17    echo "Building the documentation requires a nightly Rust toolchain"
18    exit 1
19fi
20
21rustup component add rust-src
22
23# List all targets that do currently build successfully:
24# shellcheck disable=SC1003
25grep '[\d|\w|-]* \\' ci/build.sh > targets
26sed -i.bak 's/ \\//g' targets
27grep '^[_a-zA-Z0-9-]*$' targets | sort > tmp && mv tmp targets
28
29# Create a markdown list of supported platforms in $PLATFORM_SUPPORT
30rm $PLATFORM_SUPPORT || true
31
32printf '### Platform-specific documentation\n' >> $PLATFORM_SUPPORT
33
34while read -r target; do
35    echo "documenting ${target}"
36
37    case "${target}" in
38        *apple*)
39            # FIXME:
40            # We can't build docs of apple targets from Linux yet.
41            continue
42            ;;
43        *)
44            ;;
45    esac
46
47    rustup target add "${target}" || true
48
49    # Enable extra configuration flags:
50    export RUSTDOCFLAGS="--cfg freebsd11"
51
52    # If cargo doc fails, then try with unstable feature:
53    if ! cargo doc --target "${target}" \
54        --no-default-features --features const-extern-fn,extra_traits ; then
55        cargo doc --target "${target}" \
56        -Z build-std=core,alloc \
57        --no-default-features --features const-extern-fn,extra_traits
58    fi
59
60    mkdir -p "${TARGET_DOC_DIR}/${target}"
61    cp -r "target/${target}/doc" "${TARGET_DOC_DIR}/${target}"
62
63    echo "* [${target}](${target}/doc/libc/index.html)" >> $PLATFORM_SUPPORT
64done < targets
65
66# Replace <div class="platform_support"></div> with the contents of $PLATFORM_SUPPORT
67cp $README $TARGET_DOC_DIR
68line=$(grep -n '<div class="platform_docs"></div>' $README | cut -d ":" -f 1)
69
70{ head -n "$((line-1))" $README; cat $PLATFORM_SUPPORT; tail -n "+$((line+1))" $README; } > $TARGET_DOC_DIR/$README
71
72cp $TARGET_DOC_DIR/$README $TARGET_DOC_DIR/index.md
73
74RUSTDOCFLAGS="--enable-index-page --index-page=${TARGET_DOC_DIR}/index.md -Zunstable-options" cargo doc
75
76# Tweak style
77cp ci/rust.css $TARGET_DOC_DIR
78sed -ie "8i <link rel=\"stylesheet\" type=\"text/css\" href=\"normalize.css\">" $TARGET_DOC_DIR/index.html
79sed -ie "9i <link rel=\"stylesheet\" type=\"text/css\" href=\"rust.css\">" $TARGET_DOC_DIR/index.html
80
81# Copy the licenses
82cp LICENSE-* $TARGET_DOC_DIR/
83