Skip to content

Commit

Permalink
Support building with MinGW-w64 toolchain
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Masselink <[email protected]>
  • Loading branch information
PatrickM-ZS committed Sep 20, 2024
1 parent 4c510fd commit dbfa8cd
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cmake/Modules/Generate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ function(IDLC_GENERATE)
endif()
set(_idlc_depends CycloneDDS::libidlc)
else()
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND NOT ".so" IN_LIST CMAKE_FIND_LIBRARY_SUFFIXES)
# When cross-compiling, find_library looks for libraries using naming onventions of the target,
# But here we're trying to find the idlc backend library to run on the host.
# For now, building for a Windows target on a Linux host with w64-mingw is supported by this workaround.
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES ".so")
endif()
find_library(_idlc_backend "cycloneddsidlc" NO_CMAKE_FIND_ROOT_PATH)
if (_idlc_backend)
if (NOT DEFINED _idlc_generate_skipreport)
Expand Down
2 changes: 1 addition & 1 deletion examples/roundtrip/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ static dds_time_t exampleGet99PercentileFromTimeStats (ExampleTimeStats *stats)
static dds_entity_t waitSet;

#ifdef _WIN32
#include <Windows.h>
#include <windows.h>
static bool CtrlHandler (DWORD fdwCtrlType)
{
(void)fdwCtrlType;
Expand Down
2 changes: 1 addition & 1 deletion examples/roundtrip/pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static dds_entity_t prepare_dds(dds_entity_t *writer, dds_entity_t *reader, dds_
static void finalize_dds(dds_entity_t participant, RoundTripModule_DataType data[MAX_SAMPLES]);

#ifdef _WIN32
#include <Windows.h>
#include <windows.h>
static bool CtrlHandler (DWORD fdwCtrlType)
{
(void)fdwCtrlType;
Expand Down
42 changes: 42 additions & 0 deletions ports/mingw-w64/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
ARG ARCH=x86_64

FROM ubuntu:24.04 AS build
# supports x86_64 or i686
ARG ARCH

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -q\
build-essential \
cmake \
mingw-w64

WORKDIR /project
COPY --link . src

# Host build, only for idlc (+ libidlc) but building individual targets doesn't really work...
RUN cmake -S src -B host-build
RUN cmake --build host-build --parallel
RUN cmake --install host-build --prefix /project/install

# Cross build with mingw-w64 toolchain
RUN cmake -S src -B build \
-DBUILD_EXAMPLES=1 \
-DCMAKE_TOOLCHAIN_FILE=/project/src/ports/mingw-w64/mingw-w64-${ARCH}.cmake \
-DCMAKE_PREFIX_PATH=/project/install
RUN cmake --build build --parallel
RUN cmake --install build

FROM ubuntu:24.04
ARG ARCH

RUN if [ "${ARCH}" = "i686" ]; then \
dpkg --add-architecture i386; \
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -q mingw-w64 wine wine32:i386; \
echo "export WINEARCH=win32" >> /etc/bash.bashrc; \
else \
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y -q mingw-w64 wine; \
fi

COPY --from=build /usr/local/bin /usr/local/bin
COPY --from=build /usr/local/lib /usr/local/lib
COPY --from=build /usr/local/include /usr/local/include
COPY --from=build /project/build/bin /usr/local/bin
13 changes: 13 additions & 0 deletions ports/mingw-w64/mingw-w64-i686.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR i686)

set(TOOLCHAIN_PREFIX i686-w64-mingw32)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)

set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
13 changes: 13 additions & 0 deletions ports/mingw-w64/mingw-w64-x86_64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR x86_64)

set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)

set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
4 changes: 3 additions & 1 deletion src/ddsrt/include/dds/ddsrt/types/windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
#endif

#include <windows.h>
#include <VersionHelpers.h>
#include <versionhelpers.h>
#include <stdint.h>
#include <inttypes.h>
#include <wchar.h>

#ifdef _MSC_VER
typedef SSIZE_T ssize_t;
#endif

#endif /* DDSRT_TYPES_WINDOWS_H */
2 changes: 1 addition & 1 deletion src/ddsrt/src/netstat/windows/netstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <Windows.h>
#include <windows.h>

#include <ws2def.h>
#include <ws2tcpip.h>
Expand Down
2 changes: 1 addition & 1 deletion src/ddsrt/src/sockets/windows/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "dds/ddsrt/time.h"

// Has to be included after most of windows has been included, it seems
#include <Mswsock.h>
#include <mswsock.h>

#ifdef ddsrt_select
#undef ddsrt_select /* See sockets.h for details. */
Expand Down
2 changes: 1 addition & 1 deletion src/tools/idlc/src/idlc/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string.h>

#if WIN32
#include <Windows.h>
#include <windows.h>
static const char sep[] = "/\\";
static const char lib[] = "cyclonedds";
static const char ext[] = "dll";
Expand Down

0 comments on commit dbfa8cd

Please sign in to comment.