Building GridLAB-D on Windows with MSYS2

= Installing MSYS2 = The MSYS2 environment is used to build GridLAB-D 4.1 or newer for the Windows OS. It can be downloaded from http://www.msys2.org. From the website download the appropriate MSYS2 installer for 32bit (i686) and 64bit (x86_64) OS’s and install MSYS2 on your machine.

Setting Up the MSYS2 Environment
To start the MSYS2 environment start msys2.exe (or use the "MSYS2 MinGW x64" link in the Program menu). As can be seen MSYS2 provides a Linux-like command terminal and environment for building GNU compliant C/C++ executables for Windows OS’s. When Running the MSYS2 environment for the first time updates will need to be performed. To perform update run pacman -Syuu Type "y" and hit enter to continue. if you get a line like the following :: To complete this update all MSYS2 processes including this terminal will be closed. Confirm to proceed [Y/n] Type "y" and hit enter. The MSYS2 window will close. Reopen MSYS2 (ideally with the "MSYS2 MinGW x64" shortcut). Then to continue the updates run pacman -Su Type "y" and hit enter. You shouldn't get the warning messages from before this time.

In order to build GridLAB-D There are a couple MSYS2 packages that need to be installed. They are as follows: In order to install these packages type the following: pacman -S --needed autoconf automake libtool base-devel mingw-w64-i686-gcc git mingw-w64-i686-dlfcn mingw-w64-i686-xerces-c pacman -S --needed autoconf automake libtool base-devel mingw-w64-x86_64-gcc git mingw-w64-x86_64-dlfcn mingw-w64-x86_64-xerces-c Important note!!!!! After doing this /mingw64/bin or /mingw32/bin needs to be added to the path variable. In fact it would be a good idea to build a shell script so that /mingw64/bin or /mingw32/bin is added to PATH when MSYS2 is started. The command to add it to the path (per session) is along the lines of (for 64-bit): export PATH=$PATH:/mingw64/bin Alternatively, run the "MSYS2 MinGW 64-bit" shortcut in the Programs menu.
 * autoconf: This package provides one part of the necessary autotools programs
 * automake: This package provides another part of the necessary autotools programs
 * libtool: This package provides the necessary libtool program needed to compile GridLAB-D
 * base-devel: This package provide the necessary make program needed to compile GridLAB-D
 * mingw-w64-i686-gcc (32 bit machines): This package provides gcc compiler for compiling C/C++ code.
 * mingw-w64-x86_64-gcc(64 bit machines): This package provides gcc compiler for compiling C/C++ code.
 * git: This is required so the GridLAB-D GitHub repository can be checked out.
 * mingw-w64-i686-xerces-c (32 bit machines): This package provides xerces-c, a prerequisite for compiling GridLAB-D.
 * mingw-w64-x86_64-xerces-c (64 bit machines): This package provides xerces-c, a prerequisite for compiling GridLAB-D.
 * mingw-w64-i686-dlfcn (32 bit machines): This package provides libdl.dll and dlfcn.h, prerequisites for compiling GridLAB-D.
 * mingw-w64-x86_64-dlfcn (64 bit machines): This package provides libdl.dll and dlfcn.h, prerequisites for compiling GridLAB-D.
 * 32 bit machines:
 * 64 bit machines:

= Building GridLAB-D from Source = Now That MSYS2 has been set up and all needed packages have been installed the installation process for GridLAB-D can start. Start by cloning the GridLAB-D git repository from https://github.com/gridlab-d/gridlab-d. You can do this with the command: git clone https://github.com/gridlab-d/gridlab-d.git NAMEVALUE where NAMEVALUE is the folder in the current directory you'd like to download the code. e.g., if you are in C:\Code, this will clone GridLAB-D into C:\Code\NAMEVALUE. After the code downloads, change to that folder via: cd NAMEVALUE Now checkout the correct branch, usually develop or the latest release ( in this case): git checkout release/v4.3 Installing GridLAB-D: Begin with the following. autoreconf -if ./configure --prefix=$PWD/install32 --with-xerces=/mingw32/lib --enable-silent-rules 'CFLAGS=-O2 -w' 'CXXFLAGS=-O2 -w' 'LDFLAGS=-O2 -w -L/mingw32/bin' make make install ./configure --prefix=$PWD/install64 --with-xerces=/mingw64/lib --enable-silent-rules 'CFLAGS=-O2 -w' 'CXXFLAGS=-O2 -w' 'LDFLAGS=-O2 -w -L/mingw64/bin' make make install
 * 32 bit machines:
 * 64 bit machines:

This will "install" GridLAB-D into the install64 folder of the current directory (e.g., C:\Code\NAMEVALUE\install64 using the above examples).

Building Optional Features
There are several optional features that can be built with GridLAB-D. Those are the FNCS library, HELICS library, MySQL module, and MATLAB link.

Building with the FNCS Library
In order to build GridLAB-D with the FNCS library. FNCS must be installed on your machine. For instructions on building FNCS please see https://github.com/FNCS/fncs. Once FNCS has been installed, the path to the FNCS library and its third party libraries ZMQ and CZMQ must be added to the PATH in MSYS2. To build GridLAB-D with FNCS add the following option to the configure lines in steps 2 or 3 above: For more information on the FNCS library functionality within GridLAB-D see fncs_msg.
 * --with-fncs=

Building with the HELICS Library
In order to build GridLAB-D with the FNCS library. HELICS must be installed on your machine. For instructions on building HELICS please see https://github.com/GMLC-TDC/HELICS-src. Once HELICS has been installed, the path to the HELICS library and its third party libraries ZMQ must be added to the PATH in MSYS2. To build GridLAB-D with HELICS add the following option to the configure lines in steps 2 or 3 above: For more information on the HELICS library functionality with GridLAB-D see helics_msg.
 * --with-helics=

Building with the MATLAB Link
In order to build the MATLAB link inside of GridLAB-D a version of MATLAB must be installed on the system. Add the following option to the configure line in steps 2 or 3 above: It is important to note that the MATLAB installation path contain no spaces. If there are spaces in the paths you can find the short name windows uses for the directory by opening up a command window and typing dir /X one directory above the directory with the spaces. An example of what --with-matlab might look like is below. Attention: before running a simulation with MATLAB, the environment variable %PATH% must contain the path to the MATLAB DLLs, e.g., MATLAB_DIR\bin\win64 or MATLAB_DIR\bin\win32. For more information on the MATLAB functionality within GridLAB-D see MATLAB link.
 * --with-matlab=
 * Install path on the windows explorer: C:\Program Files\MATLAB\R2017A
 * Option set with the short names: --with-matlab=c:/PROGRA~1/MATLAB/R2017A

NOTE: with MATLAB r2018a and newer, there has been a slight change to external interfaces for complex numbers. You need to tell GridLAB-D to use the older/legacy interface via the compiler definition "MATLAB_DEFAULT_RELEASE=R2017b". An example configuration command would be:

./configure --prefix=$PWD/install64 --with-xerces=/mingw64/lib --with-matlab=C:/PROGRA~1/MATLAB/R2018a --enable-silent-rules 'CFLAGS=-O2 -w -DMATLAB_DEFAULT_RELEASE=R2017b' 'CXXFLAGS=-O2 -w -DMATLAB_DEFAULT_RELEASE=R2017b' 'LDFLAGS=-O2 -w'

Building the MySQL Module
In order to build GridLAB-D with MySQL set the following option to the configure line in step 2 or 3 above: It is important that no space be contained in the paths. If there are spaces in the paths you can find the short name windows uses for the directory by opening up a command window and typing dir /X one directory above the directory with the spaces. An example of what --with-mysql might look like is below. Attention: Before running a simulation with MySQL, %PATH% must contain the path location to the MySQL DLLs, MYSQL_DIR\lib. For more information on the MySQL modules please see the main MySQL module page.
 * --with-mysql=
 * Install path on the windows explorer: C:\Program Files\MySQL\MySQL Connector C
 * Option set with short names: --with-mysql=c:/PROGRA~1/MySQL/MYQLC~1

= Running GridLAB-D from Compiled Source Code = After building GridLAB-D from the source code using MSYS2, you may want to run models from the command line. For this to work, you need to set up the command window environment.

Note that in all instances below, the INSTALL item should represent the install32 or install64 directory you set up in the configure step of the GridLAB-D build. You may also have to add the path to libdl.dll and libstrdc++-6.dll - these are located in the c:\msys64\mingw64\bin and c:\msys64\mingw64\mingw folders. If they are needed on your system, append them to the PATH variable below.

The following environment variable need to be set up:
 * PATH must contain REPOSITORY_DIR/INSTALL/bin
 * GLPATH must contain REPOSITORY_DIR/INSTALL/lib/gridlabd and REPOSITORY_DIR/INSTALL/share/gridlabd
 * CXXFLAGS must be set to include REPOSITORY_DIR/INSTALL/share/gridlabd

e.g., an example for a 64-bit install in C:\GLDCompile could be done with the following commands (note these are written like Windows/MS-DOS batch file-style commands -- for MSYS, convert the slashes to forward slashes):

REM Append path set PATH=%PATH%;C:\GLDCompile\install64\bin REM Set GLPATH set GLPATH=C:\GLDCompile\install64\lib\gridlabd;C:\GLDCompile\install64\share\gridlabd REM Set the compiler flags -- note this should be forward-slash-oriented set CXXFLAGS=-IC:/GLDCompile/install64/share/gridlabd