Skip to content

Commit feb05a7

Browse files
committed
Introduce executorch_runtime CMake function
1 parent dbd40f4 commit feb05a7

File tree

2 files changed

+107
-12
lines changed

2 files changed

+107
-12
lines changed

CMakeLists.txt

+62
Original file line numberDiff line numberDiff line change
@@ -787,5 +787,67 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
787787
)
788788
endif()
789789

790+
function(executorch_runtime target_name)
791+
set(options OPTIMIZED QUANTIZED)
792+
set(multi_value_args BACKEND EXTENSION)
793+
794+
cmake_parse_arguments(ETR "${options}" "" "${multi_value_args}" ${ARGN})
795+
796+
message(STATUS "executorch_runtime: ${target_name}")
797+
message(STATUS "ETR_BACKEND: ${ETR_BACKEND}")
798+
799+
set(runtime_deps executorch)
800+
801+
# Link backends.
802+
foreach(backend IN LISTS ETR_BACKEND)
803+
message(STATUS "backend: ${backend}")
804+
string(TOLOWER "${backend}" backend_l)
805+
message(STATUS "backend_l: ${backend_l}")
806+
807+
if(backend_l STREQUAL "coreml")
808+
list(APPEND runtime_deps coremldelegate)
809+
elseif(backend_l STREQUAL "mps")
810+
list(APPEND runtime_deps mpsdelegate)
811+
elseif(backend_l STREQUAL "qualcomm")
812+
list(APPEND runtime_deps qnn_executorch_backend)
813+
elseif(backend_l STREQUAL "vulkan")
814+
list(APPEND runtime_deps vulkan_backend)
815+
elseif(backend_l STREQUAL "xnnpack")
816+
list(APPEND runtime_deps xnnpack_backend)
817+
else()
818+
message(FATAL_ERROR "Unknown backend \"${backend}}\".")
819+
endif()
820+
endforeach()
821+
822+
# Link extensions.
823+
foreach(extension IN LISTS ETR_EXTENSION)
824+
string(TOLOWER "${extension}" extension_l)
825+
message(STATUS "Extension ${extension_l}")
826+
827+
if(extension_l STREQUAL "apple")
828+
list(APPEND runtime_deps extension_apple)
829+
elseif(extension_l STREQUAL "dataloader")
830+
list(APPEND runtime_deps extension_data_loader)
831+
elseif(extension_l STREQUAL "module")
832+
list(APPEND runtime_deps extension_module)
833+
endif()
834+
endforeach()
835+
836+
# Include optimized or portable operator library.
837+
if(ETR_OPTIMIZED)
838+
list(APPEND runtime_deps optimized_native_cpu_ops_lib)
839+
else()
840+
list(APPEND runtime_deps portable_ops_lib)
841+
endif()
842+
843+
# Include quantized operator library.
844+
if(ETR_QUANTIZED)
845+
list(APPEND runtime_deps quantized_ops_lib)
846+
endif()
847+
848+
add_library("${target_name}" INTERFACE)
849+
target_link_libraries("${target_name}" INTERFACE "${runtime_deps}")
850+
endfunction()
851+
790852
# Print all summary
791853
executorch_print_configuration_summary()

examples/llm_manual/CMakeLists.txt

+45-12
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,59 @@ project(nanogpt_runner)
1010
set(CMAKE_CXX_STANDARD 17)
1111
set(CMAKE_CXX_STANDARD_REQUIRED True)
1212

13-
# Set options for executorch build.
14-
option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "" ON)
15-
option(EXECUTORCH_BUILD_EXTENSION_MODULE "" ON)
16-
option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED "" ON)
17-
option(EXECUTORCH_BUILD_XNNPACK "" ON) # Build with Xnnpack backend
18-
1913
# Include the executorch subdirectory.
2014
add_subdirectory(
21-
${CMAKE_CURRENT_SOURCE_DIR}/third-party/executorch
15+
${CMAKE_CURRENT_SOURCE_DIR}/../..
2216
${CMAKE_BINARY_DIR}/executorch
2317
)
2418

19+
executorch_runtime(
20+
et_runtime
21+
BACKEND xnnpack
22+
EXTENSION module dataloader
23+
OPTIMIZED
24+
)
25+
26+
if(NOT CMAKE_PROPERTY_LIST)
27+
execute_process(COMMAND cmake --help-property-list OUTPUT_VARIABLE CMAKE_PROPERTY_LIST)
28+
29+
# Convert command output into a CMake list
30+
string(REGEX REPLACE ";" "\\\\;" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
31+
string(REGEX REPLACE "\n" ";" CMAKE_PROPERTY_LIST "${CMAKE_PROPERTY_LIST}")
32+
list(REMOVE_DUPLICATES CMAKE_PROPERTY_LIST)
33+
endif()
34+
35+
function(print_target_properties target)
36+
if(NOT TARGET ${target})
37+
message(STATUS "There is no target named '${target}'")
38+
return()
39+
endif()
40+
41+
foreach(property ${CMAKE_PROPERTY_LIST})
42+
string(REPLACE "<CONFIG>" "${CMAKE_BUILD_TYPE}" property ${property})
43+
44+
# Fix https://stackoverflow.com/questions/32197663/how-can-i-remove-the-the-location-property-may-not-be-read-from-target-error-i
45+
if(property STREQUAL "LOCATION" OR property MATCHES "^LOCATION_" OR property MATCHES "_LOCATION$")
46+
continue()
47+
endif()
48+
49+
get_property(was_set TARGET ${target} PROPERTY ${property} SET)
50+
if(was_set)
51+
get_target_property(value ${target} ${property})
52+
message("${target} ${property} = ${value}")
53+
endif()
54+
endforeach()
55+
endfunction()
56+
57+
print_target_properties(et_runtime)
58+
59+
get_target_property(include_dirs_temp et_runtime INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
60+
message(STATUS "include_dirs_temp: ${include_dirs_temp}")
61+
2562
# include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
2663

2764
add_executable(nanogpt_runner main.cpp)
2865
target_link_libraries(
2966
nanogpt_runner
30-
PRIVATE executorch
31-
extension_module_static # Provides the Module class
32-
optimized_native_cpu_ops_lib # Provides baseline cross-platform
33-
# kernels
34-
xnnpack_backend
67+
PRIVATE et_runtime
3568
) # Provides the XNNPACK CPU acceleration backend

0 commit comments

Comments
 (0)