ludocode / mpack

MPack - A C encoder/decoder for the MessagePack serialization format / msgpack.org[C]
MIT License
521 stars 82 forks source link

Usage in Linux kernel modules #80

Closed mloy closed 2 years ago

mloy commented 3 years ago

Hi, we are using this nice library within Linux kernel modules. Since we only need the writer, a colleague took only the necessary code and changed/removed stuff to make it compile.

This works but is of course not that nice. I haven't found any clean way of configuring the project accordingly.

Did I miss something?

best regards, Matthias

edsiper commented 3 years ago

@mloy just curious, what's the use case for that specific module?

mloy commented 3 years ago

We are doing signal processing in kernel space and use msgpack to transfer structured information.

mloy commented 3 years ago

In a former solution we had to use json which is a pain to compose in kernel space if floating point numbers are to be expressed.

In json you have to convert everything to a string. In msgpack you simply copy the value (after changing into the correct endianness...)

bernardoaraujor commented 3 years ago

@mloy interesting!

I wrote a CMake file to fetch and build mpack as a static library. I'm using all the sources, but as soon as I set in stone what I'm using and (most importantly) what I'm not using, I'll clean it up (${sources} and ${headers}) so libmpack.a is as small as possible.

here's the contents of mpack.cmake:

include(ExternalProject)

include(FetchContent)
FetchContent_Declare(
  mpack
  GIT_REPOSITORY http://github.com/ludocode/mpack.git
  GIT_TAG e150cb7455cefdab0df6359414e9e4c792723d73 #v1.0
)

message(STATUS "Fetching mpack")
FetchContent_MakeAvailable(mpack)

set(MPACK_SRC ${CMAKE_CURRENT_BINARY_DIR}/_deps/mpack-src/src/mpack)

set(sources
  ${MPACK_SRC}/mpack-common.c
  ${MPACK_SRC}/mpack-expect.c
  ${MPACK_SRC}/mpack-node.c
  ${MPACK_SRC}/mpack-platform.c
  ${MPACK_SRC}/mpack-reader.c
  ${MPACK_SRC}/mpack-writer.c
)

set(headers
  ${MPACK_SRC}/mpack-common.h
  ${MPACK_SRC}/mpack-defaults.h
  ${MPACK_SRC}/mpack-expect.h
  ${MPACK_SRC}/mpack.h
  ${MPACK_SRC}/mpack-node.h
  ${MPACK_SRC}/mpack-platform.h
  ${MPACK_SRC}/mpack-reader.h
  ${MPACK_SRC}/mpack-writer.h
)

add_library(mpack STATIC ${sources})

install(TARGETS mpack DESTINATION lib)
install(FILES ${headers} DESTINATION include)
bernardoaraujor commented 3 years ago

Although I will likely not have time to do so, I can imagine an interesting case where the mpack.cmake above morphs into a CMakeLists.txt (with a potential for a PR into upstream, in case the authors are open to having CMake integrated into the repo).

Instead of building a single libmpack.a, it would ideally provide:

with the appropriate dependencies between eachother.

Too much extra complexity for too little profit? maybe...

However, from my perspective it's an interesting angle becase I'm a system integrator before I'm a developer, and automating builds (especially in the context of OpenEmbedded and Yocto Project) are always handy.

mloy commented 3 years ago

The problem is in the headers included by mpack-platform.h. You can have a config for disable or enable features but I need a switch for kernel/user space headers.

ludocode commented 3 years ago

Which headers does MPack use that are not available in Linux kernel space? I'd love to add a configuration option or some sort of auto-detection to remove the dependency on headers that aren't available in the kernel (would __STDC_HOSTED__ work for this?)

ludocode commented 2 years ago

Building in the Linux kernel is now supported. There is a new MPACK_CONFORMING configuration that can be turned off to make MPack avoid the standard headers, and the Linux-specific headers can be included in an mpack-config.h instead. These includes are kept out of the main MPack tree so that MPack stays pure of the GPL.

The sample mpack-config.h as well as a shim to build the MPack unit test suite as a Linux kernel module are in a separate project here: https://github.com/ludocode/mpack-linux-kernel