Skip to content

cmake: work around ios.toolchain.cmake breaking feature-detections - #15559

Closed
vszakats wants to merge 12 commits into
curl:masterfrom
vszakats:cm-getpass-r-apple
Closed

vszakats wants to merge 12 commits into
curl:masterfrom
vszakats:cm-getpass-r-apple

Conversation

@vszakats

@vszakats vszakats commented Nov 12, 2024 •

Copy link
Copy Markdown
Member

Fix builds with CMake configured to falsely return successful detection
when using check_function_exists() (and check_library_exists(), and
anything based on try_compile() that's relying on the linker). After
such mis-detection the build fails when trying to use the feature that
doesn't in fact exist.

The mis-detection is caused by this CMake setting:

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

It is set by default (or on conditions) when using 3rd-party toolchain:
https://github.com/leetal/ios-cmake/blob/master/ios.toolchain.cmake

After this patch the curl build overrides this setting for the duration
of feature tests, and logs a message about it.

Also preset and skip feature tests for known mis-detections:

  • connect() in libsocket
  • getpass_r()
  • eventfd() (did not cause an issue due to a separate bug)
  • sendmmsg() (did not cause an issue because it's Linux-only)

If mis-detections are still seen, the workaround is to force-set the
specific feature by passing -DHAVE_*=OFF to cmake.
Also consider passing -DENABLE_STRICT_TRY_COMPILE=ON for
ios.toolchain.cmake to fix the root cause.

Interestingly curl itself uses this setting to speed up compile-only
detections: be17f29 #3744

Also:

  • OtherTests.cmake: restore original value of
    CMAKE_TRY_COMPILE_TARGET_TYPE. Before this patch it reset it
    to empty.
  • OtherTests.cmake: unset a local variable after use, quote a string.

Follow-up to 8e34505 #15164
Follow-up to 8b76a8a #15525
Ref: leetal/ios-cmake#47
Ref: https://gitlab.kitware.com/cmake/cmake/-/issues/18121
Ref: https://cmake.org/cmake/help/latest/variable/CMAKE_TRY_COMPILE_TARGET_TYPE.html
Reported-by: Dan Rosser
Fixes #15557

@vszakats vszakats added regression cmake appleOS specific to an Apple operating system labels Nov 12, 2024
@github-actions github-actions Bot added the build label Nov 12, 2024
@vszakats vszakats changed the title cmake: do not use getpass_r with Apple cmake: do not use getpass_r() with Apple Nov 12, 2024
@vszakats

Copy link
Copy Markdown
Member Author

The workaround for 8.11.0 is passing -DHAVE_GETPASS_R=0 to cmake.

@vszakats

This comment was marked as duplicate.

@vszakats

vszakats commented Nov 12, 2024 •

Copy link
Copy Markdown
Member Author

Upstream issues that look similar to the fallouts reported for curl:
https://gitlab.kitware.com/cmake/cmake/-/issues/18121
https://gitlab.kitware.com/cmake/cmake/-/issues/21971
https://gitlab.kitware.com/cmake/cmake/-/issues/25479
leetal/ios-cmake#47
leetal/ios-cmake@7658382

Based on these, this CMake setting:

  set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

breaks CMake's built-in check_function_exists() and check_library_exists()
feature checks by making them falsely return success.

CMake manual says:

STATIC_LIBRARY
Use add_library() with the STATIC option to name the source file in the generated project. This avoids running the linker and is intended for use with cross-compiling toolchains that cannot link without custom flags or linker scripts.

ios.toolchain.cmake uses this setting by default:

[...]
# ENABLE_STRICT_TRY_COMPILE: (ON|OFF) Enables or disables strict try_compile() on all Check* directives (will run linker
#    to actually check if linking is possible). Default OFF (will set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY)
[...]
if(NOT DEFINED ENABLE_STRICT_TRY_COMPILE)
  # Unless specified, disable strict try_compile()
  set(ENABLE_STRICT_TRY_COMPILE OFF)
  message(STATUS "[DEFAULTS] Using NON-strict compiler checks by default. ENABLE_STRICT_TRY_COMPILE not provided!")
endif()
set(ENABLE_STRICT_TRY_COMPILE_INT ${ENABLE_STRICT_TRY_COMPILE} CACHE BOOL
        "Whether or not to use strict compiler checks" FORCE)
[...]
if(NOT ENABLE_STRICT_TRY_COMPILE_INT)
  set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
endif()
[...]

https://github.com/leetal/ios-cmake/blob/ad96a372b168930c2a1ff9455e1a9ccb13021617/ios.toolchain.cmake

(An odd option to use by default because linking is supposed to work in cross-compilation.)

Earlier versions enabled this behavior unconditionally, later modified to do it for the Xcode generator, and later changed to the on-by-default separate option above.

In report #15557, the CMake configuration already has a workaround for a
mis-detected libsocket: -DHAVE_LIBSOCKET=OFF. This was not seen
in CI and likely caused by the same upstream issue.

TL;DR: ios.toolchain.cmake can break builds using the CMake built-ins
check_function_exists() and check_library_exists().

curl can try to work this around, but I don't consider this a curl issue. It's
reasonable to expected that detection functions work as designed and
are not steered to return wrong results.

A possible clean workaround is passing -DENABLE_STRICT_TRY_COMPILE=ON
to ios.toolchain.cmake to stop this behaviour.

@vszakats vszakats changed the title cmake: do not use getpass_r() with Apple cmake: never detect getpass_r() with Apple Nov 12, 2024
It's not offered by Apple operating systems, but in some cases it gets
mis-detected when using `check_function_exists()`. Then making the build
fail later.

A better fix would be to figure out why are these mis-detections. One
common pattern seen is this option, but it may or not be the cause:
```
-DCMAKE_TOOLCHAIN_FILE=[...]/toolchains/ios.toolchain.cmake
```
Ref: curl#15525

Regression from 8e34505 curl#15164

Reported-by: Dan Rosser
Fixes curl#15557
Closes curl#15558
It requires Linux, so it's safe to ignore this for Apple
@vszakats vszakats changed the title cmake: never detect getpass_r() with Apple cmake: work around ios.toolchain.cmake feature-detection issues Nov 12, 2024
@vszakats vszakats changed the title cmake: work around ios.toolchain.cmake feature-detection issues cmake: work around ios.toolchain.cmake breaking feature-detections Nov 12, 2024
@dfandrich

Copy link
Copy Markdown
Contributor

Analysis of PR #15559 at 62c5d298:

Test 1114 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Test 313 failed, which has NOT been flaky recently, so there could be a real issue in this PR.

Generated by Testclutch

@vszakats

vszakats commented Nov 13, 2024 •

Copy link
Copy Markdown
Member Author

I've pushed a commit that overrides the problematic setting with the one
necessary to make feature checks work.

This may have a risk for cases when the problematic setting is supposed to
fix something. If someone is aware of such case, leave a comment.

(We can limit it to APPLE if there is a legit case on a different platform.)

@vszakats

Copy link
Copy Markdown
Member Author

@danoli3 Does this patch resolve the issue you reported?

@danoli3

danoli3 commented Nov 13, 2024

Copy link
Copy Markdown

Yes and thank you! Some insights into some other library issues with the findings in ios toolchain thanks mate!

@vszakats vszakats closed this in 93e6e4b Nov 13, 2024
@vszakats
vszakats deleted the cm-getpass-r-apple branch November 13, 2024 09:43
vszakats added a commit that referenced this pull request Nov 13, 2024
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Fix builds with CMake configured to falsely return successful detection
when using `check_function_exists()` (and `check_library_exists()`, and
anything based on `try_compile()` that's relying on the linker). After
such mis-detection the build fails when trying to use the feature that
doesn't in fact exist.

The mis-detection is caused by this CMake setting:
```
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
```
It is set by default (or on conditions) when using 3rd-party toolchain:
https://github.com/leetal/ios-cmake/blob/master/ios.toolchain.cmake

After this patch the curl build overrides this setting for the duration
of feature tests, and logs a message about it.

Also preset and skip feature tests for known mis-detections:
- `connect()` in `libsocket`
- `getpass_r()`
- `eventfd()` (did not cause an issue due to a separate bug)
- `sendmmsg()` (did not cause an issue because it's Linux-only)

If mis-detections are still seen, the workaround is to force-set the
specific feature by passing `-DHAVE_*=OFF` to cmake.
Also consider passing `-DENABLE_STRICT_TRY_COMPILE=ON` for
`ios.toolchain.cmake` to fix the root cause.

Interestingly curl itself uses this setting to speed up compile-only
detections: be17f29 curl#3744

Also:
- OtherTests.cmake: restore original value of
  `CMAKE_TRY_COMPILE_TARGET_TYPE`. Before this patch it reset it
  to empty.
- OtherTests.cmake: unset a local variable after use, quote a string.

Follow-up to 8e34505 curl#15164
Follow-up to 8b76a8a curl#15525
Ref: leetal/ios-cmake#47
Ref: https://gitlab.kitware.com/cmake/cmake/-/issues/18121
Ref: https://cmake.org/cmake/help/latest/variable/CMAKE_TRY_COMPILE_TARGET_TYPE.html
Reported-by: Dan Rosser
Fixes curl#15557
Closes curl#15559
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

appleOS specific to an Apple operating system build cmake

Development

Successfully merging this pull request may close these issues.

8.11.0 - macOS getpass_r error building CMake

3 participants