Skip to content

passthrough cc env/args using native cc features#158

Open
PDXKimani wants to merge 2 commits intotikv:mainfrom
PDXKimani:cc-env-passthrough
Open

passthrough cc env/args using native cc features#158
PDXKimani wants to merge 2 commits intotikv:mainfrom
PDXKimani:cc-env-passthrough

Conversation

@PDXKimani
Copy link

@PDXKimani PDXKimani commented Mar 17, 2026

Use the cc-native cflags_env() and cc_env() to generate the build command.

Feature-wise, this means we'll pull through configured CC wrappers (e.g. ccache/sccache) and use them for the build.

Summary by CodeRabbit

  • Bug Fixes
    • More reliable handling of compiler flags (CFLAGS, LDFLAGS, CPPFLAGS) during configuration.
    • Improved detection and resolution of the C compiler with sensible fallbacks.
    • Environment passed to build/config steps now consistently uses resolved compiler and flags.
    • Build logs now surface the resolved compiler and preserve flag diagnostics.
    • Better support for compiler wrapper tools during builds.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 17, 2026

Welcome @PDXKimani! It looks like this is your first PR to tikv/jemallocator 🎉

@coderabbitai
Copy link

coderabbitai bot commented Mar 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9fb72f7d-1f58-4813-9182-3fa16693b66b

📥 Commits

Reviewing files that changed from the base of the PR and between d0bf012 and fbf26aa.

📒 Files selected for processing (1)
  • jemalloc-sys/build.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • jemalloc-sys/build.rs

📝 Walkthrough

Walkthrough

Updates jemalloc-sys/build.rs to source compiler/linker flags and the full compiler command from environment-aware helpers (compiler.cflags_env(), compiler.cc_env(), read_and_watch_env("LDFLAGS")), then injects CC, CFLAGS, LDFLAGS, and CPPFLAGS into the configure environment and logs them.

Changes

Cohort / File(s) Summary
Build Script Enhancement
jemalloc-sys/build.rs
Replaced manual flag extraction with compiler.cflags_env(); read LDFLAGS via read_and_watch_env("LDFLAGS"); resolve full CC with compiler.cc_env() (fallback to compiler.path()); pass resolved CC, CFLAGS, LDFLAGS, and CPPFLAGS into the configure environment and update logging.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • BusyJay

Poem

🐰 I sniffed the flags beneath the log,

Found CC wrapped in a clever bog,
CFLAGS sang, LDFLAGS chimed in tune,
I set the env, and built by moon. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: using native cc crate features (cflags_env() and cc_env()) to pass through compiler environment variables and arguments, which is the core objective of the pull request.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Signed-off-by: Dylan Abraham <dylan@momentohq.com>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@jemalloc-sys/build.rs`:
- Around line 158-160: The fallback for LDFLAGS incorrectly uses the CFLAGS
value (ldflags is set to cflags.clone()), which can pass compiler-only flags to
the linker; change the fallback in the read_and_watch_env("LDFLAGS")
unwrap_or_else to return an empty OsString instead of cflags.clone() so ldflags
only contains explicit linker flags; update the expression referencing
read_and_watch_env, ldflags, and cflags accordingly.
- Around line 157-169: The build script calls cc::Tool methods cc_env() and
cflags_env() which require cc >= 1.0.26, so update the build-dependency entry
for cc in Cargo.toml to require version 1.0.26 or later (e.g., change the
build-dependency from "cc = \"1.0.13\"" to "cc = \"^1.0.26\"") so the cc_env()
and cflags_env() methods used in build.rs are available at compile time.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0e7dc601-cfbe-4933-8d60-d115e9058f08

📥 Commits

Reviewing files that changed from the base of the PR and between baae33d and a1b43f0.

📒 Files selected for processing (1)
  • jemalloc-sys/build.rs

Comment on lines +157 to +169
let cflags = compiler.cflags_env();
let ldflags = read_and_watch_env("LDFLAGS")
.map(OsString::from)
.unwrap_or_else(|_| cflags.clone());

// Use cc_env() to get the full CC value including any wrapper (e.g. sccache).
// cc_env() returns empty when no wrapper is configured, so fall back to path().
let cc = compiler.cc_env();
let cc = if cc.is_empty() {
compiler.path().as_os_str().to_owned()
} else {
cc
};
Copy link

@coderabbitai coderabbitai bot Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

cc crate Rust cflags_env cc_env methods version

💡 Result:

In the Rust cc crate, cc_env and cflags_env are methods on cc::Tool (not on cc::Build):

  • Tool::cc_env() -> OsString: returns a string formatted like the CC environment variable (primarily useful when CC is a wrapper like sccache cc), or an empty string if no such wrapper/CC env was involved. [1]
  • Tool::cflags_env() -> OsString: returns the tool’s computed compiler arguments formatted like the CFLAGS environment variable (note: it’s the tool’s internal args, not necessarily the user’s CFLAGS). [1]

Typical usage:

let tool = cc::Build::new().get_compiler();
let cc = tool.cc_env();
let cflags = tool.cflags_env();

What version added these methods?

They were added after cc 1.0.0 and are present by cc 1.0.26:

  • Absent in 1.0.0 (Tool has no cc_env/cflags_env). [2]
  • Present in 1.0.26 (pub fn cc_env and pub fn cflags_env exist). [3]

So, if you need these APIs, require cc >= 1.0.26.

Sources:
[1] cc::Tool docs (latest) showing cc_env / cflags_env and their semantics. (docs.rs)
[2] cc-rs tag upstream/1.0.0 src/lib.rs (no cc_env / cflags_env). (chromium.googlesource.com)
[3] cc-rs tag upstream/1.0.26 src/lib.rs (includes cc_env / cflags_env). (chromium.googlesource.com)

Citations:


🏁 Script executed:

find . -name "Cargo.toml" -type f | xargs grep -l "cc\s*=" | head -5

Repository: tikv/jemallocator

Length of output: 110


🏁 Script executed:

find . -name "Cargo.toml" -type f -path "*/jemalloc-sys/*" -exec cat {} \;

Repository: tikv/jemallocator

Length of output: 1635


Update cc dependency to require version 1.0.26 or later.

The code uses cc_env() and cflags_env() methods on cc::Tool, which were not available in cc 1.0.13 (the minimum version specified in Cargo.toml). These methods were added in cc 1.0.26. Update the build-dependency to cc = "^1.0.26" to ensure the methods are available.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@jemalloc-sys/build.rs` around lines 157 - 169, The build script calls
cc::Tool methods cc_env() and cflags_env() which require cc >= 1.0.26, so update
the build-dependency entry for cc in Cargo.toml to require version 1.0.26 or
later (e.g., change the build-dependency from "cc = \"1.0.13\"" to "cc =
\"^1.0.26\"") so the cc_env() and cflags_env() methods used in build.rs are
available at compile time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PDXKimani, you are correct — I apologize for the incorrect review comment. The cc_env() and cflags_env() methods on cc::Tool are present even in cc 1.0.10, and per the docs you linked, they are definitely available in cc 1.0.13 as well. No version bump is needed. My earlier concern was mistaken.


✏️ Learnings added
Learnt from: PDXKimani
Repo: tikv/jemallocator PR: 158
File: jemalloc-sys/build.rs:157-169
Timestamp: 2026-03-17T18:53:45.020Z
Learning: The cc crate's Tool::cc_env() and Tool::cflags_env() methods are available since at least cc 1.0.10 (and are present in cc 1.0.13). Do not flag their usage as requiring a version bump from 1.0.13 or later in jemallocator/jemalloc-sys/build.rs.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (2)
jemalloc-sys/build.rs (2)

157-169: ⚠️ Potential issue | 🟠 Major

Bump the cc minimum version before using Tool::cc_env / Tool::cflags_env.

Line 157 and Line 164 use APIs that are not guaranteed by a cc = "1.0.13" floor. This can fail in minimal-version or older-lockfile builds; raise the build-dependency floor accordingly (e.g., >=1.0.26).

#!/bin/bash
set -euo pipefail

# Verify current cc build-dependency constraints in the repo
fd Cargo.toml | while read -r file; do
  if rg -n '^\s*\[build-dependencies\]|^\s*cc\s*=' "$file" >/dev/null; then
    echo "== $file =="
    rg -n -A3 -B2 '^\s*\[build-dependencies\]|^\s*cc\s*=' "$file"
    echo
  fi
done
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@jemalloc-sys/build.rs` around lines 157 - 169, The build script uses newer
Tool APIs (Tool::cc_env and Tool::cflags_env via compiler.cc_env() and
compiler.cflags_env()) that require a newer cc crate than 1.0.13; update the
build-dependencies entry for cc in Cargo.toml to a minimum version that includes
these methods (e.g., >=1.0.26) so builds with minimal-version or older lockfiles
succeed, then regenerate/update the lockfile (cargo update) to ensure the newer
cc is used during builds.

158-160: ⚠️ Potential issue | 🟠 Major

Don’t default LDFLAGS to CFLAGS.

Line 160 currently falls back to cflags.clone(), which can inject compile-only flags into linker flags and break configure/link probes.

Suggested fix
 let ldflags = read_and_watch_env("LDFLAGS")
     .map(OsString::from)
-    .unwrap_or_else(|_| cflags.clone());
+    .unwrap_or_default();
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@jemalloc-sys/build.rs` around lines 158 - 160, The code incorrectly falls
back to using cflags for linker flags; change the ldflags assignment so
read_and_watch_env("LDFLAGS") does NOT default to cflags.clone() — instead use
an empty OsString (e.g., unwrap_or_else(|_| OsString::new())) or another
linker-appropriate default; update the variable created by the call to
read_and_watch_env to avoid injecting compile-only flags into linker flags
(refer to read_and_watch_env, ldflags, and cflags).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@jemalloc-sys/build.rs`:
- Around line 157-169: The build script uses newer Tool APIs (Tool::cc_env and
Tool::cflags_env via compiler.cc_env() and compiler.cflags_env()) that require a
newer cc crate than 1.0.13; update the build-dependencies entry for cc in
Cargo.toml to a minimum version that includes these methods (e.g., >=1.0.26) so
builds with minimal-version or older lockfiles succeed, then regenerate/update
the lockfile (cargo update) to ensure the newer cc is used during builds.
- Around line 158-160: The code incorrectly falls back to using cflags for
linker flags; change the ldflags assignment so read_and_watch_env("LDFLAGS")
does NOT default to cflags.clone() — instead use an empty OsString (e.g.,
unwrap_or_else(|_| OsString::new())) or another linker-appropriate default;
update the variable created by the call to read_and_watch_env to avoid injecting
compile-only flags into linker flags (refer to read_and_watch_env, ldflags, and
cflags).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: dcbaef5f-952f-4f14-9e43-895ad7d65e2f

📥 Commits

Reviewing files that changed from the base of the PR and between a1b43f0 and fabd9f3.

📒 Files selected for processing (1)
  • jemalloc-sys/build.rs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Dylan Abraham <dylan@momentohq.com>
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.

1 participant