Repository: spack/spack

Issue:
py-pillow doesn't find jpeg even if spack finds libjpeg-turbo
### Expected Result

py-pillow is installed 

### Actual Result

spack installation for py-pillow fails as it doesn't find jpeg

### Steps to reproduce the issue

```
[sajid@apsxrmd-0002 spack]$ spack install py-pillow@5.1.0+freetype+lcms+tiff
==> bison@2.7 : externally installed in /usr
==> bison@2.7 : already registered in DB
==> bzip2 is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/bzip2-1.0.6-elwi6mipwwydc72dpxk2tyu277cizzgd
==> pkgconf is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/pkgconf-1.4.2-3lkoghcr4aenwhsz2kunjmu5ekrxrir2
==> xz is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/xz-5.2.4-vd7dt2scvbaxjlk7iz5xdmtcuhvj4i5p
==> zlib is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw
==> libxml2 is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libxml2-2.9.8-bbyhxe74nonq4dhfzbl6czgysmysi7ky
==> ncurses is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/ncurses-6.1-rpa47v3lwu5vo44yrneqmjk4qc2xsky7
==> tar is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/tar-1.30-v53jjvdof23fwe3cmkgt6gsl62cv5n4h
==> gettext is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/gettext-0.19.8.1-zbnm2mjy7r53kr4zyrimlt72t7wthq7q
==> help2man is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/help2man-1.47.4-xjjglcoxxzrfhph6z6epyndbke4sx5a5
==> m4@1.4.16 : externally installed in /usr
==> m4@1.4.16 : already registered in DB
==> flex is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/flex-2.6.3-7n4ocnhgsjalvj44gx3rlkp2ynrmf5va
==> binutils is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/binutils-2.29.1-hgsc3bl6m2orzrirn2inntvexqqpnojn
==> libpng is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libpng-1.6.34-gy6gvg4x4yifhx3ig4vgrpatcdlkyn47
==> freetype is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk
==> openssl@1.0.1e : externally installed in /usr
==> openssl@1.0.1e : already registered in DB
==> cmake is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/cmake-3.10.2-3xxofe2j2z5gfel3wvrpozuwco7srsew
==> nasm is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/nasm-2.13.03-hyny7kqxmr3rlk3gbu5k3uwsava7chyx
==> libjpeg-turbo is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb
==> libtiff is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w
==> lcms is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3
==> readline is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/readline-7.0-2753dh2vx3rmzyqyqvlkyyp4imu66klm
==> gdbm is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/gdbm-1.14.1-tfb7qsoosdfw64cw5ls5nkyfqsqr3nfa
==> sqlite is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/sqlite-3.23.1-weaj3n24nozti46hbk2ij3kkfej4p5av
==> python is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n
==> py-setuptools is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-setuptools-39.0.1-bjdhpcozafhiy2nofvtiy3wg6tyrv3zm
==> Installing py-pillow
==> Warning: There is no checksum on file to fetch py-pillow@5.1.0 safely.
==>   Fetch anyway? [y/N] y
==> Fetching with no checksum.
  Add a checksum or use --no-checksum to skip this check.
==> Fetching https://pypi.io/packages/source/P/Pillow/Pillow-5.1.0.tar.gz
######################################################################## 100.0%
==> Staging archive: /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/Pillow-5.1.0.tar.gz
==> Created stage in /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc
==> Ran patch() for py-pillow
==> Building py-pillow [PythonPackage]
==> Executing phase: 'build_ext'
==> Error: ProcessError: Command exited with status 1:
    '/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/bin/python3.6' '-s' 'setup.py' '--no-user-cfg' 'build_ext' '--enable-jpeg' '--enable-zlib' '--enable-tiff' '--enable-freetype' '--enable-lcms' '--disable-jpeg2000' '--rpath=/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib64:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib64'
See build log for details:
  /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/Pillow-5.1.0/spack-build.out
```

### Build Log
```
==> Executing phase: 'build_ext'
==> '/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/bin/python3.6' '-s' 'setup.py' '--no-user-cfg' 'build_ext' '--enable-jpeg' '--enable-zlib' '--enable-tiff' '--enable-freetype' '--enable-lcms' '--disable-jpeg2000' '--rpath=/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib64:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib64'
running build_ext


The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

Traceback (most recent call last):
  File "setup.py", line 794, in <module>
    zip_safe=not (debug_build() or PLATFORM_MINGW), )
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-setuptools-39.0.1-bjdhpcozafhiy2nofvtiy3wg6tyrv3zm/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "setup.py", line 582, in build_extensions
    raise RequiredDependencyException(f)
__main__.RequiredDependencyException: jpeg

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 806, in <module>
    raise RequiredDependencyException(msg)
__main__.RequiredDependencyException:

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html
```

I believe this is related to [8102](https://github.com/spack/spack/pull/8102) installing the library in a different location as described in [8160](https://github.com/spack/spack/issues/8160) and requires a fix similar to [8179](https://github.com/spack/spack/pull/8179). I don't know enough about how spack finds libraries and headers to edit package.py. Can anyone explain how to proceed ?


Discussion:
py-pillow has the same path usage that libjpeg-turbo had:
```
        if '+jpeg' in spec:
            setup.filter('JPEG_ROOT = None',
                         'JPEG_ROOT = ("{0}", "{1}")'.format(
                             spec['jpeg'].prefix.lib,
                             spec['jpeg'].prefix.include))
```

If you change that to `spec['jpeg'].libs[0]` does that work?
I don't know if this is what you meant, but I changed it to : 
``` 
if '+jpeg' in spec:
            setup.filter('JPEG_ROOT = None',
                         'JPEG_ROOT=("{0}","{1}")'.format(
                             spec['jpeg'].libs[0],
                             spec['jpeg'].prefix.include))
```
and I still end up with the same error.
I can't reproduce this, sorry.

Can you paste the output of
`spack -v install py-pillow@5.1.0+freetype+lcms+tiff`
with the original recipe?
I'm using ```spec['jpeg'].libs[0]``` instead of ```spec['jpeg'].prefix.lib``` and I've updated spack to include [8179](https://github.com/spack/spack/pull/8179).

( Using intel@18.0.1 on Intel Xeon Phi 7250. )

Here is the output as requested : 
```
[sajid@apsxrmd-0001 ~]$ spack -v install py-pillow@5.1.0+freetype+lcms+tiff
==> bison@2.7 : externally installed in /usr
==> bison@2.7 : already registered in DB
==> bzip2 is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/bzip2-1.0.6-elwi6mipwwydc72dpxk2tyu277cizzgd
==> pkgconf is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/pkgconf-1.4.2-3lkoghcr4aenwhsz2kunjmu5ekrxrir2
==> xz is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/xz-5.2.4-vd7dt2scvbaxjlk7iz5xdmtcuhvj4i5p
==> zlib is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw
==> libxml2 is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libxml2-2.9.8-bbyhxe74nonq4dhfzbl6czgysmysi7ky
==> ncurses is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/ncurses-6.1-rpa47v3lwu5vo44yrneqmjk4qc2xsky7
==> tar is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/tar-1.30-v53jjvdof23fwe3cmkgt6gsl62cv5n4h
==> gettext is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/gettext-0.19.8.1-zbnm2mjy7r53kr4zyrimlt72t7wthq7q
==> help2man is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/help2man-1.47.4-xjjglcoxxzrfhph6z6epyndbke4sx5a5
==> m4@1.4.16 : externally installed in /usr
==> m4@1.4.16 : already registered in DB
==> flex is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/flex-2.6.3-7n4ocnhgsjalvj44gx3rlkp2ynrmf5va
==> binutils is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/binutils-2.29.1-hgsc3bl6m2orzrirn2inntvexqqpnojn
==> libpng is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libpng-1.6.34-gy6gvg4x4yifhx3ig4vgrpatcdlkyn47
==> freetype is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk
==> openssl@1.0.1e : externally installed in /usr
==> openssl@1.0.1e : already registered in DB
==> cmake is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/cmake-3.10.2-3xxofe2j2z5gfel3wvrpozuwco7srsew
==> nasm is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/nasm-2.13.03-hyny7kqxmr3rlk3gbu5k3uwsava7chyx
==> libjpeg-turbo is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb
==> libtiff is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w
==> lcms is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3
==> readline is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/readline-7.0-2753dh2vx3rmzyqyqvlkyyp4imu66klm
==> gdbm is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/gdbm-1.14.1-tfb7qsoosdfw64cw5ls5nkyfqsqr3nfa
==> sqlite is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/sqlite-3.23.1-weaj3n24nozti46hbk2ij3kkfej4p5av
==> python is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n
==> py-setuptools is already installed in /blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-setuptools-39.0.1-bjdhpcozafhiy2nofvtiy3wg6tyrv3zm
==> Installing py-pillow
==> Warning: There is no checksum on file to fetch py-pillow@5.1.0 safely.
==>   Fetch anyway? [y/N] t
==>   Fetch anyway? [y/N] y
==> Fetching with no checksum.
  Add a checksum or use --no-checksum to skip this check.
==> Fetching https://pypi.io/packages/source/P/Pillow/Pillow-5.1.0.tar.gz
######################################################################## 100.0%
==> Staging archive: /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/Pillow-5.1.0.tar.gz
==> Created stage in /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc
==> Ran patch() for py-pillow
==> Building py-pillow [PythonPackage]
==> Executing phase: 'build_ext'
==> '/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/bin/python3.6' '-s' 'setup.py' '--no-user-cfg' 'build_ext' '--enable-jpeg' '--enable-zlib' '--enable-tiff' '--enable-freetype' '--enable-lcms' '--disable-jpeg2000' '--rpath=/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib64:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib64'
running build_ext


The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html

Traceback (most recent call last):
  File "setup.py", line 794, in <module>
    zip_safe=not (debug_build() or PLATFORM_MINGW), )
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-setuptools-39.0.1-bjdhpcozafhiy2nofvtiy3wg6tyrv3zm/lib/python3.6/site-packages/setuptools/__init__.py", line 129, in setup
    return distutils.core.setup(**attrs)
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib/python3.6/distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "setup.py", line 582, in build_extensions
    raise RequiredDependencyException(f)
__main__.RequiredDependencyException: jpeg

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 806, in <module>
    raise RequiredDependencyException(msg)
__main__.RequiredDependencyException:

The headers or library files could not be found for jpeg,
a required dependency when compiling Pillow from source.

Please see the install instructions at:
   https://pillow.readthedocs.io/en/latest/installation.html


==> Error: ProcessError: Command exited with status 1:
    '/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/bin/python3.6' '-s' 'setup.py' '--no-user-cfg' 'build_ext' '--enable-jpeg' '--enable-zlib' '--enable-tiff' '--enable-freetype' '--enable-lcms' '--disable-jpeg2000' '--rpath=/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/lib64:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/freetype-2.7.1-4vdnp2d6cahfcl6saxpvfvfdz6xucqvk/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/zlib-1.2.11-rxnhb4ttcijbf62keegdygnx6noh5cxw/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/python-3.6.5-oz2gaotbbnlmrgb76uq2gt7nx5wnbi2n/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libtiff-4.0.9-tpeqxbc6ijle7brt6wgvkljaw4zoim5w/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/lcms-2.9-54avoopvpfhrpmlyqrxwufn33thp7ai3/lib:/blues/gpfs/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib64'
See build log for details:
  /blues/gpfs/home/sajid/packages/spack/var/spack/stage/py-pillow-5.1.0-s6vnuajpcydiumbotojxad4zu74otpyc/Pillow-5.1.0/spack-build.out
```
One thing stuck out:
`libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib64`
instead of 
`libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb/lib`
so it is probably worth checking the rest of the libraries for lib64 vs lib.

Other than that and making sure the include directories that are being passed in are correct, I'm really not sure where the problem would be.
Seems right because there's no ```lib``` folder in the installed directory : 

```
[sajid@apsxrmd-0001 libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb]$ pwd
/home/sajid/packages/spack/opt/spack/linux-centos7-x86_64/intel-18.0.1/libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb
[sajid@apsxrmd-0001 libjpeg-turbo-1.5.90-rfrpz7g33kgrzwpu52yobb7fvdy7mhvb]$ ls
bin  include  lib64  share
```
The issue is with libjpeg-turbo since pillow isn't able to find the jpeg headers. I'll check the other libraries as well. 
Fixed it. 
```
if '+jpeg' in spec:
            setup.filter('JPEG_ROOT = None',
                         'JPEG_ROOT=("{0}","{1}")'.format(
                             spec['jpeg'].prefix.lib64,
                             spec['jpeg'].prefix.include))
```
Works now. 

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/py-pillow/package.py b/var/spack/repos/builtin/packages/py-pillow/package.py
--- a/var/spack/repos/builtin/packages/py-pillow/package.py
+++ b/var/spack/repos/builtin/packages/py-pillow/package.py
@@ -33,17 +33,10 @@ class PyPillow(PythonPackage):
     capabilities."""
 
     homepage = "https://python-pillow.org/"
-    url = "https://pypi.io/packages/source/P/Pillow/Pillow-3.0.0.tar.gz"
+    url = "https://pypi.io/packages/source/P/Pillow/Pillow-5.1.0.tar.gz"
 
-    # TODO: This version should be deleted once the next release comes out.
-    # TODO: It fixes a bug that prevented us from linking to Tk/Tcl.
-    # TODO: Tk/Tcl support is necessary for tkinter bitmap and photo images.
-    # TODO: If you require this support, run the following command:
-    # TODO:     `spack install py-pillow@3.3.0.dev0 ^python+tk`
-    version('3.3.0.dev0', git='https://github.com/python-pillow/Pillow.git',
-            commit='30eced62868141a6c859a4370efd40b9434a7c3f')
-
-    version('3.2.0', '7cfd093c11205d9e2ebe3c51dfcad510', preferred=True)
+    version('5.1.0', '04d2b1d1ce8b9f6831e33fb85b34316d')
+    version('3.2.0', '7cfd093c11205d9e2ebe3c51dfcad510')
     version('3.0.0', 'fc8ac44e93da09678eac7e30c9b7377d')
 
     provides('pil')
@@ -54,8 +47,7 @@ class PyPillow(PythonPackage):
     variant('tiff', default=False, description='Access to TIFF files')
     variant('freetype', default=False, description='Font related services')
     variant('lcms', default=False, description='Color management')
-    variant('jpeg2000', default=False,
-            description='Provide JPEG 2000 functionality')
+    variant('jpeg2000', default=False, description='Provide JPEG 2000 functionality')
 
     # Spack does not (yet) support these modes of building
     # variant('webp', default=False, description='Provide the WebP format')
@@ -94,8 +86,8 @@ def patch(self):
 
         if '+jpeg' in spec:
             setup.filter('JPEG_ROOT = None',
-                         'JPEG_ROOT = ("{0}", "{1}")'.format(
-                             spec['jpeg'].prefix.lib,
+                         'JPEG_ROOT=("{0}","{1}")'.format(
+                             spec['jpeg'].libs.directories[0],
                              spec['jpeg'].prefix.include))
         if '+zlib' in spec:
             setup.filter('ZLIB_ROOT = None',


Test patch: