Repository: spack/spack

Issue:
Installation issue: [MyBayes] serf cannot find libuuid
We are trying to build MyBayes with containerize, and hitting several [build errors](https://github.com/autamus/registry/pull/66/checks?check_run_id=2114274745):

Specifically, it looks like we cannot build serf because uuid is missing. It's a dependency for MrBayes, but not serf.

```bash
           -laprutil-1 -lexpat -liconv
  >> 89    /usr/bin/ld: cannot find -luuid
  >> 90    collect2: error: ld returned 1 exit status
     91    scons: *** [libserf-1.so.1.3.0] Error 1
     92    scons: building terminated because of errors.

See build log for details:
  /tmp/root/spack-stage/spack-stage-serf-1.3.9-kfnmuhyiolrxhuvgpqnuq4xhhsucthiw/spack-build-out.txt
```
The spack.yaml for containerize is:

```yaml
spack:
  specs: ["mrbayes"]
  view: true
  container:
    os_packages:
      build:
        - python3-dev
      final:
        - ca-certificates
    strip: false
```


Discussion:
Just hit this on Ubuntu 20.04, also see #21672. Will try to fix this today.

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/apr-util/package.py b/var/spack/repos/builtin/packages/apr-util/package.py
--- a/var/spack/repos/builtin/packages/apr-util/package.py
+++ b/var/spack/repos/builtin/packages/apr-util/package.py
@@ -32,6 +32,14 @@ class AprUtil(AutotoolsPackage):
     depends_on('sqlite', when='+sqlite')
     depends_on('unixodbc', when='+odbc')
 
+    @property
+    def libs(self):
+        return find_libraries(
+            ['libaprutil-{0}'.format(self.version.up_to(1))],
+            root=self.prefix,
+            recursive=True,
+        )
+
     def configure_args(self):
         spec = self.spec
 
diff --git a/var/spack/repos/builtin/packages/apr/package.py b/var/spack/repos/builtin/packages/apr/package.py
--- a/var/spack/repos/builtin/packages/apr/package.py
+++ b/var/spack/repos/builtin/packages/apr/package.py
@@ -19,3 +19,11 @@ class Apr(AutotoolsPackage):
     patch('missing_includes.patch', when='@1.7.0')
 
     depends_on('uuid', type='link')
+
+    @property
+    def libs(self):
+        return find_libraries(
+            ['libapr-{0}'.format(self.version.up_to(1))],
+            root=self.prefix,
+            recursive=True,
+        )
diff --git a/var/spack/repos/builtin/packages/serf/package.py b/var/spack/repos/builtin/packages/serf/package.py
--- a/var/spack/repos/builtin/packages/serf/package.py
+++ b/var/spack/repos/builtin/packages/serf/package.py
@@ -25,43 +25,42 @@ class Serf(SConsPackage):
     depends_on('apr-util')
     depends_on('openssl')
     depends_on('zlib')
+    depends_on('uuid')
 
     patch('py3syntax.patch')
 
     def build_args(self, spec, prefix):
-        args = [
-            'PREFIX={0}'.format(prefix),
-            'APR={0}'.format(spec['apr'].prefix),
-            'APU={0}'.format(spec['apr-util'].prefix),
-            'OPENSSL={0}'.format(spec['openssl'].prefix),
-            'ZLIB={0}'.format(spec['zlib'].prefix),
-        ]
-
-        # ZLIB variable is ignored on non-Windows platforms before and
-        # including the version 1.3.9:
-        # https://www.mail-archive.com/dev@serf.apache.org/msg01359.html
-        # The issue is fixed in the trunk. Hopefully, the next stable version
-        # will work properly.
-        if '@:1.3.9' in self.spec:
-            zlib_spec = self.spec['zlib']
-            link_flags = [zlib_spec.libs.search_flags]
-            link_flags.extend([self.compiler.cc_rpath_arg + d
-                               for d in zlib_spec.libs.directories])
-            args.append('LINKFLAGS=' + ' '.join(link_flags))
-            args.append('CPPFLAGS=' + zlib_spec.headers.cpp_flags)
-
-        if '+debug' in spec:
-            args.append('DEBUG=yes')
-        else:
-            args.append('DEBUG=no')
+        args = {
+            'PREFIX': prefix,
+            'APR': spec['apr'].prefix,
+            'APU': spec['apr-util'].prefix,
+            'OPENSSL': spec['openssl'].prefix,
+            'ZLIB': spec['zlib'].prefix,
+            'DEBUG': 'yes' if '+debug' in spec else 'no',
+        }
 
         # SCons doesn't pass Spack environment variables to the
         # execution environment. Therefore, we can't use Spack's compiler
         # wrappers. Use the actual compilers. SCons seems to RPATH things
         # on its own anyway.
-        args.append('CC={0}'.format(self.compiler.cc))
+        args['CC'] = self.compiler.cc
+
+        # Old versions of serf ignore the ZLIB variable on non-Windows platforms.
+        # Also, there is no UUID variable to specify its installation location.
+        # Pass explicit link flags for both.
+        library_dirs = []
+        include_dirs = []
+        for dep in spec.dependencies(deptype='link'):
+            query = self.spec[dep.name]
+            library_dirs.extend(query.libs.directories)
+            include_dirs.extend(query.headers.directories)
+
+        rpath = self.compiler.cc_rpath_arg
+        args['LINKFLAGS'] = '-L' + ' -L'.join(library_dirs)
+        args['LINKFLAGS'] += ' ' + rpath + (' ' + rpath).join(library_dirs)
+        args['CPPFLAGS'] = '-I' + ' -I'.join(include_dirs)
 
-        return args
+        return [key + '=' + value for key, value in args.items()]
 
     def build_test(self):
         # FIXME: Several test failures:


Test patch: