Force find_package to fail if required library type (shared vs. static) is not found

Note: a similar issue is described in Choose between shared/static library after find_package() but the answer is inadequate for some scenarios in my opinion.

Near trivial task: I want to link a shared library against dependencies installed on the system.

Now, to my understanding, CMake target_link_libraries does not support linking a shared library against static dependencies (at least I got linker errors when I last tried).

So - I want find_package to fail if the installed dependency is a static library, when BUILD_SHARED_LIBS=ON.

Edit: The solution should also work for MinGW where it appears shared libraries are split into a .dll and a .dll.a file. I would expect find_package to find the shared library so that I can perform a get_target_property for TYPE on the found package and the result is SHARED_LIBRARY

How would I best accomplish that?

This is not the meaning of this variable. It just defines shared/static for libraries that your project builds (if not set to a fixed type)

Linking to static libraries is definitely supported. However, you also have to know all transitive libraries or use something like pkg-config or cmake config files.

On some environments, you cannot always easily tell if the lib is shared or static and the property might be UNKNOWN.

Linking to static libraries is definitely supported. However, you also have to know all transitive libraries or use something like pkg-config or cmake config files.

That’s not my definition of “supported” - if I have to do a lot of workarounds myself in order to accomplish what normally is done with a single line target_link_libraries, then it’s not supported by cmake to link a shared library against static dependencies.

This is what I am asking: How do I ensure a controlled error when the user sets BUILD_SHARED_LIBS=ON, but the local system has at least one dependency only as a STATIC_LIBRARY?

The controlled error will look much cleaner than tons of linker errors during the build stage as it happened last time I had this scenario.