Skip to content
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

cmake: export targets #943

Merged
merged 5 commits into from
Aug 28, 2021
Merged

Conversation

pedrolcl
Copy link
Contributor

The build system creates two exported targets:

  • The executable FluidSynth::fluidsynth
  • The library FluidSynth::libfluidsynth

A downstream project using CMake can find and link the library target
directly with cmake (without needing pkg-config) this way:

project(sample LANGUAGES C)
find_package ( FluidSynth )
if (FluidSynth_FOUND)
  add_executable( sample sample.c )
  target_link_libraries ( sample PRIVATE FluidSynth::libfluidsynth )
endif ()

After installing fluidsynth in a prefix like "$HOME/Fluidsynth3":
cmake -DCNAKE_PREFIX_PATH="$HOME/Fluidsynth3/;..."

Instead installing, the build directory can be used directly, for
instance:

cmake -DFluidSynth_DIR="$HOME/fluidsynth-2.2.2/build/" ...

Copy link
Member

@derselbst derselbst left a comment

Choose a reason for hiding this comment

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

Looks pretty good! I've cross-checked this with some of KDE's libraries and have some comments below.

src/CMakeLists.txt Outdated Show resolved Hide resolved
FluidSynthConfig.cmake.in Show resolved Hide resolved
CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
@pedrolcl
Copy link
Contributor Author

There are two other changes on this second version:

target_link_libraries ( libfluidsynth PRIVATE ...

and

target_link_libraries ( fluidsynth PRIVATE ...

Library dependencies are transitive by default with the original signature. When the library target is linked into another target then the libraries linked to this target also were linked to the other targets. That means over-linking for the downstream projects. With the PRIVATE option, the libfluidsynth linked libraries are not included in the INTERFACE_LINK_LIBRARIES target property.

I'm afraid that this messed up some tests. Sorry!

@derselbst
Copy link
Member

The most recent push has messed up the commit history. Can you please clean it up?

The unit tests are built against libfluidsynth-OBJ. Perhaps this target should expose all dependency libs as PUBLIC or INTERFACE and only then link libfluidsynth-OBJ against libfluidsynth privately... or so. I could also look into this later on, if you want.

@pedrolcl pedrolcl force-pushed the wip-cmake-export-targets branch from 4f8f9a9 to 43d9c86 Compare July 31, 2021 21:01
@pedrolcl
Copy link
Contributor Author

The most recent push has messed up the commit history. Can you please clean it up?

done.

The unit tests are built against libfluidsynth-OBJ. Perhaps this target should expose all dependency libs as PUBLIC or INTERFACE and only then link libfluidsynth-OBJ against libfluidsynth privately... or so. I could also look into this later on, if you want.

I've reverted the changes to the target_link_libraries() because it is not really related to the exported/imported targets, but it is a preexisting issue.

The solution that you propose would work, simply applying target_link_libraries(libfluidsynth-OBJ ...) that will propagate the dependencies to the targets where this object library is linked. The only problem with that is that requires cmake 3.12 or later. See: https://cmake.org/cmake/help/latest/command/target_link_libraries.html?highlight=target_link_library#linking-object-libraries

The build system creates two exported targets:
- The executable FluidSynth::fluidsynth
- The library FluidSynth::libfluidsynth

A downstream project using CMake can find and link the library target
directly with cmake (without needing pkg-config) this way:

~~~
project(sample LANGUAGES C)
find_package ( FluidSynth )
if (FluidSynth_FOUND)
  add_executable( sample sample.c )
  target_link_libraries ( sample PRIVATE FluidSynth::libfluidsynth )
endif ()
~~~

After installing fluidsynth in a prefix like "$HOME/Fluidsynth3":
cmake -DCNAKE_PREFIX_PATH="$HOME/Fluidsynth3/;..."

Instead installing, the build directory can be used directly, for
instance:

cmake -DFluidSynth_DIR="$HOME/fluidsynth-2.2.2/build/" ...
* FluidSynthConfigVersion.cmake is created with ${VERSION} instead of
${LIB_VERSION_INFO}
* FluidSynthConfig.cmake.in simplified: it doesn't need to include the
version file.
* Simplified BUILD_INTERFACE generator expression as suggested
@pedrolcl pedrolcl force-pushed the wip-cmake-export-targets branch from 25953bc to f023d7e Compare August 2, 2021 09:36
@pedrolcl pedrolcl marked this pull request as ready for review August 6, 2021 18:28
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Member

@derselbst derselbst left a comment

Choose a reason for hiding this comment

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

Sorry, I didn't received a notification when you marked that ready. I tried to fix our CI at OBS by importing the spec and dsc files, but I made it worse. Will fix it later. Thanks again!

@derselbst derselbst added this to the 2.2 milestone Aug 28, 2021
@derselbst derselbst merged commit ec0d6e0 into FluidSynth:master Aug 28, 2021
@pedrolcl pedrolcl deleted the wip-cmake-export-targets branch August 29, 2021 07:36
This was referenced Aug 29, 2021
derselbst added a commit that referenced this pull request Sep 1, 2021
…argets

Reverts most of #943 on master as it accidentally broke compilation with CMake < 3.11, but keep the build files for OBS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants