Repository: spack/spack

Issue:
Installation issue: libint with Intel compilers
I am attempting to build CP2K using the Intel 2020/2018 compilers, but Libint fails to build correctly. I have been able to successfully build Libint using gcc@9.3.0.

There are a few warnings in the error output included below; these can be removed by changing the optimization flags and do not affect the error occurring after line 5560.

### Steps to reproduce the issue

```console
$ spack install libint@2.6.0%intel@18.0.5+fortran tune=cp2k-lmax-4 arch=linux-rhel7-haswell
...
==> 6029: Installing libint
==> Fetching https://github.com/evaleev/libint/archive/v2.6.0.tar.gz
######################################################################## 100.0%
==> Staging archive: /tmp/user/spack-stage/spack-stage-libint-2.6.0-sxoiyujczgjjleas6fc67qkddqxqem74/v2.6.0.tar.gz
==> Created stage in /tmp/user/spack-stage/spack-stage-libint-2.6.0-sxoiyujczgjjleas6fc67qkddqxqem74
==> No patches needed for libint
==> 6029: libint: Building libint [AutotoolsPackage]
==> 6029: libint: Executing phase: 'autoreconf'
==> 6029: libint: Executing phase: 'configure'
==> 6029: libint: Executing phase: 'build'
==> 6029: libint: Executing phase: 'install'
==> Error: ProcessError: Command exited with status 2:
    'make' '-j4' 'install'

3 errors found in build log:
     5555    FC fortran_example.o
     5556    /bin/sh   ../libtool --quiet --mode=link --tag=CXX      /opt/spack/lib/spack/env/intel/icpc -O2 -xSSE4.2 -axAVX,CORE-AVX2 -ipo -O2 
             -xSSE4.2 -axAVX,CORE-AVX2 -ipo   -L../lib -lint2 -lm   -L/opt/rhel7/gcc/6.5.0/lib64 -L/opt/rhel7/gcc/6.5.0/lib -L/opt/rhe
             l7/intel/parallel_studio_xe_2018_update4_cluster_edition/compilers_and_libraries_2018.5.274/linux/compiler/lib/intel64_lin -L/opt/r
             hel7/gcc/6.5.0/lib64/../lib64 -L/opt/rhel7/gcc/6.5.0/lib64/../lib64/ -L/opt/rhel7/gcc/6.5.0/lib/../lib64 -L/opt/rhel7/gcc
             /6.5.0/lib/../lib64/ -L/opt/rhel7/gcc/6.5.0/lib/gcc/x86_64-pc-linux-gnu/6.5.0/ -L/opt/rhel7/gcc/6.5.0/lib/gcc/x86_64-pc-linux-
             gnu/6.5.0/../../../../lib64 -L/opt/rhel7/gcc/6.5.0/lib/gcc/x86_64-pc-linux-gnu/6.5.0/../../../../lib64/ -L/lib/../lib64 -L/lib/../l
             ib64/ -L/usr/lib/../lib64 -L/usr/lib/../lib64/ -L/opt/rhel7/gcc/6.5.0/lib64/ -L/opt/rhel7/gcc/6.5.0/lib/ -L/opt/rhel7/gcc
             /6.5.0/lib/gcc/x86_64-pc-linux-gnu/6.5.0/../../../ -L/lib64 -L/lib/ -L/usr/lib64 -L/usr/lib -lifport -lifcoremt -limf -lsvml -lm -lipgo 
             -lirc -lpthread -lirc_s -ldl -o fortran_example fortran_example.o libint_f.o
     5557    icpc: command line warning #10121: overriding '-march=haswell' with '-xSSE4.2'
     5558    ipo: warning #11003: no IR in object file fortran_example.o; was the source file compiled with -ipo
     5559    ipo: warning #11003: no IR in object file libint_f.o; was the source file compiled with -ipo
     5560    /lib/../lib64/crt1.o: In function `_start':
  >> 5561    (.text+0x20): undefined reference to `main'
  >> 5562    make[1]: *** [fortran_example] Error 1
     5563    make[1]: Leaving directory `/tmp/user/spack-stage/spack-stage-libint-2.6.0-sxoiyujczgjjleas6fc67qkddqxqem74/spack-src/generated/fortr
             an'
  >> 5564    make: *** [fortran] Error 1

```

### Information on your system

* **Spack:** 0.15.0-49-b3bc538
* **Python:** 3.6.8
* **Platform:** linux-rhel7-haswell


### Additional information

* [spack-build-env.txt](https://github.com/spack/spack/files/4914382/spack-build-env.txt)
* [spack-build-out.txt](https://github.com/spack/spack/files/4914383/spack-build-out.txt)


### 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:

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/cp2k/package.py b/var/spack/repos/builtin/packages/cp2k/package.py
--- a/var/spack/repos/builtin/packages/cp2k/package.py
+++ b/var/spack/repos/builtin/packages/cp2k/package.py
@@ -193,7 +193,12 @@ def edit(self, spec, prefix):
             fftw_header_dir = fftw.headers.directories[0] + '/fftw'
         elif '^intel-parallel-studio+mkl' in spec:
             fftw = spec['intel-parallel-studio']
-            fftw_header_dir = fftw.headers.directories[0] + '/fftw'
+            fftw_header_dir = '<NOTFOUND>'
+            for incdir in [join_path(f, 'fftw')
+                           for f in fftw.headers.directories]:
+                if os.path.exists(incdir):
+                    fftw_header_dir = incdir
+                    break
 
         optimization_flags = {
             'gcc': [
diff --git a/var/spack/repos/builtin/packages/libint/package.py b/var/spack/repos/builtin/packages/libint/package.py
--- a/var/spack/repos/builtin/packages/libint/package.py
+++ b/var/spack/repos/builtin/packages/libint/package.py
@@ -78,10 +78,10 @@ def autoreconf(self, spec, prefix):
     @property
     def optflags(self):
         flags = '-O2'
-        # Optimizations for the Intel compiler, suggested by CP2K
-        # See ../libxc/package.py for rationale and doc.
-        if '%intel' in self.spec:
-            flags += ' -xSSE4.2 -axAVX,CORE-AVX2 -ipo'
+
+        # microarchitecture-specific optimization flags should be controlled
+        # by Spack, otherwise we may end up with contradictory or invalid flags
+        # see https://github.com/spack/spack/issues/17794
 
         return flags
 
@@ -99,18 +99,16 @@ def configure_args(self):
 
         config_args = ['--enable-shared']
 
-        optflags = self.optflags
-
         # Optimization flag names have changed in libint 2
         if self.version < Version('2.0.0'):
             config_args.extend([
-                '--with-cc-optflags={0}'.format(optflags),
-                '--with-cxx-optflags={0}'.format(optflags)
+                '--with-cc-optflags={0}'.format(self.optflags),
+                '--with-cxx-optflags={0}'.format(self.optflags)
             ])
         else:
             config_args.extend([
-                '--with-cxx-optflags={0}'.format(optflags),
-                '--with-cxxgen-optflags={0}'.format(optflags)
+                '--with-cxx-optflags={0}'.format(self.optflags),
+                '--with-cxxgen-optflags={0}'.format(self.optflags)
             ])
 
         # Options required by CP2K, removed in libint 2
@@ -199,6 +197,6 @@ def install(self, spec, prefix):
     def patch(self):
         # Use Fortran compiler to link the Fortran example, not the C++
         # compiler
-        if '+fortran' in self.spec and self.spec.satisfies('%nvhpc'):
+        if '+fortran' in self.spec:
             filter_file('$(CXX) $(CXXFLAGS)', '$(FC) $(FCFLAGS)',
                         'export/fortran/Makefile', string=True)
diff --git a/var/spack/repos/builtin/packages/libxc/package.py b/var/spack/repos/builtin/packages/libxc/package.py
--- a/var/spack/repos/builtin/packages/libxc/package.py
+++ b/var/spack/repos/builtin/packages/libxc/package.py
@@ -64,37 +64,18 @@ def libs(self):
         )
 
     def setup_build_environment(self, env):
-        optflags = '-O2'
-        if self.compiler.name == 'intel':
-            # Optimizations for the Intel compiler, suggested by CP2K
-            #
-            # Note that not every lowly login node has advanced CPUs:
-            #
-            #   $ icc  -xAVX -axCORE-AVX2 -ipo hello.c
-            #   $ ./a.out
-            #   Please verify that both the operating system and the \
-            #   processor support Intel(R) AVX instructions.
-            #
-            # NB: The same flags are applied in:
-            #   - ../libint/package.py
-            #
-            # Related:
-            #   - ../fftw/package.py        variants: simd, fma
-            #   - ../c-blosc/package.py     variant:  avx2
-            #   - ../r-rcppblaze/package.py AVX* in "info" but not in code?
-            #   - ../openblas/package.py    variants: cpu_target!?!
-            #   - ../cp2k/package.py
-            #
-            # Documentation at:
-            # https://software.intel.com/en-us/cpp-compiler-18.0-developer-guide-and-reference-ax-qax
-            #
-            optflags += ' -xSSE4.2 -axAVX,CORE-AVX2 -ipo'
-            if which('xiar'):
-                env.set('AR', 'xiar')
+        # microarchitecture-specific optimization flags should be controlled
+        # by Spack, otherwise we may end up with contradictory or invalid flags
+        # see https://github.com/spack/spack/issues/17794
+        # libxc on the other hand only sets the generic -O2 when it detects GCC
 
+        optflags = '-O2'
         env.append_flags('CFLAGS',  optflags)
         env.append_flags('FCFLAGS', optflags)
 
+        if '%intel' in self.spec and which('xiar'):
+            env.set('AR', 'xiar')
+
         if '+cuda' in self.spec:
             nvcc = self.spec['cuda'].prefix.bin.nvcc
             env.set('CCLD', '{0} -ccbin {1}'.format(nvcc, spack_cc))


Test patch: