So mingw32-make.exe
is in the rtools40/mingw64/bin
folder, but your PATH is only including the rtools40/usr/bin
folder.
Can you run the following command to append this to your PATH variable on startup:
write('PATH="${RTOOLS40_HOME}\\mingw64\\bin;${PATH}"', file = "~/.Renviron", append = TRUE)
Then restart R and try the rebuild_cmdstan()
call again?