nixpkgs/pkgs/by-name/na/nanopb/runtime.nix
Alexandros Liarokapis e36b4564d2 nanopb: 0.4.6 -> 0.4.8
This commit:
- Bumps the nanopb version
- Adds all runtime configuration options
- Implements proper cross-compilation support which is the main use-case of the library.
- Uses newer `finalAttrs` form of `mkDerivation` to allow for easier attribute overrides.

The cross-compilation support is achieved by splitting the package into two sub-packages
consisting of the build-time generator and the runtime library.

Nanopb explicitely supports this by providing specialized `GENERATOR` and
`RUNTIME` CMake configuration options.

The top-level package uses `propagatedNativeBuildInputs` and `propagatedBuildInputs` to propagate
the sub-packages and also adds convenient symlinks to make certain use cases easier.

== GENERATOR ==

The generator is a mostly ready-to-be-packaged python module tree.
We patch the library to also include the missing `__init__.py` and we
also fix the `PYTHON_INSTDIR` variable to follow best practice and to
prevent the library from attempting to install to a global directory.

We package the python module using `buildPythonPackage` and internally
override python in order to wrap the `nanopb_generator.py` executable.

We do *not* wrap `nanob_generator.py` due to it also being imported directly from python when
used through `protoc-gen-nanopb`.

== RUNTIME ==

The runtime is a simple library that consists of the common
functionality among generated headers/sources.

It is configured through `preprocessor definitions` and consumer projects *must*
be compiled with the same definitions.

This is currently achieved by exposing all configuration options through the top-level overrides and
patching the CMakeLists.txt so that the definitions are added to the to-be-installed
CMake targets as PUBLIC properties.
2024-06-26 22:15:27 +03:00

70 lines
2.0 KiB
Nix

{ cmake
, lib
, stdenv
, protobuf
, python3
, src
, version
, enableMalloc
, noPackedStructs
, maxRequiredFields
, field32bit
, noErrmsg
, bufferOnly
, systemHeader
, without64bit
, encodeArraysUnpacked
, convertDoubleFloat
, validateUtf8
, littleEndian8bit
, c99StaticAssert
, noStaticAssert
}:
stdenv.mkDerivation
({
pname = "nanopb-runtime";
inherit src version;
nativeBuildInputs = [ cmake protobuf python3 ];
patchPhase =
let
compile_definitions = target: ''
target_compile_definitions(${target}
PUBLIC
${lib.concatStringsSep "\n\t" (map (x: "PB_${x.flag}")
(builtins.filter (x: x.cond) [
{ cond = enableMalloc; flag = "ENABLE_MALLOC=1"; }
{ cond = noPackedStructs; flag = "NO_PACKED_STRUCTS=1"; }
{ cond = maxRequiredFields != null; flag = "MAX_REQUIRED_FIELDS=${maxRequiredFields}"; }
{ cond = field32bit; flag = "FIELD_32BIT=1"; }
{ cond = noErrmsg; flag = "NO_ERRMSG=1"; }
{ cond = bufferOnly; flag = "BUFFER_ONLY=1"; }
{ cond = systemHeader != null; flag = "SYSTEM_HEADER=${systemHeader}"; }
{ cond = without64bit; flag = "WITHOUT_64BIT=1"; }
{ cond = encodeArraysUnpacked; flag = "ENCODE_ARRAYS_UNPACKED=1"; }
{ cond = convertDoubleFloat; flag = "CONVERT_DOUBLE_FLOAT=1"; }
{ cond = validateUtf8; flag = "VALIDATE_UTF8=1"; }
{ cond = littleEndian8bit; flag = "LITTLE_ENDIAN_8BIT=1"; }
{ cond = c99StaticAssert; flag = "C99_STATIC_ASSERT=1"; }
{ cond = noStaticAssert; flag = "NO_STATIC_ASSERT=1"; }
]))}
)
'';
in
''
cat << EOF >> CMakeLists.txt
${compile_definitions "protobuf-nanopb"}
${compile_definitions "protobuf-nanopb-static"}
EOF
'';
cmakeFlags = [
"-DBUILD_SHARED_LIBS=ON"
"-DBUILD_STATIC_LIBS=ON"
"-Dnanopb_BUILD_GENERATOR=OFF"
"-Dnanopb_BUILD_RUNTIME=ON"
];
})