@@ -93,6 +93,15 @@ std::string local_include_flags(const CompileUnit& cu) {
9393 return flags;
9494}
9595
96+ std::string join_flags (const std::vector<std::string>& flags) {
97+ std::string out;
98+ for (auto const & flag : flags) {
99+ out += ' ' ;
100+ out += flag;
101+ }
102+ return out;
103+ }
104+
96105void write_file (const std::filesystem::path& p, std::string_view content) {
97106 std::filesystem::create_directories (p.parent_path ());
98107 std::ofstream os (p);
@@ -315,13 +324,13 @@ std::string emit_ninja_string(const BuildPlan& plan) {
315324 if constexpr (mcpp::platform::is_windows) {
316325 // Windows: skip BMI restat optimization (requires POSIX shell).
317326 append (std::format (" command = "
318- " $cxx $local_includes $cxxflags{} -c $in -o $out\n " , module_output_flag));
327+ " $cxx $local_includes $cxxflags $unit_cxxflags {} -c $in -o $out\n " , module_output_flag));
319328 } else {
320329 append (std::format (" command = "
321330 " if [ -n \" $bmi_out\" ] && [ -f \" $bmi_out\" ]; then "
322331 " cp -p \" $bmi_out\" \" $bmi_out.bak\" ; "
323332 " fi && "
324- " $cxx $local_includes $cxxflags{} -c $in -o $out && "
333+ " $cxx $local_includes $cxxflags $unit_cxxflags {} -c $in -o $out && "
325334 " if [ -n \" $bmi_out\" ] && [ -f \" $bmi_out.bak\" ] && "
326335 " cmp -s \" $bmi_out\" \" $bmi_out.bak\" ; then "
327336 " mv \" $bmi_out.bak\" \" $bmi_out\" ; "
@@ -335,15 +344,15 @@ std::string emit_ninja_string(const BuildPlan& plan) {
335344 append (" \n " );
336345
337346 append (" rule cxx_object\n " );
338- append (" command = $cxx $local_includes $cxxflags -c $in -o $out\n " );
347+ append (" command = $cxx $local_includes $cxxflags $unit_cxxflags -c $in -o $out\n " );
339348 append (" description = OBJ $out\n " );
340349 if (dyndep)
341350 append (" restat = 1\n " );
342351 append (" \n " );
343352
344353 if (need_c_rule) {
345354 append (" rule c_object\n " );
346- append (" command = $cc $local_includes $cflags -c $in -o $out\n " );
355+ append (" command = $cc $local_includes $cflags $unit_cflags -c $in -o $out\n " );
347356 append (" description = CC $out\n " );
348357 if (dyndep)
349358 append (" restat = 1\n " );
@@ -370,6 +379,7 @@ std::string emit_ninja_string(const BuildPlan& plan) {
370379 if (plan.scanDepsPath .empty ()) {
371380 // GCC path: compiler-integrated P1689 scanning.
372381 append (" command = $cxx $local_includes $cxxflags -fmodules "
382+ " $unit_cxxflags "
373383 " -fdeps-format=p1689r5 "
374384 " -fdeps-file=$out -fdeps-target=$compile_target "
375385 " -M -MM -MF $out.dep -E $in -o $compile_target\n " );
@@ -379,10 +389,10 @@ std::string emit_ninja_string(const BuildPlan& plan) {
379389 // Wrap in cmd /c for shell redirection (ninja on Windows uses
380390 // CreateProcess which doesn't interpret > as redirect).
381391 append (" command = cmd /c \" $scan_deps -format=p1689 -- "
382- " $cxx $local_includes $cxxflags -c $in -o $compile_target > $out\"\n " );
392+ " $cxx $local_includes $cxxflags $unit_cxxflags -c $in -o $compile_target > $out\"\n " );
383393 } else {
384394 append (" command = $scan_deps -format=p1689 -- "
385- " $cxx $local_includes $cxxflags -c $in -o $compile_target > $out\n " );
395+ " $cxx $local_includes $cxxflags $unit_cxxflags -c $in -o $compile_target > $out\n " );
386396 }
387397 }
388398 append (" description = SCAN $out\n\n " );
@@ -461,6 +471,8 @@ std::string emit_ninja_string(const BuildPlan& plan) {
461471 append (std::format (" compile_target = {}\n " , escape_ninja_path (cu.object )));
462472 if (auto includes = local_include_flags (cu); !includes.empty ())
463473 append (std::format (" local_includes ={}\n " , includes));
474+ if (auto flags = join_flags (cu.packageCxxflags ); !flags.empty ())
475+ append (std::format (" unit_cxxflags ={}\n " , flags));
464476 }
465477 append (" \n " );
466478
@@ -506,6 +518,13 @@ std::string emit_ninja_string(const BuildPlan& plan) {
506518 }
507519 if (auto includes = local_include_flags (cu); !includes.empty ())
508520 out_line += " local_includes =" + includes + " \n " ;
521+ if (is_c_source (cu.source )) {
522+ if (auto flags = join_flags (cu.packageCflags ); !flags.empty ())
523+ out_line += " unit_cflags =" + flags + " \n " ;
524+ } else {
525+ if (auto flags = join_flags (cu.packageCxxflags ); !flags.empty ())
526+ out_line += " unit_cxxflags =" + flags + " \n " ;
527+ }
509528 append (std::move (out_line));
510529 }
511530 append (" \n " );
@@ -546,6 +565,13 @@ std::string emit_ninja_string(const BuildPlan& plan) {
546565 out_line += " \n " ;
547566 if (auto includes = local_include_flags (cu); !includes.empty ())
548567 out_line += " local_includes =" + includes + " \n " ;
568+ if (is_c_source (cu.source )) {
569+ if (auto flags = join_flags (cu.packageCflags ); !flags.empty ())
570+ out_line += " unit_cflags =" + flags + " \n " ;
571+ } else {
572+ if (auto flags = join_flags (cu.packageCxxflags ); !flags.empty ())
573+ out_line += " unit_cxxflags =" + flags + " \n " ;
574+ }
549575 // Clang needs $bmi_out to emit -fmodule-output=$bmi_out
550576 if (cu.providesModule ) {
551577 out_line += " bmi_out = " + bmi_path (*cu.providesModule ) + " \n " ;
0 commit comments