eProsima / Fast-DDS-Gen

Fast-DDS IDL code generator tool. Looking for commercial support? Contact info@eprosima.com
Apache License 2.0
82 stars 60 forks source link

Fastddsgen with -extrastg Argument Fails at Adding ST Group #203

Closed AndreaFinazzi closed 1 year ago

AndreaFinazzi commented 1 year ago

First of all thanks for this great project and for open-sourcing it!

I'm trying to generate a custom header out of my IDL definitions using the -extrastg argument, but I can't get it to work. Couldn't find any example either.

This is how I use the tool:

export CLASSPATH=/workspaces:/workspaces/template.stg:template.stg
fastddsgen -extrastg /workspaces/template.stg msgs.h msgs_def.idl

I tried different combinations (even adding the custom template to the source template folder), but I always get:

openjdk version "19.0.2" 2023-01-17
OpenJDK Runtime Environment (build 19.0.2+7-Ubuntu-0ubuntu322.04)
OpenJDK 64-Bit Server VM (build 19.0.2+7-Ubuntu-0ubuntu322.04, mixed mode, sharing)
Loading templates from /workspaces/Fast-DDS-Gen/scripts/../share/fastddsgen/java/fastddsgen.jar
Template resource folders: com/eprosima/fastdds/idl/templates:com/eprosima/fastcdr/idl/templates:./
Processing the file msgs_def.idl...
Loading custom template template.stg...
ERROR: no such group file template.stg
ERROR: Cannot generate the files
Cannot invoke "java.lang.Throwable.printStackTrace()" because "arg1" is null
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.eprosima.fastdds.solution.Project.getIDLIncludeFiles()" because "project" is null
    at com.eprosima.fastdds.fastddsgen.execute(fastddsgen.java:441)
    at com.eprosima.fastdds.fastddsgen.main(fastddsgen.java:1558)

Could you point me to a working example, please? Any help would be much appreciated, thanks!

stingliang commented 1 year ago

@AndreaFinazzi Hi, I encountered the same problem and after multiple attempts, I found that the idl file present in src/main/java/com/pro sima/fastdds/idl/templates can be accepted by this command-line parameter.

For example, although the -example option was not used when generating code, the -extrastg option can be used to specify the code corresponding to the DDSPublisherHeader.stg template.

Example:

Opening the file reveals that publisherHeader.h has been generated. The content generated by this file should have been generated when -example was specified, but it can now also be generated through -extrastg


My solution is as follows:

  1. Pull the source code of fastddsgen from the dockerfile of building my app
  2. Modify the content of the DDSPublisherHeader.stg template file to meet my own needs (sed, gawk, or direct copy)
  3. Compile the fasddsgen code and install it
RUN git clone -b v2.5.1 https://github.com/eProsima/Fast-DDS-Gen.git \
    && sed -i 's/private\:/protected\:/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSPublisherHeader.stg \
    && sed -i 's/bool init/virtual bool init/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSPublisherHeader.stg \
    && sed -i 's/void run/virtual void run/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSPublisherHeader.stg \
    && sed -i 's/private\:/protected\:/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSSubscriberHeader.stg \
    && sed -i 's/bool init/virtual bool init/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSSubscriberHeader.stg \
    && sed -i 's/void run/virtual void run/' Fast-DDS-Gen/src/main/java/com/eprosima/fastdds/idl/templates/DDSSubscriberHeader.stg \
    && cd Fast-DDS-Gen && ./gradlew assemble && ./gradlew install \
    && rm -rf /opt/packages/Fast-DDS-Gen

This achieves the goal of customizing the template.

JLBuenoLopez commented 1 year ago

@AndreaFinazzi

Fast DDS-Gen v3.0.0 modified this feature to load the templates directly from the filesystem. Could you check if it works now as expected?