Skip to content

Commit

Permalink
Merge pull request #65 from epsilonrt/win-build
Browse files Browse the repository at this point in the history
Build mbpoll for Windows
  • Loading branch information
epsilonrt authored Apr 24, 2023
2 parents d8fe0ea + fbd3903 commit ed00121
Show file tree
Hide file tree
Showing 9 changed files with 871 additions and 569 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ compile_commands.json
obj/
.*
version.*
build*/
libmodbus/
57 changes: 47 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- CMakeLists.txt generated by CodeLite IDE. Do not edit by hand -*-

cmake_minimum_required(VERSION 2.8.11)
cmake_minimum_required(VERSION 2.9)

# Workspace name
project(mbpoll)
Expand All @@ -27,6 +27,15 @@ set(PROJECT_PATH "${CMAKE_SOURCE_DIR}/")
#{{{{ User Code 1
# Place your code here
# Modifies binary file paths if codelite is not used.
include(CheckSymbolExists)

set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(getopt "getopt.h" HAVE_GETOPT)
if (NOT HAVE_GETOPT)
set (GETOPT_SOURCES 3rdparty/getopt/getopt.c)
include_directories(BEFORE 3rdparty/getopt)
endif (NOT HAVE_GETOPT)

set (MBPOLL_CUSTOM_RTS_GPIO 1 CACHE BOOL "Enable custom Rts (if libpiduino found, only on ARM)")

if (NOT CL_USED)
Expand All @@ -39,12 +48,24 @@ endif (NOT CL_USED)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

find_package(PkgConfig REQUIRED)
pkg_check_modules (LIBMODBUS REQUIRED libmodbus>=3.1.4)

link_directories(${LIBMODBUS_LIBRARY_DIRS})
add_definitions(${LIBMODBUS_CFLAGS})
if(WIN32)
# Import libmodbus library from sources located in libmodbus/src
set(LIBMODBUS_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/libmodbus/src ${CMAKE_SOURCE_DIR}/libmodbus/src/win32)
file(GLOB_RECURSE LIBMODBUS_SRCS RELATIVE ${CMAKE_SOURCE_DIR} "libmodbus/src/*.c")
# message (STATUS "LIBMODBUS_SRCS ${LIBMODBUS_SRCS}")
find_program (CSCRIPT_PATH NAMES cscript REQUIRED)
# message (STATUS "CSCRIPT_PATH ${CSCRIPT_PATH}")

else(WIN32)
pkg_check_modules (LIBMODBUS REQUIRED libmodbus>=3.1.4)
link_directories(${LIBMODBUS_LIBRARY_DIRS})
add_definitions(${LIBMODBUS_CFLAGS})
include_directories(BEFORE ${LIBMODBUS_INCLUDE_DIRS})
list(APPEND LINK_OPTIONS ${LIBMODBUS_LIBRARIES})
endif(WIN32)

include_directories(BEFORE ${LIBMODBUS_INCLUDE_DIRS})
list(APPEND LINK_OPTIONS ${LIBMODBUS_LIBRARIES})

# search for the piduino package, if found, add the options ...
find_package(PIDUINO QUIET)
Expand Down Expand Up @@ -92,13 +113,19 @@ include_directories(

)


if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
else(MSVC)
# Compiler options
add_definitions(-O2)
add_definitions(-Wall)
add_definitions(-Wno-unused-parameter)
add_definitions(-Wno-unused-function)
add_definitions(-Wno-unused-const-variable)
set_source_files_properties(
${C_SRCS} PROPERTIES COMPILE_FLAGS
" -O2 -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-const-variable")
endif(MSVC)
add_definitions(
-DNDEBUG
)
Expand All @@ -109,6 +136,10 @@ add_definitions(

if(WIN32)
# Resource options
set(RC_SRCS
${CMAKE_SOURCE_DIR}/libmodbus/src/win32/modbus.rc
)
list(APPEND LINK_OPTIONS wsock32 ws2_32)
endif(WIN32)

# Library path
Expand All @@ -119,12 +150,10 @@ set ( C_SRCS
${CMAKE_SOURCE_DIR}/src/mbpoll.c
${CMAKE_SOURCE_DIR}/src/custom-rts.c
${CMAKE_SOURCE_DIR}/src/serial.c
${LIBMODBUS_SRCS}
${GETOPT_SOURCES}
)

set_source_files_properties(
${C_SRCS} PROPERTIES COMPILE_FLAGS
" -O2 -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-const-variable")

if(WIN32)
enable_language(RC)
set(CMAKE_RC_COMPILE_OBJECT
Expand Down Expand Up @@ -155,6 +184,14 @@ target_link_libraries(mbpoll ${LINK_OPTIONS})
install(TARGETS mbpoll RUNTIME DESTINATION bin
PERMISSIONS ${PROGRAM_PERMISSIONS})

if(WIN32)
add_custom_command(
TARGET mbpoll PRE_BUILD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/libmodbus/src/win32
COMMAND ${CSCRIPT_PATH} ${CMAKE_SOURCE_DIR}/libmodbus/src/win32/configure.js
VERBATIM)
endif(WIN32)

### Debian Package generation
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
set(CPACK_PACKAGE_VERSION "${MBPOLL_VERSION_MAJOR}.${MBPOLL_VERSION_MINOR}.${MBPOLL_VERSION_PATCH}")
Expand Down
88 changes: 88 additions & 0 deletions README-WINDOWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Build mbpoll for Windows

## Prerequisites

### Install Visual Studio Code and Build Tools for Visual Studio 2022

To successfully complete this tutorial, you must do the following :

1. Install [Visual Studio Code] (https://code.visualstudio.com/)
2. Install the [C/C++ extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools). You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X).
3. Install [Build Tools pour Visual Studio 2022](https://visualstudio.microsoft.com/fr/downloads/)

Installation following the instructions on the page [Configure VS Code for Microsoft C++](https://code.visualstudio.com/docs/cpp/config-msvc)

### Install CMake

1. Download the latest version of [CMake](https://cmake.org/download/)
2. Install the [CMake Tools extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools). . You can install the CMake Tools extension by searching for 'cmake' in the Extensions view (Ctrl+Shift+X).

### Install pkg-config

This is a step-by-step procedure to get pkg-config working on Windows.

There were multiple versions of the packages available, and in each case I just downloaded the latest version.

1. go to http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/
2. download the file [pkg-config_0.26-1_win32.zip](http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/pkg-config_0.26-1_win32.zip)
3. extract the file to C:\Info
4. download the file [gettext-runtime_0.18.1.1-2_win32.zip](http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/gettext-runtime_0.18.1.1-2_win32.zip)
5. extract the file to C:\Info
6. go to http://ftp.gnome.org/pub/gnome/binaries/win32/glib/2.28
7. download the file [glib_2.28.8-1_win32.zip](http://ftp.acc.umu.se/pub/gnome/binaries/win32/glib/2.28/glib_2.28.8-1_win32.zip)
8. extract the file to C:\Info
9. Adds the path C:\Info\bin to the PATH environment variable

### Install git for Windows

1. Download the latest version of [Git for Windows](https://gitforwindows.org/)
2. Install Git for Windows

## Clone the repositories

Open a git bash terminal :

git clone https://github.com/epsilonrt/mbpoll.git
cd mbpoll
git clone https://github.com/stephane/libmodbus.git

## Build

1. Open Visual Studio Code and open the folder `mbpoll`.
2. Choose Visual Studio Community 2022 Realease - x86 as the kit in the Cmake status bar.
3. Choose the Release configuration in the Cmake status bar.
4. Click on the Configure button in the status bar to configure the project.
5. Click on the Build button in the status bar to build the project.

[main] Building folder: mbpoll
[build] Starting build
[proc] Executing command: C:\Info\CMake\bin\cmake.EXE --build c:/Users/pasca/OneDrive/Documents/src/mbpoll/build --config Release --target ALL_BUILD -j 18 --
[build] MSBuild version 17.5.0+6f08c67f3 for .NET Framework
[build]
[build] Microsoft (R) Windows Script Host Version 5.812
[build] Copyright (C) Microsoft Corporation. Tous droits réservés.
[build]
[build] ..\modbus-version.h created.
[build] modbus.dll.manifest created.
[build] config.h created.
[build]
[build] Libmodbus configuration completed
[build]
[build] modbus-data.c
[build] C:\Users\pasca\OneDrive\Documents\src\mbpoll\libmodbus\src\modbus-data.c(77,6): warning C4273: 'modbus_set_bits_from_byte' : liaison DLL incohérente [C:\Users\pasca\OneDrive\Documents\src\mbpoll\build\mbpoll.vcxproj]
.......
[build] LINK : warning LNK4217: le symbole '_modbus_set_bits_from_bytes' défini dans 'modbus-data.obj' est importé par 'modbus.obj' dans la fonction '_modbus_reply' [C:\Users\pasca\OneDrive\Documents\src\mbpoll\build\mbpoll.vcxproj]
[build] mbpoll.vcxproj -> C:\Users\pasca\OneDrive\Documents\src\mbpoll\build\Release\mbpoll.exe
[driver] Build completed: 00:00:04.712
[build] Build finished with exit code 0

The warnings LNK4217 and C4273 are normal.

## Make Innosetup installer

1. Download the latest version of [Innosetup](https://jrsoftware.org/isdl.php)
2. Go to the folder `mbpoll\package\win` and open the file `mbpoll.iss` with Innosetup
3. Download the Microsoft Visual C++ Redistributable latest from https://aka.ms/vs/17/release/vc_redist.x86.exe and copy it to `mbpoll\package\win\tmp\vcredist_x86.exe`
4. Compile the installer with Innosetup
5. Get the installer in the folder `mbpoll\package\win\installer`

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ If you prefer, you can in the place of direct compilation create a package and i

That's all !

For Windows, you can follow the instructions in the [README-WINDOWS.md](README-WINDOWS.md) file.

## Examples

The following command is used to read the input registers 1 and 2 of the
Expand Down
Loading

0 comments on commit ed00121

Please sign in to comment.