@@ -64,6 +64,9 @@ std::unique_ptr<Backend> make_ninja_backend();
6464std::string emit_ninja_string (const BuildPlan& plan);
6565std::string filter_ninja_output (std::string_view output,
6666 std::span<const std::string> commandPrefixes);
67+ std::string compiler_launcher_contents (const std::filesystem::path& loader,
68+ const std::filesystem::path& compiler,
69+ const std::vector<std::filesystem::path>& dirs);
6770
6871// Advice appended to a failed build whose linker output names a replaceable
6972// function nothing in the graph defines. Empty when there is nothing to add.
@@ -81,6 +84,21 @@ std::string link_failure_advice(std::string_view output);
8184
8285namespace mcpp ::build {
8386
87+ std::string compiler_launcher_contents (const std::filesystem::path& loader,
88+ const std::filesystem::path& compiler,
89+ const std::vector<std::filesystem::path>& dirs) {
90+ std::string libraryPath;
91+ for (auto const & dir : dirs) {
92+ if (!libraryPath.empty ()) libraryPath += ' :' ;
93+ libraryPath += dir.string ();
94+ }
95+ return std::format (
96+ " #!/bin/sh\n exec {} --library-path {} {} \" $@\"\n " ,
97+ mcpp::platform::shell::quote (loader.string ()),
98+ mcpp::platform::shell::quote (libraryPath),
99+ mcpp::platform::shell::quote (compiler.string ()));
100+ }
101+
84102namespace {
85103
86104std::string escape_ninja_path (const std::filesystem::path& p) {
@@ -288,25 +306,20 @@ const std::vector<std::filesystem::path>& compiler_invocation_dirs(const BuildPl
288306}
289307
290308bool needs_compiler_launcher (const BuildPlan& plan) {
291- return mcpp::platform::is_linux && !compiler_invocation_dirs (plan).empty ();
309+ return mcpp::platform::is_linux
310+ && plan.toolchain .compiler == mcpp::toolchain::CompilerId::GCC
311+ && !plan.toolchain .compilerInvocationLoader .empty ();
292312}
293313
294314std::filesystem::path compiler_launcher_path (const BuildPlan& plan, bool cxx) {
295315 return plan.outputDir / (cxx ? " mcpp-cxx" : " mcpp-cc" );
296316}
297317
298318void write_compiler_launcher (const std::filesystem::path& path,
319+ const std::filesystem::path& loader,
299320 const std::filesystem::path& compiler,
300321 const std::vector<std::filesystem::path>& dirs) {
301- std::string libraryPath;
302- for (auto const & dir : dirs) {
303- if (!libraryPath.empty ()) libraryPath += ' :' ;
304- libraryPath += dir.string ();
305- }
306- write_file (path, std::format (
307- " #!/bin/sh\n LD_LIBRARY_PATH={}\n export LD_LIBRARY_PATH\n exec {} \" $@\"\n " ,
308- mcpp::platform::shell::quote (libraryPath),
309- mcpp::platform::shell::quote (compiler.string ())));
322+ write_file (path, compiler_launcher_contents (loader, compiler, dirs));
310323
311324 std::error_code ec;
312325 std::filesystem::permissions (path, std::filesystem::perms::owner_exec,
@@ -627,8 +640,9 @@ std::string emit_ninja_string(const BuildPlan& plan) {
627640 const bool need_ios_init_shim = flags.needsStreamInitShim ;
628641 auto compiler_command = [&](const std::filesystem::path& binary, bool cxx) {
629642 if (needs_compiler_launcher (plan))
630- return escape_ninja_path (compiler_launcher_path (plan, cxx));
631- return escape_ninja_path (binary);
643+ return shell_quote_arg (escape_ninja_chars (
644+ compiler_launcher_path (plan, cxx).string ()));
645+ return shell_quote_arg (escape_ninja_chars (binary.string ()));
632646 };
633647 append (std::format (" cxx = {}\n " , compiler_command (flags.cxxBinary , /* cxx=*/ true )));
634648 // clang-scan-deps receives the driver after `--` as argv, not as a shell
@@ -2325,8 +2339,10 @@ std::expected<BuildResult, BuildError> NinjaBackend::build(const BuildPlan& plan
23252339 if (needs_compiler_launcher (plan)) {
23262340 const auto & dirs = compiler_invocation_dirs (plan);
23272341 write_compiler_launcher (compiler_launcher_path (plan, /* cxx=*/ true ),
2342+ plan.toolchain .compilerInvocationLoader ,
23282343 flags.cxxBinary , dirs);
23292344 write_compiler_launcher (compiler_launcher_path (plan, /* cxx=*/ false ),
2345+ plan.toolchain .compilerInvocationLoader ,
23302346 flags.ccBinary , dirs);
23312347 }
23322348 stage (" write-compiler-launcher" );
0 commit comments