Repository: spack/spack

Issue:
Installation issue: fef05621a breaks cmake < 3.12
### Steps to reproduce the issue

Beginning with commit fef05621a77ee8b62b318370e1ec7bf39502eb7c, Spack passes options to the CMake bootstrap script. The CMake bootstrap script does not accept the given options if CMake releases before 3.12 are used. (I did not test all releases but builds for versions 3.7.2, 3.8.2, and 3.11.4 fail.)
```console
$ which spack
/home/christoph/spack/bin/spack
$ git -C ~/spack rev-parse HEAD
fef05621a77ee8b62b318370e1ec7bf39502eb7c
$ spack install cmake@3.7.2
==> Warning: Missing a source id for ncurses@6.2.20200212
[+] / (external ncurses-6.2.20200212-ujc234wnddrjvgfwk6koel3aqgamiqmz)
[+] /usr (external openssl-1.1.1f-i44um4uum2yiy4jeqsjgqttev27y2fhk)
==> Installing cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft
==> No binary for cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft found: installing from source
==> Warning: Missing a source id for ncurses@6.2.20200212
==> Using cached archive: /home/christoph/spack/var/spack/cache/_source-cache/archive/dc/dc1246c4e6d168ea4d6e042cfba577c1acd65feea27e56f5ff37df920c30cae0.tar.gz
==> Applied patch /home/christoph/spack/var/spack/repos/builtin/packages/cmake/nag-response-files.patch
==> Applied patch /home/christoph/spack/var/spack/repos/builtin/packages/cmake/ignore_crayxc_warnings.patch
==> cmake: Executing phase: 'bootstrap'
==> Error: ProcessError: Command exited with status 1:
    './bootstrap' 'CFLAGS=-O3' 'CXXFLAGS=-O3' '--prefix=/home/christoph/spack/opt/spack/linux-ubuntu20.04-sandybridge/gcc-9.3.0/cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft' '--parallel=4' '--no-system-libs' '--no-qt-gui' '--' '-DCMAKE_BUILD_TYPE=Release' '-DCMake_TEST_INSTALL=OFF' '-DCMAKE_USE_OPENSSL=True'
See build log for details:
  /tmp/christoph/spack-stage/spack-stage-cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft/spack-build-out.txt
$ cat /tmp/christoph/spack-stage/spack-stage-cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft/spack-build-out.txt 
==> cmake: Executing phase: 'bootstrap'
==> [2021-06-05-17:35:19.714530] './bootstrap' 'CFLAGS=-O3' 'CXXFLAGS=-O3' '--prefix=/home/christoph/spack/opt/spack/linux-ubuntu20.04-sandybridge/gcc-9.3.0/cmake-3.7.2-hd4q5nm7k26z5qd6pusmki63acx6b2ft' '--parallel=4' '--no-system-libs' '--no-qt-gui' '--' '-DCMAKE_BUILD_TYPE=Release' '-DCMake_TEST_INSTALL=OFF' '-DCMAKE_USE_OPENSSL=True'
Unknown option: CFLAGS=-O3
```
@chuckatkins 

Build files:
* [spack-build-env.txt](https://github.com/spack/spack/files/6602968/spack-build-env.txt)
* [spack-build-out.txt](https://github.com/spack/spack/files/6602969/spack-build-out.txt)
* [spack-build-01-bootstrap-out.txt](https://github.com/spack/spack/files/6602970/spack-build-01-bootstrap-out.txt)

Spack report:
* **Spack:** 0.16.1
* **Python:** 3.8.5
* **Platform:** linux-ubuntu20.04-sandybridge
* **Concretizer:** original

- [x] I have run `spack debug report` and reported the version of Spack/Python/Platform
- [x] I have run `spack maintainers <name-of-the-package>` and @mentioned any maintainers
- [x] I have uploaded the build log and environment files
- [x] I have searched the issues of this repo and believe this is not a duplicate



Discussion:
Can you create a pr that adds these options conditionally?
> Can you create a pr that adds these options conditionally?

There is no need to add these compiler flags conditionally because the bootstrap script always honored the environment variables `CFLAGS` and `CXXFLAGS`, see [CMake commit 850cfe](https://gitlab.kitware.com/cmake/cmake/-/commit/850cfe3a3b58db3bf76e5d5d6a2583f3849ab3e8) (January 17, 2021) and the **existing** [bootstrap code starting at line 905](https://gitlab.kitware.com/cmake/cmake/-/blob/850cfe3a3b58db3bf76e5d5d6a2583f3849ab3e8/bootstrap#L905-908).

A patch can be found below but I do not want to submit a pull request because there are no observable speed-ups for me and because the CFLAGS are re-used for the actual CMake build that is installed; this may lead to unintended effects if no `CMAKE_BUILD_TYPE` flag is set. The table below shows wall-clock time, CPU time, and system time of `spack install cmake` as measured by the bash built-in `time` on a virtual private server with 8GB RAM and an Intel(R) Xeon(R) CPU E5-2670 CPU (2.60GHz) with four cores running Ubuntu 20.04 (Spack arch `linux-ubuntu20.04-sandybridge`). The wall-clock time is barely affected and the CPU time increases monotonously with the optimization level. I do not find these results surprising given that most of the time is spent compiling C++14 code (CMake 3.7.2) or C++17 code, respectively. 

| CMake version | Optimization level | Wall-clock time (min) | CPU time (min) | System time (min) |
| -- | -- | -- | -- | -- |
| 3.7.2  | --    |  6:22 | 14:42 | 1:56 |
| 3.7.2  | `-O1` |  6:15 | 15:14 | 1:56 |
| 3.7.2  | `-O2` |  6:20 | 15:46 | 1:55 |
| 3.7.2  | `-O3` |  6:12 | 16:01 | 1:51 |
| 3.20.1 | --    |  9:47 | 28:57 | 3:01 |
| 3.20.1 | `-O3` | 10:15 | 31:16 | 3:11 |





```diff
diff --git a/var/spack/repos/builtin/packages/cmake/package.py b/var/spack/repos/builtin/packages/cmake/package.py
index ea6b7c3a..e8e5db19 100644
--- a/var/spack/repos/builtin/packages/cmake/package.py
+++ b/var/spack/repos/builtin/packages/cmake/package.py
@@ -230,8 +230,6 @@ def flag_handler(self, name, flags):
     def bootstrap_args(self):
         spec = self.spec
         args = [
-            'CFLAGS=-O3',
-            'CXXFLAGS=-O3',
             '--prefix={0}'.format(self.prefix),
             '--parallel={0}'.format(make_jobs)
         ]
@@ -276,6 +274,8 @@ def bootstrap_args(self):
 
     def bootstrap(self, spec, prefix):
         bootstrap = Executable('./bootstrap')
+        bootstrap.add_default_env('CFLAGS', '-O3')
+        bootstrap.add_default_env('CXXFLAGS', '-O3')
         bootstrap(*self.bootstrap_args())
 
     def build(self, spec, prefix):
```

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/cmake/package.py b/var/spack/repos/builtin/packages/cmake/package.py
--- a/var/spack/repos/builtin/packages/cmake/package.py
+++ b/var/spack/repos/builtin/packages/cmake/package.py
@@ -230,8 +230,6 @@ def flag_handler(self, name, flags):
     def bootstrap_args(self):
         spec = self.spec
         args = [
-            'CFLAGS=-O3',
-            'CXXFLAGS=-O3',
             '--prefix={0}'.format(self.prefix),
             '--parallel={0}'.format(make_jobs)
         ]


Test patch: