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