Skip to content

Commit baf4274

Browse files
authored
Remove invalid escape sequence (#1033)
Currently, when executing `node-gyp` to build the C++ addon, it has the following warnings: ```bash <string>:111: SyntaxWarning: invalid escape sequence '\/' <string>:1[19](https://github.com/RobotWebTools/rclnodejs/actions/runs/13325047285/job/37216429264#step:7:20): SyntaxWarning: invalid escape sequence '\l' <string>:128: SyntaxWarning: invalid escape sequence '\/' <string>:129: SyntaxWarning: invalid escape sequence '\/' <string>:130: SyntaxWarning: invalid escape sequence '\/' <string>:131: SyntaxWarning: invalid escape sequence '\/' <string>:132: SyntaxWarning: invalid escape sequence '\/' <string>:133: SyntaxWarning: invalid escape sequence '\/' <string>:134: SyntaxWarning: invalid escape sequence '\/' <string>:135: SyntaxWarning: invalid escape sequence '\/' <string>:136: SyntaxWarning: invalid escape sequence '\/' <string>:137: SyntaxWarning: invalid escape sequence '\/' <string>:138: SyntaxWarning: invalid escape sequence '\/' <string>:139: SyntaxWarning: invalid escape sequence '\/' <string>:140: SyntaxWarning: invalid escape sequence '\/' <string>:141: SyntaxWarning: invalid escape sequence '\/' <string>:142: SyntaxWarning: invalid escape sequence '\/' ``` This patch leverages the `path` module to remove platform-specific code, which caused the invalid escape sequence, including: 1. Add `config.js` to grab the path of ROS path, library path and include path programmatically. 2. Call functions defined in `config.js` and assign the returned value to `include_dirs` and `library_dirs` in `binding.gyp`. Fix: #1034
1 parent 368c0b3 commit baf4274

File tree

2 files changed

+94
-77
lines changed

2 files changed

+94
-77
lines changed

binding.gyp

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
'variables': {
1111
'ros_version': '<!(node scripts/ros_distro.js)',
1212
'runtime%': 'node',
13+
'ros_lib_dir': "<!(node -p \"require('./scripts/config.js').getROSLibPath()\")",
14+
'ros_include_root': "<!(node -p \"require('./scripts/config.js').getROSIncludeRootPath()\")",
1315
},
1416
'targets': [
1517
{
@@ -28,6 +30,7 @@
2830
'include_dirs': [
2931
'.',
3032
"<!(node -e \"require('nan')\")",
33+
'<(ros_include_root)',
3134
],
3235
'cflags!': [
3336
'-fno-exceptions'
@@ -61,45 +64,11 @@
6164
],
6265
'cflags_cc': [
6366
'-std=c++20'
64-
],
65-
'include_dirs':
66-
[
67-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/ ') + '/include/')\")",
68-
],
69-
'library_dirs': [
70-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/lib/ ') + '/lib/')\")",
71-
],
72-
'conditions': [
73-
[
74-
'ros_version > 2105', # Humble, Rolling, ...
75-
{
76-
'include_dirs':
77-
[
78-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/ ') + '/include/')\")",
79-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rcl/ ') + '/include/rcl')\")",
80-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rcutils/ ') + '/include/rcutils/')\")",
81-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rmw/ ') + '/include/rmw/')\")",
82-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rcl_yaml_param_parser/ ') + '/include/rcl_yaml_param_parser/')\")",
83-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rosidl_typesupport_interface/ ') + '/include/rosidl_typesupport_interface/')\")",
84-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rcl_action/ ') + '/include/rcl_action/')\")",
85-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/action_msgs/ ') + '/include/action_msgs/')\")",
86-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/service_msgs/ ') + '/include/service_msgs/')\")",
87-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/unique_identifier_msgs/ ') + '/include/unique_identifier_msgs/')\")",
88-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/builtin_interfaces/ ') + '/include/builtin_interfaces/')\")",
89-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rcl_lifecycle/ ') + '/include/rcl_lifecycle/')\")",
90-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/lifecycle_msgs/ ') + '/include/lifecycle_msgs/')\")",
91-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rosidl_runtime_c/ ') + '/include/rosidl_runtime_c/')\")",
92-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/rosidl_dynamic_typesupport/ ') + '/include/rosidl_dynamic_typesupport/')\")",
93-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/type_description_interfaces/ ') + '/include/type_description_interfaces/')\")",
94-
],
95-
}
96-
],
97-
],
67+
]
9868
}
9969
],
10070
[
101-
'OS=="win"',
102-
{
71+
'OS=="win"', {
10372
'defines': [
10473
'OS_WINDOWS'
10574
],
@@ -108,57 +77,24 @@
10877
],
10978
'include_dirs': [
11079
'./src/third_party/dlfcn-win32/',
111-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include ').replace(/\\\/g, '/') + '/include')\")",
11280
],
11381
'msvs_settings': {
11482
'VCCLCompilerTool': {
11583
'ExceptionHandling': '2', # /EHsc
11684
},
11785
'VCLinkerTool': {
11886
'AdditionalDependencies': ['psapi.lib'],
119-
'AdditionalLibraryDirectories': ["<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '\\\lib ').replace(/\\\/g, '/') + '/lib')\")",],
87+
'AdditionalLibraryDirectories': ['<(ros_lib_dir)'],
12088
}
121-
},
122-
'conditions': [
123-
[
124-
'ros_version > 2105', # Humble, Rolling, ... TODO - not tested due to broken setup_ros v3.3 action on windows
125-
{
126-
'include_dirs':
127-
[
128-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rcl ').replace(/\\\/g, '/') + '/include/rcl')\")",
129-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rcutils ').replace(/\\\/g, '/') + '/include/rcutils')\")",
130-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rmw ').replace(/\\\/g, '/') + '/include/rmw')\")",
131-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rcl_yaml_param_parser ').replace(/\\\/g, '/') + '/include/rcl_yaml_param_parser')\")",
132-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rosidl_runtime_c ').replace(/\\\/g, '/') + '/include/rosidl_runtime_c')\")",
133-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rosidl_typesupport_interface ').replace(/\\\/g, '/') + '/include/rosidl_typesupport_interface')\")",
134-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rcl_action ').replace(/\\\/g, '/') + '/include/rcl_action')\")",
135-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/action_msgs ').replace(/\\\/g, '/') + '/include/action_msgs')\")",
136-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/service_msgs ').replace(/\\\/g, '/') + '/include/service_msgs')\")",
137-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/unique_identifier_msgs ').replace(/\\\/g, '/') + '/include/unique_identifier_msgs')\")",
138-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/builtin_interfaces ').replace(/\\\/g, '/') + '/include/builtin_interfaces')\")",
139-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rcl_lifecycle ').replace(/\\\/g, '/') + '/include/rcl_lifecycle')\")",
140-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/lifecycle_msgs ').replace(/\\\/g, '/') + '/include/lifecycle_msgs')\")",
141-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/rosidl_dynamic_typesupport ').replace(/\\\/g, '/') + '/include/rosidl_dynamic_typesupport')\")",
142-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/;/g, '/include/type_description_interfaces/ ').replace(/\\\/g, '/') + '/include/type_description_interfaces/')\")",
143-
],
144-
}
145-
]
146-
]
89+
}
14790
}
14891
],
14992
[
150-
'OS=="mac"',
15193
# TODO - macos is no longer a tier-1 ROS platform and we have no binary ROS builds to test for Humble & Rolling
152-
{
94+
'OS=="mac"', {
15395
'defines': [
15496
'OS_MACOS'
15597
],
156-
'include_dirs': [
157-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/include/ ') + '/include/')\")",
158-
],
159-
'library_dirs': [
160-
"<!@(node -e \"console.log(process.env.AMENT_PREFIX_PATH.replace(/:/g, '/lib/ ') + '/lib/')\")",
161-
],
16298
'xcode_settings': {
16399
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
164100
'CLANG_CXX_LIBRARY': 'libc++',
@@ -168,8 +104,7 @@
168104
}
169105
],
170106
[
171-
'ros_version<=1911',
172-
{
107+
'ros_version <= 1911', {
173108
'libraries': [
174109
'-lrosidl_generator_c'
175110
],
@@ -178,9 +113,22 @@
178113
]
179114
}
180115
],
181-
['runtime=="electron"', {
182-
"defines": ["NODE_RUNTIME_ELECTRON=1"]
183-
}],
116+
[
117+
# After Galactic, e.g., Humble, Jazzy, Rolling.
118+
'ros_version > 2105', {
119+
'include_dirs': [
120+
"<!@(node -p \"require('./scripts/config.js').getIncludePaths().forEach(p => console.log(JSON.stringify(p)))\")"
121+
],
122+
'library_dirs': [
123+
'<(ros_lib_dir)',
124+
]
125+
}
126+
],
127+
[
128+
'runtime=="electron"', {
129+
"defines": ["NODE_RUNTIME_ELECTRON=1"]
130+
}
131+
],
184132
]
185133
}
186134
]

scripts/config.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
'use strict';
14+
15+
const os = require('os');
16+
const path = require('path');
17+
const { execSync } = require('child_process');
18+
19+
const dependencies = [
20+
'rcl',
21+
'rcutils',
22+
'rmw',
23+
'rcl_yaml_param_parser',
24+
'rosidl_typesupport_interface',
25+
'rcl_action',
26+
'action_msgs',
27+
'service_msgs',
28+
'unique_identifier_msgs',
29+
'builtin_interfaces',
30+
'rcl_lifecycle',
31+
'lifecycle_msgs',
32+
'rosidl_runtime_c',
33+
'rosidl_dynamic_typesupport',
34+
'type_description_interfaces',
35+
];
36+
37+
const command = os.type() === 'Windows_NT' ? 'where ros2' : 'which ros2';
38+
39+
function getROSRootPath() {
40+
try {
41+
// Execute the command to find the ROS2 installation path.
42+
let ros2Path = execSync(command).toString().trim();
43+
return path.dirname(path.dirname(ros2Path));
44+
} catch (error) {
45+
console.error(`Error: ${error.message}`);
46+
}
47+
}
48+
49+
function getROSLibPath() {
50+
return path.join(getROSRootPath(), 'lib');
51+
}
52+
53+
function getROSIncludeRootPath() {
54+
return path.join(getROSRootPath(), 'include');
55+
}
56+
57+
function getIncludePaths() {
58+
const includeRoot = getROSIncludeRootPath();
59+
return dependencies.map((dependency) => {
60+
return path.join(includeRoot, dependency);
61+
});
62+
}
63+
64+
module.exports = {
65+
getROSRootPath,
66+
getROSLibPath,
67+
getROSIncludeRootPath,
68+
getIncludePaths,
69+
};

0 commit comments

Comments
 (0)