2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2014-01-04 09:47:13 +04:00
|
|
|
#include <cmsys/FStream.hxx>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <cmsys/Process.h>
|
|
|
|
#include <cmsys/SystemTools.hxx>
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2015-08-20 23:02:20 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
2007-01-10 23:30:26 +03:00
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
2007-01-31 21:53:30 +03:00
|
|
|
// For the PATH_MAX constant
|
|
|
|
#include <sys/syslimits.h>
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
#define DebugError(x) \
|
|
|
|
ofs << x << std::endl; \
|
2015-08-20 23:02:20 +03:00
|
|
|
std::cout << x << std::endl
|
2007-01-10 23:30:26 +03:00
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
// if ( cmsys::SystemTools::FileExists(
|
2015-01-30 01:19:40 +03:00
|
|
|
std::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
|
2014-01-04 09:47:13 +04:00
|
|
|
cmsys::ofstream ofs("/tmp/output.txt");
|
2007-01-10 23:30:26 +03:00
|
|
|
|
|
|
|
CFStringRef fileName;
|
|
|
|
CFBundleRef appBundle;
|
|
|
|
CFURLRef scriptFileURL;
|
2016-05-16 17:34:04 +03:00
|
|
|
UInt8* path;
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// get CF URL for script
|
|
|
|
if (!(appBundle = CFBundleGetMainBundle())) {
|
2007-01-10 23:30:26 +03:00
|
|
|
DebugError("Cannot get main bundle");
|
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-31 21:53:30 +03:00
|
|
|
fileName = CFSTR("RuntimeScript");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!(scriptFileURL =
|
|
|
|
CFBundleCopyResourceURL(appBundle, fileName, NULL, NULL))) {
|
2007-01-10 23:30:26 +03:00
|
|
|
DebugError("CFBundleCopyResourceURL failed");
|
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// create path string
|
|
|
|
if (!(path = new UInt8[PATH_MAX])) {
|
2007-01-10 23:30:26 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// get the file system path of the url as a cstring
|
|
|
|
// in an encoding suitable for posix apis
|
|
|
|
if (CFURLGetFileSystemRepresentation(scriptFileURL, true, path, PATH_MAX) ==
|
|
|
|
false) {
|
2007-01-31 21:53:30 +03:00
|
|
|
DebugError("CFURLGetFileSystemRepresentation failed");
|
2007-01-10 23:30:26 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
// dispose of the CF variable
|
2007-01-31 21:53:30 +03:00
|
|
|
CFRelease(scriptFileURL);
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2015-01-30 01:19:40 +03:00
|
|
|
std::string fullScriptPath = reinterpret_cast<char*>(path);
|
2016-05-16 17:34:04 +03:00
|
|
|
delete[] path;
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!cmsys::SystemTools::FileExists(fullScriptPath.c_str())) {
|
2007-01-10 23:30:26 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-10 23:30:26 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string scriptDirectory =
|
|
|
|
cmsys::SystemTools::GetFilenamePath(fullScriptPath);
|
2016-06-07 00:53:32 +03:00
|
|
|
ofs << fullScriptPath << std::endl;
|
2015-01-30 01:19:40 +03:00
|
|
|
std::vector<const char*> args;
|
2007-01-10 23:30:26 +03:00
|
|
|
args.push_back(fullScriptPath.c_str());
|
|
|
|
int cc;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (cc = 1; cc < argc; ++cc) {
|
2007-01-10 23:30:26 +03:00
|
|
|
args.push_back(argv[cc]);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-01-10 23:30:26 +03:00
|
|
|
args.push_back(0);
|
|
|
|
|
|
|
|
cmsysProcess* cp = cmsysProcess_New();
|
|
|
|
cmsysProcess_SetCommand(cp, &*args.begin());
|
|
|
|
cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
|
|
|
|
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
|
|
|
|
cmsysProcess_SetTimeout(cp, 0);
|
|
|
|
cmsysProcess_Execute(cp);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2007-01-10 23:30:26 +03:00
|
|
|
std::vector<char> tempOutput;
|
|
|
|
char* data;
|
|
|
|
int length;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (cmsysProcess_WaitForData(cp, &data, &length, 0)) {
|
2007-01-10 23:30:26 +03:00
|
|
|
// Translate NULL characters in the output into valid text.
|
|
|
|
// Visual Studio 7 puts these characters in the output of its
|
|
|
|
// build process.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
if (data[i] == '\0') {
|
2007-01-10 23:30:26 +03:00
|
|
|
data[i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
std::cout.write(data, length);
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2007-01-10 23:30:26 +03:00
|
|
|
cmsysProcess_WaitForExit(cp, 0);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2007-01-10 23:30:26 +03:00
|
|
|
bool result = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
|
|
|
|
if (cmsysProcess_GetExitValue(cp) != 0) {
|
2007-01-10 23:30:26 +03:00
|
|
|
result = false;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exception) {
|
2007-01-10 23:30:26 +03:00
|
|
|
const char* exception_str = cmsysProcess_GetExceptionString(cp);
|
|
|
|
std::cerr << exception_str << std::endl;
|
|
|
|
result = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
|
2007-01-10 23:30:26 +03:00
|
|
|
const char* error_str = cmsysProcess_GetErrorString(cp);
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
result = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Expired) {
|
2007-01-10 23:30:26 +03:00
|
|
|
const char* error_str = "Process terminated due to timeout\n";
|
|
|
|
std::cerr << error_str << std::endl;
|
|
|
|
result = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2007-01-10 23:30:26 +03:00
|
|
|
cmsysProcess_Delete(cp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|