Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate C header generation #181

Merged
merged 5 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ void loop() {

### Contribution
#### How to add missing wrappers
##### Docker automatization
The generation of C header files from OpenCyphal's [public_regulated_data_types](https://github.com/OpenCyphal/public_regulated_data_types) (step 1-3 in the instructions below) can also be executed by invoking a Docker container. It can be built and executed with the following commands:

Install Docker via `snap` or follow the official [instructions](https://docs.docker.com/engine/install/ubuntu/):
```bash
sudo snap install docker
```
**Note**: You may need to add your user to the `docker` group:
```bash
sudo usermod -a -G docker $USER
```
Generating the C header files using a Docker container:
```bash
cd extras/script
./build_docker.sh
./docker_codegen.sh
```

##### Step 1) Generate C header files from DSDL
**Option A) Use [nunavut/nnvg](https://github.com/OpenCyphal/nunavut)**
* Install **nunavut**: `pip install nunavut` (you will need a functional [Python installation](https://docs.python.org/3/using/index.html))
Expand Down
29 changes: 29 additions & 0 deletions extras/script/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM ubuntu:latest

# set a directory for the app
WORKDIR /usr/src/app

# install dependencies
RUN apt-get update -y
RUN apt-get install -y apt-utils
RUN apt-get install -y locales
RUN apt-get install -yq tzdata
RUN echo "Europe/Berlin" > /etc/timezone && \
touch /etc/locale.gen && \
dpkg-reconfigure -f noninteractive tzdata && \
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
sed -i -e 's/# de_DE.UTF-8 UTF-8/de_DE.UTF-8 UTF-8/' /etc/locale.gen && \
echo 'LANG="de_DE.UTF-8"'>/etc/default/locale && \
dpkg-reconfigure --frontend=noninteractive locales && \
update-locale LANG=de_DE.UTF-8

ENV LANG de_DE.UTF-8
ENV LANGUAGE de_DE.UTF-8
ENV LC_ALL de_DE.UTF-8

RUN apt-get install -y bash
RUN apt-get install -y git
RUN apt-get install -y python3-pip
RUN pip3 install nunavut

CMD ["/bin/bash", "-c", "cd /107-Arduino-Cyphal; ./extras/script/update_c_header.sh; exit"]
3 changes: 3 additions & 0 deletions extras/script/build_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

docker build -t 107_arduino_cyphal_headergen .
3 changes: 3 additions & 0 deletions extras/script/docker_codegen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

docker run -it -v ${PWD}/../../:/107-Arduino-Cyphal 107_arduino_cyphal_headergen
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ HEADER_DIR="$SCRIPT_DIR/../../src/types"
NUNAVUT_DIR="$SCRIPT_DIR/../../src/nunavut"

cd /tmp
pip install -U nunavut
rm -rf public_regulated_data_types
git clone https://github.com/OpenCyphal/public_regulated_data_types

echo "Generating code..."
nnvg --target-language c \
--pp-max-emptylines=1 \
--pp-trim-trailing-whitespace \
Expand All @@ -24,6 +25,16 @@ nnvg --target-language c \
--outdir public_regulated_data_types/reg-header \
public_regulated_data_types/reg

echo "Copying code to destination"
cp -R public_regulated_data_types/uavcan-header/nunavut/* "$NUNAVUT_DIR"
cp -R public_regulated_data_types/uavcan-header/uavcan "$HEADER_DIR"
cp -R public_regulated_data_types/reg-header/reg "$HEADER_DIR"

echo "Fixing include paths"
cd $HEADER_DIR
find . -type f -exec sed -i 's/<reg/<types\/reg/g' {} +
find . -type f -exec sed -i 's/<uavcan/<types\/uavcan/g' {} +

echo "Fixing definition of NUNAVUT_ASSERT"
cd $NUNAVUT_DIR/support
sed -i -- 's/#define NUNAVUT_SUPPORT_SERIALIZATION_H_INCLUDED/#define NUNAVUT_SUPPORT_SERIALIZATION_H_INCLUDED\n\n#define NUNAVUT_ASSERT(expr) assert(expr)\n/g' serialization.h
9 changes: 8 additions & 1 deletion src/nunavut/support/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#ifndef NUNAVUT_SUPPORT_SERIALIZATION_H_INCLUDED
#define NUNAVUT_SUPPORT_SERIALIZATION_H_INCLUDED

#define NUNAVUT_ASSERT(expr) assert(expr)


#ifdef __cplusplus
# if (__cplusplus < 201100L)
# error "Unsupported language: ISO C11, C++11, or a newer version of either is required."
Expand Down Expand Up @@ -70,7 +73,11 @@ static_assert(sizeof(size_t) >= sizeof(size_t),
((FLT_RADIX == 2) && (DBL_MANT_DIG == 53) && (DBL_MIN_EXP == -1021) && (DBL_MAX_EXP == 1024))

#ifndef NUNAVUT_ASSERT
# define NUNAVUT_ASSERT(expr) assert(expr)
// By default Nunavut does not generate assert statements since the logic to halt a program is platform
// dependent and because this header requires an absolute minimum from a platform and from the C standard library.
// Most platforms can simply define "NUNAVUT_ASSERT(x)=assert(x)" (<assert.h> is always included by Nunavut).
# error "You must either define NUNAVUT_ASSERT or you need to disable assertions" \
" when generating serialization support code using Nunavut language options"
#endif

// This code is endianness-invariant. Use target_endianness='little' to generate little-endian-optimized code.
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/acoustics/Note_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/acoustics/Note.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.028882 UTC
// Generated at: 2022-12-15 23:20:53.209923 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.acoustics.Note
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -36,10 +36,10 @@
#define REG_UDRAL_PHYSICS_ACOUSTICS_NOTE_0_1_INCLUDED_

#include <nunavut/support/serialization.h>
#include <stdlib.h>
#include <types/uavcan/si/unit/duration/Scalar_1_0.h>
#include <types/uavcan/si/unit/frequency/Scalar_1_0.h>
#include <types/uavcan/si/unit/power/Scalar_1_0.h>
#include <stdlib.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/acoustics/Note.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/dynamics/rotation/PlanarTs_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/dynamics/rotation/PlanarTs.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.020181 UTC
// Generated at: 2022-12-15 23:20:53.216892 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.dynamics.rotation.PlanarTs
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -37,8 +37,8 @@

#include <nunavut/support/serialization.h>
#include <types/reg/udral/physics/dynamics/rotation/Planar_0_1.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>
#include <stdlib.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/dynamics/rotation/PlanarTs.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/dynamics/rotation/Planar_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/dynamics/rotation/Planar.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.017121 UTC
// Generated at: 2022-12-15 23:20:53.213656 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.dynamics.rotation.Planar
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -37,8 +37,8 @@

#include <nunavut/support/serialization.h>
#include <types/reg/udral/physics/kinematics/rotation/Planar_0_1.h>
#include <types/uavcan/si/unit/torque/Scalar_1_0.h>
#include <stdlib.h>
#include <types/uavcan/si/unit/torque/Scalar_1_0.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/dynamics/rotation/Planar.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/dynamics/translation/LinearTs_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/dynamics/translation/LinearTs.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.026066 UTC
// Generated at: 2022-12-15 23:20:53.222985 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.dynamics.translation.LinearTs
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -37,8 +37,8 @@

#include <nunavut/support/serialization.h>
#include <types/reg/udral/physics/dynamics/translation/Linear_0_1.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>
#include <stdlib.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/dynamics/translation/LinearTs.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/dynamics/translation/Linear_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/dynamics/translation/Linear.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.023097 UTC
// Generated at: 2022-12-15 23:20:53.219857 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.dynamics.translation.Linear
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -37,8 +37,8 @@

#include <nunavut/support/serialization.h>
#include <types/reg/udral/physics/kinematics/translation/Linear_0_1.h>
#include <types/uavcan/si/unit/force/Scalar_1_0.h>
#include <stdlib.h>
#include <types/uavcan/si/unit/force/Scalar_1_0.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/dynamics/translation/Linear.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/electricity/PowerTs_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/electricity/PowerTs.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.039098 UTC
// Generated at: 2022-12-15 23:20:53.228789 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.electricity.PowerTs
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -37,8 +37,8 @@

#include <nunavut/support/serialization.h>
#include <types/reg/udral/physics/electricity/Power_0_1.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>
#include <stdlib.h>
#include <types/uavcan/time/SynchronizedTimestamp_1_0.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/electricity/PowerTs.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
14 changes: 7 additions & 7 deletions src/types/reg/udral/physics/electricity/Power_0_1.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
// To avoid conflicts with definitions given in the source DSDL file, all entities created by the code generator
// are named with an underscore at the end, like foo_bar_().
//
// Generator: nunavut-1.8.2 (serialization was enabled)
// Generator: nunavut-1.9.0 (serialization was enabled)
// Source file: /tmp/public_regulated_data_types/reg/udral/physics/electricity/Power.0.1.dsdl
// Generated at: 2022-06-14 07:08:31.036347 UTC
// Generated at: 2022-12-15 23:20:53.225882 UTC
// Is deprecated: no
// Fixed port-ID: None
// Full name: reg.udral.physics.electricity.Power
// Version: 0.1
//
// Platform
// python_implementation: CPython
// python_version: 3.8.10
// python_version: 3.10.6
// python_release_level: final
// python_build: ('default', 'Mar 15 2022 12:22:08')
// python_compiler: GCC 9.4.0
// python_build: ('main', 'Nov 14 2022 16:10:14')
// python_compiler: GCC 11.3.0
// python_revision:
// python_xoptions: {}
// runtime_platform: Linux-5.4.0-117-generic-x86_64-with-glibc2.29
// runtime_platform: Linux-5.15.0-56-generic-x86_64-with-glibc2.35
//
// Language Options
// target_endianness: any
Expand All @@ -36,9 +36,9 @@
#define REG_UDRAL_PHYSICS_ELECTRICITY_POWER_0_1_INCLUDED_

#include <nunavut/support/serialization.h>
#include <stdlib.h>
#include <types/uavcan/si/unit/electric_current/Scalar_1_0.h>
#include <types/uavcan/si/unit/voltage/Scalar_1_0.h>
#include <stdlib.h>

static_assert( NUNAVUT_SUPPORT_LANGUAGE_OPTION_TARGET_ENDIANNESS == 1693710260,
"/tmp/public_regulated_data_types/reg/udral/physics/electricity/Power.0.1.dsdl is trying to use a serialization library that was compiled with "
Expand Down
Loading