OpenFAST: compiling from source, but cmake fails (Matlab_MEX_EXTENSION Matlab_ROOT_DIR not found)

Hello everybody,
so far I have used OpenFAST through an anaconda environment, but now I need to compile it from source on a Ubuntu 20.04 workstation, and am following the instruction on readthedocs (sections 2.2.1, 2.2.2, 2.2.4, 2.2.5).
I have customized the cmake command a first time as follow:

cmake .. -DBUILD_DOCUMENTATION=ON -DBUILD_FASTFARM=ON -DBUILD_OPENFAST_SIMULINK_API=ON -DBUILD_TESTING=ON -DOPENMP=ON

and when I run it from the /build directory I have the following CLI output:

- The CXX compiler identification is GNU 9.4.0
-- The C compiler identification is GNU 9.4.0
-- The Fortran compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working Fortran compiler: /usr/bin/gfortran
-- Check for working Fortran compiler: /usr/bin/gfortran  -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /usr/bin/gfortran supports Fortran 90
-- Checking whether /usr/bin/gfortran supports Fortran 90 -- yes
-- Performing Test HAS_FORTRAN2008
-- Performing Test HAS_FORTRAN2008 - Success
-- Enabling Fortran 2008 features
-- Looking for Fortran sgemm
-- Looking for Fortran sgemm - not found
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Looking for Fortran dgemm
-- Looking for Fortran dgemm - found
-- Found BLAS: /usr/lib/x86_64-linux-gnu/libf77blas.so;/usr/lib/x86_64-linux-gnu/libatlas.so  
-- Looking for Fortran cheev
-- Looking for Fortran cheev - not found
-- Looking for Fortran cheev
-- Looking for Fortran cheev - found
-- A library with LAPACK API found.
-- Setting system file as: src/SysMatlabLinuxGnu.f90
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find Matlab (missing: Matlab_INCLUDE_DIRS Matlab_MEX_LIBRARY
  Matlab_MEX_EXTENSION Matlab_ROOT_DIR) (found version "NOTFOUND")
Call Stack (most recent call first):
  /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindMatlab.cmake:1693 (find_package_handle_standard_args)
  modules/nwtc-library/CMakeLists.txt:127 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/reius/OpenFASTcompiled/OpenFASTcompld_original/OpenFAST/build/CMakeFiles/CMakeOutput.log".
See also "/home/reius/OpenFASTcompiled/OpenFASTcompld_original/OpenFAST/build/CMakeFiles/CMakeError.log".

after which, if I proceed with the next command make I get the error

make: *** No targets specified and no makefile found.  Stop.

then, thinking that the matlab-related problem came from the simulink option, I tried cmake again without the option relative to simulink:

cmake .. -DBUILD_DOCUMENTATION=ON -DBUILD_FASTFARM=ON -DBUILD_TESTING=ON -DOPENMP=ON

and the output has been:

-- Enabling Fortran 2008 features
-- A library with LAPACK API found.
-- Setting system file as: src/SysMatlabLinuxGnu.f90
CMake Error at /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:146 (message):
  Could NOT find Matlab (missing: Matlab_INCLUDE_DIRS Matlab_MEX_LIBRARY
  Matlab_MEX_EXTENSION Matlab_ROOT_DIR) (found version "NOTFOUND")
Call Stack (most recent call first):
  /usr/share/cmake-3.16/Modules/FindPackageHandleStandardArgs.cmake:393 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindMatlab.cmake:1693 (find_package_handle_standard_args)
  modules/nwtc-library/CMakeLists.txt:127 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/reius/OpenFASTcompiled/OpenFASTcompld_original/OpenFAST/build/CMakeFiles/CMakeOutput.log".
See also "/home/reius/OpenFASTcompiled/OpenFASTcompld_original/OpenFAST/build/CMakeFiles/CMakeError.log".

and the attempt to run make resulted in the same output as after the cmake command with the simulink option on.
I read CMakeOutput.log and CMakeError.log, but found nothing explicit about matlab…
I do have Matlab installed in this Ubuntu workstation.
Can someone help me solving this error and proceed with the compiling?

Thank you for your time and attention,
Henny

Hi Henny,

After you initialize the build directory by running CMake the first time, a file called CMakeCache.txt is created. Any time you run the CMake command again, you are simply updating that file, not creating a new one. So in your case, you initialized the file with DBUILD_OPENFAST_SIMULINK_API enabled, but simply removing that flag doesn’t disable it. The second command should have had this flag: DBUILD_OPENFAST_SIMULINK_API=OFF. To start a CMake configuration over from scratch, you should delete the whole build directory or delete the CMakeCache.txt file.

For more info, this is described in the OpenFAST documentation here. The CMake documentation includes a more detailed description here.

As for compiling with Matlab, this is trickier and the OpenFAST project has incomplete support for this. However, on Ubuntu with a typical Matlab installation you should be able to configure your environment to do it. Read through the documentation at the beginning of the FindMatlab CMake file and set the variable highlighted there: cmake .. -D<variable>=/path/to/lib.

thank you Rafael for your fast reply.

set the variable highlighted there: cmake .. -D<variable>=/path/to/lib .

did not work. I tried it after deleting the CMakeCache.txt file and again after deleting the whole build directory and creating it anew, but the error was always the same as in my first post.
Finally I completed the cmake step and all the installation process without the option for simulink with the following command:

cmake ..  -DMatlab_ROOT_DIR=/home/reius/MATLAB_R2022a -DBUILD_DOCUMENTATION=OFF -DBUILD_FASTFARM=ON -DBUILD_OPENFAST_SIMULINK_API=OFF -DBUILD_TESTING=OFF -DOPENMP=ON

Perhaps, my problem was that my matlab installation was not typical, as I have matlab installed in a folder in my home directory.

Thank you again for your help,
Henny