From dfa11a7f63a5acf1f1d57d30420010dd34f1de51 Mon Sep 17 00:00:00 2001 From: Christian Rauch Date: Tue, 16 Jul 2024 20:01:49 +0200 Subject: [PATCH] do not use a process to gather and store environment variables --- src/project_manager/ros_package_wizard.cpp | 4 +- src/project_manager/ros_utils.cpp | 113 +++++++++++---------- src/project_manager/ros_utils.h | 20 ++-- 3 files changed, 70 insertions(+), 67 deletions(-) diff --git a/src/project_manager/ros_package_wizard.cpp b/src/project_manager/ros_package_wizard.cpp index 26f3f643..334e3a98 100644 --- a/src/project_manager/ros_package_wizard.cpp +++ b/src/project_manager/ros_package_wizard.cpp @@ -372,7 +372,9 @@ bool ROSPackageWizard::writeFiles(const Core::GeneratedFiles &files, QString *er QProcess create_pkg_proc; create_pkg_proc.setWorkingDirectory(packagePath.path()); - ROSUtils::sourceROS(&create_pkg_proc, project->distribution()); + QProcessEnvironment env; + ROSUtils::sourceROS(env, project->distribution()); + create_pkg_proc.setProcessEnvironment(env); create_pkg_proc.start("bash", {"-c", create_args.join(" ")}); if (!create_pkg_proc.waitForStarted(-1)) { Core::MessageManager::writeFlashing(tr("[ROS Error] Failed to start catkin_create_pkg.")); diff --git a/src/project_manager/ros_utils.cpp b/src/project_manager/ros_utils.cpp index 465bb91a..bbfdca08 100644 --- a/src/project_manager/ros_utils.cpp +++ b/src/project_manager/ros_utils.cpp @@ -59,15 +59,15 @@ QString ROSUtils::buildTypeName(const ROSUtils::BuildType &buildType) } } -bool ROSUtils::sourceROS(QProcess *process, const Utils::FilePath &rosDistribution) +bool ROSUtils::sourceROS(QProcessEnvironment &env, const Utils::FilePath &rosDistribution) { - sourceWorkspaceHelper(process, Utils::FilePath(rosDistribution).pathAppended(QLatin1String("setup.bash")).toString()); + sourceWorkspaceHelper(env, Utils::FilePath(rosDistribution).pathAppended(QLatin1String("setup.bash")).toString()); return true; } -bool ROSUtils::sourceWorkspace(QProcess *process, const WorkspaceInfo &workspaceInfo) +bool ROSUtils::sourceWorkspace(QProcessEnvironment &env, const WorkspaceInfo &workspaceInfo) { - if (!initializeWorkspace(process, workspaceInfo)) + if (!initializeWorkspace(env, workspaceInfo)) return false; Utils::FilePath sourcePath(workspaceInfo.develPath); @@ -94,7 +94,7 @@ bool ROSUtils::sourceWorkspace(QProcess *process, const WorkspaceInfo &workspace source_path = QString{}; } - sourceWorkspaceHelper(process, source_path); + sourceWorkspaceHelper(env, source_path); return true; } @@ -169,23 +169,26 @@ bool ROSUtils::initializeWorkspaceFolders(const WorkspaceInfo &workspaceInfo) return true; } -bool ROSUtils::initializeWorkspace(QProcess *process, const WorkspaceInfo &workspaceInfo) +bool ROSUtils::initializeWorkspace(QProcessEnvironment &env, const WorkspaceInfo &workspaceInfo) { WorkspaceInfo workspace = workspaceInfo; - if (sourceROS(process, workspaceInfo.rosDistribution)) + if (sourceROS(env, workspaceInfo.rosDistribution)) { if (!isWorkspaceInitialized(workspaceInfo)) { + QProcess process; + process.setProcessEnvironment(env); + switch (workspaceInfo.buildSystem) { case CatkinMake: { if( !initializeWorkspaceFolders(workspaceInfo) ) return false; - process->setWorkingDirectory(workspaceInfo.sourcePath.toString()); - process->start(QLatin1String("bash"), QStringList() << QStringList() << QLatin1String("-c") << QLatin1String("catkin_init_workspace")); + process.setWorkingDirectory(workspaceInfo.sourcePath.toString()); + process.start(QLatin1String("bash"), QStringList() << QStringList() << QLatin1String("-c") << QLatin1String("catkin_init_workspace")); - if( !process->waitForFinished() ) + if( !process.waitForFinished() ) return false; break; @@ -201,10 +204,10 @@ bool ROSUtils::initializeWorkspace(QProcess *process, const WorkspaceInfo &works if( !initializeWorkspaceFolders(workspace) ) return false; - process->setWorkingDirectory(workspace.path.toString()); - process->start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin init")); + process.setWorkingDirectory(workspace.path.toString()); + process.start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin init")); - if( !process->waitForFinished() ) + if( !process.waitForFinished() ) return false; break; @@ -220,45 +223,46 @@ bool ROSUtils::initializeWorkspace(QProcess *process, const WorkspaceInfo &works break; } - } + } // switch - if (process->exitStatus() != QProcess::CrashExit) - return buildWorkspace(process, workspace); + if (process.exitStatus() != QProcess::CrashExit) + return buildWorkspace(process, workspace); - Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Failed to initialize workspace: %1.").arg(workspace.path.toString())); - return false; + Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Failed to initialize workspace: %1.").arg(workspace.path.toString())); + return false; + } // if } return true; } -bool ROSUtils::buildWorkspace(QProcess *process, const WorkspaceInfo &workspaceInfo) +bool ROSUtils::buildWorkspace(QProcess &process, const WorkspaceInfo &workspaceInfo) { switch(workspaceInfo.buildSystem) { case CatkinMake: { - process->setWorkingDirectory(workspaceInfo.path.toString()); - process->start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin_make --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); - process->waitForFinished(); + process.setWorkingDirectory(workspaceInfo.path.toString()); + process.start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin_make --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); + process.waitForFinished(); break; } case CatkinTools: { - process->setWorkingDirectory(workspaceInfo.path.toString()); - process->start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin build --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); - process->waitForFinished(); + process.setWorkingDirectory(workspaceInfo.path.toString()); + process.start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("catkin build --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); + process.waitForFinished(); break; } case Colcon: { - process->setWorkingDirectory(workspaceInfo.path.toString()); - process->start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("colcon build --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); - process->waitForFinished(); + process.setWorkingDirectory(workspaceInfo.path.toString()); + process.start(QLatin1String("bash"), QStringList() << QLatin1String("-c") << QLatin1String("colcon build --cmake-args -G \"CodeBlocks - Unix Makefiles\"")); + process.waitForFinished(); break; } } - if (process->exitStatus() != QProcess::CrashExit) + if (process.exitStatus() != QProcess::CrashExit) return true; Core::MessageManager::writeSilently(QObject::tr("[ROS Warning] Failed to build workspace: %1.").arg(workspaceInfo.path.toString())); @@ -315,29 +319,27 @@ QList ROSUtils::installedDistributions() return distributions; } -void ROSUtils::sourceWorkspaceHelper(QProcess *process, const QString &path) +void ROSUtils::sourceWorkspaceHelper(QProcessEnvironment &env, const QString &path) { - QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + if (path.isEmpty()) + return; - if (!path.isEmpty()) - { - const QString cmd = QLatin1String("source ") + path + QLatin1String(" && env"); - process->start(QLatin1String("bash"), QStringList()); - process->waitForStarted(); - process->write(cmd.toLatin1()); - process->closeWriteChannel(); - process->waitForFinished(); - - if (process->exitStatus() != QProcess::CrashExit) - { - while (process->canReadLine()) { - const QStringList env_kv = QString::fromLocal8Bit(process->readLine().trimmed()).split('='); - env.insert(env_kv[0], env_kv[1]); - } - } - } + QProcess process; - process->setProcessEnvironment(env); + const QString cmd = QLatin1String("source ") + path + QLatin1String(" && env"); + process.start(QLatin1String("bash"), QStringList()); + process.waitForStarted(); + process.write(cmd.toLatin1()); + process.closeWriteChannel(); + process.waitForFinished(); + + if (process.exitStatus() == QProcess::CrashExit) + return; + + while (process.canReadLine()) { + const QStringList env_kv = QString::fromLocal8Bit(process.readLine().trimmed()).split('='); + env.insert(env_kv[0], env_kv[1]); + } } bool ROSUtils::generateQtCreatorWorkspaceFile(QXmlStreamWriter &xmlFile, const ROSProjectFileContent &content) @@ -1209,15 +1211,14 @@ ROSUtils::WorkspaceInfo ROSUtils::getWorkspaceInfo(const Utils::FilePath &worksp QProcessEnvironment ROSUtils::getWorkspaceEnvironment(const WorkspaceInfo &workspaceInfo, const Utils::Environment& current_environment) { - QProcess process; - - process.setProcessEnvironment(current_environment.toProcessEnvironment()); - - sourceWorkspace(&process, workspaceInfo); - - QProcessEnvironment env = process.processEnvironment(); + // initialise environment + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.insert(current_environment.toProcessEnvironment()); env.insert("PWD", workspaceInfo.path.toString()); + // source workspaces + sourceWorkspace(env, workspaceInfo); + return env; } diff --git a/src/project_manager/ros_utils.h b/src/project_manager/ros_utils.h index dee5ce9d..60f84fe2 100644 --- a/src/project_manager/ros_utils.h +++ b/src/project_manager/ros_utils.h @@ -171,19 +171,19 @@ class ROSUtils { /** * @brief Source ROS - * @param process QProcess to execute the ROS bash command + * @param env environment to create from sourcing workspaces * @param rosDistribution ROS distribution * @return True if successful, otherwise false */ - static bool sourceROS(QProcess *process, const Utils::FilePath &rosDistribution); + static bool sourceROS(QProcessEnvironment &env, const Utils::FilePath &rosDistribution); /** * @brief Source Workspace - * @param process QProcess to execute the ROS bash command + * @param env environment to create from sourcing workspaces * @param workspaceInfo Workspace information * @return True if successful */ - static bool sourceWorkspace(QProcess *process, + static bool sourceWorkspace(QProcessEnvironment &env, const WorkspaceInfo &workspaceInfo); /** @@ -195,11 +195,11 @@ class ROSUtils { /** * @brief Initialize workspace - * @param process QProcess to execute the ROS bash command + * @param env QProcess to execute the ROS bash command * @param workspaceInfo Workspace information * @return True if successfully executed, otherwise false */ - static bool initializeWorkspace(QProcess *process, + static bool initializeWorkspace(QProcessEnvironment &env, const WorkspaceInfo &workspaceInfo); /** @@ -211,11 +211,11 @@ class ROSUtils { /** * @brief Build workspace - * @param process QProcess to execute the catkin_make + * @param process QProcess to execute the build tool commands * @param workspaceInfo Workspace information * @return True if successfully executed, otherwise false */ - static bool buildWorkspace(QProcess *process, + static bool buildWorkspace(QProcess &process, const WorkspaceInfo &workspaceInfo); /** @@ -431,10 +431,10 @@ class ROSUtils { private: /** * @brief sourceWorkspaceHelper - Source workspace helper function - * @param process - QProcess to execute source bash command + * @param env - environment to create from sourcing workspaces * @param path - Path to workspace setup.bash */ - static void sourceWorkspaceHelper(QProcess *process, const QString &path); + static void sourceWorkspaceHelper(QProcessEnvironment &env, const QString &path); /** * @brief This will parse the CodeBlock file and get the build info (incudes, Cxx Flags, etc.)