OSXScriptLauncher.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
  20. cmsys::ofstream ofs("/tmp/output.txt");
  21. CFStringRef fileName;
  22. CFBundleRef appBundle;
  23. CFURLRef scriptFileURL;
  24. UInt8* path;
  25. // get CF URL for script
  26. if (!(appBundle = CFBundleGetMainBundle())) {
  27. DebugError("Cannot get main bundle");
  28. return 1;
  29. }
  30. fileName = CFSTR("RuntimeScript");
  31. if (!(scriptFileURL =
  32. CFBundleCopyResourceURL(appBundle, fileName, NULL, NULL))) {
  33. DebugError("CFBundleCopyResourceURL failed");
  34. return 1;
  35. }
  36. // create path string
  37. if (!(path = new UInt8[PATH_MAX])) {
  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, PATH_MAX) ==
  43. false) {
  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);
  50. delete[] path;
  51. if (!cmsys::SystemTools::FileExists(fullScriptPath.c_str())) {
  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(0);
  64. cmsysProcess* cp = cmsysProcess_New();
  65. cmsysProcess_SetCommand(cp, &*args.begin());
  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. std::vector<char> tempOutput;
  71. char* data;
  72. int length;
  73. while (cmsysProcess_WaitForData(cp, &data, &length, 0)) {
  74. // Translate NULL characters in the output into valid text.
  75. // Visual Studio 7 puts these characters in the output of its
  76. // build process.
  77. for (int i = 0; i < length; ++i) {
  78. if (data[i] == '\0') {
  79. data[i] = ' ';
  80. }
  81. }
  82. std::cout.write(data, length);
  83. }
  84. cmsysProcess_WaitForExit(cp, 0);
  85. bool result = true;
  86. if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
  87. if (cmsysProcess_GetExitValue(cp) != 0) {
  88. result = false;
  89. }
  90. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exception) {
  91. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  92. std::cerr << exception_str << std::endl;
  93. result = false;
  94. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
  95. const char* error_str = cmsysProcess_GetErrorString(cp);
  96. std::cerr << error_str << std::endl;
  97. result = false;
  98. } else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Expired) {
  99. const char* error_str = "Process terminated due to timeout\n";
  100. std::cerr << error_str << std::endl;
  101. result = false;
  102. }
  103. cmsysProcess_Delete(cp);
  104. return 0;
  105. }