cxx-once.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env bash
  2. # Compiler wrapper used by scripts/build-local.sh for g++ / clang++ (not zig, which caches objects itself).
  3. #
  4. # `$CXX a.cpp b.cpp ... -o x` compiles its sources one after the other, and every tool build-local.sh links against the
  5. # core compiled the whole core again: 9 serial core compiles were ~90 of the ~100 minutes of a CI "Core" job (round
  6. # speed185a). Through this wrapper each source is compiled ONCE per build (keyed on the source path and the flags),
  7. # LYNX_BUILD_JOBS compiles at a time, and every link uses the objects. Same compiler, same flags: the same code.
  8. #
  9. # LYNX_CXX_ONCE_REAL the real compiler command (words split on spaces, so "ccache g++" works)
  10. # LYNX_CXX_ONCE_RUN an id of the current build: objects are reused within it, compiled again in the next one
  11. # LYNX_BUILD_JOBS parallel compiles (default: the CPUs)
  12. # A command with -c (one translation unit) is passed straight to the compiler.
  13. set -u
  14. read -r -a real <<< "${LYNX_CXX_ONCE_REAL:?LYNX_CXX_ONCE_REAL is not set}"
  15. run="${LYNX_CXX_ONCE_RUN:?LYNX_CXX_ONCE_RUN is not set}"
  16. if [[ "${1:-}" == --one ]]; then # --one <flags file> <object> <source>: one compile (through xargs below)
  17. flagsfile="$2" o="$3" src="$4" flags=()
  18. while IFS= read -r -d '' f; do flags+=("$f"); done < "$flagsfile"
  19. "${real[@]}" "${flags[@]}" -c "$src" -o "$o.tmp$$" && mv -f "$o.tmp$$" "$o"
  20. exit
  21. fi
  22. for a in "$@"; do [[ "$a" == -c ]] && exec "${real[@]}" "$@"; done
  23. # compile flags = every argument but the sources, the objects / libraries and the output
  24. flags=() others=() srcs=() out=
  25. while [[ $# -gt 0 ]]; do
  26. case "$1" in
  27. -o) out="$2"; shift ;;
  28. -iquote | -isystem | -idirafter | -include | -imacros | -I | -D | -U | -x | -target | -arch) flags+=("$1" "$2"); shift ;;
  29. *.cpp | *.cc | *.cxx) srcs+=("$1") ;;
  30. -*) flags+=("$1") ;;
  31. *) others+=("$1") ;;
  32. esac
  33. shift
  34. done
  35. [[ ${#srcs[@]} -gt 0 && -n "$out" ]] || exec "${real[@]}" "${flags[@]}" ${others[@]+"${others[@]}"} ${out:+-o "$out"}
  36. key="$(printf '%s\n' "${real[*]}" "${flags[@]}" | cksum | cut -d' ' -f1)"
  37. dir="build-local/obj/$key"
  38. mkdir -p "$dir"
  39. [[ -f "$dir/flags" ]] || { printf '%s\0' "${flags[@]}" > "$dir/flags.tmp$$" && mv -f "$dir/flags.tmp$$" "$dir/flags"; }
  40. stamp="$dir/.done-$run"
  41. touch "$stamp"
  42. objs=() todo=()
  43. for s in "${srcs[@]}"; do
  44. o="$dir/$(printf '%s' "$s" | tr '/.' '__').o"
  45. objs+=("$o")
  46. grep -qxF "$s" "$stamp" || todo+=("$o" "$s")
  47. done
  48. if [[ ${#todo[@]} -gt 0 ]]; then
  49. jobs="${LYNX_BUILD_JOBS:-$(nproc 2> /dev/null || sysctl -n hw.ncpu 2> /dev/null || echo 4)}"
  50. printf '%s\n' "${todo[@]}" | xargs -n 2 -P "$jobs" "$0" --one "$dir/flags" || exit 1
  51. # (recorded only once compiled: a stopped or failed compile is done again by the next command)
  52. for ((i = 1; i < ${#todo[@]}; i += 2)); do echo "${todo[$i]}" >> "$stamp"; done
  53. fi
  54. exec "${real[@]}" "${flags[@]}" "${objs[@]}" ${others[@]+"${others[@]}"} -o "$out"