OSXScriptLauncher.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <cstddef>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <cm/memory>
  8. #include <CoreFoundation/CoreFoundation.h>
  9. #include "cmsys/FStream.hxx"
  10. #include "cmsys/Process.h"
  11. #include "cmsys/SystemTools.hxx"
  12. // For the PATH_MAX constant
  13. #include <sys/syslimits.h>
  14. #define DebugError(x) \
  15. ofs << x << std::endl; \
  16. std::cout << x << std::endl
  17. int main(int argc, char* argv[])
  18. {
  19. // if ( cmsys::SystemTools::FileExists(
  20. cmsys::ofstream ofs("/tmp/output.txt");
  21. CFStringRef fileName;
  22. CFBundleRef appBundle;
  23. CFURLRef scriptFileURL;
  24. // get CF URL for script
  25. if (!(appBundle = CFBundleGetMainBundle())) {
  26. DebugError("Cannot get main bundle");
  27. return 1;
  28. }
  29. fileName = CFSTR("RuntimeScript");
  30. if (!(scriptFileURL =
  31. CFBundleCopyResourceURL(appBundle, fileName, nullptr, nullptr))) {
  32. DebugError("CFBundleCopyResourceURL failed");
  33. return 1;
  34. }
  35. // create path string
  36. auto path = cm::make_unique<UInt8[]>(PATH_MAX);
  37. if (!path) {
  38. return 1;
  39. }
  40. // get the file system path of the url as a cstring
  41. // in an encoding suitable for posix apis
  42. if (!CFURLGetFileSystemRepresentation(scriptFileURL, true, path.get(),
  43. PATH_MAX)) {
  44. DebugError("CFURLGetFileSystemRepresentation failed");
  45. return 1;
  46. }
  47. // dispose of the CF variable
  48. CFRelease(scriptFileURL);
  49. std::string fullScriptPath = reinterpret_cast<char*>(path.get());
  50. path.reset();
  51. if (!cmsys::SystemTools::FileExists(fullScriptPath)) {
  52. return 1;
  53. }
  54. std::string scriptDirectory =
  55. cmsys::SystemTools::GetFilenamePath(fullScriptPath);
  56. ofs << fullScriptPath << std::endl;
  57. std::vector<const char*> args;
  58. args.push_back(fullScriptPath.c_str());
  59. int cc;
  60. for (cc = 1; cc < argc; ++cc) {
  61. args.push_back(argv[cc]);
  62. }
  63. args.push_back(nullptr);
  64. cmsysProcess* cp = cmsysProcess_New();
  65. cmsysProcess_SetCommand(cp, args.data());
  66. cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
  67. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  68. cmsysProcess_SetTimeout(cp, 0);
  69. cmsysProcess_Execute(cp);
  70. char* data;
  71. int length;
  72. while (cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
  73. // Translate NULL characters in the output into valid text.
  74. for (int i = 0; i < length; ++i) {
  75. if (data[i] == '\0') {
  76. data[i] = ' ';
  77. }
  78. }
  79. std::cout.write(data, length);
  80. }
  81. cmsysProcess_WaitForExit(cp, nullptr);
  82. bool result = true;
  83. if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
  84. if (cmsysProcess_GetExitValue(cp) != 0) {
  85. result = false;
  86. }
  87. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exception) {
  88. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  89. std::cerr << exception_str << std::endl;
  90. result = false;
  91. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
  92. const char* error_str = cmsysProcess_GetErrorString(cp);
  93. std::cerr << error_str << std::endl;
  94. result = false;
  95. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Expired) {
  96. const char* error_str = "Process terminated due to timeout\n";
  97. std::cerr << error_str << std::endl;
  98. result = false;
  99. }
  100. cmsysProcess_Delete(cp);
  101. return 0;
  102. }