Skip to content

Commit 727eaa3

Browse files
committed
Check for foxy or galactic on Linux, add global path prefix to library loading, build with rpath=.
Signed-off-by: Adam Dabrowski <[email protected]>
1 parent f47ffc1 commit 727eaa3

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ fi
2323

2424
echo $MSG
2525
#TODO - call ros2cs ./build.sh instead, but with this workspace directory (parametrize the script)
26-
colcon build --merge-install --event-handlers console_direct+ --cmake-args -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=$TESTS && $SCRIPTPATH/deploy_unity_plugins.sh $SCRIPTPATH/src/Ros2ForUnity/Plugins/
26+
colcon build --merge-install --event-handlers console_direct+ --cmake-args -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=$TESTS -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-rpath=." && $SCRIPTPATH/deploy_unity_plugins.sh $SCRIPTPATH/src/Ros2ForUnity/Plugins/

src/Ros2ForUnity/Scripts/ROS2ForUnity.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -132,35 +132,18 @@ private void SetEnvPathVariable()
132132
Environment.SetEnvironmentVariable(GetEnvPathVariableName(), pluginPath + envPathSep + currentPath);
133133
}
134134

135-
private void EnsureROS2PluginVisibility()
136-
{
137-
string pluginPath = GetPluginPath();
138-
string currentEnvironmentPath = GetEnvPathVariableValue();
139-
140-
if (string.IsNullOrEmpty(currentEnvironmentPath) || !currentEnvironmentPath.Contains(pluginPath))
141-
{
142-
Debug.LogError("environment variables do not contain required settings (variable value | expected): " + currentEnvironmentPath + " | " + pluginPath);
143-
#if UNITY_EDITOR
144-
EditorApplication.isPlaying = false;
145-
throw new System.InvalidOperationException("Missing library path to plugins in environment. Make sure you launch with the start app/editor script ");
146-
#else
147-
const int LD_LIBRARY_PATH_ENTRY_MISSING = 32;
148-
Application.Quit(LD_LIBRARY_PATH_ENTRY_MISSING);
149-
#endif
150-
}
151-
}
152-
153135
/// <summary>
154136
/// Check if the ros version is supported, only applicable to non-standalone plugin versions
155137
/// (i. e. without ros2 libraries included in the plugin).
156138
/// </summary>
157139
private void CheckROSVersionSourced()
158140
{
159141
string currentVersion = Environment.GetEnvironmentVariable("ROS_DISTRO");
160-
const string supportedVersion = "foxy";
142+
List<string> supportedVersions = new List<string>() { "foxy", "galactic" };
143+
var supportedVersionsString = String.Join(", ", supportedVersions);
161144
if (string.IsNullOrEmpty(currentVersion))
162145
{
163-
string errMessage = "No ROS environment sourced. You need to source your ROS2 " + supportedVersion
146+
string errMessage = "No ROS environment sourced. You need to source your ROS2 " + supportedVersionsString
164147
+ " environment before launching Unity. Make sure you launch with the start app/editor script";
165148
Debug.LogError(errMessage);
166149
#if UNITY_EDITOR
@@ -172,10 +155,10 @@ private void CheckROSVersionSourced()
172155
#endif
173156
}
174157

175-
if (currentVersion != supportedVersion)
158+
if (!supportedVersions.Contains(currentVersion))
176159
{
177160
string errMessage = "Currently sourced ROS version differs from supported one. Sourced: " + currentVersion
178-
+ ", supported: " + supportedVersion;
161+
+ ", supported: " + supportedVersionsString + ".";
179162
Debug.LogError(errMessage);
180163
#if UNITY_EDITOR
181164
EditorApplication.isPlaying = false;
@@ -185,7 +168,7 @@ private void CheckROSVersionSourced()
185168
Application.Quit(ROS_BAD_VERSION_CODE);
186169
#endif
187170
}
188-
Debug.Log("Running with a supported ROS 2 version: " + supportedVersion);
171+
Debug.Log("Running with a supported ROS 2 version: " + currentVersion);
189172
}
190173

191174
private void RegisterCtrlCHandler()
@@ -210,13 +193,17 @@ private void ConnectLoggers()
210193

211194
internal ROS2ForUnity()
212195
{
196+
// TODO: Find a way to determine whether we run standalone build
213197
if (GetOS() == Platform.Windows) {
198+
// Windows version can run standalone, modifies PATH to ensure all plugins visibility
214199
SetEnvPathVariable();
215200
} else {
216-
EnsureROS2PluginVisibility();
201+
// Linux version needs to have ros2 sourced, which is checked here. It also loads plugins by absolute path
202+
// since LD_LIBRARY_PATH cannot be set dynamically within the process for dlopen() which is used under the hood.
203+
// Since libraries are built with -rpath=".", dependencies will be correcly located within plugins directory
204+
CheckROSVersionSourced();
205+
ROS2.GlobalVariables.absolutePath = GetPluginPath() + "/";
217206
}
218-
// TODO: Find a way to determine whether we run standalone build
219-
//CheckROSVersionSourced();
220207
ConnectLoggers();
221208
Ros2cs.Init();
222209
RegisterCtrlCHandler();

0 commit comments

Comments
 (0)