HOW-TO: Using the new build system
==================================

Here is a summary of the process:

    Install the required prerequisites. Depending on your platform this might be as easy as install system packages or can be more elaborated.
    Install casacore and libsakura
    Install CASA C++ (aka casacpp).

In the instructions below the variable $CASAINSTALL has been chosen as a placeholder for the directory where the compiled software will be installed. Define the variable or replace it accordingly.

Here follows a detailed explanation of these steps:

Install the required prerequisites
----------------------------------

The following packages are needed to install casa, casacore and libsakura:

    cmake
    C++ compiler (in the case of gcc at least version 4.9 and optionally support for OpenMP)
    Fortran compiler
    OpenMPI (optional)
    readline library
    ncurses library
    blas library
    lapack library
    fftw3 library
    wcslib library
    libxml++ library
    libxslt library
    gsl library
    sqlite library
    protobuf library + protobuf compiler
    gRPC + gRPC protobuf plugin
    python3
    numpy
    flex command
    bison command
    pkg-config command
    curl command
    tar command

Requirements for GPU build (development still in progress):

    C++ compiler (gcc 9+)
    CUDA
    cuFFT
    ATLAS
    Kokkos (libkokkosscore)

To install these dependencies one can either install them manually using any native method (which might not be trivial) or use package managers. The later is way easier and we have collected the instructions for the following operating systems:

    RHEL equivalent (tested on RockyLinux 8)
    Fedora (tested on 36)
    Ubuntu (tested on 22.04)
    Debian (tested on 11)
    macOS (tested on 11 and 12)

What follows is the detailed instructions to install the prerequisites for all these platforms.

Installing prerequisites in RHEL equivalent  (tested only on RockyLinux 8)
--------------------------------------------------------------------------

Run as root or as a used with sudo rights the following commands:
# Packages needed for casacore development
$ sudo dnf -y install git cmake gcc-c++ gcc-gfortran gtest-devel ccache readline-devel ncurses-devel blas-devel lapack-devel cfitsio-devel fftw-devel wcslib-devel python3 python3-devel python3-numpy flex bison tar curl
 
# Packages needed for libsakura development
$ sudo dnf -y install eigen3-devel fftw-devel
 
# Additional packages needed for casa development
dnf -y install install java-1.?.0-openjdk-devel python3-cerberus python3-wheel python3-numpy swig pkgconf-pkg-config xerces-c-devel libxml++-devel libxslt-devel gsl-devel sqlite-devel wcslib-devel openmpi-devel
 
$ sudo alternatives --set python /usr/bin/python3
 
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/protobuf-3.6.1-3.el8.x86_64.rpm
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/protobuf-compiler-3.6.1-3.el8.x86_64.rpm
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/protobuf-devel-3.6.1-3.el8.x86_64.rpm
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/grpc-1.18.0-2.el8.x86_64.rpm
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/grpc-devel-1.18.0-2.el8.x86_64.rpm
$ dnf -y install https://ftp.eso.org/pub/dfs/pipelines/libraries/grpc/grpc-plugins-1.18.0-2.el8.x86_64.rpm

Installing prerequisites in Fedora (tested only in Fedora 36)
-------------------------------------------------------------

Run as root or as a used with sudo rights the following commands:
# Packages needed for casacore development
$ sudo dnf -y install git cmake gcc-c++ gcc-gfortran gtest-devel ccache readline-devel ncurses-devel blas-devel lapack-devel cfitsio-devel fftw-devel wcslib-devel python3 python3-devel python3-numpy python-unversioned-command flex bison tar curl
 
# Packages needed for libsakura development
$ sudo dnf -y install eigen3-devel fftw-devel
 
# Additional packages needed for casa development
$ sudo dnf -y install java-*-openjdk-devel python3-cerberus python3-wheel python3-build python3-numpy swig pkgconf-pkg-config xerces-c-devel libxml++-devel libxslt-devel gsl-devel sqlite-devel protobuf-compiler grpc grpc-devel grpc-plugins wcslib-devel openmpi-devel

Installing prerequisites in Ubuntu  (tested only in Ubuntu 22.04)
-----------------------------------------------------------------

Run as root or as a used with sudo rights the following commands:
# Packages needed for casacore development
$ sudo apt -y install git g++ cmake gfortran libreadline-dev libncurses5-dev libblas-dev liblapack-dev libfftw3-dev libcfitsio-dev wcslib-dev python3-numpy python-is-python3 flex bison tar curl
 
# Packages needed for libsakura development
$ sudo apt -y install libfftw3-dev libeigen3-dev
 
# Additional packages needed for CASA development
$ sudo apt -y install default-jdk python3-cerberus python3-wheel python3-venv python3-build python3-numpy swig ccache pkg-config libxerces-c-dev libxml++2.6-dev libxslt1-dev libgsl-dev libsqlite3-dev protobuf-compiler-grpc libgrpc-dev grpc-proto libgrpc++-dev wcslib-dev libopenmpi-dev openmpi-bin

Installing prerequisites in Debian  (tested only in Debian 11)
--------------------------------------------------------------

Run as root or as a used with sudo rights the following commands:
# Packages needed for casacore development
$ sudo apt -y install git g++ cmake gfortran libreadline-dev libncurses5-dev libblas-dev liblapack-dev libfftw3-dev libcfitsio-dev wcslib-dev python3-numpy python-is-python3 flex bison tar curl
 
# Packages needed for libsakura development
$ sudo apt -y install libfftw3-dev libeigen3-dev
 
# Additional packages needed for CASA development
$ sudo apt -y install default-jdk python3-cerberus python3-wheel python3-venv python3-build python3-numpy swig ccache pkg-config libxerces-c-dev libxml++2.6-dev libxslt1-dev libgsl-dev libsqlite3-dev protobuf-compiler-grpc libgrpc-dev grpc-proto libgrpc++-dev wcslib-dev libopenmpi-dev openmpi-bin

Installing prerequisites in macOS using macports (tested only on 11 and 12 Intel)
---------------------------------------------------------------------------------

As a prerequisite XCode must be installed, as well as macports (https://macports.org). It could be that the prerequisistes could be installed from Homebrew, but no attempt has been to try it out.

The XCode installation already fulfills the requirement of the compiler (point 2. in the list of requirements).

When using any of the below commands, make sure that macports is in the PATH. That's part of the macports installation instructions so it could be that the PATH is already correct:
export PATH=/opt/local/bin:$PATH

Run as root or as a used with sudo rights the following commands:
# Packages needed for casacore development
$ sudo port install git cmake gcc11 +gfortran cfitsio wcslib
 
# Packages needed for libsakura development
$ sudo port install fftw-3 fftw-3-single eigen3
 
# Additional packages needed for CASA development
$ sudo port install ccache pkgconfig protobuf3-cpp grpc gsl libxslt openmpi-clang libxmlxx2 fftw-3 fftw-3-single

(note that we exclude boost related packages that are optional in the casacore build instructions: libboost-dev, libboost-python-dev. We do not need those as CASA does not use the python bindings from casacore. In addition, if one wanted to compile all the boost related functionality of casacore (there is additional code that uses boost for Arrays and Dysco tests), the following packages would be needed: libboost-filesystem-dev libboost-test-dev libboost-system-dev).

Install casacore and libsakura
==============================

To ease the instructions, define a variable where al the software will be installed, which changes depending on the shell being used:
$ export CASAINSTALL=/installation/path/  # (bash, zsh, POSIX shell)
$ setenv CASAINSTALL /installation/path/  # (csh, tcsh)


To install libsakura:

    Get the tarball:
    $ curl -L https://github.com/tnakazato/sakura/archive/refs/tags/libsakura-5.1.3.tar.gz | gunzip | tar -xvf -

    Compile and install with cmake (you might change the build directory or the make options).
    $ cd sakura-libsakura*/libsakura
    $ mkdir build
    $ cd build
    $ cmake  \
        -DCMAKE_INSTALL_PREFIX=$CASAINSTALL \
        -DBUILD_DOC:BOOL=OFF \
        -DPYTHON_BINDING:BOOL=OFF \
        -DSIMD_ARCH=GENERIC \
        -DENABLE_TEST:BOOL=OFF \
         ..
    $ make install -j 5


To install casacore:

    Install measures data:
    $ mkdir -p $CASAINSTALL/data
    $ curl ftp://ftp.astron.nl/outgoing/Measures/WSRT_Measures.ztar | tar -C $CASAINSTALL/data -xzf -

    Go to casacore sources inside casatools as usual (it is assumed that the git submodule has been initialized):
    $ cd casatools/casacore
    (macOS only) For the time being until the pointer to casacore is not changed in casa ( CAS-13926 - Update casacore pointer to better support pkg-config Verifying ), modify the following line inside casacore/casacore.pc.in. Also, once casacore is built and installed in the next step, edit the installed lib/pkgconfig/casacore.pc to change the "Libs:" line as explained in CAS-13921 - Support the modular build system in macOS Abierta (first comment, temporary workaround for an issue with LAPACK).

    Compile and install with cmake (you might change the build directory or the make options):
    $ mkdir build
    $ cd build
    $ export FC=/opt/local/bin/gfortran-mp-11  # macOS only!
     
    # NOTE! Macs have an extra cmake directive compared to linux, so be certain to use the cmake command below that is valid for your OS
     
    # LINUX ONLY! use the following cmake command
    $ cmake \
        -DCMAKE_INSTALL_PREFIX=$CASAINSTALL \
        -DDATA_DIR=$CASAINSTALL/data \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DUSE_OPENMP=ON \
        -DUSE_THREADS=ON \
        -DBUILD_FFTPACK_DEPRECATED=ON \
        -DBUILD_TESTING=ON \
        -DBUILD_PYTHON3=OFF \
        -DBUILD_DYSCO=OFF \
        -DUseCcache=1 \
        ..
     
    # MAC ONLY! use the following cmake command
    cmake \
        -DCMAKE_INSTALL_PREFIX=$CASAINSTALL \
        -DDATA_DIR=$CASAINSTALL/data \
        -DCMAKE_BUILD_TYPE=RelWithDebInfo \
        -DUSE_OPENMP=ON \
        -DUSE_THREADS=ON \
        -DBUILD_FFTPACK_DEPRECATED=ON \
        -DBUILD_TESTING=ON \
        -DBUILD_PYTHON3=OFF \
        -DBUILD_DYSCO=OFF \
        -DUseCcache=1 \
        -DPRIVATE_LIBS="-framework Accelerate -lm -ldl" \
        ..
     
    $ make install -j 5

    NOTE: here we use a build of type "RelWithDebInfo" (with code optimization and debug line info. Other alternatives include Debug, Release, etc.
    NOTE: The modular system allows to install casacore from other repository which is not the git submodule configured in casatools. This adds flexibility to get a customized casacore if needed. However, it is up to the developer to ensure that the version installed is the one that needs to be used for the subsequent casa C++ build. For instance, one could use a single casacore installation for several casa branches if that's practical or desired.


Install CASA C++ (aka casacpp)
==============================

     Go to the casa code sources:
    $ cd casatools/src/code

    Compile and install with cmake
    $ mkdir build
    $ cd build
    $ export PATH=/usr/lib64/openmpi/bin/:$PATH   # This is not needed in Debian or Ubuntu and is optional in the other platforms if no MPI support at the C++ level is needed
     
    $ PKG_CONFIG_PATH=$CASAINSTALL/lib/pkgconfig cmake \
         -DCMAKE_INSTALL_PREFIX=$CASAINSTALL \
         -DPKG_CONFIG_USE_CMAKE_PREFIX_PATH=$CASAINSTALL \
          ..
     
    $ make install -j 5

Run casacpp unit tests (optional)

    Go to the build directory.
    $ cd casatools/src/code
    $ cd build

    Export the variable CASADATA to point to the contents of the casatestdata repository
    $ export CASADATA=/path/to/casatestdata

    Run the tests
    $ ctest -T test --output-on-failure

Install CASA casatools (WIP)
============================

    Create build directory
    $ cd casatools
    $ mkdir build
    $ cd build

    REQUIRED in Rocky Linux 8 and Ubuntu 22.04! OPTIONAL for other platforms. Create a virtual environment that has the needed packages:
    $ mkdir build_env
    $ python3 -m venv --ssytem-site-packages build_env
    $ . ./build_env/bin/activate
    $ pip install build
    $ pip install --upgrade setuptools
    $ pip install --upgrade wheel

    Create casatools wheel
    $ PKG_CONFIG_PATH=$CASAINSTALL/lib/pkgconfig python3 -m build -n -o $CASAINSTALL/dist ..
    The output wheel will be created inside the $CASAINSTALL/dist directory

Test casatools (WIP)
====================

    Create a virtual environment for testing purposes. It is recommended, although not strictly neccesary. Otherwise casatools will be installed in $HOME and be available to all python sesions.
    $ python3 -m venv --system-site-packages test_env

    Install the casatools wheel
    $ pip install $CASAINSTALL/dist/*whl
    $ pip install casatestutils

    Apply a number of workarounds needed for the time being (typecheck.py can be found here). These ones depend on CAS-13945, CAS-13948, CAS-13949 being resolved.
    # WORKAROUND 1
    # casac_dir=`find test_env -name __casac__`
    # cp -f typecheck.py $casac_dir/../
     
    # WORKAROUND 2
    $ sed -i 's/from \\.__casac__ import \\(\\w\\w*\\)/from \\.__casac__\\.\\1 import \\1/g'  test_env/lib64/python*/site-packages/casatools/*.py

    Run the tests:
    python casatools/tests/tools/agentflagger/test_tool_agentflagger.py 

