Skip to content

[CMake] Set the visibility to hidden and only export the API symbols - #602

Closed
stac47 wants to merge 1 commit into
libssh2:masterfrom
stac47:hide_non_api_symbols
Closed

stac47 wants to merge 1 commit into
libssh2:masterfrom
stac47:hide_non_api_symbols

Conversation

@stac47

@stac47 stac47 commented May 17, 2021

Copy link
Copy Markdown
Contributor

Hello,
I just find out a discrepancy in the shared libs produced by autotools and by CMake. It seems by default the CMake build does not hide any symbols.

>abidiff ~/libssh2.so.cmake libssh2.so.autotools
Functions changes summary: 0 Removed, 0 Changed, 0 Added function
Variables changes summary: 0 Removed, 0 Changed, 0 Added variable
Function symbols changes summary: 119 Removed, 0 Added function symbols not referenced by debug info
Variable symbols changes summary: 1 Removed, 0 Added variable symbol not referenced by debug info

119 Removed function symbols not referenced by debug info:

  [D] Blowfish_decipher
  [D] Blowfish_encipher
  [D] Blowfish_expand0state
  [D] Blowfish_expandstate
  [D] Blowfish_initstate
  [D] Blowfish_stream2word
  [D] _fini
  [D] _init
  [D] _libssh2_aes_ctr_increment
  [D] _libssh2_base64_encode
  [D] _libssh2_bcrypt_pbkdf
  [D] _libssh2_calloc
  [D] _libssh2_channel_close
  [D] _libssh2_channel_extended_data
  [D] _libssh2_channel_flush
  [D] _libssh2_channel_forward_cancel
  [D] _libssh2_channel_free
  [D] _libssh2_channel_locate
  [D] _libssh2_channel_nextid
  [D] _libssh2_channel_open
...

From CMake 3.12, we can set the default symbol visibility to 'hidden' and generate the macro LIBSSH2_API in a portable way. (https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html)

I send you a proposal. Maybe we can rework it so that we only enable hidden symbols visibility by default only when CMake version >=3.12.
Though I feel, the minimal required version of CMake is outdated. It is perhaps worth upgrading it.
Whatever the decision, I can resubmit the PR with your comments.

Regards,
Laurent

@stac47

stac47 commented May 18, 2021

Copy link
Copy Markdown
Contributor Author

This PR failed due to:

toomanyrequests: You have reached your pull rate limit. You may increase the limit by authenticating and upgrading: https://www.docker.com/increase-rate-limit

@stac47

stac47 commented May 19, 2021

Copy link
Copy Markdown
Contributor Author

Just a remark regarding the minimal CMake required version, the latest CMake (3.20) states:

CMake Deprecation Warning at CMakeLists.txt:17 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.

So I think at some point, we will have to upgrade.

@willco007

Copy link
Copy Markdown
Member

FWIW, CMake 3.12 came out in July 2018.

@stac47

stac47 commented May 20, 2021 •

Copy link
Copy Markdown
Contributor Author

@willco007 I should have elaborated a bit more in my previous message to avoid any confusion. What I meant is that I changed the cmake_minimum_required from 2.8.11 to 3.12.0. For sure, it is maybe a bit an aggressive change. What I wanted to say, is that we will have at least to set a value in the 3.x series as the warning states.
If the 3.x chosen version is below 3.12.0, we will have to conditionally enable the hidden symbols visibility.

@stale

stale Bot commented Apr 16, 2022

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Apr 16, 2022
@willco007

willco007 commented Apr 18, 2022 •

Copy link
Copy Markdown
Member

I'm not sure hiding symbols is really necessary as this is an open source library and people can find the symbols easily. However, this PR seems fine with me after being rebased.

@stale stale Bot removed the stale label Apr 18, 2022
@dfandrich

dfandrich commented Apr 18, 2022 via email

Copy link
Copy Markdown
Contributor

@stale

stale Bot commented Sep 21, 2022

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale Bot added the stale label Sep 21, 2022
@willco007

Copy link
Copy Markdown
Member

Sorry for the delay on this one, it needs to be rebased and we'll get it merged.

@stale stale Bot removed the stale label Sep 21, 2022
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 9, 2023
- implement symbol hiding on non-Windows platforms.

  The essence of the detection logic was copied from:
  https://github.com/curl/curl/blob/dfabe8bca218d2524af052bd551aa87e13b8a10b/CMake/CurlSymbolHiding.cmake

  Then simplified and shortened. This method doesn't require a recent
  CMake version, nor an external, auto-generated C header.

  Move `configure_file()` after `set(LIBSSH2_API ...)`, for the config
  file to pick up `LIBSSH2_API`s value.

  Fixes libssh2#602

- add CMake option `HIDE_SYMBOLS`.

  This setting means to hide non-public functions from the libssh2
  dynamic library when set to `ON`. The default.

  When set to `OFF`, make all non-static/internal functions visible
  in the dynamic library.

- honor this setting on Windows.

  By setting the `LIBSSH2_EXPORTS` manual macro again, and stop
  recognizing the automatic CMake macro for this purpose:
  `libssh2_shared_EXPORT`.

Closes #xxx
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 9, 2023
- implement symbol hiding on non-Windows platforms.

  The essence of the detection logic was copied from:
  https://github.com/curl/curl/blob/dfabe8bca218d2524af052bd551aa87e13b8a10b/CMake/CurlSymbolHiding.cmake

  Then simplified and shortened. This method doesn't require a recent
  CMake version, nor an external, auto-generated C header.

  Move `configure_file()` after `set(LIBSSH2_API ...)`, for the config
  file to pick up `LIBSSH2_API`s value.

  Closes libssh2#602

- add CMake option `HIDE_SYMBOLS`.

  This setting means to hide non-public functions from the libssh2
  dynamic library when set to `ON`. The default.

  When set to `OFF`, make all non-static/internal functions visible
  in the dynamic library.

  This setting requires `BUILD_SHARED_LIBS=ON`.

- honor this setting on Windows.

  By setting the `LIBSSH2_EXPORTS` manual macro again, and stop
  recognizing the automatic CMake macro for this purpose:
  `libssh2_shared_EXPORT`.

Closes #xxx

only with shared libs enabled
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 9, 2023
- implement symbol hiding on non-Windows platforms.

  The essence of the detection logic was copied from:
  https://github.com/curl/curl/blob/dfabe8bca218d2524af052bd551aa87e13b8a10b/CMake/CurlSymbolHiding.cmake

  Then simplified and shortened. This method doesn't require a recent
  CMake version, nor an external, auto-generated C header.

  Move `configure_file()` after `set(LIBSSH2_API ...)`, for the config
  file to pick up `LIBSSH2_API`s value.

  Closes libssh2#602

- add CMake option `HIDE_SYMBOLS`.

  This setting means to hide non-public functions from the libssh2
  dynamic library when set to `ON`. The default.

  When set to `OFF`, make all non-static/internal functions visible
  in the dynamic library.

  This setting requires `BUILD_SHARED_LIBS=ON`.

- honor this setting on Windows.

  By setting the `LIBSSH2_EXPORTS` manual macro again, and stop
  recognizing the automatic CMake macro for this purpose:
  `libssh2_shared_EXPORT`.

Closes libssh2#939

only with shared libs enabled
@vszakats vszakats closed this in 8017592 Apr 9, 2023
agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
- implement symbol hiding on non-Windows platforms.

  The essence of the detection logic was copied from:
  https://github.com/curl/curl/blob/dfabe8bca218d2524af052bd551aa87e13b8a10b/CMake/CurlSymbolHiding.cmake

  Then simplified and shortened. This method doesn't require a recent
  CMake version, nor an external, auto-generated C header.

  Move `configure_file()` after `set(LIBSSH2_API ...)`, for the config
  file to pick up `LIBSSH2_API`s value.

  Closes libssh2#602

- add CMake option `HIDE_SYMBOLS`.

  This setting means to hide non-public functions from the libssh2
  dynamic library when set to `ON`. The default.

  When set to `OFF`, make all non-static/internal functions visible
  in the dynamic library.

  This setting requires `BUILD_SHARED_LIBS=ON`.

- honor this setting on Windows.

  By setting the `LIBSSH2_EXPORTS` manual macro again, and stop
  recognizing the automatic CMake macro for this purpose:
  `libssh2_shared_EXPORT`.

Closes libssh2#939
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