Using oneMath in your project with CMake

Using oneMath in your project with CMake#

The CMake build tool can help you use oneMath in your own project. Instead of manually linking and including directories, you can use the CMake targets exported by the oneMath project. You can use oneMath in one of two forms, with the target names depending on the approach taken:

  • you can use a previously installed copy, either from a binary distribution or built from source. This can be imported using CMake’s find_package command. See the section using_from_installed_binary.

  • or you can have CMake automatically download and build oneMath as part of the build process using CMake’s FetchContent functionality. See the section using_with_fetchcontent.

Using an installed oneMath#

If oneMath has been previously installed, either by building from source or as a distributed binary, they can be consumed using CMake using find_package(oneMath REQUIRED). The compiler used for the target library or application should match that used to build oneMath.

For example:

find_package(oneMath REQUIRED)
target_link_libraries(myTarget PRIVATE ONEMATH::onemath)

Different targets can be used depending on the requirements of oneMath. To link against the entire library, the ONEMATH::onemath target should be used. For specific domains, ONEMATH::onemath_<domain> should be used. And for specific backends, ONEMATH::onemath_<domain>_<backend> should be used.

When using a binary, it may be useful to know the backends that were enabled during the build. To check for the existence of backends, CMake’s if(TARGET <target>) construct can be used. For example, with the cufft backend:

if(TARGET ONEMATH::onemath_dft_cufft)
    target_link_libraries(myTarget PRIVATE ONEMATH::onemath_dft_cufft)
else()
    message(FATAL_ERROR "oneMath was not built with CuFFT backend")
endif()

Using CMake’s FetchContent#

The FetchContent functionality of CMake can be used to download, build and install oneMath as part of the build.

For example:

include(FetchContent)
set(BUILD_FUNCTIONAL_TESTS False)
set(BUILD_EXAMPLES False)
set(ENABLE_<BACKEND_NAME>_BACKEND True)
FetchContent_Declare(
        onemath_library
        GIT_REPOSITORY https://github.com/uxlfoundation/oneMath.git
        GIT_TAG develop
)
FetchContent_MakeAvailable(onemath_library)

target_link_libraries(myTarget PRIVATE onemath)

The build parameters should be appropriately set before FetchContent_Declare. See Building the Project with DPC++ or Building the Project with AdaptiveCpp.

To link against the main library with run-time dispatching, use the target onemath. To link against particular domains, use the target onemath_<domain>. For example, onemath_blas or onemath_dft. To link against particular backends (as required for static dispatch of oneAPI calls to a particular backend), use the target onemath_<domain>_<backend>. For example, onemath_dft_cufft.