OSXScriptLauncher.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "cmsys/FStream.hxx"
  4. #include "cmsys/Process.h"
  5. #include "cmsys/SystemTools.hxx"
  6. #include <iostream>
  7. #include <stddef.h>
  8. #include <string>
  9. #include <vector>
  10. #include <CoreFoundation/CoreFoundation.h>
  11. // For the PATH_MAX constant
  12. #include <sys/syslimits.h>
  13. #define DebugError(x) \
  14. ofs << x << std::endl; \
  15. std::cout << x << std::endl
  16. int main(int argc, char* argv[])
  17. {
  18. // if ( cmsys::SystemTools::FileExists(
  19. cmsys::ofstream ofs("/tmp/output.txt");
  20. CFStringRef fileName;
  21. CFBundleRef appBundle;
  22. CFURLRef scriptFileURL;
  23. UInt8* path;
  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. if (!(path = new UInt8[PATH_MAX])) {
  37. return 1;
  38. }
  39. // get the file system path of the url as a cstring
  40. // in an encoding suitable for posix apis
  41. if (CFURLGetFileSystemRepresentation(scriptFileURL, true, path, PATH_MAX) ==
  42. false) {
  43. DebugError("CFURLGetFileSystemRepresentation failed");
  44. return 1;
  45. }
  46. // dispose of the CF variable
  47. CFRelease(scriptFileURL);
  48. std::string fullScriptPath = reinterpret_cast<char*>(path);
  49. delete[] path;
  50. if (!cmsys::SystemTools::FileExists(fullScriptPath.c_str())) {
  51. return 1;
  52. }
  53. std::string scriptDirectory =
  54. cmsys::SystemTools::GetFilenamePath(fullScriptPath);
  55. ofs << fullScriptPath << std::endl;
  56. std::vector<const char*> args;
  57. args.push_back(fullScriptPath.c_str());
  58. int cc;
  59. for (cc = 1; cc < argc; ++cc) {
  60. args.push_back(argv[cc]);
  61. }
  62. args.push_back(nullptr);
  63. cmsysProcess* cp = cmsysProcess_New();
  64. cmsysProcess_SetCommand(cp, &*args.begin());
  65. cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
  66. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  67. cmsysProcess_SetTimeout(cp, 0);
  68. cmsysProcess_Execute(cp);
  69. std::vector<char> tempOutput;
  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. }