Repository: spack/spack

Issue:
Installation issue: gperftools when `build_system=cmake`
### Steps to reproduce the issue

Fails to build on macos when `build_system=cmake`
```console
$ spack install gperftools@2.15 build_system=cmake # failure
$ spack install gperftools@2.15 build_system=autotools # fine
```


### Error message

<details><summary>Error message</summary>

<pre>
==> Installing gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2 [64/94]
==> No binary for gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2 found: installing from source
==> Fetching https://github.com/gperftools/gperftools/releases/download/gperftools-2.15/gperftools-2.15.tar.gz
==> No patches needed for gperftools
==> gperftools: Executing phase: 'cmake'
==> gperftools: Executing phase: 'build'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j10'

5 errors found in build log:
     4669    /Users/cmarsh/Documents/science/code/spack/lib/spack/env/gcc/g++  -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -fsized-deallocation -O3 -DNDEBUG -arch arm64
             -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk -mmacosx-version-min=14.0 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/malloc_bench.
             dir/benchmark/malloc_bench.cc.o -o malloc_bench  librun_benchmark.a libtcmalloc_minimal.a libspinlock.a libsysinfo.a liblogging.a
     4670    ld: warning: duplicate -rpath '/opt/homebrew/Cellar/gcc/14.1.0/lib/gcc/current/gcc/aarch64-apple-darwin23/14' ignored
     4671    ld: warning: duplicate -rpath '/opt/homebrew/Cellar/gcc/14.1.0/lib/gcc/current' ignored
     4672    [100%] Linking CXX executable malloc_bench_shared
     4673    /Users/cmarsh/Documents/science/code/spack/opt/spack/darwin-sonoma-m1/apple-clang-15.0.0/cmake-3.27.9-bkoybdfelo2yfkczhbum3wygrumyt4bn/bin/cmake -E cmake_link_script CMakeFi
             les/malloc_bench_shared.dir/link.txt --verbose=1
     4674    /Users/cmarsh/Documents/science/code/spack/lib/spack/env/gcc/g++  -Wall -Wwrite-strings -Woverloaded-virtual -Wno-sign-compare -fsized-deallocation -O3 -DNDEBUG -arch arm64
             -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX14.4.sdk -mmacosx-version-min=14.0 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/malloc_bench_
             shared.dir/benchmark/malloc_bench.cc.o -o malloc_bench_shared  -Wl,-rpath,/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-
             2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-build-irmbw4y librun_benchmark.a libtcmalloc_minimal.9.16.5.dylib
  >> 4675    Undefined symbols for architecture arm64:
     4676      "_tc_delete_sized", referenced from:
     4677          bench_fastpath_simple_sized(long, unsigned long) in malloc_bench.cc.o
     4678          _main in malloc_bench.cc.o
     4679      "_tc_memalign", referenced from:
     4680          bench_fastpath_memalign(long, unsigned long) in malloc_bench.cc.o
     4681          _main in malloc_bench.cc.o
     4682    ld: symbol(s) not found for architecture arm64
  >> 4683    collect2: error: ld returned 1 exit status
  >> 4684    make[2]: *** [CMakeFiles/malloc_bench.dir/build.make:105: malloc_bench] Error 1
     4685    make[2]: Leaving directory '/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-bu
             ild-irmbw4y'
  >> 4686    make[1]: *** [CMakeFiles/Makefile2:2028: CMakeFiles/malloc_bench.dir/all] Error 2
     4687    make[1]: *** Waiting for unfinished jobs....
     4688    ld: warning: duplicate -rpath '/opt/homebrew/Cellar/gcc/14.1.0/lib/gcc/current/gcc/aarch64-apple-darwin23/14' ignored
     4689    ld: warning: duplicate -rpath '/opt/homebrew/Cellar/gcc/14.1.0/lib/gcc/current' ignored
     4690    make[2]: Leaving directory '/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-bu
             ild-irmbw4y'
     4691    [100%] Built target malloc_bench_shared
     4692    [100%] Linking CXX executable binary_trees

     ...

     4708    make[2]: Leaving directory '/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-bu
             ild-irmbw4y'
     4709    [100%] Built target binary_trees
     4710    [100%] Built target binary_trees_shared
     4711    make[2]: Leaving directory '/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-bu
             ild-irmbw4y'
     4712    [100%] Built target tcmalloc_minimal_debug_unittest
     4713    make[1]: Leaving directory '/private/var/folders/tr/v_kqwm7x25x3y68l_p5qh1gw0000gn/T/cmarsh/spack-stage/spack-stage-gperftools-2.15-irmbw4yfweem5cyeoui5yvdcigt74wb2/spack-bu
             ild-irmbw4y'
  >> 4714    make: *** [Makefile:169: all] Error 2
</pre></details>


### Information on your system

```
* **Spack:** 0.23.0.dev0 (1ac2ee8043ab802ff5c9161ac35c269f22df3c34)
* **Python:** 3.12.3
* **Platform:** darwin-sonoma-m1
* **Concretizer:** clingo
```

### Additional information

_No response_

### General information

- [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:
Maintainers: @albestro @eschnett @msimberg @teonnik
Thanks for that. Also my logs are missing, attached here:
[spack-build-env.txt](https://github.com/spack/spack/files/15367359/spack-build-env.txt)
[spack-build-out.txt](https://github.com/spack/spack/files/15367360/spack-build-out.txt)

Considering the [message printed](https://github.com/gperftools/gperftools/blob/de12f89d2a5b88a0ebf88c0700981751afaf90c7/CMakeLists.txt#L914) by `gperftools` when configuring using CMake

```
CMake Warning at CMakeLists.txt:1486 (message):
  note: gperftools' cmake support is incomplete and is best-effort only
```

it might be an option to change the default to `autotools`.
I can't test if this helps (because I'm not on a mac, and it builds ok on linux), but it may also help to disable building benchmarks (and perhaps tests as well, since they seem to be enabled by default):
```diff
diff --git a/var/spack/repos/builtin/packages/gperftools/package.py b/var/spack/repos/builtin/packages/gperftools/package.py
index 144ac5ddc8..7ec0d3e531 100644
--- a/var/spack/repos/builtin/packages/gperftools/package.py
+++ b/var/spack/repos/builtin/packages/gperftools/package.py
@@ -68,6 +68,7 @@ def cmake_args(self):
             ),
             self.define_from_variant("GPERFTOOLS_BUILD_DEBUGALLOC", "debugalloc"),
             self.define_from_variant("gperftools_enable_libunwind", "libunwind"),
+            self.define("gperftools_build_benchmark", False),
+            self.define("BUILD_TESTING", False),
         ]
```
I don't know if that just postpones the failure, and it obviously doesn't fix the underlying issue, but may be worth trying? It might be a good idea to report this upstream, even if it's "best-effort".
We moved from autotools to cmake in fd68f8916ca7243380639a539c3348aae87b53a3 because of accidental linking to system libunwind.

However, since 82df0e549dbebcc8c10cc91eb505cfd7df293175 that may no longer be an issue.
@msimberg I can confirm that resolves the build issue for me. I built a little toy program and linked in the allocator and it "Seemed To Work For Me" without a segfault. I think this is a better than reverting to `autotools`. To @albestro's point I note that the cmake build doesn't seem to produce any cmake config files :( 

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/gperftools/package.py b/var/spack/repos/builtin/packages/gperftools/package.py
--- a/var/spack/repos/builtin/packages/gperftools/package.py
+++ b/var/spack/repos/builtin/packages/gperftools/package.py
@@ -68,6 +68,8 @@ def cmake_args(self):
             ),
             self.define_from_variant("GPERFTOOLS_BUILD_DEBUGALLOC", "debugalloc"),
             self.define_from_variant("gperftools_enable_libunwind", "libunwind"),
+            self.define("gperftools_build_benchmark", False),
+            self.define("BUILD_TESTING", False),
         ]
 
 


Test patch: