Skip to content

Commit

Permalink
Windows MSVC build working with meson.
Browse files Browse the repository at this point in the history
- Updated guide to windows build
  • Loading branch information
subhacom committed Jul 10, 2024
1 parent e4b7655 commit 52bf161
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 71 deletions.
96 changes: 31 additions & 65 deletions WindowsBuild.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,61 @@
# Building MOOSE on Windows with MSVC

You may want to use one of the virtual environment systems like Anaconda, Miniforge with Mamba, or Micromamba, which will allow you to create isolated Python environments. There are binary packages for most of the requirements in the conda channels (conda-forge). In contrast, pip will actually download the package source code and try to build it locally, which opens up a chain of dependencies on various other libraries.
## Virtual environment
You may want to use one of the virtual environment systems like Anaconda, Miniforge with Mamba, or Micromamba (https://mamba.readthedocs.io/en/latest/), which will allow you to create isolated Python environments. There are binary packages for most of the requirements in the conda channels (conda-forge). In contrast, pip will actually download the package source code and try to build it locally, which opens up a chain of dependencies on various other libraries.

If you want to keep things slim, `micromamba` may be ideal because it is a single statically linked C++ executable and does not install any base environment.

In this guide, `conda` command can be replaced by `mamba` or `micromamba` if you are using one of those systems.

To create an environment, open Anaconda command prompt (below we assume Windows CMD shell, you may need to change some commands for PowerShell) and enter

```
conda create -n moose -c conda-forge
conda create -n moose meson gsl hdf5 cmake numpy matplotlib vpython doxygen pybind11[global] -c conda-forge
```

Then switch to this environment for your build
This will create an environment name `moose`. In some terminals (windows cmd?) you may get an error for `pybind11[global]`. Put it inside quotes to work around it.

Then activate this environment for your build :

```
conda activate moose
```

## Requirements
* Install either MS Visual Studio 2015 or newer or MS Visual Studio Build Tools.
Add path to this folder in your PATH variable
* Install git fow Windows
* Install vcpkg (https://github.com/microsoft/vcpkg)

```
git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
.\vcpkg\vcpkg integrate install
```

* Install cmake
Using conda (mamba)
```
conda install cmake -c conda-forge
```

* Install GSL using ~~vcpkg~~ conda (enter the following in the command line):

```
.\vcpkg\vcpkg install gsl:x64-windows
```
* Install git for Windows
* [Skip] For MPI install MS-MPI (https://github.com/microsoft/Microsoft-MPI/releases/), the only free MPI for Windows
- TODO: MPI-build on Windows is not supported yet
* [Skip] Install doxygen
* Get the environment variable for MS Visual Studio command line tools set up by running

```
conda install gsl -c conda-forge
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
```
* Install HDF5 (for NSDF support)

Do not use the vcpkg version, it seems to miss H5DS function defs.
```
.\vcpkg\vcpkg.exe install hdf5:x64-windows
```
Use the conda-forge version instead:
```
conda install hdf5
Gotcha: if you are on a 64 bit machine, the machine type is `x64`. MSVC comes with cross compilation support for various machine-os combos (x86, x86_64).
* Clone `moose-core` source code using git
* Build moose
```
cd moose-core
* Install pybind11 (https://pybind11.readthedocs.io/en/stable/installing.html)

```
.\vcpkg\vcpkg.exe install pybind11
meson setup --wipe _build --prefix=%CD%\\_build_install -D use_mpi=false --buildtype=release -Ddebug=false
ninja -v -C _build
meson install -C _build
```
* [Skip] For MPI install MS-MPI (https://github.com/microsoft/Microsoft-MPI/releases/), the only free MPI for Windows
- TODO: MPI-build on Windows is not supported yet
* [Skip] Install doxygen

```
.\vcpkg\vcpkg.exe install doxygen:x64-windows
```
This will create `moose` module inside `moose-core/_build_install` directory. To make moose importable from any terminal, add this directory to your `PYTHONPATH` environment variable.

* Install python package requirements (any of these that you don't have already installed).
```
pip install numpy
pip install matplotlib
pip install vpython
```

* Get the environment variable for MS BuildTools set up by running
To build a wheel, you need `build` and `meson-python` modules:

```
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\LaunchDevCmd.bat
conda install meson-python
conda install build
```

Gotcha: if you are on a 64 bit machine, the machine type is x64. MSVC comes with various cross compilation support (x86, x86_64). Running `"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"` worked.
In a terminal, `cd` to `moose-core` and run the following:

* Clone `moose-core` source code using git
* Build moose
```
cd moose-core
pip install .
python -m build
```

* Rename files

* The build process will create everything in the folder `_temp__build`
* The `python` subdirectory contains the moose python module.
* The binary library for moose is created as `_temp__build\python\moose\Release\moose.pyd`
* Move this file to `_temp__build\python\moose\_moose.pyd`
* Add `_temp__build\python` to your `PYTHONPATH` environment variable and you are ready to go.
34 changes: 28 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,40 @@ sublibs = [
utility_lib,
]

py.extension_module('moose', 'pybind11/pymoose.cpp',
link_with: sublibs,
dependencies: [gsl_dep, mpi_dep, pybind_dep, libsoda_dep],
include_directories: ['external/getopt'],
install: true)

moose_dir = py.get_install_dir()

pymoose = py.extension_module('_moose', 'pybind11/pymoose.cpp',
link_with: sublibs,
dependencies: [gsl_dep, mpi_dep, pybind_dep, libsoda_dep],
include_directories: ['external/getopt'],
install: true, install_dir: moose_dir / 'moose')

if is_windows
rename_cmd = 'move'
else
rename_cmd = 'mv'
endif

# custom_target('mooselib',
# depends: pymoose,
# input: pymoose,
# output: '_moose.so',
# install_dir: moose_dir / 'moose',
# command: [rename_cmd, pymoose.full_path(), '@OUTPUT@'],
# build_by_default: true,
# install: true
# )

install_subdir(
'python/moose',
install_dir: moose_dir)

install_subdir(
'python/redesigneur',
install_dir: moose_dir)

# run_target('rename_lib',
# command: [rename_cmd, pymoose.full_path(), moose_dir / '_moose.pyd'],
# depends: pymoose)

message('Finished installing moose in', pymoose.full_path())

0 comments on commit 52bf161

Please sign in to comment.