Skip to content

Commit

Permalink
do not use a process to gather and store environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrauch committed Jul 16, 2024
1 parent a1a8d95 commit 72b1115
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 66 deletions.
4 changes: 3 additions & 1 deletion src/project_manager/ros_package_wizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] Faild to start catkin_create_pkg."));
Expand Down
112 changes: 57 additions & 55 deletions src/project_manager/ros_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()));
Expand Down Expand Up @@ -315,29 +319,27 @@ QList<Utils::FilePath> 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;

const QString cmd = QLatin1String("source ") + path + QLatin1String(" && env");
process.start(QLatin1String("bash"), QStringList());
process.waitForStarted();
process.write(cmd.toLatin1());
process.closeWriteChannel();
process.waitForFinished();

process->setProcessEnvironment(env);
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)
Expand Down Expand Up @@ -1209,14 +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());
// initialise environment
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(current_environment.toProcessEnvironment());
env.insert("PWD", workspaceInfo.path.toString());

sourceWorkspace(&process, workspaceInfo);
// source workspaces
sourceWorkspace(env, workspaceInfo);

QProcessEnvironment env = process.processEnvironment();
env.insert("PWD", workspaceInfo.path.toString());

return env;
}
Expand Down
20 changes: 10 additions & 10 deletions src/project_manager/ros_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand All @@ -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);

/**
Expand All @@ -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);

/**
Expand Down Expand Up @@ -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.)
Expand Down

0 comments on commit 72b1115

Please sign in to comment.