Skip to content

Try linking from gphoto2 from package config #18864

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tenderlove
Copy link

If gphoto2 was found in package config, but was not found via the normal find_path / find_library calls, try looking again but with the paths supplied by pkg-config

This is probably a niche usecase, but tethering wasn't working with my OM-1 Mkii. I tracked the problem to a bug in libgphoto2, and sent a PR to fix libgphoto2. I was able to confirm that gphoto2 could control my camera, but I wanted to test darktable integration. I installed a development version of libgphoto2 to a special directory, and even though I could get CMake to find my pkg-config files, it wouldn't compile / link against them. I added the code in this PR to try compiling and linking against gphoto2 found in pkg-config so I could verify my changes fixed darktable too.

Here's a screenshot of darktable tethering with my OM-1 Mkii:

Screenshot 2025-05-23 at 2 49 57 PM

I know this is a niche usecase, so it's fine if my PR is rejected. But I wanted to send it in case someone else is trying to test edge versions of dependencies (or maybe there's an easier way I don't know).

Thanks!

If gphoto2 was found in package config, but was _not_ found via the
normal `find_path` / `find_library` calls, try looking again but with
the paths supplied by pkg-config
@TurboGit
Copy link
Member

Looks good to me but I'll prefer a CMake expert look at this, @kmilos can you review ?

@kmilos
Copy link
Contributor

kmilos commented May 24, 2025

This looks very contorted and unnecessary to me...

What is the exact problem you're trying to solve, can you provide some output when trying to link to libghpoto2 in a custom location?

@tenderlove
Copy link
Author

tenderlove commented May 24, 2025

What is the exact problem you're trying to solve, can you provide some output when trying to link to libghpoto2 in a custom location?

I've installed libgphoto2 to $HOME/.local/lib. I set PKG_CONFIG_PATH to $HOME/.local/lib/pkgconfig. I expected the CMake configuration to compile and link against the values returned by pkg-config, but instead it searches the system libraries for libgphoto2 and uses pkg-config to determine the version.

If I specify PKG_CONFIG_PATH, pkg-config will pick up the right cflags etc:

$ PKG_CONFIG_PATH="$HOME/.local/lib/pkgconfig" pkg-config --cflags libgphoto2
-I/Users/aaron/.local/include -I/Users/aaron/.local/include/gphoto2 -I/opt/homebrew/Cellar/libexif/0.6.25/include

AFAICT the find_library call will not consult pkg-config for paths to search.

Here is the script I'm using to build:

begin
  set -lx DEVELOPER_DIR "/Applications/Xcode.app/Contents/Developer"
  set -lx CC cc
  set -lx CXX c++
  set -lx MACOSX_DEPLOYMENT_TARGET 14.0
  set -lx SRC_DIR $HOME/git/darktable
  set -lx BUILD_DIR $SRC_DIR/build
  set -lx INSTALL_PREFIX $BUILD_DIR/macosx
  set -lx ECO "-DBINARY_PACKAGE_BUILD=ON -DBUILD_CURVE_TOOLS=ON -DBUILD_NOISE_TOOLS=ON -DUSE_GRAPHICSMAGICK=OFF -DUSE_IMAGEMAGICK=ON"
  set -lx CMAKE_BUILD_TYPE Release
  set -lx GENERATOR Ninja
  set -lx TARGET skiptest
  set -lx PKG_CONFIG_PATH $HOME/.local/lib/pkgconfig

  pushd $SRC_DIR
  git submodule init
  git config submodule.src/tests/integration.update none
  git submodule update
  popd

  pushd $SRC_DIR

  cmake -E make_directory $BUILD_DIR
  cmake -E make_directory $INSTALL_PREFIX
  $SRC_DIR/.ci/ci-script.sh
  $SRC_DIR/packaging/macosx/3_make_hb_darktable_package.sh

  popd
end

Here is the logfile made by building on master.

Here is the logfile made from building on my branch.

On master:

-- Could NOT find Gphoto2 (missing: Gphoto2_LIBRARY Gphoto2_INCLUDE_DIR) (Required is at least version "2.5")

On my branch:

-- Found Gphoto2: /Users/aaron/.local/lib/libgphoto2.dylib (Required is at least version "2.5")

otool output on master (it's empty):

$ otool -l build/macosx/bin/darktable | grep gphoto

otool output on my branch:

$ otool -l build/macosx/bin/darktable | grep gphoto
         name /Users/aaron/.local/lib/libgphoto2.6.dylib (offset 24)
         name /Users/aaron/.local/lib/libgphoto2_port.12.dylib (offset 24)

I think this PR could be rewritten like this:

diff --git a/cmake/modules/FindGphoto2.cmake b/cmake/modules/FindGphoto2.cmake
index 936122c75d..e1d3726cdf 100644
--- a/cmake/modules/FindGphoto2.cmake
+++ b/cmake/modules/FindGphoto2.cmake
@@ -19,19 +19,21 @@
 
 include(LibFindMacros)
 
-find_path(Gphoto2_INCLUDE_DIR gphoto2/gphoto2.h)
-mark_as_advanced(Gphoto2_INCLUDE_DIR)
 
 set(Gphoto2_NAMES ${Gphoto2_NAMES} gphoto2 libgphoto2)
 set(Gphoto2_PORT_NAMES ${Gphoto2_PORT_NAMES} gphoto2_port libgphoto2_port)
-find_library(Gphoto2_LIBRARY NAMES ${Gphoto2_NAMES} )
-find_library(Gphoto2_PORT_LIBRARY NAMES ${Gphoto2_PORT_NAMES} )
-mark_as_advanced(Gphoto2_LIBRARY)
-mark_as_advanced(Gphoto2_PORT_LIBRARY)
 
 # Detect libgphoto2 version
 libfind_pkg_check_modules(Gphoto2_PKGCONF libgphoto2)
 if(Gphoto2_PKGCONF_FOUND)
+  find_path(Gphoto2_INCLUDE_DIR gphoto2/gphoto2.h PATHS ${Gphoto2_PKGCONF_INCLUDE_DIRS} )
+  mark_as_advanced(Gphoto2_INCLUDE_DIR)
+
+  find_library(Gphoto2_LIBRARY NAMES ${Gphoto2_NAMES} PATHS ${Gphoto2_PKGCONF_LIBRARY_DIRS} )
+  find_library(Gphoto2_PORT_LIBRARY NAMES ${Gphoto2_PORT_NAMES} PATHS ${Gphoto2_PKGCONF_LIBRARY_DIRS} )
+  mark_as_advanced(Gphoto2_LIBRARY)
+  mark_as_advanced(Gphoto2_PORT_LIBRARY)
+
   set(Gphoto2_VERSION_STRING "${Gphoto2_PKGCONF_VERSION}")
 endif()

But I'm not a CMake expert. Also I suspect I could manually pass CFLAGS etc, but since the build scripts require gphoto2 to be available via pkg-config, it seems like we should use the paths pkg-config tell us to use.

Hope this makes sense. Thank you for your patience.

@kmilos
Copy link
Contributor

kmilos commented May 26, 2025

Yep, the FindGphoto2.cmake module is not really implemented in the best way, find_path is called before any pkgconf stuff, which is indeed a bit strange? I might try to refactor the whole thing at some point...

Btw, are we sure gphoto2 ships a .pc file on all platforms? (It does for me on MSYS2 at least.)

@kmilos
Copy link
Contributor

kmilos commented May 26, 2025

@tenderlove Can you please give #18875 a go instead?

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.

3 participants