OSXScriptLauncher.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/SystemTools.hxx>
  11. #include <cmsys/Process.h>
  12. #include <cmsys/ios/iostream>
  13. #include <cmsys/FStream.hxx>
  14. #include <CoreFoundation/CoreFoundation.h>
  15. // For the PATH_MAX constant
  16. #include <sys/syslimits.h>
  17. #define DebugError(x) \
  18. ofs << x << cmsys_ios::endl; \
  19. cmsys_ios::cout << x << cmsys_ios::endl
  20. int main(int argc, char* argv[])
  21. {
  22. //if ( cmsys::SystemTools::FileExists(
  23. cmsys_stl::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. {
  32. DebugError("Cannot get main bundle");
  33. return 1;
  34. }
  35. fileName = CFSTR("RuntimeScript");
  36. if (! (scriptFileURL = CFBundleCopyResourceURL(appBundle, fileName, NULL,
  37. NULL)))
  38. {
  39. DebugError("CFBundleCopyResourceURL failed");
  40. return 1;
  41. }
  42. //create path string
  43. if (! (path = new UInt8[PATH_MAX]))
  44. {
  45. return 1;
  46. }
  47. //get the file system path of the url as a cstring
  48. //in an encoding suitable for posix apis
  49. if ( CFURLGetFileSystemRepresentation(scriptFileURL, true, path,
  50. PATH_MAX) == false)
  51. {
  52. DebugError("CFURLGetFileSystemRepresentation failed");
  53. return 1;
  54. }
  55. //dispose of the CF variable
  56. CFRelease(scriptFileURL);
  57. cmsys_stl::string fullScriptPath = reinterpret_cast<char*>(path);
  58. delete [] path;
  59. if (! cmsys::SystemTools::FileExists(fullScriptPath.c_str()))
  60. {
  61. return 1;
  62. }
  63. cmsys_stl::string scriptDirectory = cmsys::SystemTools::GetFilenamePath(
  64. fullScriptPath);
  65. ofs << fullScriptPath.c_str() << cmsys_ios::endl;
  66. cmsys_stl::vector<const char*> args;
  67. args.push_back(fullScriptPath.c_str());
  68. int cc;
  69. for ( cc = 1; cc < argc; ++ cc )
  70. {
  71. args.push_back(argv[cc]);
  72. }
  73. args.push_back(0);
  74. cmsysProcess* cp = cmsysProcess_New();
  75. cmsysProcess_SetCommand(cp, &*args.begin());
  76. cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
  77. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  78. cmsysProcess_SetTimeout(cp, 0);
  79. cmsysProcess_Execute(cp);
  80. std::vector<char> tempOutput;
  81. char* data;
  82. int length;
  83. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  84. {
  85. // Translate NULL characters in the output into valid text.
  86. // Visual Studio 7 puts these characters in the output of its
  87. // build process.
  88. for(int i=0; i < length; ++i)
  89. {
  90. if(data[i] == '\0')
  91. {
  92. data[i] = ' ';
  93. }
  94. }
  95. cmsys_ios::cout.write(data, length);
  96. }
  97. cmsysProcess_WaitForExit(cp, 0);
  98. bool result = true;
  99. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  100. {
  101. if ( cmsysProcess_GetExitValue(cp) != 0 )
  102. {
  103. result = false;
  104. }
  105. }
  106. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  107. {
  108. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  109. std::cerr << exception_str << std::endl;
  110. result = false;
  111. }
  112. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  113. {
  114. const char* error_str = cmsysProcess_GetErrorString(cp);
  115. std::cerr << error_str << std::endl;
  116. result = false;
  117. }
  118. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  119. {
  120. const char* error_str = "Process terminated due to timeout\n";
  121. std::cerr << error_str << std::endl;
  122. result = false;
  123. }
  124. cmsysProcess_Delete(cp);
  125. return 0;
  126. }