Develop 4diac FORTE on Visual Studio Code

Develop 4diac FORTE on Visual Studio Code

From Wikipedia:

Visual Studio Code, also commonly referred to as VS Code,[9] is a source-code editor developed by Microsoft for Windows, Linux and macOS.[10] Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git. Users can change the theme, keyboard shortcuts, preferences, and install extensions that add functionality.

Aside from the whole idea of being lightweight and starting quickly, Visual Studio Code has much features that was adapted from Visual Studio technology and available for several different operating system. The code in the Visual Studio Code repository is open source under the MIT License. Although the Community edition of Microsoft Visual Studio is available free of charge, in my opinion Visual Studio Code is a better IDE choice for 4diac FORTE development or any other open source project in general.

We can quickly develop FORTE with standard features in Visual Studio Code but we will take a bit long journey to build FORTE with almost complete features including Modbus, LuaJIT, MQTT, and OPC UA. Let's start it!

Preparing build tools for FORTE

  1. Download and install 64-bit Git for Windows
  2. Download and install 64-bit CMake for Windows
  3. Download and install 64-bit Python for Windows
  4. Download and install 64-bit Perl for Windows
  5. Download and install MSYS2

When MSYS2 is ready for you and a terminal for the UCRT64 environment launched, check MSYS2 update via pacman:

$ pacman -Suy

In some cases, certain core packages will get updated and pacman will prompt you to close all terminals:

:: To complete this update all MSYS2 processes including this terminal will be closed.
   Confirm to proceed [Y/n]

After confirming you need to start a new terminal and run the update again (pacman -Suy) to update the remaining non-core packages.

  1. Install MinGW-w64 gcc, make, and autotools from inside MSYS2 terminal:
$ pacman -S mingw-w64-ucrt-x86_64-gcc
$ pacman -S mingw-w64-ucrt-x86_64-make
$ pacman -S mingw-w64-ucrt-x86_64-autotools
$ ln -sf /ucrt64/bin/mingw32-make.exe /ucrt64/bin/make.exe

GNU autotools with automake, autoconf, and libtool inside needed by libmodbus build from source.

  1. Update MSYS2 PATH variable to tell MSYS2 where to find native Windows utilities (Git, CMake, Python):
$ echo "PATH=\$PATH:/c/Program\ Files/Git/bin:/c/Program\ Files/CMake/bin:/c/Program\ Files/Python312:/c/Program\ Files/Python312/Scripts" >> ~/.bash_profile
$ exit

Preparing build tools for FORTE build on Windows finish. Yes, actually we can build FORTE completely on MSYS2 terminal without a nice IDE.

  1. Build FORTE library dependencies on MSYS2 terminal

Start MSYS2 terminal again to compile two library dependencies for FORTE that must be compiled in UNIX like terminal on Windows. Follow these steps to build libmodbus:

Update: Run autoreconf-2.69 before running ./autogen.sh to force libmodbus to use autoconf version 2.69
$ mkdir -p /c/Users/$(whoami)/vscode/forte/sources
$ mkdir -p /c/Users/$(whoami)/vscode/forte/install/x86_w64
$ cd /c/Users/$(whoami)/vscode/forte/sources
$ git clone https://github.com/stephane/libmodbus.git --depth=1
$ cd libmodbus
$ autoreconf-2.69
$ ./autogen.sh
$ ./configure --with-pic --enable-static --enable-shared=no --disable-tests --prefix=/c/Users/$(whoami)/vscode/forte/install/x86_w64
$ make -j4
$ make install
$ make clean

And follow these steps to build LuaJIT:

$ cd /c/Users/$(whoami)/vscode/forte/sources
$ git clone https://github.com/LuaJIT/LuaJIT.git --depth=1
$ cd LuaJIT
$ make -j4 CFLAGS=-DLUAJIT_ENABLE_LUA52COMPAT TARGET_SYS=Windows
$ make install PREFIX=/c/Users/$(whoami)/vscode/forte/install/x86_w64
$ rm /c/Users/$(whoami)/vscode/forte/install/x86_w64/bin/luajit*
$ rm /c/Users/$(whoami)/vscode/forte/install/x86_w64/lib/libluajit*
$ cp src/luajit.exe /c/Users/$(whoami)/vscode/forte/install/x86_w64/bin
$ cp src/lua51.dll /c/Users/$(whoami)/vscode/forte/install/x86_w64/bin
$ make clean

Clone the rest FORTE library dependencies, we will build it later on Visual Studio Code:

$ cd /c/Users/$(whoami)/vscode/forte/sources
$ git clone https://github.com/Mbed-TLS/mbedtls.git --depth=1
$ cd mbedtls
$ python -m pip install --user -r scripts/basic.requirements.txt
$ git submodule update --init --depth 1
$ cd ../
$ git clone https://github.com/eclipse/paho.mqtt.c.git --depth=1
$ git clone https://github.com/open62541/open62541.git -b pack/v1.3.9 --depth=1
$ cd open62541
$ git submodule update --init --recursive --depth 1
$ cd

Preparing Visual Studio Code for FORTE

  1. Set Windows PATH variable

Enter "Edit the system environment variables" in the Search bar and click the button "Environment Variables". Change the PATH variable and add the path where your MinGW-w64 has been installed to e.g., C:\msys64\ucrt64\bin. Strawberry Perl includes their own make and cmake version, make sure their environment settings have a lower priority than our MinGW-w64 and the official CMake installation environment settings. Also check Python environment setting according to the previous settings in MSYS2 PATH variable.

  1. Install Visual Studio Code

Download the latest version of Visual Studio Code, follow their installation guide for Windows and read the basic introduction to Visual Studio Code user interface. Install C/C++ Extension Pack from Extensions Side Bar (Ctrl+Shift+X). This extension includes C/C++, C/C++ Themes, and CMake Tools extension, a suitable extension for FORTE development.

  1. Build MBed TLS library on Visual Studio Code
  • Run Visual Studio Code
  • Open Mbed TLS source code folder (Ctrl+K+O), find it depend on your user name e.g., C:\Users\user\vscode\forte\sources\mbedtls
  • CMake Tools will auto detect it as a CMake project and will fail to configure. Click Cancel on the error message dialog and open CMake Tools extension on Side Bar
  • Open View - Command Pallete... (Ctrl+Shift+P), type cmake kit and click CMake: Select a Kit, make sure a compiler from C:\msys64\ucrt64\bin is selected
  • Open Command Pallete, type cmake var and click CMake: Select Variant, select Release
  • Open Command Pallete, type cmake edit and click CMake: Edit CMake Cache. Search and set some entries as follow:
CMAKE_INSTALL_PREFIX:PATH=C:/Users/user/vscode/forte/install/x86_w64
ENABLE_PROGRAMS:BOOL=OFF
ENABLE_TESTING:BOOL=OFF
GEN_FILES:BOOL=ON
  • Open Command Pallete, type cmake conf and click CMake: Configure, CMake configuration should run and be successful
  • Open Command Pallete, type cmake build and select CMake: Build, CMake build should run and be successful
  • To install Mbed TLS to C:/Users/user/vscode/forte/install/x86_w64, run CMake: Install from Command Pallete.

The steps above seem complicated, but if you get used to them, you will find them easy. Explore the CMake Tools GUI further to find shortcuts for all the features we've done.

  1. Build Paho MQTT library on Visual Studio Code
  • Follow previous step to build Mbed TLS and change CMakeCache.txt as follow:
CMAKE_INSTALL_PREFIX:PATH=C:/Users/user/vscode/forte/install/x86_w64
PAHO_BUILD_SHARED:BOOL=FALSE
PAHO_BUILD_STATIC:BOOL=TRUE
PAHO_ENABLE_TESTING:BOOL=OFF
PAHO_HIGH_PERFORMANCE:BOOL=TRUE
  • Run Cmake: Build and then end with CMake: Install
  1. Build open62541 OPC UA library on Visual Studio Code
  • Follow previous step to build Mbed TLS and change CMakeCache.txt as follow:
CMAKE_INSTALL_PREFIX:PATH=C:/Users/user/vscode/forte/install/x86_w64
UA_ENABLE_AMALGAMATION:BOOL=ON
UA_ENABLE_DISCOVERY:BOOL=ON
UA_ENABLE_DISCOVERY_MULTICAST:BOOL=ON
UA_ENABLE_ENCRYPTION:STRING=MBEDTLS
  • Run CMake: Configure, CMake will automatically set:
MBEDCRYPTO_LIBRARY:FILEPATH=C:/Users/user/vscode/forte/install/x86_w64/lib/libmbedcrypto.a
MBEDTLS_INCLUDE_DIRS:PATH=C:/Users/user/vscode/forte/install/x86_w64/include
MBEDTLS_LIBRARY:FILEPATH=C:/Users/user/vscode/forte/install/x86_w64/lib/libmbedtls.a
MBEDX509_LIBRARY:FILEPATH=C:/Users/user/vscode/forte/install/x86_w64/lib/libmbedx509.a
  • Run Cmake: Build and then end with CMake: Install
We already build all FORTE library dependencies and ready to develop it on a lightweight and quick IDE Visual Studio Code with support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git.

Developing FORTE on Visual Studio Code

  1. Open Visual Studio Code
  2. Close open folder or just open a new Window (Ctrl+Shift+N)
  3. Open Command Pallete (Ctrl+Shift+P), type terminal and click on Terminal: Create New Terminal (With Profile) and select Git Bash Run the following commands on VS Code Terminal to clone FORTE develop branch source code and then create a new local branch named as local:
$ cd /c/Users/$(whoami)/vscode/forte/sources
$ git clone https://github.com/eclipse-4diac/4diac-forte.git --depth=100
$ cd 4diac-forte
$ git checkout -b local
Update: All my pull requests to FORTE source code have been accepted, no need to applying them manually anymore.
  1. Open FORTE source code folder 4diac-forte and follow previous step when building Mbed TLS library. On the first configuration error, click Cancel on the error dialog, change CMakeCache.txt as follow:
CMAKE_INSTALL_PREFIX:PATH=C:/Users/user/vscode/forte/install/x86_w64
FORTE_ARCHITECTURE:STRING=Win32
  1. Run CMake configuration again, this time should be successful
  2. Run CMake build to compile FORTE with basic feature without Modbus, LuaJIT, MQTT, and OPC UA.

Configuring CMake for a complex project like FORTE is much easier by using the CMake GUI application. Open it and set source directory to e.g., C:/Users/user/vscode/forte/sources/4diac-forte and build directory to e.g., C:/Users/user/vscode/forte/sources/4diac-forte/build, CMake GUI will open the CMake configuration that we initially created on VS Code. It may be necessary to regenerate Makefile to show related CMake config by clicking the Generate button again after making a change.

FORTE_COM_MODBUS=ON
FORTE_COM_MODBUS_LIB_DIR=C:/Users/user/vscode/forte/install/x86_w64
Modbus Configuration
FORTE_COM_OPC_UA=ON
FORTE_COM_OPC_UA_ENCRYPTION=ON
FORTE_COM_OPC_UA_ENCRYPTION_INCLUDE_DIR=C:/Users/user/vscode/forte/install/x86_w64/include
FORTE_COM_OPC_UA_ENCRYPTION_LIB_DIR=C:/Users/user/vscode/forte/install/x86_w64/lib
FORTE_COM_OPC_UA_ENCRYPTION_MBEDTLS=ON
FORTE_COM_OPC_UA_INCLUDE_DIR=C:/Users/user/vscode/forte/install/x86_w64/include
FORTE_COM_OPC_UA_LIB=libopen62541.a
FORTE_COM_OPC_UA_LIB_DIR=C:/Users/user/vscode/forte/install/x86_w64/lib
FORTE_COM_OPC_UA_MULTICAST=ON
FORTE_COM_OPC_UA_CLIENT_PUB_INTERVAL=1000.0
FORTE_COM_OPC_UA_SERVER_PUB_INTERVAL=1000.0
OPC UA Configuration
FORTE_COM_PAHOMQTT=ON
FORTE_COM_PAHOMQTT_INCLUDE_DIR=C:/Users/user/vscode/forte/install/x86_w64/include
FORTE_COM_PAHOMQTT_LIB=libpaho-mqtt3a-static.a
FORTE_COM_PAHOMQTT_LIB_DIR=C:/Users/user/vscode/forte/install/x86_w64/lib
MQTT Configuration
FORTE_USE_LUATYPES=LuaJIT
LUAJIT_INCLUDE_DIR=C:/Users/user/vscode/forte/install/x86_w64/include/luajit-2.1
LUAJIT_LIBRARY=C:/Users/user/vscode/forte/install/x86_w64/bin/lua51.dll
LuaJIT Configuration
CMAKE_C_STANDARD_LIBRARIES=
CMAKE_CXX_STANDARD_LIBRARIES=-lws2_32 -lcrypt32 -lbcrypt
Libraries Configuration
FORTE_COM_HTTP=ON
FORTE_COM_SER=ON
FORTE_EXTERNAL_MODULES_DIRECTORY=C:/Users/user/vscode/forte/ext_modules
FORTE_MODULE_CONVERT=ON
FORTE_MODULE_IEC61131=ON
FORTE_MODULE_RECONFIGURATION=ON
FORTE_MODULE_RT_Events=ON
FORTE_MODULE_SIGNALPROCESSING=ON
FORTE_MODULE_UTILS=ON
FORTE_IPLayerRecvBufferSize=25000
The rest FORTE Configuration

After Makefile configuration finish without any error, we can back to VS Code to build, install, and learn more about FORTE underlying features. FORTE's weakness is the documentation is incomplete so we need to learn it from source level.

Update: We can create CMake configuration settings as settings.json inside .vscode directory in FORTE source code root directory. With this settings, we can configure and build FORTE completely on VS Code IDE.
{
    "cmake.configureOnOpen": false,
    "cmake.configureSettings": {
      "CMAKE_BUILD_TYPE": "Release",
      "CMAKE_INSTALL_PREFIX": "C:/Users/user/vscode/forte/install/x86_w64",
      "CMAKE_CXX_STANDARD_LIBRARIES": "-lws2_32 -lcrypt32 -lbcrypt",
      "FORTE_ARCHITECTURE": "Win32",
      "FORTE_LOGLEVEL": "LOGINFO",
      "FORTE_COM_ETH": true,
      "FORTE_COM_FBDK": true,
      "FORTE_COM_LOCAL": true,
      "FORTE_COM_RAW":true,
      "FORTE_COM_STRUCT_MEMBER":true,
      "FORTE_COM_SER": true,
      "FORTE_MODULE_CONVERT": true,
      "FORTE_MODULE_IEC61131": true,
      "FORTE_MODULE_RECONFIGURATION": true,
      "FORTE_MODULE_RT_Events": true,
      "FORTE_MODULE_SIGNALPROCESSING": true,
      "FORTE_MODULE_UTILS": true,
      "FORTE_EXTERNAL_MODULES_DIRECTORY": "C:/Users/user/vscode/forte/ext_modules",
      "FORTE_COM_HTTP": true,
      "FORTE_COM_MODBUS": true,
      "FORTE_COM_MODBUS_LIB_DIR": "C:/Users/user/vscode/forte/install/x86_w64",
      "FORTE_COM_PAHOMQTT": true,
      "FORTE_COM_PAHOMQTT_INCLUDE_DIR": "C:/Users/user/vscode/forte/install/x86_w64/include",
      "FORTE_COM_PAHOMQTT_LIB_DIR": "C:/Users/user/vscode/forte/install/x86_w64/lib",
      "FORTE_COM_PAHOMQTT_LIB": "libpaho-mqtt3a-static.a",
      "FORTE_COM_OPC_UA": true,
      "FORTE_COM_OPC_UA_INCLUDE_DIR": "C:/Users/user/vscode/forte/install/x86_w64/include",
      "FORTE_COM_OPC_UA_LIB_DIR": "C:/Users/user/vscode/forte/install/x86_w64/lib",
      "FORTE_COM_OPC_UA_LIB": "libopen62541.a",
      "FORTE_COM_OPC_UA_MULTICAST": true,
      "FORTE_COM_OPC_UA_ENCRYPTION": true,
      "FORTE_COM_OPC_UA_ENCRYPTION_MBEDTLS": true,
      "FORTE_COM_OPC_UA_ENCRYPTION_INCLUDE_DIR": "C:/Users/user/vscode/forte/install/x86_w64/include",
      "FORTE_COM_OPC_UA_ENCRYPTION_LIB_DIR": "C:/Users/user/vscode/forte/install/x86_w64/lib",
      "FORTE_USE_LUATYPES": "LuaJIT",
      "LUAJIT_INCLUDE_DIR": "C:/Users/user/vscode/forte/install/x86_w64/include/luajit-2.1",
      "LUAJIT_LIBRARY": "C:/Users/user/vscode/forte/install/x86_w64/bin/lua51.dll",
      "FORTE_IPLayerRecvBufferSize": "25000"
    },
    "C_Cpp.default.includePath": [
      "${workspaceFolder}/**",
      "C:/Users/user/vscode/forte/install/x86_w64/include",
      "C:/Users/user/vscode/forte/install/x86_w64/include/luajit-2.1",
      "C:/Users/user/vscode/forte/install/x86_w64/include/mbedtls",
      "C:/Users/user/vscode/forte/install/x86_w64/include/modbus"
    ]
}
.vscode/settings.json

Visual Studio Code IntelliSense:

IntelliSense is a general term for various code editing features including: code completion, parameter info, quick info, and member lists. IntelliSense features are sometimes called by other names such as "code completion", "content assist", and "code hinting."

VS Code also supports remote programming to WSL. In the case of FORTE, I needed to build FORTE using musl libc for a bit outdated device but a compiler with musl libc was not available on Windows.

I also use VS Code to develop Arduino applications via PlatformIO extension and with VS Code features I can contribute more to a multiplatform C# project that I use at daily basis for years.

Maybe I will share FORTE libraries dependencies so you don't need to build all the dependencies from source code and focus directly to VS Code and FORTE.