Skip to content

Commit ea29bda

Browse files
committed
Add __init__() method to generated modules to avoid double @rosimport when precompiling results of rostypegen
1 parent 8d04836 commit ea29bda

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

docs/src/index.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ regenerated, the first version will remain.
7272
### Compatibility with Package Precompilation
7373
As described above, by default `rostypegen` creates modules in `Main` -- however,
7474
this behavior is incompatible with Julia package precompilation. If you are using
75-
`RobotOS` in the context of a module, as opposed to a script, you may reduce
75+
`RobotOS` in your own module or package, as opposed to a script, you may reduce
7676
load-time latency (useful for real-life applications!) by generating the ROS type
7777
modules inside your package module using an approach similar to the example below:
7878

@@ -87,20 +87,12 @@ modules inside your package module using an approach similar to the example belo
8787
import .geometry_msgs.msg: Pose
8888
# ...
8989

90-
function __init__()
91-
@rosimport geometry_msgs.msg: Pose
92-
# ...
93-
end
94-
9590
end
9691

9792
In this case, we have provided `rostypegen` with a root module (`MyROSPackage`)
9893
for type generation. The Julia type corresponding to `geometry_msgs/Pose` now
9994
lives at `MyROSPackage.geometry_msgs.msg.Pose`; note the extra dot in
100-
`import .geometry_msgs.msg: Pose`. A precompiled package using `RobotOS` must
101-
also duplicate any `@rosimport` statements within the `__init__` function in
102-
its module so that necessary memory/objects in the Python runtime can be
103-
allocated each time the package is used.
95+
`import .geometry_msgs.msg: Pose`.
10496

10597
## Usage: ROS API
10698

src/gentypes.jl

+13-4
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,13 @@ function _import_rospy_pkg(package::String)
277277
end
278278

279279
#The function that creates and fills the generated top-level modules
280-
function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
280+
function buildpackage(pkg::ROSPackage, rosrootmod::Module)
281281
@debug("Building package: ", _name(pkg))
282282

283283
#Create the top-level module for the package in Main
284284
pkgsym = Symbol(_name(pkg))
285285
pkgcode = :(module ($pkgsym) end)
286+
pkginitcode = :(function __init__() end)
286287

287288
#Add msg and srv submodules if needed
288289
@debug_addindent
@@ -293,6 +294,9 @@ function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
293294
push!(msgmod.args[3].args, expr)
294295
end
295296
push!(pkgcode.args[3].args, msgmod)
297+
for typ in pkg.msg.members
298+
push!(pkginitcode.args[2].args, :(@rosimport $(pkgsym).msg: $(Symbol(typ))))
299+
end
296300
end
297301
if length(pkg.srv.members) > 0
298302
srvmod = :(module srv end)
@@ -301,14 +305,19 @@ function buildpackage(pkg::ROSPackage, rosrootmod::Module=Main)
301305
push!(srvmod.args[3].args, expr)
302306
end
303307
push!(pkgcode.args[3].args, srvmod)
308+
for typ in pkg.srv.members
309+
push!(pkginitcode.args[2].args, :(@rosimport $(pkgsym).srv: $(Symbol(typ))))
310+
end
304311
end
312+
push!(pkgcode.args[3].args, :(import RobotOS.@rosimport))
313+
push!(pkgcode.args[3].args, pkginitcode)
305314
pkgcode = Expr(:toplevel, pkgcode)
306315
rosrootmod.eval(pkgcode)
307316
@debug_subindent
308317
end
309318

310319
#Generate all code for a .msg or .srv module
311-
function modulecode(mod::ROSModule, rosrootmod::Module=Main)
320+
function modulecode(mod::ROSModule, rosrootmod::Module)
312321
@debug("submodule: ", _fullname(mod))
313322
modcode = Expr[]
314323

@@ -340,13 +349,13 @@ function modulecode(mod::ROSModule, rosrootmod::Module=Main)
340349
end
341350

342351
#The imports specific to each module, including dependant packages
343-
function _importexprs(mod::ROSMsgModule, rosrootmod::Module=Main)
352+
function _importexprs(mod::ROSMsgModule, rosrootmod::Module)
344353
imports = Expr[Expr(:import, :RobotOS, :AbstractMsg)]
345354
othermods = filter(d -> d != _name(mod), mod.deps)
346355
append!(imports, [Expr(:using,Symbol(rosrootmod),Symbol(m),:msg) for m in othermods])
347356
imports
348357
end
349-
function _importexprs(mod::ROSSrvModule, rosrootmod::Module=Main)
358+
function _importexprs(mod::ROSSrvModule, rosrootmod::Module)
350359
imports = Expr[
351360
Expr(:import, :RobotOS, :AbstractSrv),
352361
Expr(:import, :RobotOS, :AbstractService),

0 commit comments

Comments
 (0)