1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: OSXScriptLauncher.cxx,v $
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");
39 CFBundleRef appBundle
;
40 CFURLRef scriptFileURL
;
43 //get CF URL for script
44 if (! (appBundle
= CFBundleGetMainBundle()))
46 DebugError("Cannot get main bundle");
49 fileName
= CFSTR("RuntimeScript");
50 if (! (scriptFileURL
= CFBundleCopyResourceURL(appBundle
, fileName
, NULL
,
53 DebugError("CFBundleCopyResourceURL failed");
58 if (! (path
= new UInt8
[PATH_MAX
]))
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
,
68 DebugError("CFURLGetFileSystemRepresentation failed");
72 //dispose of the CF variable
73 CFRelease(scriptFileURL
);
75 cmsys_stl::string fullScriptPath
= reinterpret_cast<char*>(path
);
79 if (! cmsys::SystemTools::FileExists(fullScriptPath
.c_str()))
84 cmsys_stl::string scriptDirectory
= cmsys::SystemTools::GetFilenamePath(
86 ofs
<< fullScriptPath
.c_str() << cmsys_ios::endl
;
87 cmsys_stl::vector
<const char*> args
;
88 args
.push_back(fullScriptPath
.c_str());
90 for ( cc
= 1; cc
< argc
; ++ cc
)
92 args
.push_back(argv
[cc
]);
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
;
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
111 for(int i
=0; i
< length
; ++i
)
118 cmsys_ios::cout
.write(data
, length
);
121 cmsysProcess_WaitForExit(cp
, 0);
124 if(cmsysProcess_GetState(cp
) == cmsysProcess_State_Exited
)
126 if ( cmsysProcess_GetExitValue(cp
) != 0 )
131 else if(cmsysProcess_GetState(cp
) == cmsysProcess_State_Exception
)
133 const char* exception_str
= cmsysProcess_GetExceptionString(cp
);
134 std::cerr
<< exception_str
<< std::endl
;
137 else if(cmsysProcess_GetState(cp
) == cmsysProcess_State_Error
)
139 const char* error_str
= cmsysProcess_GetErrorString(cp
);
140 std::cerr
<< error_str
<< std::endl
;
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
;
150 cmsysProcess_Delete(cp
);