cmake/Source/CPack/OSXScriptLauncher.cxx

123 lines
3.5 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2020-02-01 23:06:01 +01:00
#include <cstddef>
2015-11-17 17:22:37 +01:00
#include <iostream>
2017-04-14 19:02:05 +02:00
#include <string>
#include <vector>
2015-11-17 17:22:37 +01:00
2020-08-30 11:54:41 +02:00
#include <cm/memory>
#include <CoreFoundation/CoreFoundation.h>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
#include "cmsys/Process.h"
#include "cmsys/SystemTools.hxx"
// For the PATH_MAX constant
#include <sys/syslimits.h>
2016-07-09 11:21:54 +02:00
#define DebugError(x) \
ofs << x << std::endl; \
2015-11-17 17:22:37 +01:00
std::cout << x << std::endl
int main(int argc, char* argv[])
{
2016-07-09 11:21:54 +02:00
// if ( cmsys::SystemTools::FileExists(
2014-08-03 19:52:23 +02:00
cmsys::ofstream ofs("/tmp/output.txt");
CFStringRef fileName;
CFBundleRef appBundle;
CFURLRef scriptFileURL;
2016-07-09 11:21:54 +02:00
// get CF URL for script
if (!(appBundle = CFBundleGetMainBundle())) {
DebugError("Cannot get main bundle");
return 1;
2016-07-09 11:21:54 +02:00
}
fileName = CFSTR("RuntimeScript");
2016-07-09 11:21:54 +02:00
if (!(scriptFileURL =
2018-01-26 17:06:56 +01:00
CFBundleCopyResourceURL(appBundle, fileName, nullptr, nullptr))) {
DebugError("CFBundleCopyResourceURL failed");
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
// create path string
2020-08-30 11:54:41 +02:00
auto path = cm::make_unique<UInt8[]>(PATH_MAX);
if (!path) {
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
// get the file system path of the url as a cstring
// in an encoding suitable for posix apis
2020-08-30 11:54:41 +02:00
if (!CFURLGetFileSystemRepresentation(scriptFileURL, true, path.get(),
PATH_MAX)) {
DebugError("CFURLGetFileSystemRepresentation failed");
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
// dispose of the CF variable
CFRelease(scriptFileURL);
2020-08-30 11:54:41 +02:00
std::string fullScriptPath = reinterpret_cast<char*>(path.get());
path.reset();
2020-08-30 11:54:41 +02:00
if (!cmsys::SystemTools::FileExists(fullScriptPath)) {
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
std::string scriptDirectory =
cmsys::SystemTools::GetFilenamePath(fullScriptPath);
2016-10-30 18:24:19 +01:00
ofs << fullScriptPath << std::endl;
2015-08-17 11:37:30 +02:00
std::vector<const char*> args;
args.push_back(fullScriptPath.c_str());
int cc;
2016-07-09 11:21:54 +02:00
for (cc = 1; cc < argc; ++cc) {
args.push_back(argv[cc]);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
args.push_back(nullptr);
cmsysProcess* cp = cmsysProcess_New();
2019-11-11 23:01:05 +01:00
cmsysProcess_SetCommand(cp, args.data());
cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
cmsysProcess_SetTimeout(cp, 0);
cmsysProcess_Execute(cp);
2013-03-16 19:13:01 +02:00
char* data;
int length;
2018-01-26 17:06:56 +01:00
while (cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
// Translate NULL characters in the output into valid text.
2016-07-09 11:21:54 +02:00
for (int i = 0; i < length; ++i) {
if (data[i] == '\0') {
data[i] = ' ';
}
}
2016-07-09 11:21:54 +02:00
std::cout.write(data, length);
}
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
cmsysProcess_WaitForExit(cp, nullptr);
2013-03-16 19:13:01 +02:00
bool result = true;
2016-07-09 11:21:54 +02:00
if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exited) {
if (cmsysProcess_GetExitValue(cp) != 0) {
result = false;
}
2016-07-09 11:21:54 +02:00
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Exception) {
const char* exception_str = cmsysProcess_GetExceptionString(cp);
std::cerr << exception_str << std::endl;
result = false;
2016-07-09 11:21:54 +02:00
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Error) {
const char* error_str = cmsysProcess_GetErrorString(cp);
std::cerr << error_str << std::endl;
result = false;
2016-07-09 11:21:54 +02:00
} else if (cmsysProcess_GetState(cp) == cmsysProcess_State_Expired) {
const char* error_str = "Process terminated due to timeout\n";
std::cerr << error_str << std::endl;
result = false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
cmsysProcess_Delete(cp);
return 0;
}