cmake/Source/QtDialog/CMakeSetup.cxx

320 lines
10 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 <iostream>
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include "QCMake.h" // include to disable MS warnings
#include <QApplication>
#include <QDir>
#include <QLocale>
2016-07-09 11:21:54 +02:00
#include <QString>
#include <QTranslator>
2016-10-30 18:24:19 +01:00
#include <QtPlugin>
2020-02-01 23:06:01 +01:00
#include "cmsys/CommandLineArguments.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/SystemTools.hxx"
#include "CMakeSetupDialog.h"
#include "cmAlgorithms.h"
#include "cmDocumentation.h"
#include "cmDocumentationEntry.h"
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h" // IWYU pragma: keep
2020-02-01 23:06:01 +01:00
#include "cmake.h"
2016-10-30 18:24:19 +01:00
2018-01-26 17:06:56 +01:00
static const char* cmDocumentationName[][2] = { { nullptr,
2016-07-09 11:21:54 +02:00
" cmake-gui - CMake GUI." },
2018-01-26 17:06:56 +01:00
{ nullptr, nullptr } };
2016-07-09 11:21:54 +02:00
static const char* cmDocumentationUsage[][2] = {
2018-08-09 18:06:22 +02:00
{ nullptr,
" cmake-gui [options]\n"
" cmake-gui [options] <path-to-source>\n"
2019-11-11 23:01:05 +01:00
" cmake-gui [options] <path-to-existing-build>\n"
2021-09-14 00:13:48 +02:00
" cmake-gui [options] -S <path-to-source> -B <path-to-build>\n"
" cmake-gui [options] --browse-manual\n" },
2018-01-26 17:06:56 +01:00
{ nullptr, nullptr }
};
2021-09-14 00:13:48 +02:00
static const char* cmDocumentationOptions[][2] = {
{ "-S <path-to-source>", "Explicitly specify a source directory." },
{ "-B <path-to-build>", "Explicitly specify a build directory." },
{ "--preset=<preset>", "Specify a configure preset." },
{ nullptr, nullptr }
};
2015-08-17 11:37:30 +02:00
#if defined(Q_OS_MAC)
static int cmOSXInstall(std::string dir);
2016-07-09 11:21:54 +02:00
static void cmAddPluginPath();
2015-08-17 11:37:30 +02:00
#endif
2016-10-30 18:24:19 +01:00
#if defined(USE_QXcbIntegrationPlugin)
Q_IMPORT_PLUGIN(QXcbIntegrationPlugin);
#endif
2018-01-26 17:06:56 +01:00
#if defined(USE_QWindowsIntegrationPlugin)
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
2019-11-11 23:01:05 +01:00
# if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
Q_IMPORT_PLUGIN(QWindowsVistaStylePlugin);
# endif
2018-01-26 17:06:56 +01:00
#endif
2021-09-14 00:13:48 +02:00
int CMakeGUIExec(CMakeSetupDialog* window);
void SetupDefaultQSettings();
void OpenReferenceManual();
int main(int argc, char** argv)
{
2019-11-11 23:01:05 +01:00
cmSystemTools::EnsureStdPipes();
2014-08-03 19:52:23 +02:00
cmsys::Encoding::CommandLineArguments encoding_args =
cmsys::Encoding::CommandLineArguments::Main(argc, argv);
int argc2 = encoding_args.argc();
char const* const* argv2 = encoding_args.argv();
2018-04-23 21:13:27 +02:00
cmSystemTools::InitializeLibUV();
2014-08-03 19:52:23 +02:00
cmSystemTools::FindCMakeResources(argv2[0]);
// check docs first so that X is not need to get docs
// do docs, if args were given
cmDocumentation doc;
2012-04-19 19:04:21 +03:00
doc.addCMakeStandardDocSections();
2016-07-09 11:21:54 +02:00
if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
// Construct and print requested documentation.
2019-11-11 23:01:05 +01:00
cmake hcm(cmake::RoleInternal, cmState::Unknown);
2015-08-17 11:37:30 +02:00
hcm.SetHomeDirectory("");
hcm.SetHomeOutputDirectory("");
hcm.AddCMakePaths();
2019-11-11 23:01:05 +01:00
auto generators = hcm.GetGeneratorsDocumentation();
doc.SetName("cmake");
2016-07-09 11:21:54 +02:00
doc.SetSection("Name", cmDocumentationName);
doc.SetSection("Usage", cmDocumentationUsage);
doc.AppendSection("Generators", generators);
doc.PrependSection("Options", cmDocumentationOptions);
2016-07-09 11:21:54 +02:00
return (doc.PrintRequestedDocumentation(std::cout) ? 0 : 1);
}
2015-08-17 11:37:30 +02:00
#if defined(Q_OS_MAC)
2016-07-09 11:21:54 +02:00
if (argc2 == 2 && strcmp(argv2[1], "--install") == 0) {
2015-08-17 11:37:30 +02:00
return cmOSXInstall("/usr/local/bin");
2016-07-09 11:21:54 +02:00
}
if (argc2 == 2 && cmHasLiteralPrefix(argv2[1], "--install=")) {
return cmOSXInstall(argv2[1] + 10);
}
#endif
// When we are on OSX and we are launching cmake-gui from a symlink, the
// application will fail to launch as it can't find the qt.conf file which
// tells it what the name of the plugin folder is. We need to add this path
// BEFORE the application is constructed as that is what triggers the
// searching for the platform plugins
#if defined(Q_OS_MAC)
cmAddPluginPath();
2015-08-17 11:37:30 +02:00
#endif
2021-09-14 00:13:48 +02:00
// HighDpiScaling is always enabled starting with Qt6, but will still issue a
// deprecation warning if you try to set the attribute for it
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) && \
QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
2018-01-26 17:06:56 +01:00
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
2021-09-14 00:13:48 +02:00
SetupDefaultQSettings();
QApplication app(argc, argv);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
setlocale(LC_NUMERIC, "C");
2013-03-16 19:13:01 +02:00
// tell the cmake library where cmake is
QDir cmExecDir(QApplication::applicationDirPath());
#if defined(Q_OS_MAC)
cmExecDir.cd("../../../");
#endif
// pick up translation files if they exists in the data directory
QDir translationsDir = cmExecDir;
2012-08-04 10:26:08 +03:00
translationsDir.cd(QString::fromLocal8Bit(".." CMAKE_DATA_DIR));
translationsDir.cd("i18n");
QTranslator translator;
2021-09-14 00:13:48 +02:00
if (translator.load(QLocale(), "cmake", "_", translationsDir.path())) {
QApplication::installTranslator(&translator);
}
2013-03-16 19:13:01 +02:00
// app setup
2019-11-11 23:01:05 +01:00
QApplication::setApplicationName("CMakeSetup");
QApplication::setOrganizationName("Kitware");
2010-03-17 14:00:29 +02:00
QIcon appIcon;
appIcon.addFile(":/Icons/CMakeSetup32.png");
appIcon.addFile(":/Icons/CMakeSetup128.png");
2021-09-14 00:13:48 +02:00
QApplication::setWindowIcon(QIcon::fromTheme("cmake-gui", appIcon));
2013-03-16 19:13:01 +02:00
CMakeSetupDialog dialog;
dialog.show();
2013-03-16 19:13:01 +02:00
2019-11-11 23:01:05 +01:00
QStringList args = QApplication::arguments();
std::string binaryDirectory;
std::string sourceDirectory;
2021-09-14 00:13:48 +02:00
std::string presetName;
2019-11-11 23:01:05 +01:00
for (int i = 1; i < args.size(); ++i) {
const QString& arg = args[i];
if (arg.startsWith("-S")) {
QString path = arg.mid(2);
if (path.isEmpty()) {
++i;
if (i >= args.size()) {
std::cerr << "No source directory specified for -S" << std::endl;
return 1;
}
path = args[i];
if (path[0] == '-') {
std::cerr << "No source directory specified for -S" << std::endl;
return 1;
}
}
sourceDirectory =
cmSystemTools::CollapseFullPath(path.toLocal8Bit().data());
cmSystemTools::ConvertToUnixSlashes(sourceDirectory);
} else if (arg.startsWith("-B")) {
QString path = arg.mid(2);
if (path.isEmpty()) {
++i;
if (i >= args.size()) {
std::cerr << "No build directory specified for -B" << std::endl;
return 1;
}
path = args[i];
if (path[0] == '-') {
std::cerr << "No build directory specified for -B" << std::endl;
return 1;
}
}
binaryDirectory =
cmSystemTools::CollapseFullPath(path.toLocal8Bit().data());
cmSystemTools::ConvertToUnixSlashes(binaryDirectory);
2021-09-14 00:13:48 +02:00
} else if (arg.startsWith("--preset=")) {
QString preset = arg.mid(cmStrLen("--preset="));
if (preset.isEmpty()) {
std::cerr << "No preset specified for --preset" << std::endl;
return 1;
}
presetName = preset.toLocal8Bit().data();
} else if (arg == "--browse-manual") {
OpenReferenceManual();
return 0;
2019-11-11 23:01:05 +01:00
}
}
2021-09-14 00:13:48 +02:00
if (!sourceDirectory.empty() &&
(!binaryDirectory.empty() || !presetName.empty())) {
2012-08-04 10:26:08 +03:00
dialog.setSourceDirectory(QString::fromLocal8Bit(sourceDirectory.c_str()));
2021-09-14 00:13:48 +02:00
if (!binaryDirectory.empty()) {
dialog.setBinaryDirectory(
QString::fromLocal8Bit(binaryDirectory.c_str()));
if (!presetName.empty()) {
dialog.setStartupBinaryDirectory(true);
}
}
if (!presetName.empty()) {
dialog.setDeferredPreset(QString::fromLocal8Bit(presetName.c_str()));
}
2016-07-09 11:21:54 +02:00
} else {
if (args.count() == 2) {
std::string filePath =
cmSystemTools::CollapseFullPath(args[1].toLocal8Bit().data());
2011-01-16 11:35:12 +01:00
// check if argument is a directory containing CMakeCache.txt
2020-08-30 11:54:41 +02:00
std::string buildFilePath = cmStrCat(filePath, "/CMakeCache.txt");
2011-01-16 11:35:12 +01:00
// check if argument is a CMakeCache.txt file
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetFilenameName(filePath) == "CMakeCache.txt" &&
cmSystemTools::FileExists(filePath.c_str())) {
2011-01-16 11:35:12 +01:00
buildFilePath = filePath;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// check if argument is a directory containing CMakeLists.txt
2020-08-30 11:54:41 +02:00
std::string srcFilePath = cmStrCat(filePath, "/CMakeLists.txt");
2011-01-16 11:35:12 +01:00
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileExists(buildFilePath.c_str())) {
dialog.setBinaryDirectory(QString::fromLocal8Bit(
cmSystemTools::GetFilenamePath(buildFilePath).c_str()));
} else if (cmSystemTools::FileExists(srcFilePath.c_str())) {
2012-08-04 10:26:08 +03:00
dialog.setSourceDirectory(QString::fromLocal8Bit(filePath.c_str()));
2016-07-09 11:21:54 +02:00
dialog.setBinaryDirectory(QString::fromLocal8Bit(
cmSystemTools::CollapseFullPath(".").c_str()));
}
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2021-09-14 00:13:48 +02:00
return CMakeGUIExec(&dialog);
}
2015-08-17 11:37:30 +02:00
#if defined(Q_OS_MAC)
2020-02-01 23:06:01 +01:00
# include <cerrno>
# include <cstring>
2018-08-09 18:06:22 +02:00
# include <unistd.h>
2020-02-01 23:06:01 +01:00
# include "cm_sys_stat.h"
2015-08-17 11:37:30 +02:00
static bool cmOSXInstall(std::string const& dir, std::string const& tool)
{
2016-07-09 11:21:54 +02:00
if (tool.empty()) {
2015-08-17 11:37:30 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
std::string link = dir + cmSystemTools::GetFilenameName(tool);
struct stat st;
2016-07-09 11:21:54 +02:00
if (lstat(link.c_str(), &st) == 0 && S_ISLNK(st.st_mode)) {
2015-08-17 11:37:30 +02:00
char buf[4096];
2016-07-09 11:21:54 +02:00
ssize_t s = readlink(link.c_str(), buf, sizeof(buf) - 1);
if (s >= 0 && std::string(buf, s) == tool) {
2015-08-17 11:37:30 +02:00
std::cerr << "Exists: '" << link << "' -> '" << tool << "'\n";
return true;
}
2016-07-09 11:21:54 +02:00
}
cmSystemTools::MakeDirectory(dir);
if (symlink(tool.c_str(), link.c_str()) == 0) {
2015-08-17 11:37:30 +02:00
std::cerr << "Linked: '" << link << "' -> '" << tool << "'\n";
return true;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
int err = errno;
std::cerr << "Failed: '" << link << "' -> '" << tool
<< "': " << strerror(err) << "\n";
return false;
2015-08-17 11:37:30 +02:00
}
static int cmOSXInstall(std::string dir)
{
2016-07-09 11:21:54 +02:00
if (!cmHasLiteralSuffix(dir, "/")) {
2015-08-17 11:37:30 +02:00
dir += "/";
2016-07-09 11:21:54 +02:00
}
return (cmOSXInstall(dir, cmSystemTools::GetCMakeCommand()) &&
cmOSXInstall(dir, cmSystemTools::GetCTestCommand()) &&
cmOSXInstall(dir, cmSystemTools::GetCPackCommand()) &&
cmOSXInstall(dir, cmSystemTools::GetCMakeGUICommand()) &&
cmOSXInstall(dir, cmSystemTools::GetCMakeCursesCommand()))
? 0
: 1;
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
// Locate the PlugIns directory and add it to the QApplication library paths.
// We need to resolve all symlinks so we have a known relative path between
// MacOS/CMake and the PlugIns directory.
//
// Note we are using cmSystemTools since Qt can't provide the path to the
// executable before the QApplication is created, and that is when plugin
// searching occurs.
static void cmAddPluginPath()
{
std::string const& path = cmSystemTools::GetCMakeGUICommand();
if (path.empty()) {
return;
}
std::string const& realPath = cmSystemTools::GetRealPath(path);
QFileInfo appPath(QString::fromLocal8Bit(realPath.c_str()));
QDir pluginDir = appPath.dir();
bool const foundPluginDir = pluginDir.cd("../PlugIns");
if (foundPluginDir) {
QApplication::addLibraryPath(pluginDir.path());
}
}
2015-08-17 11:37:30 +02:00
#endif