From 1af737f3d94652c35e8df8447a19189e54a0d65c Mon Sep 17 00:00:00 2001 From: Alex Tereschenko Date: Fri, 1 Jul 2016 23:09:34 +0200 Subject: [PATCH] mock: mraa with mock platform now works in Windows under MSYS2 Signed-off-by: Alex Tereschenko Signed-off-by: Brendan Le Foll --- api/mraa/iio_kernel_headers.h | 5 +++ docs/mock.md | 48 +++++++++++++++++++++++++ include/linux/posix_types.h | 2 ++ include/linux/spi_kernel_headers.h | 58 ++++++++++++++++++++++++++++++ src/CMakeLists.txt | 10 +++++- src/i2c/i2c.c | 3 ++ src/iio/iio.c | 3 ++ src/python/python2/CMakeLists.txt | 2 +- src/python/python3/CMakeLists.txt | 2 +- src/spi/spi.c | 9 +++++ src/uart/uart.c | 2 ++ 11 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 include/linux/spi_kernel_headers.h diff --git a/api/mraa/iio_kernel_headers.h b/api/mraa/iio_kernel_headers.h index 5fbe81f..fab9bf4 100644 --- a/api/mraa/iio_kernel_headers.h +++ b/api/mraa/iio_kernel_headers.h @@ -106,7 +106,12 @@ enum iio_event_direction { }; //linux/iio/events.h +#if defined(MSYS) +#define __USE_LINUX_IOCTL_DEFS +#include +#else #include +#endif /** * struct iio_event_data - The actual event being pushed to userspace diff --git a/docs/mock.md b/docs/mock.md index f2346d8..f9d146e 100644 --- a/docs/mock.md +++ b/docs/mock.md @@ -35,3 +35,51 @@ CMake options. To build under Linux, follow standard instructions, just make sure to set the `-DBUILDARCH="MOCK"` CMake option. + +### Windows + +Mocking capability allows us to build and use libmraa under Windows. That helps +if you e.g. don't want to leave your customary Windows-based Python IDE, but +want to develop libmraa-based programs. + +Building Node.js bindings was not yet tested under Windows as MSYS2 +does not have a ready-made package. Java was not tested either. + +#### Prerequisites + +You'll need the following to build libmraa under Windows: + +* [MSYS2](http://mingw-w64.org/doku.php/download/msys2) basic installation +* Several additional packages, install them by running + + ```bash + pacman -S cmake base-devel gcc git + ``` + +#### Compiling + +The procedure is conceptually the same as under Linux - you first need to run +CMake with specific options to generate makefiles and then run make to build everything. + +* Run MSYS2 shell (not a MinGW one) +* Clone the libmraa git repo (let's assume into `/home/test/mraa/mraa-src` dir) +* Create a build directory outside of the clone one (let's say `/home/test/mraa/mraa-build`) +* Run CMake, switching off unsupported options and enabling mock platform: + + ```bash + cmake ../mraa-src/ -DBUILDARCH="MOCK" -DBUILDSWIGNODE=OFF -DENABLEEXAMPLES=OFF + ``` + +* Make, install and test: + + ```bash + make clean && make install && make test + ``` + + All tests should pass. + +**Note:** To have autocompletion in Python IDE, just point it to MSYS2's Python +and make sure to specify any additional paths pointing to site-packages dir +with mraa module if IDE requires that ("Interpreter Paths" in PyCharm). +With the above settings the module will be installed into `/usr/local/lib/python2.7/site-packages` +and the libmraa itself - into `/usr/local/bin`. diff --git a/include/linux/posix_types.h b/include/linux/posix_types.h index 33b0fa2..2a74ab1 100644 --- a/include/linux/posix_types.h +++ b/include/linux/posix_types.h @@ -28,6 +28,8 @@ typedef struct { typedef void (*__kernel_sighandler_t)(int); typedef int __kernel_key_t; typedef int __kernel_mqd_t; +#if !defined(MSYS) #include +#endif /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ #endif diff --git a/include/linux/spi_kernel_headers.h b/include/linux/spi_kernel_headers.h new file mode 100644 index 0000000..cd984e4 --- /dev/null +++ b/include/linux/spi_kernel_headers.h @@ -0,0 +1,58 @@ +/* + * This header was manually generated from a Linux kernel header + * linux/spi/spidev.h, to make information necessary for compilation + * to be available under MSYS. It contains only constants, + * structures, and macros generated from the original header, and thus, + * contains no copyrightable information. + */ +#ifndef _SPI_KERNEL_HEADERS_H +#define _SPI_KERNEL_HEADERS_H + +#include + +#define SPI_CPHA 0x01 +#define SPI_CPOL 0x02 + +#define SPI_MODE_0 (0|0) +#define SPI_MODE_1 (0|SPI_CPHA) +#define SPI_MODE_2 (SPI_CPOL|0) +#define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) + +#define SPI_IOC_MAGIC 'k' + +struct spi_ioc_transfer { + __u64 tx_buf; + __u64 rx_buf; + + __u32 len; + __u32 speed_hz; + + __u16 delay_usecs; + __u8 bits_per_word; + __u8 cs_change; + __u8 tx_nbits; + __u8 rx_nbits; + __u16 pad; +}; + +#define SPI_MSGSIZE(N) \ + ((((N)*(sizeof (struct spi_ioc_transfer))) < (1 << _IOC_SIZEBITS)) \ + ? ((N)*(sizeof (struct spi_ioc_transfer))) : 0) +#define SPI_IOC_MESSAGE(N) _IOW(SPI_IOC_MAGIC, 0, char[SPI_MSGSIZE(N)]) + +#define SPI_IOC_RD_MODE _IOR(SPI_IOC_MAGIC, 1, __u8) +#define SPI_IOC_WR_MODE _IOW(SPI_IOC_MAGIC, 1, __u8) + +#define SPI_IOC_RD_LSB_FIRST _IOR(SPI_IOC_MAGIC, 2, __u8) +#define SPI_IOC_WR_LSB_FIRST _IOW(SPI_IOC_MAGIC, 2, __u8) + +#define SPI_IOC_RD_BITS_PER_WORD _IOR(SPI_IOC_MAGIC, 3, __u8) +#define SPI_IOC_WR_BITS_PER_WORD _IOW(SPI_IOC_MAGIC, 3, __u8) + +#define SPI_IOC_RD_MAX_SPEED_HZ _IOR(SPI_IOC_MAGIC, 4, __u32) +#define SPI_IOC_WR_MAX_SPEED_HZ _IOW(SPI_IOC_MAGIC, 4, __u32) + +#define SPI_IOC_RD_MODE32 _IOR(SPI_IOC_MAGIC, 5, __u32) +#define SPI_IOC_WR_MODE32 _IOW(SPI_IOC_MAGIC, 5, __u32) + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c498233..03f7ae1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -104,6 +104,9 @@ endif() if (MOCKPLAT) add_subdirectory(mock) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMOCKPLAT=1") + if (MSYS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DMSYS=1") + endif () endif() if (USBPLAT) @@ -228,4 +231,9 @@ set_target_properties( SOVERSION ${mraa_VERSION_MAJOR} VERSION ${mraa_VERSION_STRING} ) -install(TARGETS mraa DESTINATION ${LIB_INSTALL_DIR}) +if (MSYS) + # Under MSYS we have to put our generated DLL into bin, otherwise it's not picked up + install(TARGETS mraa DESTINATION ${CMAKE_INSTALL_BINDIR}) +else () + install(TARGETS mraa DESTINATION ${LIB_INSTALL_DIR}) +endif () diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index 85b0172..7ee3cf0 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -34,6 +34,9 @@ #include #include #include +#if defined(MSYS) +#define __USE_LINUX_IOCTL_DEFS +#endif #include #include "linux/i2c-dev.h" #include diff --git a/src/iio/iio.c b/src/iio/iio.c index d4d9842..ef2da55 100644 --- a/src/iio/iio.c +++ b/src/iio/iio.c @@ -27,6 +27,9 @@ #include "dirent.h" #include #include +#if defined(MSYS) +#define __USE_LINUX_IOCTL_DEFS +#endif #include #include diff --git a/src/python/python2/CMakeLists.txt b/src/python/python2/CMakeLists.txt index 10d23c2..3d8ace3 100644 --- a/src/python/python2/CMakeLists.txt +++ b/src/python/python2/CMakeLists.txt @@ -31,7 +31,7 @@ if (PYTHON2_LIBRARY) COMPILE_FLAGS "${CMAKE_C_FLAGS} -DSWIGPYTHON=${SWIG_FOUND}" ) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/_mraa.so + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/_mraa${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_CURRENT_BINARY_DIR}/mraa.py DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON2_PACKAGES_PATH}) endif() diff --git a/src/python/python3/CMakeLists.txt b/src/python/python3/CMakeLists.txt index 5fb8822..8d688ba 100644 --- a/src/python/python3/CMakeLists.txt +++ b/src/python/python3/CMakeLists.txt @@ -18,7 +18,7 @@ if (PYTHON3_LIBRARY) COMPILE_FLAGS "${CMAKE_C_FLAGS} -DSWIGPYTHON=${SWIG_FOUND}" ) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/_mraa.so + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/_mraa${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_CURRENT_BINARY_DIR}/mraa.py DESTINATION ${CMAKE_INSTALL_PREFIX}/${PYTHON3_PACKAGES_PATH}) endif () diff --git a/src/spi/spi.c b/src/spi/spi.c index 570c207..e7a6b47 100644 --- a/src/spi/spi.c +++ b/src/spi/spi.c @@ -25,8 +25,17 @@ #include #include +#if defined(MSYS) +#define __USE_LINUX_IOCTL_DEFS +#endif #include +#if defined(MSYS) +// There's no spidev.h on MSYS, so we need to provide our own, +// and only *after* including ioctl.h as that one contains prerequisites. +#include "linux/spi_kernel_headers.h" +#else #include +#endif #include #include #include diff --git a/src/uart/uart.c b/src/uart/uart.c index d39559f..0901f34 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -102,10 +102,12 @@ uint2speed(unsigned int speed) return B2500000; case 3000000: return B3000000; +#if !defined(MSYS) case 3500000: return B3500000; case 4000000: return B4000000; +#endif default: // if we are here, then an unsupported baudrate was selected. return 0;