Skip to content

cmake: fix find Threads#830

Open
abique wants to merge 2 commits intoxiph:masterfrom
abique:fix-cmake-threads-deps
Open

cmake: fix find Threads#830
abique wants to merge 2 commits intoxiph:masterfrom
abique:fix-cmake-threads-deps

Conversation

@abique
Copy link

@abique abique commented Apr 14, 2025

For reference: microsoft/vcpkg#44940

@dg0yt
Copy link
Contributor

dg0yt commented Apr 17, 2025

This complements

if(HAVE_PTHREAD)
target_link_libraries(FLAC PUBLIC Threads::Threads)
endif()
.

@Croydon
Copy link

Croydon commented Nov 5, 2025

Related issue: #820

@ktmf01
Copy link
Collaborator

ktmf01 commented Jan 25, 2026

I'm not fluent in CMake, so please help me out here. This dependency is added when the ENABLE_MULTITHREADING input is ON, but would using the output variable HAVE_PTHREAD be better? This is suggested in #820

@dg0yt
Copy link
Contributor

dg0yt commented Jan 26, 2026

If your library links Threads::Threads, this is exposed in the exported CMake config, and the CMake config must provide it. Basically, the config mirrors build-time find_package with usage-time find_dependency (minus the REQUIRED, but this is a different topic).

So this is what this PR does. Canonical, minimal CMake, mirroring CMakeLists.txt.

#820 is different:

  • It uses a different condition. I don't know which condition is used in CMakeLists.txt. But the thread CMake module, and Threads::Threads, is generally also used to represent Windows threads. @HAVE_PTHTREAD@ doesn't look like it would represent this case.
  • It sets THREADS_PREFER_PTHREAD_FLAG. I'm not sure if this belongs to a CMake config. It may change the properties of Threads::Threads, but this might be for the project to decide.
  • It sets CMAKE_THREAD_PREFER_PTHREAD. AFAIU this is obsolete now, removed in CMake 3.14.

@dg0yt
Copy link
Contributor

dg0yt commented Jan 26, 2026

BTW this change is applied in the vcpkg port.

@ktmf01 ktmf01 linked an issue Jan 26, 2026 that may be closed by this pull request
@ktmf01
Copy link
Collaborator

ktmf01 commented Jan 26, 2026

Would that mean that the wrong variable is used in the lines you quoted last time?

if(HAVE_PTHREAD)
target_link_libraries(FLAC PUBLIC Threads::Threads)
endif()

Also, I'm wondering what would happen if find_package(Threads) doesn't find any multithreading capability. Then the build would not require a lib, but the file would say it does. Maybe we shouldn't check for HAVE_PTHREAD or ENABLE_MULTITHREADING, but instead for Threads_FOUND?

Finally, and I understand this is not something FLAC-specific, how does this work if configuration during build finds one library (for example pthread), and configuration with that flac-config.cmake file finds another multithreading capability. Would that even be possible?

@dg0yt
Copy link
Contributor

dg0yt commented Jan 27, 2026

Would that mean that the wrong variable is used in the lines you quoted last time?

I deliberately said "different", not "wrong". The full context needs to see

flac/CMakeLists.txt

Lines 225 to 236 in afb801b

if(ENABLE_MULTITHREADING)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)
if(CMAKE_USE_PTHREADS_INIT)
set(HAVE_PTHREAD 1)
else()
if(HAVE_THREADS_H)
set(HAVE_C11THREADS 1)
endif()
endif()
endif()

So, HAVE_PTHREAD might indeed be more suitable to mirror the actual wiring in the CMakeLists.txt files. (The option "ENABLE_MULTITHREADING" is not directly wired to the use of Threads::Threads, and it may not even enable multithreading.)

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.

CMake: flac-config.cmake is missing find_dependency(Threads)

4 participants