OSXScriptLauncher.cxx 3.9 KB

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