brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.3 KiB · 2a8fb3a Raw
224 lines · plain
1# CMake find_package() module for level-zero2#3# Example usage:4#5# find_package(LevelZero)6#7# If successful, the following variables will be defined:8# LevelZero_FOUND9# LevelZero_INCLUDE_DIRS10# LevelZero_LIBRARY11# LevelZero_LIBRARIES_DIR12#13# By default, the module searches the standard paths to locate the "ze_api.h"14# and the ze_loader shared library. When using a custom level-zero installation,15# the environment variable "LEVEL_ZERO_DIR" should be specified telling the16# module to get the level-zero library and headers from that location.17 18include(FindPackageHandleStandardArgs)19 20# Search path priority21# 1. CMake Variable LEVEL_ZERO_DIR22# 2. Environment Variable LEVEL_ZERO_DIR23if(NOT LEVEL_ZERO_DIR)24    if(DEFINED ENV{LEVEL_ZERO_DIR})25        set(LEVEL_ZERO_DIR "$ENV{LEVEL_ZERO_DIR}")26    endif()27endif()28 29if(LEVEL_ZERO_DIR)30    find_path(LevelZeroRuntime_INCLUDE_DIR31        NAMES level_zero/ze_api.h32        PATHS ${LEVEL_ZERO_DIR}/include33        NO_DEFAULT_PATH34    )35 36    if(LINUX)37        find_library(LevelZeroRuntime_LIBRARY38            NAMES ze_loader39            PATHS ${LEVEL_ZERO_DIR}/lib40            ${LEVEL_ZERO_DIR}/lib/x86_64-linux-gnu41            NO_DEFAULT_PATH42        )43    else()44        find_library(LevelZeroRuntime_LIBRARY45            NAMES ze_loader46            PATHS ${LEVEL_ZERO_DIR}/lib47            NO_DEFAULT_PATH48        )49    endif()50else()51    find_path(LevelZeroRuntime_INCLUDE_DIR52        NAMES level_zero/ze_api.h53    )54 55    find_library(LevelZeroRuntime_LIBRARY56        NAMES ze_loader57    )58endif()59 60# Compares the two version string that are supposed to be in x.y.z format61# and reports if the argument VERSION_STR1 is greater than or equal than62# version_str2. The strings are compared lexicographically after conversion to63# lists of equal lengths, with the shorter string getting zero-padded.64function(compare_versions VERSION_STR1 VERSION_STR2 OUTPUT)65    # Convert the strings to list66    string(REPLACE "." ";" VL1 ${VERSION_STR1})67    string(REPLACE "." ";" VL2 ${VERSION_STR2})68 69    # get lengths of both lists70    list(LENGTH VL1 VL1_LEN)71    list(LENGTH VL2 VL2_LEN)72    set(LEN ${VL1_LEN})73 74    # If they differ in size pad the shorter list with 0s75    if(VL1_LEN GREATER VL2_LEN)76        math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)77        foreach(IDX RANGE 1 ${DIFF} 1)78            list(APPEND VL2 "0")79        endforeach()80    elseif(VL2_LEN GREATER VL2_LEN)81        math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)82        foreach(IDX RANGE 1 ${DIFF} 1)83            list(APPEND VL2 "0")84        endforeach()85        set(LEN ${VL2_LEN})86    endif()87    math(EXPR LEN_SUB_ONE "${LEN}-1")88    foreach(IDX RANGE 0 ${LEN_SUB_ONE} 1)89        list(GET VL1 ${IDX} VAL1)90        list(GET VL2 ${IDX} VAL2)91 92        if(${VAL1} GREATER ${VAL2})93            set(${OUTPUT} TRUE PARENT_SCOPE)94            break()95        elseif(${VAL1} LESS ${VAL2})96            set(${OUTPUT} FALSE PARENT_SCOPE)97            break()98        else()99            set(${OUTPUT} TRUE PARENT_SCOPE)100        endif()101    endforeach()102endfunction(compare_versions)103 104# Creates a small function to run and extract the LevelZero loader version.105function(get_l0_loader_version)106    set(L0_VERSIONEER_SRC107        [====[108        #include <iostream>109        #include <level_zero/loader/ze_loader.h>110        #include <string>111        int main() {112            ze_result_t result;113            std::string loader("loader");114            zel_component_version_t *versions;115            size_t size = 0;116            result = zeInit(0);117            if (result != ZE_RESULT_SUCCESS) {118                std::cerr << "Failed to init ze driver" << std::endl;119                return -1;120            }121            zelLoaderGetVersions(&size, nullptr);122            versions = new zel_component_version_t[size];123            zelLoaderGetVersions(&size, versions);124            for (size_t i = 0; i < size; i++) {125                if (loader.compare(versions[i].component_name) == 0) {126                    std::cout << versions[i].component_lib_version.major << "."127                              << versions[i].component_lib_version.minor << "."128                              << versions[i].component_lib_version.patch;129                    break;130                }131            }132            delete[] versions;133            return 0;134        }135        ]====]136    )137 138    set(L0_VERSIONEER_FILE ${CMAKE_BINARY_DIR}/temp/l0_versioneer.cpp)139 140    file(WRITE ${L0_VERSIONEER_FILE} "${L0_VERSIONEER_SRC}")141 142    # We need both the directories in the include path as ze_loader.h143    # includes "ze_api.h" and not "level_zero/ze_api.h".144    list(APPEND INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR})145    list(APPEND INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR}/level_zero)146    list(JOIN INCLUDE_DIRS ";" INCLUDE_DIRS_STR)147    try_run(L0_VERSIONEER_RUN L0_VERSIONEER_COMPILE148        "${CMAKE_BINARY_DIR}"149        "${L0_VERSIONEER_FILE}"150        LINK_LIBRARIES ${LevelZeroRuntime_LIBRARY}151        CMAKE_FLAGS152        "-DINCLUDE_DIRECTORIES=${INCLUDE_DIRS_STR}"153        RUN_OUTPUT_VARIABLE L0_VERSION154    )155 156    if(${L0_VERSIONEER_COMPILE} AND(DEFINED L0_VERSIONEER_RUN))157        set(LevelZeroRuntime_VERSION ${L0_VERSION} PARENT_SCOPE)158        message(STATUS "Found Level Zero of version: ${L0_VERSION}")159    else()160        message(FATAL_ERROR161            "Could not compile a level-zero program to extract loader version"162        )163    endif()164endfunction(get_l0_loader_version)165 166if(LevelZeroRuntime_INCLUDE_DIR AND LevelZeroRuntime_LIBRARY)167    list(APPEND LevelZeroRuntime_LIBRARIES "${LevelZeroRuntime_LIBRARY}")168    list(APPEND LevelZeroRuntime_INCLUDE_DIRS ${LevelZeroRuntime_INCLUDE_DIR})169 170    if(OpenCL_FOUND)171        list(APPEND LevelZeroRuntime_INCLUDE_DIRS ${OpenCL_INCLUDE_DIRS})172    endif()173 174    cmake_path(GET LevelZeroRuntime_LIBRARY PARENT_PATH LevelZeroRuntime_LIBRARIES_PATH)175    set(LevelZeroRuntime_LIBRARIES_DIR ${LevelZeroRuntime_LIBRARIES_PATH})176 177    if(NOT TARGET LevelZeroRuntime::LevelZeroRuntime)178        add_library(LevelZeroRuntime::LevelZeroRuntime INTERFACE IMPORTED)179        set_target_properties(LevelZeroRuntime::LevelZeroRuntime180            PROPERTIES INTERFACE_LINK_LIBRARIES "${LevelZeroRuntime_LIBRARIES}"181        )182        set_target_properties(LevelZeroRuntime::LevelZeroRuntime183            PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LevelZeroRuntime_INCLUDE_DIRS}"184        )185    endif()186endif()187 188# Check if a specific version of Level Zero is required189if(LevelZeroRuntime_FIND_VERSION)190    get_l0_loader_version()191    set(VERSION_GT_FIND_VERSION FALSE)192    compare_versions(193        ${LevelZeroRuntime_VERSION}194        ${LevelZeroRuntime_FIND_VERSION}195        VERSION_GT_FIND_VERSION196    )197 198    if(${VERSION_GT_FIND_VERSION})199        set(LevelZeroRuntime_FOUND TRUE)200    else()201        set(LevelZeroRuntime_FOUND FALSE)202    endif()203else()204    set(LevelZeroRuntime_FOUND TRUE)205endif()206 207find_package_handle_standard_args(LevelZeroRuntime208    REQUIRED_VARS209    LevelZeroRuntime_FOUND210    LevelZeroRuntime_INCLUDE_DIRS211    LevelZeroRuntime_LIBRARY212    LevelZeroRuntime_LIBRARIES_DIR213    HANDLE_COMPONENTS214)215mark_as_advanced(LevelZeroRuntime_LIBRARY LevelZeroRuntime_INCLUDE_DIRS)216 217if(LevelZeroRuntime_FOUND)218    find_package_message(LevelZeroRuntime "Found LevelZero: ${LevelZeroRuntime_LIBRARY}"219        "(found version ${LevelZeroRuntime_VERSION})"220    )221else()222    find_package_message(LevelZeroRuntime "Could not find LevelZero" "")223endif()224