Skip to content

Latest commit

 

History

History
239 lines (181 loc) · 11.6 KB

File metadata and controls

239 lines (181 loc) · 11.6 KB

Build and Install on Windows 11 (LLVM flang MSVC-like)

For this tutorial, we are going to use Windows 11 mostly in a command prompt-only fashion, compiling the library with LLVM flang (MSVC-like). LLVM flang is obtained from conda-forge with Miniforge.

Note

At the time of writing this guide, recent versions of LLVM flang (MSVC-like) requires Visual Studio 2022 or 2019 native build tools to work nicely.

Table of Contents

Requirements

  1. Have the skills needed to adapt the commands contained here for Windows 10, if it applies;
  2. Command prompt running as administrator opened;
  3. Internet connection;
  4. Tools:
    • Microsoft Visual C/C++ (MSVC) build tools for native C/C++ x86/x64 development (latest) + Windows SDK (suitable for your target platform);
    • Miniforge;
    • LLVM flang (MSVC-like) from conda-forge.
    • git;
    • Ninja (≥ 1.10);
    • cmake.

Pre-installation steps

According to Microsoft, the recommended way to install winget (a package manager for Windows maintained by Microsoft) goes through the Microsoft Store, distributed within the App Installer package. So, go ahead and install it.

Note

On my personal experience using a fresh Windows 11 installation for this guide, if you download and install all the Windows updates and restart your computer the many times required, winget will get installed in the process.

You can check that winget got properly installed if the search command

winget search "visual studio"

works well on cmd once you answer Y when asked. You would receive an output like this:

Screenshot from 2024-05-05 10-54-50

Install the usual tools and prepare to install LLVM flang

Tip

You might skip the steps for the tools that you already have installed on your computer.

  1. Close any Visual Studio instances opened on your machine;

  2. Open a command prompt running as administrator to avoid permission issues when installing the tools required here;

    Screenshot from 2024-05-05 12-27-04

  3. Make sure to have installed both MSVC Build Tools 2022 for native C/C++ x86/x64 development, and a Windows SDK:

    • If you don't have Visual Studio 2022 installed and just want MSVC native C/C++ compilers for x86/x64, install them with the command below depending on the Windows version that you are targeting
      • Windows 10
        winget install --id Microsoft.VisualStudio.2022.BuildTools --source winget --silent --override "--quiet --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows10SDK.18362"
      • Windows 11
        winget install --id Microsoft.VisualStudio.2022.BuildTools --source winget --silent --override "--quiet --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows11SDK.22000"
    • Otherwise, assuming that you already have a Visual Studio 2022 instance previously installed, you can modify it to include native C/C++ x86/x64 development + Windows SDK depending on the Windows version that you are targeting:
      • Windows 10
        if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -property properties.setupEngineFilePath`) do ( for /f "usebackq tokens=*" %j in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -property installationPath`) do ( "%comspec%" /C ""%i" modify --quiet --installPath "%j" --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows10SDK.18362" ) ) ) else ( echo "Unable to find vswhere.exe" )
      • Windows 11
        if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -property properties.setupEngineFilePath`) do ( for /f "usebackq tokens=*" %j in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -property installationPath`) do ( "%comspec%" /C ""%i" modify --quiet --installPath "%j" --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 --add Microsoft.VisualStudio.Component.Windows11SDK.22000" ) ) ) else ( echo "Unable to find vswhere.exe" )
  4. (Optional if you have Miniforge3) Install Miniforge

    winget install --id CondaForge.Miniforge3 --source winget --accept-package-agreements --accept-source-agreements --silent
  5. (Optional if you have git) Install git

    winget install --id Git.Git --source winget --accept-package-agreements --accept-source-agreements --silent
  6. (Optional if you have Ninja ≥ 1.10) Install Ninja

    winget install --id Ninja-build.Ninja --source winget --accept-package-agreements --accept-source-agreements --silent
  7. (Optional if you have a recent CMake - tested on CMake 3.29.3) Install CMake

    winget install --id Kitware.CMake --source winget --accept-package-agreements --accept-source-agreements --silent

Important

You must close the command prompt, because the installed tools are not on your system environment PATH variable until you launch a new fresh command prompt.

Install LLVM flang (MSVC-like) with Miniforge

  1. Launch a vanilla (admin rights not needed) new fresh x64 Native Tools Command Prompt for VS 2022 to build 64-bit flang.

    Screenshot from 2024-05-05 17-31-44

  2. Activate Miniforge

    %userprofile%\miniforge3\Scripts\activate.bat %userprofile%\miniforge3

Warning

%userprofile% is a system environment variable on Windows that points to C:\Users\<you user name>. If you performed a standard Miniforge install, it gets installed at %userprofile%\miniforge3. However, in case you installed Miniforge elsewhere, adjust the path above replacing %userprofile%\miniforge3 by the installation directory.

  1. Check that conda is available:

    conda --version
  2. Create an environment for minpack-builder installing LLVM flang (MSVC-like) from conda-forge:

    conda create --yes -c conda-forge -n minpack-builder flang_win-64 flang-rt_win-64
    01-install-flang-steps-1-to-4
  3. Activate the minpack-builder environment created in the latest step

    conda activate minpack-builder
  4. Check that LLVM flang (MSVC-like) is available and print its version

    flang --version
    02-install-flang-steps-5-to-6

Important

If the test above worked fined, close the command prompt.

Build and install minpack

  1. Launch a vanilla (admin rights not needed) new fresh x64 Native Tools Command Prompt for VS 2022 to build 64-bit minpack.

    Screenshot from 2024-05-05 17-31-44

  2. Activate Miniforge

    %userprofile%\miniforge3\Scripts\activate.bat %userprofile%\miniforge3
  3. Activate minpack-builder environment created earlier

    conda activate minpack-builder
  4. Check that all the required tools are available:

    cl
    flang --version
    git --version
    cmake --version
    ninja --version
    03-build-minpack-steps-1-to-4
  5. Set a variable to hold the path to a working directory, create that directory, and then change dir to this new directory

    set working_dir=%homedrive%\minpack-builder-flang-guide
    mkdir %working_dir%
    cd %working_dir%
  6. Set variables for the build and install directories

    set BUILD_DIR=%working_dir%\build-flang
    set INSTALL_DIR=%working_dir%\local-install-flang

Note

If your %homedrive% variable points to C:, we are going for a local installation at C:\minpack-builder-flang-guide\local-install-flang, building the package at C:\minpack-builder-flang-guide\build-flang.

  1. Set a variable for the build tools to treat flang as the Fortran Compiler

    set FC=flang
  2. Clone minpack-builder by running

    git clone https://github.com/luau-project/minpack-builder
    04-build-minpack-steps-5-to-8
  3. By default, minpack-builder downloads (optional param USE_DOWNLOAD is ON) minpack source code to the same directory of minpack-builder (provided by optional param DOWNLOAD_DIR).

    cmake -G Ninja --install-prefix "%INSTALL_DIR%" -S minpack-builder -B "%BUILD_DIR%"
    

Important

In my personal tests, the only generator that worked properly was Ninja, which needs to be version ≥ 1.10 to build Fortran projects. Both Visual Studio or NMake generators failed in the configuration step.

Note

If the optional param CMAKE_BUILD_TYPE is not specified, a Release build is configured.

  1. Review the configuration summary and proceed to build

    05-build-minpack-steps-9-to-10
  2. Build the library.

    cmake --build "%BUILD_DIR%"
    

Note

By default, only the shared library is built. In my personal tests, if you build the static library by feeding -DBUILD_STATIC_LIBS=ON in the configuration step, the generated static library minpack.lib will ask you where Clang and flang libraries are stored once you use it on other projects. Since this topic of locating Clang and flang libraries goes too far from the scope presented here, prefer to build only the shared library.

Important

Unlike Linux, on Windows you cannot build both shared and static libraries in the same step, because the library name collides (minpack.lib). Then, you can only build one version at a time. Be aware that if you install both in the same directory, the latter will map to minpack.lib.

  1. Install the library

    cmake --install "%BUILD_DIR%"
    
    06-install-minpack-step-12

Documentation