From 21968acf99c2f8de5d0cc879c627568db49e0982 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Steven=20Dee=20=28J=C5=8Dshin=29?= <git@wholezero.org>
Date: Sun, 5 Jan 2025 20:47:34 -0800
Subject: [PATCH] Standard make path (#1353)

Modifies download-cosmocc.sh to maintain a .cosmocc/current symlink that
always points to the most recently downloaded version of cosmocc. We can
use this to point at a canonical make for a bootstrapped repository. For
first-time builds, we suggest: https://cosmo.zip/pub/cosmos/bin/make and
have updated the docs in a few places to mention this.

Fixes the other part of #1346.
---
 Makefile                  |  5 +++--
 README.md                 | 23 +++++++++++++++--------
 ape/apeinstall.sh         |  4 ++--
 build/download-cosmocc.sh |  5 +++++
 test/posix/sigchld_test.c |  2 +-
 tool/zsh/mmake            | 17 +++++++++++++++--
 6 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index 27b241b77..33fbcbdae 100644
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,8 @@ COMMA := ,
 PWD := $(shell pwd)
 
 # detect wsl2 running cosmopolitan binaries on the host by checking whether:
-# - user ran build/bootstrap/make, in which case make's working directory is in wsl
+# - user ran .cosmocc/current/bin/make, in which case make's working directory
+#   is in wsl
 # - user ran make, in which case cocmd's working directory is in wsl
 ifneq ($(findstring //wsl.localhost/,$(CURDIR) $(PWD)),)
 $(warning wsl2 interop is enabled)
@@ -89,7 +90,7 @@ UNAME_S := $(shell uname -s)
 
 # apple still distributes a 17 year old version of gnu make
 ifeq ($(MAKE_VERSION), 3.81)
-$(error please use build/bootstrap/make)
+$(error please use https://cosmo.zip/pub/cosmos/bin/make)
 endif
 
 LC_ALL = C
diff --git a/README.md b/README.md
index eda1f14bc..f444cab16 100644
--- a/README.md
+++ b/README.md
@@ -87,15 +87,22 @@ ape/apeinstall.sh
 ```
 
 You can now build the mono repo with any modern version of GNU Make. To
-make life easier, we've included one in the cosmocc toolchain, which is
-guaranteed to be compatible and furthermore includes our extensions for
-doing build system sandboxing.
+bootstrap your build, you can install Cosmopolitan Make from this site:
+
+https://cosmo.zip/pub/cosmos/bin/make
+
+E.g.:
 
 ```sh
-build/bootstrap/make -j8
+curl -LO https://cosmo.zip/pub/cosmos/bin/make
+./make -j8
 o//examples/hello
 ```
 
+After you've built the repo once, you can also use the make from your
+cosmocc at `.cosmocc/current/bin/make`. You might even prefer to alias
+make to `$COSMO/.cosmocc/current/bin/make`.
+
 Since the Cosmopolitan repository is very large, you might only want to
 build one particular thing. Here's an example of a target that can be
 compiled relatively quickly, which is a simple POSIX test that only
@@ -103,7 +110,7 @@ depends on core LIBC packages.
 
 ```sh
 rm -rf o//libc o//test
-build/bootstrap/make o//test/posix/signal_test
+.cosmocc/current/bin/make o//test/posix/signal_test
 o//test/posix/signal_test
 ```
 
@@ -112,21 +119,21 @@ list out each individual one. For example if you wanted to build and run
 all the unit tests in the `TEST_POSIX` package, you could say:
 
 ```sh
-build/bootstrap/make o//test/posix
+.cosmocc/current/bin/make o//test/posix
 ```
 
 Cosmopolitan provides a variety of build modes. For example, if you want
 really tiny binaries (as small as 12kb in size) then you'd say:
 
 ```sh
-build/bootstrap/make m=tiny
+.cosmocc/current/bin/make m=tiny
 ```
 
 You can furthermore cut out the bloat of other operating systems, and
 have Cosmopolitan become much more similar to Musl Libc.
 
 ```sh
-build/bootstrap/make m=tinylinux
+.cosmocc/current/bin/make m=tinylinux
 ```
 
 For further details, see [//build/config.mk](build/config.mk).
diff --git a/ape/apeinstall.sh b/ape/apeinstall.sh
index 2a0a28590..73f24965f 100755
--- a/ape/apeinstall.sh
+++ b/ape/apeinstall.sh
@@ -10,8 +10,8 @@ if [ ! -f ape/loader.c ]; then
   cd "$COSMO" || exit
 fi
 
-if [ -x build/bootstrap/make ]; then
-  MAKE=build/bootstrap/make
+if [ -x .cosmocc/current/bin/make ]; then
+  MAKE=.cosmocc/current/bin/make
 else
   MAKE=make
 fi
diff --git a/build/download-cosmocc.sh b/build/download-cosmocc.sh
index 13310a4e4..52c89b091 100755
--- a/build/download-cosmocc.sh
+++ b/build/download-cosmocc.sh
@@ -99,3 +99,8 @@ rm -f cosmocc.zip cosmocc.zip.sha256sum
 # commit output directory
 cd "${OLDPWD}" || die
 mv "${OUTPUT_TMP}" "${OUTPUT_DIR}" || die
+
+# update current symlink
+BASE=$(basename "${OUTPUT_DIR}")
+DIR=$(dirname "${OUTPUT_DIR}")
+ln -sfn "$BASE" "$DIR/current"
diff --git a/test/posix/sigchld_test.c b/test/posix/sigchld_test.c
index 36cf1f032..6915f7cf2 100644
--- a/test/posix/sigchld_test.c
+++ b/test/posix/sigchld_test.c
@@ -32,7 +32,7 @@
 #include <unistd.h>
 
 // clang-format off
-// sh -c 'build/bootstrap/make -j8 V=1 o//test/posix/sigchld_test.runs'
+// sh -c '.cosmocc/current/bin/make -j8 V=1 o//test/posix/sigchld_test.runs'
 // clang-format on
 
 void Assert(const char *file, int line, bool ok) {
diff --git a/tool/zsh/mmake b/tool/zsh/mmake
index 0b5315bae..d75b29a19 100644
--- a/tool/zsh/mmake
+++ b/tool/zsh/mmake
@@ -38,8 +38,21 @@ done
   whence nproc >/dev/null || autoload -Uz nproc
   j=-j$(nproc)
 }
-local make=${MAKE:-${COSMOCC:-/opt/cosmocc/current}/bin/make}
-[[ -x $make ]] || make=${COSMO:-$PWD}/build/bootstrap/make
+local make=$(
+  case $MAKE in
+    */*) echo $MAKE ;;
+    ?*)  command -v $MAKE ;;
+    *)   echo .cosmocc/current/bin/make
+  esac
+)
+if [[ ! -x $make ]]; then
+  { echo 'please install a suitable make, for example:'
+    echo
+    echo 'https://cosmo.zip/pub/cosmos/bin/make'
+    echo
+    echo 'then either put it on your $PATH or point to it with $MAKE.'
+  } >&2; return 1
+fi
 ( set -x
   exec $make $j $flags MODE=$mode $targs )
 # vim:ft=zsh