OSXScriptLauncher.cxx 4.1 KB

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