8000 cmake: add libre namespace export by sreimers · Pull Request #786 · baresip/re · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

cmake: add libre namespace export #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 14, 2023
Merged

cmake: add libre namespace export #786

merged 7 commits into from
May 14, 2023

Conversation

sreimers
Copy link
Member
@sreimers sreimers commented May 4, 2023

refs #785

With this PR there are now multiple ways how a project can use libre:

find_package without namespace and CONFIG (needed if you want import RE_DEFINITIONS):

find_package(re REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE re)
# Optional RE_DEFINITIONS
find_package(re CONFIG REQUIRED HINTS ../re/cmake)

With namespace static linked:

find_package(libre CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE libre::re)

With namespace shared linked:

find_package(libre CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE libre::re-shared)

@alfredh
Copy link
Contributor
alfredh commented May 4, 2023

for reference, other open-source projects:

https://github.com/xiph/opus/blob/master/CMakeLists.txt#L257

add_library(opus ${opus_headers} ${opus_sources} ${opus_sources_float} ${Opus_PUBLIC_HEADER})
add_library(Opus::opus ALIAS opus)

@sreimers
Copy link
Member Author
sreimers commented May 4, 2023

I think the ALIAS name does not matter much, since the orginal target names are used by cmake generated libre configs:

cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j
cmake --install build --prefix dist

dist/lib/cmake/libre/libre.cmake:

# Generated by CMake

if("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.8)
   message(FATAL_ERROR "CMake >= 2.8.0 required")
endif()
if(CMAKE_VERSION VERSION_LESS "2.8.3")
   message(FATAL_ERROR "CMake >= 2.8.3 required")
endif()
cmake_policy(PUSH)
cmake_policy(VERSION 2.8.3...3.24)
#----------------------------------------------------------------
# Generated CMake target import file.
#----------------------------------------------------------------

# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)

# Protect against multiple inclusion, which would fail when already imported targets are added once more.
set(_cmake_targets_defined "")
set(_cmake_targets_not_defined "")
set(_cmake_expected_targets "")
foreach(_cmake_expected_target IN ITEMS libre::re-shared libre::re)
  list(APPEND _cmake_expected_targets "${_cmake_expected_target}")
  if(TARGET "${_cmake_expected_target}")
    list(APPEND _cmake_targets_defined "${_cmake_expected_target}")
  else()
    list(APPEND _cmake_targets_not_defined "${_cmake_expected_target}")
  endif()
endforeach()
unset(_cmake_expected_target)
if(_cmake_targets_defined STREQUAL _cmake_expected_targets)
  unset(_cmake_targets_defined)
  unset(_cmake_targets_not_defined)
  unset(_cmake_expected_targets)
  unset(CMAKE_IMPORT_FILE_VERSION)
  cmake_policy(POP)
  return()
endif()
if(NOT _cmake_targets_defined STREQUAL "")
  string(REPLACE ";" ", " _cmake_targets_defined_text "${_cmake_targets_defined}")
  string(REPLACE ";" ", " _cmake_targets_not_defined_text "${_cmake_targets_not_defined}")
  message(FATAL_ERROR "Some (but not all) targets in this export set were already defined.\nTargets Defined: ${_cmake_targets_defined_text}\nTargets not yet defined: ${_cmake_targets_not_defined_text}\n")
endif()
unset(_cmake_targets_defined)
unset(_cmake_targets_not_defined)
unset(_cmake_expected_targets)


# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
  set(_IMPORT_PREFIX "")
endif()

# Create imported target libre::re-shared
add_library(libre::re-shared SHARED IMPORTED)

set_target_properties(libre::re-shared PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/re"
)

# Create imported target libre::re
add_library(libre::re STATIC IMPORTED)

set_target_properties(libre::re PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include/re"
  INTERFACE_LINK_LIBRARIES "Threads::Threads;resolv;ZLIB::ZLIB;OpenSSL::SSL;OpenSSL::Crypto;-lm;dl"
)

if(CMAKE_VERSION VERSION_LESS 2.8.12)
  message(FATAL_ERROR "This file relies on consumers using CMake 2.8.12 or greater.")
endif()

# Load information for each installed configuration.
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/libre-*.cmake")
foreach(_cmake_config_file IN LISTS _cmake_config_files)
  include("${_cmake_config_file}")
endforeach()
unset(_cmake_config_file)
unset(_cmake_config_files)

# Cleanup temporary variables.
set(_IMPORT_PREFIX)

# Loop over all imported files and verify that they actually exist
foreach(_cmake_target IN LISTS _cmake_import_check_targets)
  foreach(_cmake_file IN LISTS "_cmake_import_check_files_for_${_cmake_target}")
    if(NOT EXISTS "${_cmake_file}")
      message(FATAL_ERROR "The imported target \"${_cmake_target}\" references the file
   \"${_cmake_file}\"
but this file does not exist.  Possible reasons include:
* The file was deleted, renamed, or moved to another location.
* An install or uninstall procedure did not complete successfully.
* The installation package was faulty and contained
   \"${CMAKE_CURRENT_LIST_FILE}\"
but not all the files it references.
")
    endif()
  endforeach()
  unset(_cmake_file)
  unset("_cmake_import_check_files_for_${_cmake_target}")
endforeach()
unset(_cmake_target)
unset(_cmake_import_check_targets)

# This file does not depend on other imported targets which have
# been exported from the same project but in a separate export set.

# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)
cmake_policy(POP)

dist/lib/cmake/libre/libre-release.cmake

#----------------------------------------------------------------
# Generated CMake target import file for configuration "Release".
#----------------------------------------------------------------

# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)

# Import target "libre::re-shared" for configuration "Release"
set_property(TARGET libre::re-shared APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(libre::re-shared PROPERTIES
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libre.so.15.1.0"
  IMPORTED_SONAME_RELEASE "libre.so.15"
  )

list(APPEND _cmake_import_check_targets libre::re-shared )
list(APPEND _cmake_import_check_files_for_libre::re-shared "${_IMPORT_PREFIX}/lib/libre.so.15.1.0" )

# Import target "libre::re" for configuration "Release"
set_property(TARGET libre::re APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(libre::re PROPERTIES
  IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libre.a"
  )

list(APPEND _cmake_import_check_targets libre::re )
list(APPEND _cmake_import_check_files_for_libre::re "${_IMPORT_PREFIX}/lib/libre.a" )

# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

@sreimers sreimers marked this pull request as ready for review May 4, 2023 09:46
@sreimers sreimers marked this pull request as draft May 4, 2023 09:59
@alfredh
Copy link
Contributor
alfredh commented May 4, 2023

great stuff, this looks good.

I can also test the patch with the VCPKG PR.

@alfredh
Copy link
Contributor
alfredh commented May 4, 2023

I tested with vcpkg, but it looks like the header files are still missing:

$ ./vcpkg install libre
Computing installation plan...
The following packages will be built and installed:
    libre[core]:arm64-osx -> 3.1.0
Detecting compiler hash for triplet arm64-osx...
Restored 0 package(s) from /Users/alfredh/.cache/vcpkg/archives in 14.3 us. Use --debug to see more details.
Installing 1/1 libre:arm64-osx...
Building libre[core]:arm64-osx...
warning: -- Using community triplet arm64-osx. This triplet configuration is not guaranteed to succeed.
-- [COMMUNITY] Loading triplet configuration from: /Users/alfredh/tmp/vcpkg/triplets/community/arm64-osx.cmake
-- Using cached baresip-re-v3.1.0.tar.gz.
-- Extracting source /Users/alfredh/tmp/vcpkg/downloads/baresip-re-v3.1.0.tar.gz
-- Applying patch 778.diff
-- Applying patch 786.diff
-- Using source at /Users/alfredh/tmp/vcpkg/buildtrees/libre/src/v3.1.0-ee4d65e63c.clean
-- Configuring arm64-osx
-- Building arm64-osx-dbg
-- Building arm64-osx-rel
-- Fixing pkgconfig file: /Users/alfredh/tmp/vcpkg/packages/libre_arm64-osx/lib/pkgconfig/libre.pc
-- Fixing pkgconfig file: /Users/alfredh/tmp/vcpkg/packages/libre_arm64-osx/debug/lib/pkgconfig/libre.pc
-- Installing: /Users/alfredh/tmp/vcpkg/packages/libre_arm64-osx/share/libre/copyright
-- Performing post-build validation
warning: Include files should not be duplicated into the /debug/include directory. If this cannot be disabled in the project cmake, use
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
error: Found 1 post-build check problem(s). To submit these ports to curated catalogs, please first correct the portfile: /Users/alfredh/tmp/vcpkg/ports/libre/portfile.cmake
Stored binaries in 1 destinations.
Elapsed time to handle libre:arm64-osx: 4 s
Total install time: 6 s
The package libre is compatible with built-in CMake targets:

    find_package(re REQUIRED)
    target_link_libraries(main PRIVATE re)

Here is the list of files:

Alfreds-MacBook-Pro-M1:vcpkg alfredh$ ./vcpkg owns libre
libre:arm64-osx: arm64-osx/debug/lib/libre.a
libre:arm64-osx: arm64-osx/debug/lib/pkgconfig/libre.pc
libre:arm64-osx: arm64-osx/lib/libre.a
libre:arm64-osx: arm64-osx/lib/pkgconfig/libre.pc
libre:arm64-osx: arm64-osx/share/libre/copyright
libre:arm64-osx: arm64-osx/share/libre/re-config.cmake
libre:arm64-osx: arm64-osx/share/libre/usage
libre:arm64-osx: arm64-osx/share/libre/vcpkg.spdx.json
libre:arm64-osx: arm64-osx/share/libre/vcpkg_abi_info.txt

@sreimers
Copy link
Member Author
sreimers commented May 4, 2023

Looks like this is apple specific, https://cmake.org/cmake/help/latest/command/install.html#command:install:

Any PUBLIC_HEADER files associated with a library are installed in the destination specified by the PUBLIC_HEADER argument on non-Apple platforms. Rules defined by this argument are ignored for FRAMEWORK libraries on Apple platforms because the associated files are installed into the appropriate locations inside the framework folder. See PUBLIC_HEADER for details.

I will try to figure out, if there is a different way.

@sreimers
Copy link
Member Author
sreimers commented May 4, 2023

@alfredh I refactored the headers install, does this work with vcpkg?: 84359d7

@@ -680,6 +684,7 @@ if(LIBRE_BUILD_SHARED)
${PROJECT_SOVERSION}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
set_target_properties(re-shared PROPERTIES SOVERSION ${PROJECT_SOVERSION})
set_target_properties(re-shared PROPERTIES OUTPUT_NAME "re")
add_library(libre::re-shared ALIAS re-shared)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the application know if the library is shared or static ?

for example OpenSSL alias is "OpenSSL::SSL" ...

@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/libre.cmake")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between these two files:

libre-config.cmake
re-config.cmake

could they be the same file ?

@alfredh
Copy link
Contributor
alfredh commented May 6, 2023

I have tested again with VCPKG on Linux, basic things are working, except headers.
I suspect that is a VCPKG problem, and not a problem with this patch.

When installing I noticed:

-- Up-to-date: /usr/local/lib/cmake/libre/libre.cmake
-- Up-to-date: /usr/local/lib/cmake/libre/libre-noconfig.cmake
-- Up-to-date: /usr/local/lib/cmake/re/re-config.cmake
-- Up-to-date: /usr/local/lib/cmake/libre/libre-config.cmake

is this on purpose? is it possible to use only one directory, e.g. "libre" ?

@sreimers
Copy link
Member Author

is this on purpose? is it possible to use only one directory, e.g. "libre" ?

I'm going to take a closer look, but I think the new namespace is a breaking change for find_package config I think:

https://github.com/baresip/baresip/blob/47208fa660c37f3e647d2c5625018e778a81e3ef/CMakeLists.txt#L90

@alfredh
Copy link
Contributor
alfredh commented May 11, 2023

we can merge what we have now.

more tuning can be done later ...

@sreimers sreimers marked this pull request as ready for review May 13, 2023 13:58
@sreimers sreimers merged commit 6db4603 into main May 14, 2023
@sreimers sreimers deleted the cmake_namespace_export branch May 14, 2023 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0