Skip to content

Build with MSVC and add an MSVC CI job - #458

Merged
leducp merged 7 commits into
leducp:masterfrom
nanotec-electronic:msvc-portability
Nov 13, 2026
Merged

leducp merged 7 commits into
leducp:masterfrom
nanotec-electronic:msvc-portability

Conversation

@milbradt-nanotec

Copy link
Copy Markdown
Contributor

Second pull request announced in #456.

What

KickCAT builds with MSVC through its own CMake with the default options, and
the unit tests pass. A CI job keeps it that way.

The commits are meant to be read one by one:

  1. Master library headers and sources: KICKCAT_PACKED_BEGIN/END
    (kickcat/packed.h) instead of __attribute__((__packed__)), with
    static_asserts on the structure sizes; FoE::opcode::ERROR renamed to
    FoE::opcode::ERR and the parameter interface renamed to ifname (both
    are macros of the Windows API, see Introduction: Nanotec is using KickCAT in a fork, and would like to contribute #456); DOMAIN of the MSVC <math.h>.
  2. Library through CMake: /W4 for MSVC instead of the GCC warning options,
    /permissive- and NOMINMAX, and Mutex/ConditionVariable on the
    native primitives (SRWLOCK, CONDITION_VARIABLE), because MSVC has no
    pthread. MinGW keeps the pthread sources.
  3. Unit tests: packed structures in two test files, a missing include, GNU
    linker options only for MinGW.
  4. Examples and tools: packed structures, C++17 for four targets that relied
    on the compiler default.
  5. CI: a second windows-latest entry in the build_x86 matrix that builds
    with MSVC, with a conan profile for it.
  6. TapSocket: the interface parameter that commit 1 missed.

Notes for the review

  • No change for GCC, Clang and MinGW: same sources, same options.
  • /permissive- and NOMINMAX are PUBLIC on the kickcat target, because
    the headers need them too.
  • As with MinGW, the Windows Mutex has no priority inheritance and is not
    shareable between processes.
  • /W4 reports warnings in existing code (mostly conversions and format
    specifiers), none in the new files. They do not fail the build.
  • The test step passes -C to ctest for all entries: the Visual Studio
    generators are multi-config, single-config generators ignore it.
  • The CI entry uses windows-latest, like the MinGW entry. Its conan profile
    names the compiler of that image (Visual Studio 2026); for Visual Studio
    2022 pass -s compiler.version=194.
  • I could not run the workflow itself in our fork. I ran the same commands
    locally on a clean export: conan install, configure, build, ctest.

Testing

  • MSVC 19.51 (Visual Studio 2026) and 19.36 (Visual Studio 2022), default
    options: 830 tests pass.
  • Linux, GCC: 806 tests pass.
  • Windows, MinGW: 801 tests pass.

- Add kickcat/packed.h with KICKCAT_PACKED_BEGIN/END: #pragma pack for
  MSVC, __attribute__((__packed__)) for all other compilers. Convert the
  packed structures and add static_asserts for their sizes.
- Rename FoE::opcode::ERROR to FoE::opcode::ERR: ERROR is a macro of the
  Windows API (wingdi.h), so the header did not compile after <windows.h>.
  Same naming as mailbox::ERR. The value (0x05) is unchanged.
- Rename the parameter "interface" to "ifname": "interface" is a macro of
  the Windows API as well.
- CoE/OD.h: undefine the DOMAIN macro of the MSVC <math.h>.
- OS/Windows/Time.cc: sleep() uses std::this_thread::sleep_for with MSVC,
  which has no clock_nanosleep().

No functional change for GCC and Clang.
- cmake/Config.cmake: MSVC gets /W4 instead of the GCC warning options,
  which cl rejects (D8021 on /Wextra). GCC and Clang keep their options.
- lib/CMakeLists.txt: with MSVC the library is compiled with /permissive-
  (the code uses the alternative operator tokens and, or, not) and with
  NOMINMAX (<windows.h> defines min and max as macros otherwise). Both are
  PUBLIC because the headers need them as well.
- Mutex and ConditionVariable: MSVC has no pthread. Add an implementation
  on the native primitives (SRWLOCK, CONDITION_VARIABLE) in OS/Windows,
  used with MSVC only. MinGW keeps the pthread sources. As with MinGW, the
  primitives are not shareable between processes and there is no priority
  inheritance.
- types.h, helpers.cc: select the Windows headers with MSVC too.

No change for GCC, Clang and MinGW.
- bus-t.cc, gateway-t.cc: declare the packed structures with
  KICKCAT_PACKED_BEGIN/END instead of __attribute__((__packed__)).
- kickcat-t.cc: include <array>, which the other standard libraries
  provided indirectly.
- unit/CMakeLists.txt: -static-libgcc and -static-libstdc++ are options of
  the GNU toolchain, do not pass them to the MSVC linker (LNK4044).

The 801 tests of the Windows build pass with MSVC.
- Master examples: declare the packed structures with
  KICKCAT_PACKED_BEGIN/END instead of __attribute__((packed)).
- marvin.cc: define _USE_MATH_DEFINES before the first standard header,
  <iostream> already includes <cmath> with MSVC.
- debugUseCase.cc, eeprom.cc: select the Windows socket with MSVC too.
- od_generator, hw_test_bench, esi_validate, esi_boot: require C++17.
  These targets relied on the default standard of the compiler, which is
  C++14 with MSVC.

With this, a build with the default options succeeds with MSVC.
Add a second windows-latest entry to the build_x86 matrix that builds with
MSVC. It installs the dependencies with the new
conan/profile_windows_msvc_x86_64.txt and lets CMake use the installed
Visual Studio. It builds the default options and runs the unit tests, like
the MinGW entry.

The profile names the compiler of the current windows-latest image
(Visual Studio 2026, compiler.version=195). For Visual Studio 2022 pass
-s compiler.version=194.

ctest gets the configuration (-C): the Visual Studio generators are
multi-config and ctest finds no test without it. Single-config generators
ignore the option.
Same change as for AbstractSocket and the Windows Socket: "interface" is
a macro of the Windows API. It is only hidden as long as <windows.h> is
included with WIN32_LEAN_AND_MEAN.
The include was added to undefine the DOMAIN macro of the MSVC <math.h>.
Other C libraries do not need it, and it breaks the NuttX builds whose
board configuration has no math library (arduino-due, freedom-k64f:
"'acos' has not been declared in 'std'").
@github-actions

Copy link
Copy Markdown

File Coverage Lines Branches
All files 87% 90% 83% ✅
lib/include/kickcat/Error.h 91% 100% 83% ✅
lib/include/kickcat/LockedRing.h 87% 94% 81% ✅
lib/include/kickcat/LoopbackSocket.h 81% 82% 80% ✅
lib/include/kickcat/SimulatorControl.h 84% 97% 70% ✅
lib/include/kickcat/utils/xml.h 98% 100% 97% ✅
lib/master/include/kickcat/AbstractLink.h 95% 90% 100% ✅
lib/master/src/Bus.cc 91% 94% 89% ✅
lib/master/src/CoE.cc 86% 90% 81% ✅
lib/master/src/FoE.cc 98% 100% 96% ✅
lib/master/src/Link.cc 98% 100% 96% ✅
lib/master/src/MailboxSequencer.cc 97% 100% 94% ✅
lib/master/src/MasterOD.cc 96% 99% 93% ✅
lib/master/src/Prints.cc 88% 88% 88% ✅
lib/master/src/Slave.cc 72% 87% 58% ❌
lib/master/src/dc.cc 76% 82% 70% ✅
lib/master/src/CoE/CiA/DS402/Drive.cc 78% 87% 70% ✅
lib/simulation/include/kickcat/simulation/devices/Ds402Motor.h 75% 91% 58% ✅
lib/simulation/src/DirectoryStorage.cc 93% 94% 92% ✅
lib/simulation/src/SimulatedSlave.cc 57% 67% 47% ❌
lib/simulation/src/SimulatorControlServer.cc 91% 97% 84% ✅
lib/simulation/src/Topology.cc 73% 70% 76% ❌
lib/slave/src/AbstractESC.cc 94% 100% 88% ✅
lib/slave/src/AbstractEmulatedEEPROM.cc 0% 0% 0% ❌
lib/slave/src/ESMStates.cc 95% 98% 92% ✅
lib/slave/src/ESM.cc 94% 100% 89% ✅
lib/slave/src/EmulatedNetwork.cc 98% 99% 97% ✅
lib/slave/src/PDO.cc 93% 97% 89% ✅
lib/slave/src/ESC/EmulatedESC.cc 87% 92% 82% ✅
lib/slave/src/ESC/Lan9252.cc 0% 0% 0% ❌
lib/src/Frame.cc 96% 96% 97% ✅
lib/src/Mailbox.cc 93% 96% 89% ✅
lib/src/SIIParser.cc 93% 95% 90% ✅
lib/src/protocol.cc 87% 84% 90% ✅
lib/src/CoE/OD.cc 95% 96% 94% ✅
lib/src/CoE/protocol.cc 92% 92% 92% ✅
lib/src/CoE/CiA/DS402/StateMachine.cc 93% 94% 92% ✅
lib/src/CoE/mailbox/request.cc 84% 86% 83% ✅
lib/src/CoE/mailbox/response.cc 87% 92% 82% ✅
lib/src/ESI/Parser.cc 86% 89% 83% ✅
lib/src/ESI/SIIBuilder.cc 86% 94% 77% ✅
lib/src/EoE/protocol.cc 0% 0% 0% ❌
lib/src/FoE/protocol.cc 22% 28% 17% ❌
lib/src/FoE/mailbox/request.cc 95% 97% 93% ✅
lib/src/FoE/mailbox/response.cc 89% 93% 86% ✅
lib/src/utils/xml.cc 96% 98% 95% ✅
lib/src/utils/xsd.cc 96% 98% 95% ✅

Minimum allowed coverage is 75%

Generated by 🐒 cobertura-action against 39bc1c0

@leducp
leducp merged commit 2502290 into leducp:master Nov 13, 2026
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants