OSXScriptLauncher.cxx 4.0 KB

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