OSXScriptLauncher.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <CoreFoundation/CoreFoundation.h>
  8. // For the PATH_MAX constant
  9. #include <sys/syslimits.h>
  10. #define DebugError(x) \
  11. ofs << x << std::endl; \
  12. std::cout << x << std::endl
  13. int main(int argc, char* argv[])
  14. {
  15. // if ( cmsys::SystemTools::FileExists(
  16. std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
  17. cmsys::ofstream ofs("/tmp/output.txt");
  18. CFStringRef fileName;
  19. CFBundleRef appBundle;
  20. CFURLRef scriptFileURL;
  21. UInt8* path;
  22. // get CF URL for script
  23. if (!(appBundle = CFBundleGetMainBundle())) {
  24. DebugError("Cannot get main bundle");
  25. return 1;
  26. }
  27. fileName = CFSTR("RuntimeScript");
  28. if (!(scriptFileURL =
  29. CFBundleCopyResourceURL(appBundle, fileName, NULL, NULL))) {
  30. DebugError("CFBundleCopyResourceURL failed");
  31. return 1;
  32. }
  33. // create path string
  34. if (!(path = new UInt8[PATH_MAX])) {
  35. return 1;
  36. }
  37. // get the file system path of the url as a cstring
  38. // in an encoding suitable for posix apis
  39. if (CFURLGetFileSystemRepresentation(scriptFileURL, true, path, PATH_MAX) ==
  40. false) {
  41. DebugError("CFURLGetFileSystemRepresentation failed");
  42. return 1;
  43. }
  44. // dispose of the CF variable
  45. CFRelease(scriptFileURL);
  46. std::string fullScriptPath = reinterpret_cast<char*>(path);
  47. delete[] path;
  48. if (!cmsys::SystemTools::FileExists(fullScriptPath.c_str())) {
  49. return 1;
  50. }
  51. std::string scriptDirectory =
  52. cmsys::SystemTools::GetFilenamePath(fullScriptPath);
  53. ofs << fullScriptPath << std::endl;
  54. std::vector<const char*> args;
  55. args.push_back(fullScriptPath.c_str());
  56. int cc;
  57. for (cc = 1; cc < argc; ++cc) {
  58. args.push_back(argv[cc]);
  59. }
  60. args.push_back(0);
  61. cmsysProcess* cp = cmsysProcess_New();
  62. cmsysProcess_SetCommand(cp, &*args.begin());
  63. cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
  64. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  65. cmsysProcess_SetTimeout(cp, 0);
  66. cmsysProcess_Execute(cp);
  67. std::vector<char> tempOutput;
  68. char* data;
  69. int length;
  70. while (cmsysProcess_WaitForData(cp, &data, &length, 0)) {
  71. // Translate NULL characters in the output into valid text.
  72. // Visual Studio 7 puts these characters in the output of its
  73. // build process.
  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, 0);
  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. }