[CMake] find_package(): fails because static library is not installed
Hi, I have added support for find_package(primesieve) to my primesieve library and I have exported 2 targets in my main CMakeLists.txt: primesieve::libprimesieve (shared library) primesieve::libprimesieve-static It works fine if both the static and shared primesieve libraries have previously been installed. As a test I have now deleted the static libprimesieve from my PC and now find_package(primesieve) fails: CMake Error at /usr/local/lib/cmake/primesieve/primesieveTargets.cmake:88 (message): The imported target "primesieve::libprimesieve-static" references the file "/usr/local/lib/libprimesieve.a" but this file does not exist. Possible reasons include... The error seems reasonable but if I leave my code as is this error will be triggered if somebody installs the libprimesieve RPM package on Fedora or Red Hat Linux. The libprimesieve package contains only the shared libprimesieve. Fedora requires that static libraries are put into a separate package. Hence the static primesieve library is in libprimesieve-static. How can I solve this issue? If the shared libprimesieve is installed but the static libprimesieve is not installed then I would like to only export: primesieve::libprimesieve (shared library) But if both the static and shared libprimesieve are installed I would like to export: primesieve::libprimesieve (shared library) primesieve::libprimesieve-static Do you know any library which has already implemented this? Thanks, Kim -- 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: https://cmake.org/mailman/listinfo/cmake
[CMake] [ANNOUNCE] CMake 3.11.0-rc1 is now ready for testing
I am proud to announce the first CMake 3.11 release candidate. https://cmake.org/download/ Documentation is available at: https://cmake.org/cmake/help/v3.11 Release notes appear below and are also published at https://cmake.org/cmake/help/v3.11/release/3.11.html Some of the more significant changes in CMake 3.11 are: * The Makefile Generators and the "Ninja" generator learned to add compiler launcher tools along with the compiler for the "Fortran" language ("C", "CXX", and "CUDA" were supported previously). See the "CMAKE__COMPILER_LAUNCHER" variable and "_COMPILER_LAUNCHER" target property for details. * Visual Studio Generators learned to support the "COMPILE_LANGUAGE" "generator expression" in target-wide "COMPILE_DEFINITIONS", "INCLUDE_DIRECTORIES", "COMPILE_OPTIONS", and "file(GENERATE)". See generator expression documentation for caveats. * The "Xcode" Generator learned to support the "COMPILE_LANGUAGE" "generator expression" in target-wide "COMPILE_DEFINITIONS" and "INCLUDE_DIRECTORIES". It previously supported only "COMPILE_OPTIONS" and "file(GENERATE)". See generator expression documentation for caveats. * "add_library()" and "add_executable()" commands can now be called without any sources and will not complain as long as sources are added later via the "target_sources()" command. * The "target_compile_definitions()" command learned to set the "INTERFACE_COMPILE_DEFINITIONS" property on Imported Targets. * The "target_compile_features()" command learned to set the "INTERFACE_COMPILE_FEATURES" property on Imported Targets. * The "target_compile_options()" command learned to set the "INTERFACE_COMPILE_OPTIONS" property on Imported Targets. * The "target_include_directories()" command learned to set the "INTERFACE_INCLUDE_DIRECTORIES" property on Imported Targets. * The "target_sources()" command learned to set the "INTERFACE_SOURCES" property on Imported Targets. * The "target_link_libraries()" command learned to set the "INTERFACE_LINK_LIBRARIES" property on Imported Targets. * The "COMPILE_DEFINITIONS" source file property learned to support "generator expressions". * A "COMPILE_OPTIONS" source file property was added to manage list of options to pass to the compiler. * When using "AUTOMOC" or "AUTOUIC", CMake now starts multiple parallel "moc" or "uic" processes to reduce the build time. A new "CMAKE_AUTOGEN_PARALLEL" variable and "AUTOGEN_PARALLEL" target property may be set to specify the number of parallel "moc" or "uic" processes to start. The default is derived from the number of CPUs on the host. CMake 3.11 Release Notes Changes made since CMake 3.10 include the following. New Features Platforms - * TI C/C++ compilers are now supported by the "Ninja" generator. Generators -- * The "CodeBlocks" extra generator learned to check a "CMAKE_CODEBLOCKS_COMPILER_ID" variable for a custom compiler identification value to place in the project file. * The Makefile Generators and the "Ninja" generator learned to add compiler launcher tools along with the compiler for the "Fortran" language ("C", "CXX", and "CUDA" were supported previously). See the "CMAKE__COMPILER_LAUNCHER" variable and "_COMPILER_LAUNCHER" target property for details. * Visual Studio Generators learned to support the "COMPILE_LANGUAGE" "generator expression" in target-wide "COMPILE_DEFINITIONS", "INCLUDE_DIRECTORIES", "COMPILE_OPTIONS", and "file(GENERATE)". See generator expression documentation for caveats. * The "Xcode" generator learned to support the "COMPILE_LANGUAGE" "generator expression" in target-wide "COMPILE_DEFINITIONS" and "INCLUDE_DIRECTORIES". It previously supported only "COMPILE_OPTIONS" and "file(GENERATE)". See generator expression documentation for caveats. Commands * "add_library()" and "add_executable()" commands can now be called without any sources and will not complain as long as sources are added later via the "target_sources()" command. * The "file(DOWNLOAD)" and "file(UPLOAD)" commands gained "NETRC" and "NETRC_FILE" options to specify use of a ".netrc" file. * The "target_compile_definitions()" command learned to set the "INTERFACE_COMPILE_DEFINITIONS" property on Imported Targets. * The "target_compile_features()" command learned to set the "INTERFACE_COMPILE_FEATURES" property on Imported Targets. * The "target_compile_options()" command learned to set the "INTERFACE_COMPILE_OPTIONS" property on Imported Targets. * The "target_include_directories()" command learned to set the "INTERFACE_INCLUDE_DIRECTORIES" property on Imported Targets. * The "target_sources()" command learned to set the "INTERFACE_SOURCES" property on Imported Targets. * The "target_link_libraries()" command learned to set the "INTERFACE_LINK_LIBRARIES" property on Imported Targets. Variables - * A "CMAKE_GENERATOR_I
Re: [CMake] Config.cmake.in: Conditionally serve static or shared library
It would be really nice though if CMake would support library aliases for import targets, then I could achieve the same using much nicer code. 3.11 will include support for aliasing import targets, as long as the import target was marked with GLOBAL visibility. On Wed, Feb 14, 2018 at 3:47 PM, Kim Walisch wrote: > Thanks, I got it working! > > Unfortunately I could not make the generator expression work inside > PrimesieveConfig.cmake.in, here is the error message: > > CMake Error: > Error evaluating generator expression: > $ > Expression did not evaluate to a known generator expression > > Note that in my main CMakeLists.txt the generator expression worked > fine. But I figured out I don't actually need the generator expression, > the following code inside my PrimesieveConfig.cmake.in does the trick: > > if(BUILD_SHARED_LIBS) > add_library(Primesieve::Primesieve INTERFACE IMPORTED) > set_target_properties(Primesieve::Primesieve PROPERTIES > INTERFACE_LINK_LIBRARIES "Primesieve::libprimesieve") > else() > add_library(Primesieve::Primesieve INTERFACE IMPORTED) > set_target_properties(Primesieve::Primesieve PROPERTIES > INTERFACE_LINK_LIBRARIES "Primesieve::libprimesieve-static") > endif() > > This trick is also used by the c-ares library: > https://github.com/gjasny/c-ares/blob/fec405a420597180244e6c66912f09ae84b31123/c-ares-config.cmake.in > > The trick was already discussed on the cmake mailing list before: > https://cmake.org/pipermail/cmake-developers/2017-June/030115.html > > It would be really nice though if CMake would support library aliases for > import targets, then I could achieve the same using much nicer code. > > Kim > > On Wed, Feb 14, 2018 at 7:08 PM, Kim Walisch wrote: >> >> CMake currently does not allow alias libraries for import targets: >> >> $ cmake .. >> CMake Error at /usr/local/lib/cmake/primesieve/PrimesieveConfig.cmake:7 >> (add_library): >> add_library cannot create ALIAS target "my_libprimesieve" because target >> "Primesieve::libprimesieve-static" is IMPORTED. >> Call Stack (most recent call first): >> CMakeLists.txt:6 (find_package) >> >> -- Configuring incomplete, errors occurred! >> >> Kim >> >> >> On Wed, Feb 14, 2018 at 5:34 PM, ThePhD wrote: >>> >>> I think you can use what's called an "Alias Library": >>> https://cmake.org/cmake/help/latest/command/add_library.html#alias-libraries >>> >>> if (BUILD_SHARED_LIBS) >>> add_library(my_libprimesieve ALIAS libprimesieve) >>> else >>> add_library(my_libprimesieve ALIAS libprimesieve-static) >>> endif(BUILD_SHARED_LIBS) >>> >>> Then, set it properly to Primesieve::Primesieve and it should work. >>> >>> >>> On Wed, Feb 14, 2018 at 10:21 AM, Kim Walisch >>> wrote: Hi, My primesieve project builds as both a static and a shared library, hence there are 2 targets in my main CMakeLists.txt: 1) libprimesieve (shared library) 2) libprimesieve-static I am now trying to add support for find_package(Primesieve). Ideally I would like that users can link against libprimesieve as follows: find_package(Primesieve REQUIRED) target_link_libraries(user_library Primesieve::Primesieve) And I want Primesieve::Primesieve to be an alias for Primesieve::libprimesieve if BUILD_SHARED_LIBS=ON (in the user's CMakeLists.txt) and and alias for Primesieve::libprimesieve-static if BUILD_SHARED_LIBS=OFF (or if it is not set). Is this possible? Do you know any library that has already implemented this? Thanks, Kim -- 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: https://cmake.org/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: >>>