# Copyright Contributors to the Open Shading Language project.
# SPDX-License-Identifier: BSD-3-Clause
# https://github.com/AcademySoftwareFoundation/OpenShadingLanguage

# SPI-specific settings
ifneq (${VERBOSE},)
$(info Including spi/Makefile-bits)
endif

$(info Building generic OSL)
PYLIB_LIB_PREFIX ?= 0
PYLIB_INCLUDE_SONAME ?= 0
#$(info MAKECMDGOALS = $(MAKECMDGOALS))


## Detect which SPI platform and set $platform, $COMPILER,
## and PYTHON_VERSION. Lots of other decisions are based on these.
ifeq (${SP_OS}, rhel7)
    # Rhel7 (current)
    platform := rhel7
    SPI_COMPILER_PLATFORM ?= gcc-6.3
    ifeq (${SPI_COMPILER_PLATFORM},gcc-6.3)
        CMAKE_CXX_STANDARD ?= 14
    endif
    LLVM_VERSION ?= 12.0.0
else ifeq (${platform}, macosx)
    # Generic OSX machines (including LG's laptop)
    COMPILER ?= clang
    SP_OS ?= macosx
else
    $(error Unknown SP_OS=${SP_OS} platform=${platform})
endif  # endif ${SP_OS}

MY_CMAKE_FLAGS += -DSP_OS=${SP_OS}
MY_CMAKE_FLAGS += -DSPI_COMPILER_PLATFORM=${SPI_COMPILER_PLATFORM}
MY_CMAKE_FLAGS += -DOSL_SITE:STRING=spi
MY_CMAKE_FLAGS += -DSPI_TESTS=1
MY_CMAKE_FLAGS += -DOSL_NAMESPACE_INCLUDE_PATCH=1
REZ_PACKAGE_ROOT ?= /shots/spi/home/software/packages
SPCOMP2_ROOT ?= /shots/spi/home/lib/SpComp2

MY_CMAKE_FLAGS += -DOSL_EXTRA_NVCC_ARGS="--compiler-bindir=/opt/rh/devtoolset-6/root/bin/gcc"

## Rhel7 (current)
ifeq (${SP_OS}, rhel7)
    PYTHON_VERSION ?= 2.7
    USE_SIMD ?= sse4.1,aes
    CMAKE ?= cmake
    USE_NINJA ?= 1
    NINJA ?= ninja
    ifeq ($(USE_NINJA),1)
        MY_CMAKE_FLAGS += -DCMAKE_MAKE_PROGRAM=${NINJA}
    endif
    export CCACHE_CPP2 ?= 1
    # Override lib64 and use SPI convention of lib
    MY_CMAKE_FLAGS += -DCMAKE_INSTALL_LIBDIR="${INSTALL_PREFIX}/lib"

    #
    # Compilers:
    #
    # A variety of tags can be used to try specific versions of gcc or
    # clang from the site-specific places we have installed them.
    LLVM_DIRECTORY ?= /shots/spi/home/lib/arnold/rhel7/llvm_12.0.0
    LLVM_DIRECTORY ?= /shots/spi/home/software/packages/llvm/${LLVM_VERSION}/${SPI_COMPILER_PLATFORM}
    ifeq (${COMPILER},clang9)
        LLVM_DIRECTORY := /shots/spi/home/software/packages/llvm/9.0.1-1/${SPI_COMPILER_PLATFORM}
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=${LLVM_DIRECTORY}/bin/clang \
                          -DCMAKE_CXX_COMPILER=${LLVM_DIRECTORY}/bin/clang++
    else ifeq (${COMPILER},clang10)
        LLVM_DIRECTORY := /shots/spi/home/software/packages/llvm/10.0.0-1/${SPI_COMPILER_PLATFORM}
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=${LLVM_DIRECTORY}/bin/clang \
                          -DCMAKE_CXX_COMPILER=${LLVM_DIRECTORY}/bin/clang++
    else ifeq (${COMPILER},clang11)
        LLVM_DIRECTORY := /shots/spi/home/software/packages/llvm/11.0.0/${SPI_COMPILER_PLATFORM}
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=${LLVM_DIRECTORY}/bin/clang \
                          -DCMAKE_CXX_COMPILER=${LLVM_DIRECTORY}/bin/clang++
    else ifeq (${COMPILER},clang12)
        LLVM_DIRECTORY := /shots/spi/home/software/packages/llvm/12.0.0-final/${SPI_COMPILER_PLATFORM}
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=${LLVM_DIRECTORY}/bin/clang \
                          -DCMAKE_CXX_COMPILER=${LLVM_DIRECTORY}/bin/clang++
    else ifeq (${COMPILER},clang)
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
    else ifeq (${COMPILER}, gcc6)
        MY_CMAKE_FLAGS += \
           -DCMAKE_C_COMPILER=/opt/rh/devtoolset-6/root/usr/bin/gcc \
           -DCMAKE_CXX_COMPILER=/opt/rh/devtoolset-6/root/usr/bin/g++
    else ifeq (${COMPILER},gcc)
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
    else ifeq (${COMPILER},)
        # default compiler is clang, taken from the LLVM directory
        MY_CMAKE_FLAGS += \
           -DCMAKE_C_COMPILER=${LLVM_DIRECTORY}/bin/clang \
           -DCMAKE_CXX_COMPILER=${LLVM_DIRECTORY}/bin/clang++
    endif

    MY_CMAKE_FLAGS +=
        -DLLVM_STATIC=1 \
        -DLLVM_VERSION=${LLVM_VERSION} \
        -DLLVM_DIRECTORY=${LLVM_DIRECTORY}

    #
    # Boost
    #
    BOOSTVERS ?= 1.70
    MY_CMAKE_FLAGS += \
            -DBOOST_INCLUDEDIR=/usr/include/boostroot/boost${BOOSTVERS}.0 \
            -DBOOST_LIBRARYDIR=/usr/lib64/boostroot/boost${BOOSTVERS}.0

    #
    # Python
    #
    Python_INCLUDE_DIR ?= /usr/include/python${PYTHON_VERSION}
    PYTHON_LIBRARY_DIR ?= /usr/lib64
    Python_LIBRARY ?= ${PYTHON_LIBRARY_DIR}/libpython${PYTHON_VERSION}.so
    ifeq (${PYTHON_VERSION},3.7)
        # Special sauce for Python 3.7
        Python_INCLUDE_DIR := /usr/include/python${PYTHON_VERSION}m
        Python_LIBRARY := ${PYTHON_LIBRARY_DIR}/libpython${PYTHON_VERSION}m.so
    else ifeq (${PYTHON_VERSION},2.7)
        # Special sauce for Python 2.7
        Python_INCLUDE_DIR := /usr/include/python${PYTHON_VERSION}
        Python_LIBRARY := ${PYTHON_LIBRARY_DIR}/libpython${PYTHON_VERSION}.so
    endif
    MY_CMAKE_FLAGS += \
        -DPython_INCLUDE_DIR:STRING=${Python_INCLUDE_DIR} \
        -DPython_LIBRARY:STRING=${Python_LIBRARY} \
        -Dpybind11_ROOT:STRING=${REZ_PACKAGE_ROOT}/pybind11/2.4.3/python-${PYTHON_VERSION}


    # Set our preferred OpenEXR path, but allow env variable to override
    # with custom/test version.
    OPENEXR_VERSION ?= 2.4.1-2
    OpenEXR_ROOT ?= ${REZ_PACKAGE_ROOT}/OpenEXR/${OPENEXR_VERSION}/${SPI_COMPILER_PLATFORM}
    $(info OpenEXR_ROOT = $(OpenEXR_ROOT))
    MY_CMAKE_FLAGS += -DOpenEXR_ROOT=${OpenEXR_ROOT}

    Partio_VERSION ?= 1.10.6
    partio_ROOT ?= ${REZ_PACKAGE_ROOT}/partio/${Partio_VERSION}/${SPI_COMPILER_PLATFORM}/python-${PYTHON_VERSION}/boost-${BOOSTVERS}
    MY_CMAKE_FLAGS += -Dpartio_ROOT="${partio_ROOT}"

    pugixml_VERSION ?= 1.10.0
    pugixml_ROOT ?= ${REZ_PACKAGE_ROOT}/pugixml/${pugixml_VERSION}/${SPI_COMPILER_PLATFORM}
    MY_CMAKE_FLAGS += -Dpugixml_ROOT="${pugixml_ROOT}"

    ifneq ($(OpenImageIO_ROOT),)
        MY_CMAKE_FLAGS += -DOpenImageIO_ROOT=${OpenImageIO_ROOT}
    endif

    # Cuda/OptiX
    OSL_USE_OPTIX ?= 1
    CUDA_VERSION ?= 11.1.1
    OPTIX_VERSION ?= 7.2.0
    # CUDA_TOOLKIT_ROOT_DIR = $(shell rez-env -b cuda-${CUDA_VERSION} ${SPI_COMPILER_PLATFORM} -c "echo '$CUDA_TOOLKIT_ROOT'")
    CUDA_TOOLKIT_ROOT_DIR ?= /shots/spi/home/lib/arnold/rhel7/cuda_${CUDA_VERSION}
    OptiX_ROOT ?= /shots/spi/home/lib/arnold/rhel7/optix_${OPTIX_VERSION}
    OPTIXHOME = ${OptiX_ROOT}
    MY_CMAKE_FLAGS += -DOSL_USE_OPTIX=${OSL_USE_OPTIX} \
        -DCUDA_TOOLKIT_ROOT_DIR=${CUDA_TOOLKIT_ROOT_DIR} \
        -DCUDA_EXTRA_LIBS=${CUDA_EXTRA_LIBS} \
        -DOPTIXHOME=${OPTIXHOME} -DOPTIX_EXTRA_LIBS=lzma

    # end rhel7

## Generic OSX machines (including LG's laptop)
else ifeq (${platform}, macosx)
    CMAKE_CXX_STANDARD ?= 14
    USE_SIMD ?= sse4.2
    MY_CMAKE_FLAGS += -DCMAKE_BUILD_WITH_INSTALL_RPATH=1
    MY_CMAKE_FLAGS += -DCMAKE_INSTALL_NAME_DIR="${working_dir}/dist/lib"

    BOOSTVERS=${BOOSTVERS}${BOOSTSPSUFFIX}
    LLVM_DIRECTORY ?= /usr/local/opt/llvm

    # A variety of tags can be used to try specific versions of gcc or
    # clang from the site-specific places we have installed them.
    ifeq (${COMPILER}, gcc6)
        MY_CMAKE_FLAGS += \
           -DCMAKE_C_COMPILER=gcc-6 -DCMAKE_CXX_COMPILER=g++-6
        USE_LIBCPLUSPLUS := 0
    else ifeq (${COMPILER}, gcc9)
        MY_CMAKE_FLAGS += \
           -DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9
        USE_LIBCPLUSPLUS := 0
    else ifeq (${COMPILER},clang14)
        MY_CMAKE_FLAGS += \
            -DCMAKE_C_COMPILER=/usr/local/opt/llvm@14/bin/clang \
            -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm@14/bin/clang++
    else ifeq (${COMPILER},clang15)
        MY_CMAKE_FLAGS += \
            -DCMAKE_C_COMPILER=/usr/local/opt/llvm@15/bin/clang \
            -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm@15/bin/clang++
    else ifeq (${COMPILER},clang16)
        MY_CMAKE_FLAGS += \
                -DCMAKE_C_COMPILER=/usr/local/opt/llvm@16/bin/clang \
                -DCMAKE_CXX_COMPILER=/usr/local/opt/llvm@16/bin/clang++
    else ifeq (${COMPILER},clang)
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
    else ifeq (${COMPILER},gcc)
        MY_CMAKE_FLAGS += -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
    else ifeq (${COMPILER},)
        # default compiler is clang, taken from the LLVM directory
        MY_CMAKE_FLAGS += \
           -DCMAKE_C_COMPILER=${LLVM_ROOT}/bin/clang \
           -DCMAKE_CXX_COMPILER=${LLVM_ROOT}/bin/clang++
    else ifeq (${COMPILER},llvm)
        # "llvm" means "use the clang from our llvm"
        MY_CMAKE_FLAGS += \
            -DCMAKE_C_COMPILER=${LLVM_ROOT}/bin/clang \
            -DCMAKE_CXX_COMPILER=${LLVM_ROOT}/bin/clang++
    endif

    # end generic OSX

else
    $(error Unknown SP_OS=${SP_OS})
endif  # endif ${SP_OS}


ifneq (${VERBOSE},)
    $(info MY_CMAKE_FLAGS: $(MY_CMAKE_FLAGS))
endif
