Repository: spack/spack

Issue:
Installation issue: py-matplotlib fails to build on macOS, "error: Failed to download FreeType."
py-matplotlib fails to build on macOS because it "Failed to download FreeType." However, the freetype package has already been installed.

<!-- Thanks for taking the time to report this build failure. To proceed with the report please:

1. Title the issue "Installation issue: <name-of-the-package>".
2. Provide the information required below.

We encourage you to try, as much as possible, to reduce your problem to the minimal example that still reproduces the issue. That would help us a lot in fixing it quickly and effectively! -->

### Steps to reproduce the issue

<!-- Fill in the exact spec you are trying to build and the relevant part of the error message -->
```console
$ spack install py-matplotlib
==> Warning: apple-clang@10.0.1 cannot build optimized binaries for "skylake". Using best target possible: "x86_64"
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/libiconv-1.16-ausgetpcabhkhefhml7aohux6ywzdtnp
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/zlib-1.2.11-27722zxlpjz5kk2eyomjdixx6rw2gjm7
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/pkgconf-1.7.3-g5njv6xazw2xnltpt3kgjord7rn2rt55
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/expat-2.2.9-fn2cnfvzzoodgh573cwige6d6u433k4q
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/xz-5.2.5-cfdn64lpkwbcrgruvfzkj4sqgyz6bpum
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/libffi-3.3-wjergkhxn7xikjtuqgzgco2brz6ogowv
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/openblas-0.3.10-vguqquz6767z6vx4nzs47agxwbxvlk47
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/nasm-2.14.02-ds2jfe2x7tpuk6bqh62q2tpwgdgoxmwu
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/tar-1.32-co74oe3mmug7jdjaxqfuncqkrcgxlc5b
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/diffutils-3.7-hml22naokf2d474rkgjp4smkvlim6kcy
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/libpng-1.6.37-bqoqngpvyp7yhy3fqcgfe6gvxntjcoa3
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/ncurses-6.2-tvko6avssxtp43nb7btahd6b77vn6ml5
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/libxml2-2.9.10-sai2i6ahxsutclprkmj67oxqyykejuzu
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/bzip2-1.0.8-xoruqziz2ittnufh3n3m5phtfm5jclk6
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/readline-8.0-b5r5sazll3rr4bbvwub7rznbd7xjms36
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/gettext-0.20.2-ea7k7glpbeiifvpnbk55svwokjkoy3ds
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/freetype-2.10.1-mcvpnvk6dtevwjkfbivg2fasywixndil
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/gdbm-1.18.1-2cpvevcbskefhgykghcanf2xnqwmrhle
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/sqlite-3.31.1-extjulizcpa6ofsmbzrydu7rrlzqknj4
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/perl-5.30.3-incuoxl77doouovatpilrteacfkoeu7a
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/openssl-1.1.1g-g5gqph3dznwtuthinoqikv3kd5qyrvhm
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/python-3.7.7-cfsuwuswvhk664rhmrcdfyp5rjau2amf
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/cmake-3.17.3-drekp4wvfzxvaeimoqldjpymfn7hcwfc
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-setuptools-46.1.3-patdra3bpzj4vc5lcwfpw3skxjiihi3r
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-six-1.14.0-kegosqburh7jsnsuurryyu6ys5q4xhj4
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/libjpeg-turbo-2.0.4-htupumbx6sajeukwsmy4dwnjihubwlpu
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-setuptools-scm-4.1.2-tehxfqitujedxxpjby7oytt6cfvfrmbf
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-kiwisolver-1.1.0-iipg2j4e26jdzf5gq3r3wbhfqaza7epd
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-pyparsing-2.4.2-qwu6o7tizv4k7luthu6cxu5pg4tefvoc
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-cython-0.29.16-hpampgiaiihk5rdjjgvtqbyi3l4jztym
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-cycler-0.10.0-bd456f2coxae36l5rzhhojyppumogzrt
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-pillow-7.0.0-7wljqekj52p4joarvmdrzqu5j3v2bc6j
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-python-dateutil-2.8.0-or55z3ahv3n5tzmrhbpvdschkj5k4ckz
[+] /Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/py-numpy-1.19.0-3y6mfyxmwdq4csswicypdrx23u6ex6fy
==> 52114: Installing py-matplotlib
==> Finding buildcaches at https://spack-llnl-mirror.s3-us-west-2.amazonaws.com/build_cache
==> Fetching https://spack-llnl-mirror.s3-us-west-2.amazonaws.com/build_cache/darwin-mojave-x86_64-apple-clang-10.0.1-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp.spec.yaml
curl: (22) The requested URL returned error: 404 Not Found                                                                                                                                                 

==> Failed to fetch file from URL: https://spack-llnl-mirror.s3-us-west-2.amazonaws.com/build_cache/darwin-mojave-x86_64-apple-clang-10.0.1-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp.spec.yaml
    URL https://spack-llnl-mirror.s3-us-west-2.amazonaws.com/build_cache/darwin-mojave-x86_64-apple-clang-10.0.1-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp.spec.yaml was not found!
==> Fetching from https://spack-llnl-mirror.s3-us-west-2.amazonaws.com/build_cache/darwin-mojave-x86_64-apple-clang-10.0.1-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp.spec.yaml failed.
==> Warning: microarchitecture specific optimizations are not supported yet on mixed compiler toolchains [check apple-clang@10.0.1 for further details]
==> Using cached archive: /Users/balos1/.local/src/spack/var/spack/cache/_source-cache/archive/24/24e8db94948019d531ce0bcd637ac24b1c8f6744ac86d2aa0eb6dbaeb1386f82.tar.gz
==> Staging archive: /var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T/balos1/spack-stage/spack-stage-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp/matplotlib-3.3.0.tar.gz
==> Created stage in /var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T/balos1/spack-stage/spack-stage-py-matplotlib-3.3.0-btyth6mh52sdyjmzon7fa74xcgm7jdnp
==> No patches needed for py-matplotlib
==> 52114: py-matplotlib: Building py-matplotlib [PythonPackage]
==> 52114: py-matplotlib: Executing phase: 'build'
==> Error: ProcessError: Command exited with status 1:
    '/Users/balos1/.local/src/spack/opt/spack/darwin-mojave-x86_64/apple-clang-10.0.1/python-3.7.7-cfsuwuswvhk664rhmrcdfyp5rjau2amf/bin/python3.7' '-s' 'setup.py' '--no-user-cfg' 'build'

1 error found in build log:
     572    creating var/folders/hz
     573    creating var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx
     574    creating var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T
     575    /Users/balos1/.local/src/spack/lib/spack/env/clang/clang++ -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/Users/balos1/.local/src/spack/opt/spack/
            darwin-mojave-x86_64/apple-clang-10.0.1/python-3.7.7-cfsuwuswvhk664rhmrcdfyp5rjau2amf/include/python3.7m -c /var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmp2wum3a10.cpp -o var/folders/hz/
            kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmp2wum3a10.o -fvisibility=hidden
     576    /Users/balos1/.local/src/spack/lib/spack/env/clang/clang++ -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/Users/balos1/.local/src/spack/opt/spack/
            darwin-mojave-x86_64/apple-clang-10.0.1/python-3.7.7-cfsuwuswvhk664rhmrcdfyp5rjau2amf/include/python3.7m -c /var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmp5y0207dm.cpp -o var/folders/hz/
            kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmp5y0207dm.o -fvisibility-inlines-hidden
     577    /Users/balos1/.local/src/spack/lib/spack/env/clang/clang++ -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/Users/balos1/.local/src/spack/opt/spack/
            darwin-mojave-x86_64/apple-clang-10.0.1/python-3.7.7-cfsuwuswvhk664rhmrcdfyp5rjau2amf/include/python3.7m -c /var/folders/hz/kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmpupvv221a.cpp -o var/folders/hz/
            kznrl52d6wx4t1wzg3rm13fr000vsx/T/tmpupvv221a.o -flto
  >> 578    error: Failed to download FreeType. Please download one of ['https://downloads.sourceforge.net/project/freetype/freetype2/2.6.1/freetype-2.6.1.tar.gz', 'https://download.savannah.gnu.org/rele
            ases/freetype/freetype-2.6.1.tar.gz'] and extract it into build/freetype-2.6.1 at the top-level of the source repository.
...
```

### Information on your system

<!-- Please include the output of `spack debug report` -->

```
$ spack debug report
* **Spack:** 0.15.1-182-3949a85f9
* **Python:** 3.6.10
* **Platform:** darwin-mojave-skylake
```

<!-- If you have any relevant configuration detail (custom `packages.yaml` or `modules.yaml`, etc.) you can add that here as well. -->

### Additional information

<!-- Please upload the following files. They should be present in the stage directory of the failing build. Also upload any config.log or similar file if one exists. -->

* [spack-build-out.txt](https://github.com/spack/spack/files/4955461/spack-build-out.txt)
* [spack-build-env.txt](https://github.com/spack/spack/files/4955463/spack-build-env.txt)

<!-- Some packages have maintainers who have volunteered to debug build failures. Run `spack maintainers <name-of-the-package>` and @mention them here if they exist. -->

@adamjstewart 

### General information

<!-- These boxes can be checked by replacing [ ] with [x] or by clicking them after submitting the issue. -->
- [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:
I know what the problem is and how to fix it. Unfortunately my internet went out so I can't submit a PR just yet. Basically, if you look at `setup.cfg.template`, it says that by default, matplotlib downloads and installs its own copy of freetype and qhull unless `system_freetype` and `system_qhull` are True. It looks like this behavior is new as of 3.3, which is why no one noticed it. I'll submit a PR as soon as my internet comes back.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/py-matplotlib/package.py b/var/spack/repos/builtin/packages/py-matplotlib/package.py
--- a/var/spack/repos/builtin/packages/py-matplotlib/package.py
+++ b/var/spack/repos/builtin/packages/py-matplotlib/package.py
@@ -81,7 +81,8 @@ class PyMatplotlib(PythonPackage):
     depends_on('python@2.7:2.8,3.4:', when='@:2', type=('build', 'link', 'run'))
     depends_on('python@3.5:', when='@3:', type=('build', 'link', 'run'))
     depends_on('python@3.6:', when='@3.1:', type=('build', 'link', 'run'))
-    depends_on('freetype@2.3:')
+    depends_on('freetype@2.3:')  # freetype 2.6.1 needed for tests to pass
+    depends_on('qhull@2015.2:', when='@3.3:')
     depends_on('libpng@1.2:')
     depends_on('py-numpy@1.11:', type=('build', 'run'))
     depends_on('py-numpy@1.15:', when='@3.3:', type=('build', 'run'))
@@ -144,7 +145,7 @@ class PyMatplotlib(PythonPackage):
     patch('freetype-include-path.patch', when='@2.2.2:2.9.9')
 
     @run_before('build')
-    def set_backend(self):
+    def configure(self):
         """Set build options with regards to backend GUI libraries."""
 
         backend = self.spec.variants['backend'].value
@@ -154,6 +155,13 @@ def set_backend(self):
             setup.write('[rc_options]\n')
             setup.write('backend = ' + backend + '\n')
 
+            # Starting with version 3.3.0, freetype is downloaded by default
+            # Force matplotlib to use Spack installations of freetype and qhull
+            if self.version >= Version('3.3.0'):
+                setup.write('[libs]\n')
+                setup.write('system_freetype = True\n')
+                setup.write('system_qhull = True\n')
+
     def test(self):
         pytest = which('pytest')
         pytest()


Test patch: