Skip to content

Conversation

haampie
Copy link
Contributor

@haampie haampie commented Jul 9, 2023

Not an autotools expert, but LIBS contains libssh2's own dependencies, so when doing shared linking, it makes no sense to put them under Libs: ... in the pkg-config file. Libs.private is what you want to use for that, since during static linking you do have to provide transitive deps.

What's not fixed in this PR: adding the -L search dirs when doing static linking. Apparently when openssl has its libraries in <prefix>/lib64, no -L<prefix>/lib64 is added. But when it's in <prefix>/lib, it is? I guess this is a matter of custom logic in libssh2's autotools stuff, where LIBS = -L... -lssl is used for the lib check (custom?), while LDFLAGS = -L... and LIBS = -lssl is used for the lib64 check (autotools internals?)

@haampie
Copy link
Contributor Author

haampie commented Jul 9, 2023

Hope that this helps with review: git blame tells me the pc file was inspired by curl, curl in fact dropped @LIBS@ too in their pc file

@vszakats
Copy link
Member

vszakats commented Jul 9, 2023

Looks good to me! With the note that we also use this .pc template for CMake, where this patch should apply the same.

Can't comment on -L. Is there perhaps some clue in curl's autotools sources?

@vszakats vszakats merged commit 1209c16 into libssh2:master Jul 11, 2023
@vszakats
Copy link
Member

vszakats commented Jul 13, 2023

Found this commit from 2020 that fixes the referenced curl commit above from 2012:
curl/curl@98e5904

It seems to be something that will hit libssh2 too for static-only builds, after merging the first patch.

@haampie What do you think?

Untested attempt to adapt that patch for libssh2:

diff --git a/configure.ac b/configure.ac
index b5a5791e..be7dd717 100644
--- a/configure.ac
+++ b/configure.ac
@@ -392,6 +392,14 @@ fi
 
 LIBS="${LIBS} ${LTLIBZ}"
 
+dnl merge the pkg-config Libs.private field into Libs when static-only
+if test "x$enable_shared" = "xno"; then
+  LIBS_NO_SHARED=$LIBS
+else
+  LIBS_NO_SHARED=
+fi
+AC_SUBST(LIBS_NO_SHARED)
+
 AC_CONFIG_FILES([Makefile
                  src/Makefile
                  tests/Makefile
diff --git a/libssh2.pc.in b/libssh2.pc.in
index 9cbebb7f..61b93efe 100644
--- a/libssh2.pc.in
+++ b/libssh2.pc.in
@@ -15,6 +15,6 @@ URL: https://www.libssh2.org/
 Description: Library for SSH-based communication
 Version: @LIBSSH2VER@
 Requires.private: @LIBSREQUIRED@
-Libs: -L${libdir} -lssh2
+Libs: -L${libdir} -lssh2 @LIBS_NO_SHARED@
 Libs.private: @LIBS@
 Cflags: -I${includedir}
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8638ac41..11caa5de 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -189,6 +189,12 @@ endif()
 set(LIBSSH2VER ${LIBSSH2_VERSION})
 set(LIBSREQUIRED ${PC_REQUIRES_PRIVATE})
 set(LIBS ${PC_LIBS})
+# merge the pkg-config Libs.private field into Libs when static-only
+if(BUILD_SHARED_LIBS)
+  set(LIBS_NO_SHARED "")
+else()
+  set(LIBS_NO_SHARED "${PC_LIBS}")
+endif()
 set(prefix ${CMAKE_INSTALL_PREFIX})
 set(exec_prefix "\${prefix}")
 set(libdir "\${prefix}/${CMAKE_INSTALL_LIBDIR}")

vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 13, 2023
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 13, 2023
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 14, 2023
Quoting the curl commit message (adapted for libssh2):

"A project being built entirely statically will call pkg-config with
`--static`, which utilises the `Libs.private` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs` fields.

Consider that this is what the Meson build system does when it
generates pkg-config files."

Follow-up to 1209c16 libssh2#1114

Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes libssh2#1119
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 15, 2023
Quoting the curl commit message (adapted for libssh2):

"A project being built entirely statically will call pkg-config with
`--static`, which utilises the `Libs.private` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs` fields.

Consider that this is what the Meson build system does when it
generates pkg-config files."

Follow-up to 1209c16 libssh2#1114

Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes libssh2#1119
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 15, 2023
Quoting the curl commit message (adapted for libssh2):

"A project being built entirely statically will call pkg-config with
`--static`, which utilises the `Libs.private` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs` fields.

Consider that this is what the Meson build system does when it
generates pkg-config files."

Follow-up to 1209c16 libssh2#1114

Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes libssh2#1119
vszakats added a commit that referenced this pull request Jul 18, 2023
Adapted for libssh2 from the curl commit message by James Le Cuirot:

"A project built entirely statically will call `pkg-config` with
`--static`, which utilises the `Libs.private:` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs:` fields.

Consider that this is what the Meson build system does when it generates
`pkg-config` files."

This patch extends the above to `Requires:`, to mirror `Libs:` with
`pkg-config` package names.

Follow-up to 1209c16 #1114

Ref: #1114 (comment)
Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes #1119
lampmanyao pushed a commit to lampmanyao/libssh2 that referenced this pull request Sep 3, 2023
Adapted for libssh2 from the curl commit message by James Le Cuirot:

"A project built entirely statically will call `pkg-config` with
`--static`, which utilises the `Libs.private:` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs:` fields.

Consider that this is what the Meson build system does when it generates
`pkg-config` files."

This patch extends the above to `Requires:`, to mirror `Libs:` with
`pkg-config` package names.

Follow-up to 1209c16 libssh2#1114

Ref: libssh2#1114 (comment)
Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes libssh2#1119
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
Adapted for libssh2 from the curl commit message by James Le Cuirot:

"A project built entirely statically will call `pkg-config` with
`--static`, which utilises the `Libs.private:` field. Conversely it will
not use `--static` when not being built entirely statically, even if
there is only a static build of libssh2 available. This will most
likely cause the build to fail due to underlinking unless we merge the
`Libs:` fields.

Consider that this is what the Meson build system does when it generates
`pkg-config` files."

This patch extends the above to `Requires:`, to mirror `Libs:` with
`pkg-config` package names.

Follow-up to 1209c16 libssh2#1114

Ref: libssh2#1114 (comment)
Ref: curl/curl@98e5904
Ref: curl/curl#5373
Closes libssh2#1119
@haampie haampie deleted the patch-1 branch November 3, 2024 09:15
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