@@ -96,7 +96,8 @@ CompileFlags compute_flags(const BuildPlan& plan) {
9696 // potentially-stale paths, then provide all flags explicitly.
9797 //
9898 // Fallback: if no PayloadPaths, use --sysroot from probe_sysroot().
99- std::string sysroot_flag;
99+ std::string compile_toolchain_flags;
100+ std::string link_toolchain_flags;
100101 bool isClangWithCfg = false ;
101102 std::filesystem::path cfgPath;
102103 if (mcpp::toolchain::is_clang (plan.toolchain )) {
@@ -109,51 +110,54 @@ CompileFlags compute_flags(const BuildPlan& plan) {
109110 // Clang with cfg: bypass cfg and provide all paths explicitly.
110111 auto llvmRoot = plan.toolchain .binaryPath .parent_path ().parent_path ();
111112 auto libcxxInclude = llvmRoot / " include" / " c++" / " v1" ;
112- sysroot_flag = " --no-default-config -nostdinc++" ;
113+ compile_toolchain_flags = " --no-default-config -nostdinc++" ;
113114 // libc++ headers
114- sysroot_flag += " -stdlib=libc++" ;
115- sysroot_flag += " -isystem" + escape_path (libcxxInclude);
115+ compile_toolchain_flags += " -isystem" + escape_path (libcxxInclude);
116116 if (!plan.toolchain .targetTriple .empty ()) {
117117 auto targetInclude = llvmRoot / " include"
118118 / plan.toolchain .targetTriple / " c++" / " v1" ;
119119 if (std::filesystem::exists (targetInclude))
120- sysroot_flag += " -isystem" + escape_path (targetInclude);
120+ compile_toolchain_flags += " -isystem" + escape_path (targetInclude);
121121 }
122122 // C library + kernel headers from payload
123123 if (plan.toolchain .payloadPaths ) {
124124 auto & pp = *plan.toolchain .payloadPaths ;
125- sysroot_flag += " -isystem" + escape_path (pp.glibcInclude );
125+ compile_toolchain_flags += " -isystem" + escape_path (pp.glibcInclude );
126126 if (!pp.linuxInclude .empty ())
127- sysroot_flag += " -isystem" + escape_path (pp.linuxInclude );
127+ compile_toolchain_flags += " -isystem" + escape_path (pp.linuxInclude );
128128 } else if (auto sdk = mcpp::platform::macos::sdk_path ()) {
129- sysroot_flag += " --sysroot=" + escape_path (*sdk);
129+ auto sysroot_flag = " --sysroot=" + escape_path (*sdk);
130+ compile_toolchain_flags += sysroot_flag;
131+ link_toolchain_flags += sysroot_flag;
130132 } else if (!plan.toolchain .sysroot .empty ()) {
131- sysroot_flag += " --sysroot=" + escape_path (plan.toolchain .sysroot );
133+ auto sysroot_flag = " --sysroot=" + escape_path (plan.toolchain .sysroot );
134+ compile_toolchain_flags += sysroot_flag;
135+ link_toolchain_flags += sysroot_flag;
132136 }
133137 // Linker flags that cfg normally provides
134- sysroot_flag += " -fuse-ld=lld --rtlib=compiler-rt --unwindlib=libunwind" ;
135- f.sysroot = sysroot_flag;
138+ link_toolchain_flags = " --no-default-config" + link_toolchain_flags
139+ + " -stdlib=libc++ -fuse-ld=lld --rtlib=compiler-rt --unwindlib=libunwind" ;
140+ f.sysroot = link_toolchain_flags;
136141 } else if (!plan.toolchain .sysroot .empty ()) {
137142 // GCC (or Clang without cfg): use --sysroot from probe.
138143 // GCC requires --sysroot for include-fixed headers (stdlib.h wrapper).
139144 // Supplement with -isystem for linux kernel headers from payload
140145 // if the probed sysroot is missing them.
141- sysroot_flag = " --sysroot=" + escape_path (plan.toolchain .sysroot );
146+ auto sysroot_flag = " --sysroot=" + escape_path (plan.toolchain .sysroot );
147+ compile_toolchain_flags = sysroot_flag;
148+ link_toolchain_flags = sysroot_flag;
142149 if (plan.toolchain .payloadPaths && !plan.toolchain .payloadPaths ->linuxInclude .empty ()) {
143150 auto sysrootLinux = plan.toolchain .sysroot / " usr" / " include" / " linux" / " limits.h" ;
144151 if (!std::filesystem::exists (sysrootLinux))
145- sysroot_flag += " -isystem" + escape_path (plan.toolchain .payloadPaths ->linuxInclude );
152+ compile_toolchain_flags += " -isystem" + escape_path (plan.toolchain .payloadPaths ->linuxInclude );
146153 }
147- f.sysroot = sysroot_flag ;
154+ f.sysroot = link_toolchain_flags ;
148155 } else if (plan.toolchain .payloadPaths ) {
149156 // No sysroot but have payload paths: use -isystem.
150157 auto & pp = *plan.toolchain .payloadPaths ;
151- sysroot_flag += " -isystem" + escape_path (pp.glibcInclude );
158+ compile_toolchain_flags += " -isystem" + escape_path (pp.glibcInclude );
152159 if (!pp.linuxInclude .empty ())
153- sysroot_flag += " -isystem" + escape_path (pp.linuxInclude );
154- f.sysroot = sysroot_flag;
155- sysroot_flag = " --sysroot=" + escape_path (plan.toolchain .sysroot );
156- f.sysroot = sysroot_flag;
160+ compile_toolchain_flags += " -isystem" + escape_path (pp.linuxInclude );
157161 }
158162
159163 // Binutils -B flag
@@ -224,9 +228,9 @@ CompileFlags compute_flags(const BuildPlan& plan) {
224228 }
225229 f.cxx = std::format (" -std=c++23{}{}{}{}{}{}{}{}{}{}" , module_flag, std_module_flag,
226230 std_compat_module_flag, prebuilt_module_flag,
227- opt_flag, pic_flag, sysroot_flag , b_flag, include_flags, user_cxxflags);
228- f.cc = std::format (" -std={}{}{}{}{}{}{}" , c_std, opt_flag, pic_flag, sysroot_flag, b_flag ,
229- include_flags, user_cflags);
231+ opt_flag, pic_flag, compile_toolchain_flags , b_flag, include_flags, user_cxxflags);
232+ f.cc = std::format (" -std={}{}{}{}{}{}{}" , c_std, opt_flag, pic_flag, compile_toolchain_flags ,
233+ b_flag, include_flags, user_cflags);
230234
231235 // Link flags
232236 f.staticStdlib = plan.manifest .buildConfig .staticStdlib ;
@@ -257,7 +261,8 @@ CompileFlags compute_flags(const BuildPlan& plan) {
257261 } else if constexpr (mcpp::platform::needs_explicit_libcxx) {
258262 f.ld = std::format (" {}{}{} -lc++" , full_static, static_stdlib, b_flag);
259263 } else {
260- f.ld = std::format (" {}{}{}{}{}{}" , full_static, static_stdlib, sysroot_flag, b_flag, runtime_dirs, payload_ld);
264+ f.ld = std::format (" {}{}{}{}{}{}" , full_static, static_stdlib, link_toolchain_flags, b_flag,
265+ runtime_dirs, payload_ld);
261266 }
262267
263268 return f;
0 commit comments