bazel-contrib / bazel-contrib/rules_foreign_cc
CMake build script always sets `CMAKE_RANLIB` to an empty string.
- Dominant language
- Starlark
- Stars
- 737
- Forks
- 270
- PR merge metrics
- No merged PRs in 30d
Description
The [cmake build script](https://github.com/bazelbuild/rules_foreign_cc/tree/99d018f0592bd7de492cbc2fa5e44828aea1ef4c/foreign_cc/private/cmake_script.bzl) always overrides the cache value `CMAKE_RANLIB` with an empty string.
This is, I assume, due to the [following snippet](https://github.com/bazelbuild/rules_foreign_cc/tree/99d018f0592bd7de492cbc2fa5e44828aea1ef4c/foreign_cc/private/cmake_script.bzl):
```python
# However, if no CMAKE_RANLIB was passed, pass the empty value for it explicitly,
# as it is legacy and autodetection of ranlib made by CMake automatically
# breaks some cross compilation builds,
# see https://github.com/envoyproxy/envoy/pull/6991
if not params.cache.get("CMAKE_RANLIB"):
params.cache.update({"CMAKE_RANLIB": ""})
```
As far as I can tell, the condition of this if-statement always evaluates to `True` since as `params.cache` is built the item corresponding to the key `CMAKE_RANLIB` is popped (i.e. removed) if it was present ([here](https://github.com/bazelbuild/rules_foreign_cc/blob/99d018f0592bd7de492cbc2fa5e44828aea1ef4c/foreign_cc/private/cmake_script.bzl#L290)) and naturally evaluates to `True` if the mentioned key was not present at all.
I assume to fix this one need to do basically do two things:
- Check the condition of the if-statement based on the input variable `user_cache` which stores the cache entries as they are taken
- And put the original value back into `params.cache` from `user_cache` if it is present. I.e. something like this:
```diff
diff --git a/foreign_cc/private/cmake_script.bzl b/foreign_cc/private/cmake_script.bzl
index bfe21bd..b4410ee 100644
--- a/foreign_cc/private/cmake_script.bzl
+++ b/foreign_cc/private/cmake_script.bzl
@@ -113,8 +113,15 @@ def create_cmake_script(
# as it is legacy and autodetection of ranlib made by CMake automatically
# breaks some cross compilation builds,
# see https://github.com/envoyproxy/envoy/pull/6991
- if not params.cache.get("CMAKE_RANLIB"):
+ # We do this by checking the key in `user_cache` since from `params.cache`
+ # this value is moved even if it was present before by the following call:
+ # https://github.com/bazelbuild/rules_foreign_cc/blob/99d018f0592bd7de492cbc2fa5e44828aea1ef4c/foreign_cc/private/cmake_script.bzl#L290
+ # Additionally, we also put the original value back if it got removed by
+ # previously.
+ if not user_cache.get("CMAKE_RANLIB"):
params.cache.update({"CMAKE_RANLIB": ""})
+ else:
+ params.cache.update({"CMAKE_RANLIB": user_cache.get("CMAKE_RANLIB")})
# Avoid CMake passing the wrong linker flags when cross compiling
# by setting CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR,
```
Contributor guide
Research direction
Start in foreign_cc/private/cmake_script.bzl at create_cmake_script and inspect how user_cache and params.cache are handled around the referenced cache-entry removal. Preserve an explicitly supplied CMAKE_RANLIB value while retaining the empty fallback when it was not supplied; done means the generated CMake configuration no longer overrides the cache value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100