12add0d91Sopenharmony_ci#!/usr/bin/env sh
22add0d91Sopenharmony_ci
32add0d91Sopenharmony_ci# Builds documentation for all target triples that we have a registered URL for
42add0d91Sopenharmony_ci# in liblibc. This scrapes the list of triples to document from `src/lib.rs`
52add0d91Sopenharmony_ci# which has a bunch of `html_root_url` directives we pick up.
62add0d91Sopenharmony_ci
72add0d91Sopenharmony_ciset -ex
82add0d91Sopenharmony_ci
92add0d91Sopenharmony_ciTARGET_DOC_DIR="target/doc"
102add0d91Sopenharmony_ciREADME="README.md"
112add0d91Sopenharmony_ciPLATFORM_SUPPORT="platform-support.md"
122add0d91Sopenharmony_ci
132add0d91Sopenharmony_cirm -rf "$TARGET_DOC_DIR"
142add0d91Sopenharmony_cimkdir -p "$TARGET_DOC_DIR"
152add0d91Sopenharmony_ci
162add0d91Sopenharmony_ciif ! rustc --version | grep -E "nightly" ; then
172add0d91Sopenharmony_ci    echo "Building the documentation requires a nightly Rust toolchain"
182add0d91Sopenharmony_ci    exit 1
192add0d91Sopenharmony_cifi
202add0d91Sopenharmony_ci
212add0d91Sopenharmony_cirustup component add rust-src
222add0d91Sopenharmony_ci
232add0d91Sopenharmony_ci# List all targets that do currently build successfully:
242add0d91Sopenharmony_ci# shellcheck disable=SC1003
252add0d91Sopenharmony_cigrep '[\d|\w|-]* \\' ci/build.sh > targets
262add0d91Sopenharmony_cised -i.bak 's/ \\//g' targets
272add0d91Sopenharmony_cigrep '^[_a-zA-Z0-9-]*$' targets | sort > tmp && mv tmp targets
282add0d91Sopenharmony_ci
292add0d91Sopenharmony_ci# Create a markdown list of supported platforms in $PLATFORM_SUPPORT
302add0d91Sopenharmony_cirm $PLATFORM_SUPPORT || true
312add0d91Sopenharmony_ci
322add0d91Sopenharmony_ciprintf '### Platform-specific documentation\n' >> $PLATFORM_SUPPORT
332add0d91Sopenharmony_ci
342add0d91Sopenharmony_ciwhile read -r target; do
352add0d91Sopenharmony_ci    echo "documenting ${target}"
362add0d91Sopenharmony_ci
372add0d91Sopenharmony_ci    case "${target}" in
382add0d91Sopenharmony_ci        *apple*)
392add0d91Sopenharmony_ci            # FIXME:
402add0d91Sopenharmony_ci            # We can't build docs of apple targets from Linux yet.
412add0d91Sopenharmony_ci            continue
422add0d91Sopenharmony_ci            ;;
432add0d91Sopenharmony_ci        *)
442add0d91Sopenharmony_ci            ;;
452add0d91Sopenharmony_ci    esac
462add0d91Sopenharmony_ci
472add0d91Sopenharmony_ci    rustup target add "${target}" || true
482add0d91Sopenharmony_ci
492add0d91Sopenharmony_ci    # Enable extra configuration flags:
502add0d91Sopenharmony_ci    export RUSTDOCFLAGS="--cfg freebsd11"
512add0d91Sopenharmony_ci
522add0d91Sopenharmony_ci    # If cargo doc fails, then try with unstable feature:
532add0d91Sopenharmony_ci    if ! cargo doc --target "${target}" \
542add0d91Sopenharmony_ci        --no-default-features --features const-extern-fn,extra_traits ; then
552add0d91Sopenharmony_ci        cargo doc --target "${target}" \
562add0d91Sopenharmony_ci        -Z build-std=core,alloc \
572add0d91Sopenharmony_ci        --no-default-features --features const-extern-fn,extra_traits
582add0d91Sopenharmony_ci    fi
592add0d91Sopenharmony_ci
602add0d91Sopenharmony_ci    mkdir -p "${TARGET_DOC_DIR}/${target}"
612add0d91Sopenharmony_ci    cp -r "target/${target}/doc" "${TARGET_DOC_DIR}/${target}"
622add0d91Sopenharmony_ci
632add0d91Sopenharmony_ci    echo "* [${target}](${target}/doc/libc/index.html)" >> $PLATFORM_SUPPORT
642add0d91Sopenharmony_cidone < targets
652add0d91Sopenharmony_ci
662add0d91Sopenharmony_ci# Replace <div class="platform_support"></div> with the contents of $PLATFORM_SUPPORT
672add0d91Sopenharmony_cicp $README $TARGET_DOC_DIR
682add0d91Sopenharmony_ciline=$(grep -n '<div class="platform_docs"></div>' $README | cut -d ":" -f 1)
692add0d91Sopenharmony_ci
702add0d91Sopenharmony_ci{ head -n "$((line-1))" $README; cat $PLATFORM_SUPPORT; tail -n "+$((line+1))" $README; } > $TARGET_DOC_DIR/$README
712add0d91Sopenharmony_ci
722add0d91Sopenharmony_cicp $TARGET_DOC_DIR/$README $TARGET_DOC_DIR/index.md
732add0d91Sopenharmony_ci
742add0d91Sopenharmony_ciRUSTDOCFLAGS="--enable-index-page --index-page=${TARGET_DOC_DIR}/index.md -Zunstable-options" cargo doc
752add0d91Sopenharmony_ci
762add0d91Sopenharmony_ci# Tweak style
772add0d91Sopenharmony_cicp ci/rust.css $TARGET_DOC_DIR
782add0d91Sopenharmony_cised -ie "8i <link rel=\"stylesheet\" type=\"text/css\" href=\"normalize.css\">" $TARGET_DOC_DIR/index.html
792add0d91Sopenharmony_cised -ie "9i <link rel=\"stylesheet\" type=\"text/css\" href=\"rust.css\">" $TARGET_DOC_DIR/index.html
802add0d91Sopenharmony_ci
812add0d91Sopenharmony_ci# Copy the licenses
822add0d91Sopenharmony_cicp LICENSE-* $TARGET_DOC_DIR/
83