Good practice: using INTERFACE-libraries in FindABC.cmake?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Good practice: using INTERFACE-libraries in FindABC.cmake?

Patrick Boettcher
Hi list,

I came across the INTERFACE-type of libraries when writing a
FindModule.cmake-file for custom libraries installed by my
project.

Here is what I'm doing after having found the libraries and the
determined the paths: LIB1 is the library and LIB1_INCLUDE_DIRS its
include-dirs:

  add_library(name INTERFACE)

  target_link_libraries(name INTERFACE ${LIB1})

  if(FFTW3_FOUND)
  target_include_directories(name INTERFACE
  {$FFTW3_INCLUDE_DIRS})
  target_link_libraries(name INTERFACE ${FFTW3_LIBRARIES} )
  endif()

  target_include_directories(name INTERFACE ${LIB1_INCLUDE_DIRS})

  # need c++11 compile options for if-name
  set_property(TARGET name PROPERTY
                INTERFACE_COMPILE_FEATURES cxx_range_for)

This makes that in the CMakeLists.txt which includes my package-file a
user needing libname for his executable does:

  add_executable(main main.cpp)
  target_link_libraries(main name)

This will

1) set C++11 flags for compilation
2) set the right include-pathes
3) link with the right libraries as per my order

Awesome. But is this a good practice? Are there any pitfalls?

Thank you for your help in advance.

best regards,
--
Patrick.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Good practice: using INTERFACE-libraries in FindABC.cmake?

Nils Gladitz-2
On 02/29/2016 03:34 PM, Patrick Boettcher wrote:

> Hi list,
>
> I came across the INTERFACE-type of libraries when writing a
> FindModule.cmake-file for custom libraries installed by my
> project.
>
> Here is what I'm doing after having found the libraries and the
> determined the paths: LIB1 is the library and LIB1_INCLUDE_DIRS its
> include-dirs:
>
>    add_library(name INTERFACE)
>
>    target_link_libraries(name INTERFACE ${LIB1})
>
>    if(FFTW3_FOUND)
>     target_include_directories(name INTERFACE
>     {$FFTW3_INCLUDE_DIRS})
>     target_link_libraries(name INTERFACE ${FFTW3_LIBRARIES} )
>    endif()
>
>    target_include_directories(name INTERFACE ${LIB1_INCLUDE_DIRS})
>
>    # need c++11 compile options for if-name
>    set_property(TARGET name PROPERTY
> INTERFACE_COMPILE_FEATURES cxx_range_for)
>
> This makes that in the CMakeLists.txt which includes my package-file a
> user needing libname for his executable does:
>
>    add_executable(main main.cpp)
>    target_link_libraries(main name)
>
> This will
>
> 1) set C++11 flags for compilation
> 2) set the right include-pathes
> 3) link with the right libraries as per my order
>
> Awesome. But is this a good practice? Are there any pitfalls?

This sounds like a use case for IMPORTED rather than INTERFACE libraries.
INTERFACE libraries can be IMPORTED as well but are not intended for
pre-build linkable libraries (which you seem to have).

This documents how to create find modules with IMPORTED targets:
https://cmake.org/cmake/help/v3.4/manual/cmake-developer.7.html#find-modules

Nils
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Good practice: using INTERFACE-libraries in FindABC.cmake?

Lectem

Speaking of imported targets, I'm wondering why alias imported targets aren't added to all default FindXXX.cmake scripts. This would help provide better examples, and have the community use better cmakelists.txt and hopefully reduce the bad usages of include_directories and such


Le lun. 29 févr. 2016 09:43, Nils Gladitz <[hidden email]> a écrit :
On 02/29/2016 03:34 PM, Patrick Boettcher wrote:
> Hi list,
>
> I came across the INTERFACE-type of libraries when writing a
> FindModule.cmake-file for custom libraries installed by my
> project.
>
> Here is what I'm doing after having found the libraries and the
> determined the paths: LIB1 is the library and LIB1_INCLUDE_DIRS its
> include-dirs:
>
>    add_library(name INTERFACE)
>
>    target_link_libraries(name INTERFACE ${LIB1})
>
>    if(FFTW3_FOUND)
>       target_include_directories(name INTERFACE
>               {$FFTW3_INCLUDE_DIRS})
>       target_link_libraries(name INTERFACE ${FFTW3_LIBRARIES} )
>    endif()
>
>    target_include_directories(name INTERFACE ${LIB1_INCLUDE_DIRS})
>
>    # need c++11 compile options for if-name
>    set_property(TARGET name PROPERTY
>               INTERFACE_COMPILE_FEATURES cxx_range_for)
>
> This makes that in the CMakeLists.txt which includes my package-file a
> user needing libname for his executable does:
>
>    add_executable(main main.cpp)
>    target_link_libraries(main name)
>
> This will
>
> 1) set C++11 flags for compilation
> 2) set the right include-pathes
> 3) link with the right libraries as per my order
>
> Awesome. But is this a good practice? Are there any pitfalls?

This sounds like a use case for IMPORTED rather than INTERFACE libraries.
INTERFACE libraries can be IMPORTED as well but are not intended for
pre-build linkable libraries (which you seem to have).

This documents how to create find modules with IMPORTED targets:
https://cmake.org/cmake/help/v3.4/manual/cmake-developer.7.html#find-modules

Nils
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Good practice: using INTERFACE-libraries in FindABC.cmake?

Nils Gladitz-2
On 02/29/2016 04:06 PM, Clément Gregoire wrote:
>
> Speaking of imported targets, I'm wondering why alias imported targets
> aren't added to all default FindXXX.cmake scripts. This would help
> provide better examples, and have the community use better
> cmakelists.txt and hopefully reduce the bad usages of
> include_directories and such
>

I assume you don't mean alias as in "ALIAS" targets ...

Many of the Find*.cmake modules included with CMake do provide imported
targets now.
e.g. according to grep:
     FindIce.cmake
     FindPNG.cmake
     FindGLUT.cmake
     FindXalanC.cmake
     FindOpenSSL.cmake
     FindThreads.cmake
     FindQt4.cmake
     FindXercesC.cmake
     FindTIFF.cmake
     FindGTK2.cmake
     FindZLIB.cmake
     FindGTest.cmake
     FindGSL.cmake
     FindBoost.cmake
     FindGLEW.cmake

For the rest there probably is just a lack of maintainers ... patches
are probably welcome.

Nils
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Good practice: using INTERFACE-libraries in FindABC.cmake?

Stephen Kelly-2
In reply to this post by Patrick Boettcher
Patrick Boettcher wrote:

> I came across the INTERFACE-type of libraries when writing a
> FindModule.cmake-file for custom libraries installed by my
> project.

You don't provide FindModules for your CMake-built libraries.

See

 https://cmake.org/cmake/help/v3.4/manual/cmake-packages.7.html

Thanks,

Steve.


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Good practice: using INTERFACE-libraries in FindABC.cmake?

Patrick Boettcher
On Mon, 29 Feb 2016 21:20:59 +0100
Stephen Kelly <[hidden email]> wrote:

> Patrick Boettcher wrote:
>
> > I came across the INTERFACE-type of libraries when writing a
> > FindModule.cmake-file for custom libraries installed by my
> > project.  
>
> You don't provide FindModules for your CMake-built libraries.

Thank you for the pointer. I wasn't aware of it and haven't entirely
understood it yet.

However, when I said 'for custom libraries installed by my project' I
was not referring to cmake-based projects. These libraries are used as
is and for the moment I don't want to integrate them via ExternalProject.

--
Patrick.
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake