MCPcopy Create free account
hub / github.com/Kitware/CMake / WriteCommandScript

Method WriteCommandScript

Source/cmLocalNinjaGenerator.cxx:424–481  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

422}
423
424std::string cmLocalNinjaGenerator::WriteCommandScript(
425 std::vector<std::string> const& cmdLines, std::string const& outputConfig,
426 std::string const& commandConfig, std::string const& customStep,
427 cmGeneratorTarget const* target) const
428{
429 std::string scriptPath;
430 if (target) {
431 scriptPath = target->GetSupportDirectory();
432 } else {
433 scriptPath = cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
434 }
435 scriptPath += this->GetGlobalNinjaGenerator()->ConfigDirectory(outputConfig);
436 cmSystemTools::MakeDirectory(scriptPath);
437 scriptPath += '/';
438 scriptPath += customStep;
439 if (this->GlobalGenerator->IsMultiConfig()) {
440 scriptPath += cmStrCat('-', commandConfig);
441 }
442#ifdef _WIN32
443 scriptPath += ".bat";
444#else
445 scriptPath += ".sh";
446#endif
447
448 cmsys::ofstream script(scriptPath.c_str());
449
450#ifdef _WIN32
451 script << "@echo off\n";
452 int line = 1;
453#else
454 script << "set -e\n\n";
455#endif
456
457 for (auto const& i : cmdLines) {
458 std::string cmd = i;
459 // The command line was built assuming it would be written to
460 // the build.ninja file, so it uses '$$' for '$'. Remove this
461 // for the raw shell script.
462 cmSystemTools::ReplaceString(cmd, "$$", "$");
463#ifdef _WIN32
464 script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
465 << '\n';
466#else
467 script << cmd << '\n';
468#endif
469 }
470
471#ifdef _WIN32
472 script << "goto :EOF\n\n"
473 ":ABORT\n"
474 "set ERROR_CODE=%ERRORLEVEL%\n"
475 "echo Batch file failed at line %FAIL_LINE% "
476 "with errorcode %ERRORLEVEL%\n"
477 "exit /b %ERROR_CODE%";
478#endif
479
480 return scriptPath;
481}

Callers 1

BuildCommandLineMethod · 0.95

Calls 6

GetSupportDirectoryMethod · 0.80
c_strMethod · 0.80
cmStrCatFunction · 0.70
ConfigDirectoryMethod · 0.45
IsMultiConfigMethod · 0.45

Tested by

no test coverage detected