1818cmake_minimum_required (VERSION 3.19)
1919project (llama_runner)
2020
21+ # Duplicating options as root CMakeLists.txt
2122option (EXECUTORCH_BUILD_OPTIMIZED "Build the optimized kernels" OFF )
2223
24+ include (CMakeDependentOption)
25+ #
26+ # pthreadpool: build pthreadpool library. Disable on unsupported platforms
27+ #
28+ cmake_dependent_option(
29+ EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON
30+ "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF )
31+ #
32+ # cpuinfo: build cpuinfo library. Disable on unsupported platforms
33+ #
34+ cmake_dependent_option(EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON
35+ "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF )
36+
37+
2338if (NOT PYTHON_EXECUTABLE)
2439 set (PYTHON_EXECUTABLE python3)
2540endif ()
@@ -49,55 +64,84 @@ set(_common_compile_options -Wno-deprecated-declarations -fPIC)
4964# Let files say "include <executorch/path/to/header.h>".
5065set (_common_include_directories ${EXECUTORCH_ROOT} /..)
5166
52- # For some reason android build is not able to find where gflags is
53- # and hence cannot find corresponding .cmake file
67+ # For some reason android build is not able to find where gflags is and hence
68+ # cannot find corresponding .cmake file
5469set (gflags_DIR ${CMAKE_CURRENT_BINARY_DIR} /../../../third-party/gflags)
5570find_package (gflags REQUIRED)
5671
5772#
5873# llama_main: test binary to run llama, with tokenizer and sampler integrated
5974#
60- add_executable (llama_main main.cpp
61- ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/threadpool/cpuinfo_utils.cpp)
62- if (CMAKE_BUILD_TYPE EQUAL "RELEASE" )
63- target_link_options (llama_main PRIVATE "LINKER:--gc-sections" )
64- endif ()
6575
66- # find `executorch` libraries
67- # Same as for gflags
76+ # find `executorch` libraries Same as for gflags
6877set (executorch_DIR ${CMAKE_CURRENT_BINARY_DIR} /../../../lib/cmake/ExecuTorch)
6978find_package (executorch CONFIG REQUIRED)
7079if (CMAKE_TOOLCHAIN_IOS OR ANDROID)
7180 target_link_options_shared_lib(executorch)
7281endif ()
7382
7483# custom ops library
75- add_subdirectory (custom_ops)
84+ if (EXECUTORCH_BUILD_CUSTOM)
85+ add_subdirectory (custom_ops)
86+ endif ()
7687
7788# llama_runner library
7889add_subdirectory (runner)
7990
80- target_include_directories (llama_main PUBLIC
81- ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/third-party/cpuinfo/include )
82- target_include_directories (llama_main PUBLIC
83- ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/third-party/pthreadpool/include )
84-
8591set (link_libraries )
92+ set (_srcs main.cpp)
8693
8794if (EXECUTORCH_BUILD_OPTIMIZED)
88- list (APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels
89- portable_kernels cpublas eigen_blas)
95+ list (
96+ APPEND
97+ link_libraries
98+ optimized_native_cpu_ops_lib
99+ optimized_kernels
100+ portable_kernels
101+ cpublas
102+ eigen_blas)
90103 target_link_options_shared_lib(optimized_native_cpu_ops_lib)
91104else ()
92105 list (APPEND link_libraries portable_ops_lib portable_kernels)
93106 target_link_options_shared_lib(portable_ops_lib)
94107endif ()
95108
96- target_link_libraries (llama_main PUBLIC gflags llama_runner custom_ops_lib)
109+ if (EXECUTORCH_BUILD_CUSTOM)
110+ target_link_options_shared_lib(custom_ops_lib)
111+ list (APPEND link_libraries custom_ops_lib)
112+ endif ()
113+
114+ # Extra compile option and include dir for pthreadpool
115+ if (EXECUTORCH_BUILD_PTHREADPOOL)
116+ list (APPEND _common_compile_options -DET_USE_THREADPOOL)
117+ list (APPEND link_libraries pthreadpool)
118+ list (
119+ APPEND
120+ _srcs
121+ ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/threadpool/threadpool.cpp
122+ ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/threadpool/threadpool_guard.cpp
123+ )
124+ list (APPEND _common_include_directories ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/third-party/pthreadpool/include )
125+ endif ()
126+
127+ # Extra sources for cpuinfo
128+ if (EXECUTORCH_BUILD_CPUINFO)
129+ list (APPEND link_libraries cpuinfo)
130+ list (
131+ APPEND
132+ _srcs
133+ ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/threadpool/cpuinfo_utils.cpp
134+ )
135+ list (
136+ APPEND
137+ _common_include_directories
138+ ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack/third-party/cpuinfo/include
139+ )
140+ endif ()
97141
98- # XNNPACK pthreadpool cpuinfo
142+ # XNNPACK
99143if (TARGET xnnpack_backend)
100- set (xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo )
144+ set (xnnpack_backend_libs xnnpack_backend XNNPACK)
101145 list (APPEND link_libraries ${xnnpack_backend_libs} )
102146 target_link_options_shared_lib(xnnpack_backend)
103147endif ()
@@ -114,15 +158,19 @@ if(TARGET qnn_executorch_backend)
114158 target_link_options_shared_lib(qnn_executorch_backend)
115159endif ()
116160
117- # This one is needed for cpuinfo where it uses android
118- # specific log lib
161+ # This one is needed for cpuinfo where it uses android specific log lib
119162if (ANDROID)
120163 list (APPEND link_libraries log )
121164endif ()
122165
123- target_compile_options (llama_main PUBLIC ${_common_compile_options}
124- -DET_USE_THREADPOOL)
125- target_link_libraries (llama_main PUBLIC ${link_libraries} )
166+ add_executable (llama_main ${_srcs} )
167+ if (CMAKE_BUILD_TYPE EQUAL "RELEASE" )
168+ target_link_options (llama_main PRIVATE "LINKER:--gc-sections" )
169+ endif ()
170+
171+ target_include_directories (llama_main PUBLIC ${_common_include_directories} )
172+ target_link_libraries (llama_main PUBLIC gflags llama_runner ${link_libraries} )
173+ target_compile_options (llama_main PUBLIC ${_common_compile_options} )
126174
127175if (APPLE )
128176 target_link_options_shared_lib(executorch)
0 commit comments