Skip to content

Commit b63147e

Browse files
author
Diptorup Deb
committed
Adds a new CMake module to find Level zero.
- Adds a FindLevelZero cmake module to locate a system level zero installation. - If the LEVEL_ZERO_DIR environment variable is set, then only that path is searched.
1 parent 1270a25 commit b63147e

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

cmake/modules/FindLevelZero.cmake

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Copyright 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# CMake find_package() module for level-zero
16+
#
17+
# Example usage:
18+
#
19+
# find_package(LevelZero)
20+
#
21+
# If successful, the following variables will be defined:
22+
# LevelZero_FOUND
23+
# LevelZero_INCLUDE_DIR
24+
# LevelZero_LIBRARY
25+
# LevelZero_LIBRARIES_DIR
26+
#
27+
# By default, the module searches the standard paths to locate the "ze_api.h"
28+
# and the ze_loader shared library. When using a custom level-zero installation,
29+
# the environment variable "LEVEL_ZERO_DIR" should be specified telling the
30+
# module to get the level-zero library and headers from that location.
31+
32+
include(FindPackageHandleStandardArgs)
33+
34+
if(DEFINED ENV{LEVEL_ZERO_DIR})
35+
find_path(LevelZero_INCLUDE_DIR
36+
NAMES level_zero/ze_api.h
37+
PATHS $ENV{LEVEL_ZERO_DIR}/include
38+
NO_DEFAULT_PATH
39+
)
40+
41+
find_library(LevelZero_LIBRARY
42+
NAMES ze_loader
43+
PATHS $ENV{LEVEL_ZERO_DIR}/lib
44+
NO_DEFAULT_PATH
45+
)
46+
else()
47+
find_path(LevelZero_INCLUDE_DIR
48+
NAMES level_zero/ze_api.h
49+
)
50+
51+
find_library(LevelZero_LIBRARY
52+
NAMES ze_loader
53+
)
54+
endif()
55+
56+
# Compares the two version string that are supposed to be in x.y.z format
57+
# and reports if the argument VERSION_STR1 is greater than or equal than
58+
# version_str2. The strings are compared lexicographically after conversion to
59+
# lists of equal lengths, with the shorter string getting zero-padded.
60+
function(compare_versions VERSION_STR1 VERSION_STR2 OUTPUT)
61+
# Convert the strings to list
62+
string(REPLACE "." ";" VL1 ${VERSION_STR1})
63+
string(REPLACE "." ";" VL2 ${VERSION_STR2})
64+
# get lengths of both lists
65+
list(LENGTH VL1 VL1_LEN)
66+
list(LENGTH VL2 VL2_LEN)
67+
set(LEN ${VL1_LEN})
68+
# If they differ in size pad the shorter list with 0s
69+
if(VL1_LEN GREATER VL2_LEN)
70+
math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)
71+
foreach(IDX RANGE 1 ${DIFF} 1)
72+
list(APPEND VL2 "0")
73+
endforeach()
74+
elseif(VL2_LEN GREATER VL2_LEN)
75+
math(EXPR DIFF "${VL1_LEN} - ${VL2_LEN}" OUTPUT_FORMAT DECIMAL)
76+
foreach(IDX RANGE 1 ${DIFF} 1)
77+
list(APPEND VL2 "0")
78+
endforeach()
79+
set(LEN ${VL2_LEN})
80+
endif()
81+
math(EXPR LEN_SUB_ONE "${LEN}-1")
82+
foreach(IDX RANGE 0 ${LEN_SUB_ONE} 1)
83+
list(GET VL1 ${IDX} VAL1)
84+
list(GET VL2 ${IDX} VAL2)
85+
86+
if(${VAL1} GREATER ${VAL2})
87+
set(${OUTPUT} TRUE PARENT_SCOPE)
88+
break()
89+
elseif(${VAL1} LESS ${VAL2})
90+
set(${OUTPUT} FALSE PARENT_SCOPE)
91+
break()
92+
else()
93+
set(${OUTPUT} TRUE PARENT_SCOPE)
94+
endif()
95+
endforeach()
96+
97+
endfunction(compare_versions)
98+
99+
# Creates a small function to run and extract the LevelZero loader version.
100+
function(get_l0_loader_version)
101+
102+
set(L0_VERSIONEER_SRC
103+
[====[
104+
#include <iostream>
105+
#include <level_zero/loader/ze_loader.h>
106+
#include <string>
107+
int main() {
108+
ze_result_t result;
109+
std::string loader("loader");
110+
zel_component_version_t *versions;
111+
size_t size = 0;
112+
result = zeInit(0);
113+
if (result != ZE_RESULT_SUCCESS) {
114+
std::cerr << "Failed to init ze driver" << std::endl;
115+
return -1;
116+
}
117+
zelLoaderGetVersions(&size, nullptr);
118+
versions = new zel_component_version_t[size];
119+
zelLoaderGetVersions(&size, versions);
120+
for (size_t i = 0; i < size; i++) {
121+
if (loader.compare(versions[i].component_name) == 0) {
122+
std::cout << versions[i].component_lib_version.major << "."
123+
<< versions[i].component_lib_version.minor << "."
124+
<< versions[i].component_lib_version.patch;
125+
break;
126+
}
127+
}
128+
delete[] versions;
129+
return 0;
130+
}
131+
]====]
132+
)
133+
134+
set(L0_VERSIONEER_FILE ${CMAKE_BINARY_DIR}/temp/l0_versioneer.cpp)
135+
136+
file(WRITE ${L0_VERSIONEER_FILE} "${L0_VERSIONEER_SRC}")
137+
138+
# We need both the directories in the include path as ze_loader.h
139+
# includes "ze_api.h" and not "level_zero/ze_api.h".
140+
list(APPEND INCLUDE_DIRS ${LevelZero_INCLUDE_DIR})
141+
list(APPEND INCLUDE_DIRS ${LevelZero_INCLUDE_DIR}/level_zero)
142+
list(JOIN INCLUDE_DIRS ";" INCLUDE_DIRS_STR)
143+
try_run(L0_VERSIONEER_RUN L0_VERSIONEER_COMPILE
144+
"${CMAKE_BINARY_DIR}"
145+
"${L0_VERSIONEER_FILE}"
146+
LINK_LIBRARIES ${LevelZero_LIBRARY}
147+
CMAKE_FLAGS
148+
"-DINCLUDE_DIRECTORIES=${INCLUDE_DIRS_STR}"
149+
RUN_OUTPUT_VARIABLE L0_VERSION
150+
)
151+
if(${L0_VERSIONEER_COMPILE} AND (DEFINED L0_VERSIONEER_RUN))
152+
set(LevelZero_VERSION ${L0_VERSION} PARENT_SCOPE)
153+
message(STATUS "Found Level Zero of version: ${L0_VERSION}")
154+
else()
155+
message(FATAL_ERROR
156+
"Could not compile a level-zero program to extract loader version"
157+
)
158+
endif()
159+
endfunction(get_l0_loader_version)
160+
161+
if(LevelZero_INCLUDE_DIR AND LevelZero_LIBRARY)
162+
list(APPEND LevelZero_LIBRARIES "${LevelZero_LIBRARY}")
163+
list(APPEND LevelZero_INCLUDE_DIRS ${LevelZero_INCLUDE_DIR})
164+
if(OpenCL_FOUND)
165+
list(APPEND LevelZero_INCLUDE_DIRS ${OpenCL_INCLUDE_DIRS})
166+
endif()
167+
168+
cmake_path(GET LevelZero_LIBRARY PARENT_PATH LevelZero_LIBRARIES_PATH)
169+
set(LevelZero_LIBRARIES_DIR ${LevelZero_LIBRARIES_PATH})
170+
171+
if(NOT TARGET LevelZero::LevelZero)
172+
add_library(LevelZero::LevelZero INTERFACE IMPORTED)
173+
set_target_properties(LevelZero::LevelZero
174+
PROPERTIES INTERFACE_LINK_LIBRARIES "${LevelZero_LIBRARIES}"
175+
)
176+
set_target_properties(LevelZero::LevelZero
177+
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${LevelZero_INCLUDE_DIRS}"
178+
)
179+
endif()
180+
endif()
181+
182+
# Check if a specific version of Level Zero is required
183+
if(LevelZero_FIND_VERSION)
184+
get_l0_loader_version()
185+
set(VERSION_GT_FIND_VERSION FALSE)
186+
compare_versions(
187+
${LevelZero_VERSION}
188+
${LevelZero_FIND_VERSION}
189+
VERSION_GT_FIND_VERSION
190+
)
191+
if(${VERSION_GT_FIND_VERSION})
192+
set(LevelZero_FOUND TRUE)
193+
else()
194+
set(LevelZero_FOUND FALSE)
195+
endif()
196+
else()
197+
set(LevelZero_FOUND TRUE)
198+
endif()
199+
200+
find_package_handle_standard_args(LevelZero
201+
REQUIRED_VARS
202+
LevelZero_FOUND
203+
LevelZero_INCLUDE_DIR
204+
LevelZero_LIBRARY
205+
LevelZero_LIBRARIES_DIR
206+
HANDLE_COMPONENTS
207+
)
208+
mark_as_advanced(LevelZero_LIBRARY LevelZero_INCLUDE_DIR)
209+
210+
if(LevelZero_FOUND)
211+
find_package_message(LevelZero "Found Level Zero: ${LevelZero_LIBRARY}"
212+
"(found version ${LevelZero_VERSION})"
213+
)
214+
else()
215+
find_package_message(LevelZero "Could not find LevelZero")
216+
endif()

0 commit comments

Comments
 (0)