@@ -54,6 +54,8 @@ include(${PROJECT_SOURCE_DIR}/tools/cmake/common/preset.cmake)
5454include (${PROJECT_SOURCE_DIR} /tools/cmake/Utils.cmake)
5555include (CMakeDependentOption)
5656include (ExternalProject)
57+ include (GNUInstallDirs)
58+ include (CMakePackageConfigHelpers)
5759
5860if (NOT CMAKE_CXX_STANDARD)
5961 set (CMAKE_CXX_STANDARD 17)
@@ -109,46 +111,53 @@ set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
109111# built under the same directory, so no extra rpath is being added. To properly
110112# fix this we need to install `portable_lib` into the correct path.
111113set (CMAKE_INSTALL_RPATH_USE_LINK_PATH ON )
114+
112115# ------------------------------ OPTIONS -------------------------------------
113116# WARNING: Please don't add example specific options in this CMakeLists.txt.
114117# Instead please use `find_package(executorch REQUIRED)` in the example
115118# directory and add a new executable in the example `CMakeLists.txt`.
116119
117- if (NOT EXECUTORCH_ENABLE_LOGGING)
118- # Avoid pulling in the logging strings, which can be large. Note that this
119- # will set the compiler flag for all targets in this directory, and for all
120- # subdirectories included after this point.
121- add_definitions (-DET_LOG_ENABLED=0)
122- endif ()
123-
124- add_definitions (-DET_MIN_LOG_LEVEL=${ET_MIN_LOG_LEVEL} )
125-
126- if (NOT EXECUTORCH_ENABLE_PROGRAM_VERIFICATION)
127- # Avoid pulling in the flatbuffer data verification logic, which can add about
128- # 20kB. Note that this will set the compiler flag for all targets in this
129- # directory, and for all subdirectories included after this point.
130- add_definitions (-DET_ENABLE_PROGRAM_VERIFICATION=0)
131- endif ()
132-
133- if (EXECUTORCH_ENABLE_EVENT_TRACER)
134- add_definitions (-DET_EVENT_TRACER_ENABLED)
135- endif ()
120+ # Create interface libraries for modern CMake practices
121+ add_library (executorch_base_config INTERFACE )
122+ add_library (executorch_compile_options INTERFACE )
123+
124+ # Set base compile definitions using generator expressions
125+ target_compile_definitions (executorch_base_config INTERFACE
126+ C10_USING_CUSTOM_GENERATED_MACROS
127+ $<$<NOT :$<BOOL :${EXECUTORCH_ENABLE_LOGGING} >>:ET_LOG_ENABLED=0>
128+ ET_MIN_LOG_LEVEL=${ET_MIN_LOG_LEVEL}
129+ $<$<NOT :$<BOOL :${EXECUTORCH_ENABLE_PROGRAM_VERIFICATION} >>:ET_ENABLE_PROGRAM_VERIFICATION=0>
130+ $<$<BOOL :${EXECUTORCH_ENABLE_EVENT_TRACER} >:ET_EVENT_TRACER_ENABLED>
131+ )
136132
137- # -ffunction-sections -fdata-sections: breaks function and data into sections so
138- # they can be properly gc'd. -s: strip symbol.
139- set (CMAKE_CXX_FLAGS_RELEASE
140- "-ffunction-sections -fdata-sections ${CMAKE_CXX_FLAGS_RELEASE} "
133+ # Modern compiler flag management using generator expressions
134+ target_compile_options (executorch_compile_options INTERFACE
135+ -Wno-deprecated-declarations
136+ -fPIC
137+ $<$<CONFIG:Release>:-ffunction-sections>
138+ $<$<CONFIG:Release>:-fdata-sections>
139+ $<$<AND :$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU>>:-s>
140+ $<$<AND :$<BOOL :${EXECUTORCH_OPTIMIZE_SIZE} >,$<CONFIG:Release>>:-Os>
141+ $<$<AND :$<NOT :$<BOOL :${EXECUTORCH_OPTIMIZE_SIZE} >>,$<CONFIG:Release>>:-O2>
141142)
142- if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
143- set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s" )
144- endif ()
145143
146- if (EXECUTORCH_OPTIMIZE_SIZE)
147- # -Os: Optimize for size.
148- set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os" )
149- else ()
150- # -O2: Moderate opt.
151- set (CMAKE_CXX_FLAGS_RELEASE "-O2 ${CMAKE_CXX_FLAGS_RELEASE} " )
144+ # Modern code coverage support
145+ if (EXECUTORCH_USE_CPP_CODE_COVERAGE)
146+ target_compile_options (executorch_compile_options INTERFACE
147+ $<$<CXX_COMPILER_ID:GNU>:--coverage -fprofile-abs-path >
148+ $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fprofile-instr-generate -fcoverage-mapping>
149+ )
150+ target_link_options (executorch_compile_options INTERFACE
151+ $<$<CXX_COMPILER_ID:GNU>:--coverage>
152+ $<$<CXX_COMPILER_ID:Clang,AppleClang>:-fprofile-instr-generate>
153+ )
154+
155+ # Verify compiler support
156+ if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang|AppleClang" )
157+ message (FATAL_ERROR
158+ "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported"
159+ )
160+ endif ()
152161endif ()
153162
154163if (EXECUTORCH_BUILD_TESTS)
@@ -233,36 +242,14 @@ if(EXECUTORCH_BUILD_PTHREADPOOL)
233242 set (PTHREADPOOL_SYNC_PRIMITIVE
234243 "condvar"
235244 CACHE STRING ""
236- )
245+ )
237246 endif ()
238247 add_subdirectory ("${PTHREADPOOL_SOURCE_DIR} " )
239248 set (CMAKE_POSITION_INDEPENDENT_CODE
240249 ${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG}
241250 )
242251endif ()
243252
244- # TODO(dbort): Fix these warnings and remove this flag.
245- set (_common_compile_options -Wno-deprecated-declarations -fPIC)
246-
247- # Let files say "include <executorch/path/to/header.h>".
248- # TODO(#6475): This requires/assumes that the repo lives in a directory named
249- # exactly `executorch`. Check the assumption first. Remove this check once we
250- # stop relying on the assumption.
251- cmake_path(GET CMAKE_CURRENT_SOURCE_DIR FILENAME _repo_dir_name)
252- if (NOT "${_repo_dir_name} " STREQUAL "executorch" )
253- message (
254- FATAL_ERROR
255- "The ExecuTorch repo must be cloned into a directory named exactly "
256- "`executorch`; found `${_repo_dir_name} `. See "
257- "https://github.com/pytorch/executorch/issues/6475 for progress on a "
258- "fix for this restriction."
259- )
260- endif ()
261- set (_common_include_directories
262- ${CMAKE_CURRENT_SOURCE_DIR} /..
263- ${CMAKE_CURRENT_SOURCE_DIR} /runtime/core/portable_type/c10
264- )
265-
266253#
267254# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
268255#
@@ -299,23 +286,6 @@ else()
299286 set (CMAKE_TOOLCHAIN_ANDROID OFF )
300287endif ()
301288
302- # Add code coverage flags to supported compilers
303- if (EXECUTORCH_USE_CPP_CODE_COVERAGE)
304- if ("${CMAKE_CXX_COMPILER_ID} " STREQUAL "GNU" )
305- string (APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path" )
306- string (APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path" )
307- elseif ("${CMAKE_CXX_COMPILER_ID} " MATCHES "Clang" )
308- string (APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping" )
309- string (APPEND CMAKE_CXX_FLAGS
310- " -fprofile-instr-generate -fcoverage-mapping"
311- )
312- else ()
313- message (FATAL_ERROR
314- "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported"
315- )
316- endif ()
317- endif ()
318-
319289#
320290# program_schema: Generated .h files from schema/*.fbs inputs
321291#
@@ -342,25 +312,37 @@ add_library(executorch_core ${_executorch_core__srcs})
342312# Legacy name alias.
343313add_library (executorch_no_prim_ops ALIAS executorch_core)
344314
345- target_link_libraries (executorch_core PRIVATE program_schema)
315+ # Modern target configuration
316+ target_link_libraries (executorch_core
317+ PRIVATE
318+ program_schema
319+ executorch_compile_options
320+ PUBLIC
321+ executorch_base_config
322+ )
323+
324+ # Platform-specific dependencies using modern CMake
346325if (ANDROID)
347- target_link_libraries (executorch_core PUBLIC log )
326+ find_library (LOG_LIBRARY log REQUIRED)
327+ target_link_libraries (executorch_core PUBLIC ${LOG_LIBRARY} )
348328endif ()
329+
349330if (EXECUTORCH_USE_DL)
350- # Check if dl exists for this toolchain and only then link it.
351- find_library (DL_LIBRARY_EXISTS NAMES dl)
352- # Check if the library was found
353- if (DL_LIBRARY_EXISTS)
354- target_link_libraries (executorch_core PRIVATE dl) # For dladdr()
331+ find_library (DL_LIBRARY dl)
332+ if (DL_LIBRARY)
333+ target_link_libraries (executorch_core PRIVATE ${DL_LIBRARY} )
355334 endif ()
356335endif ()
357- target_include_directories (
358- executorch_core PUBLIC ${_common_include_directories}
359- )
360- target_compile_definitions (
361- executorch_core PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
336+
337+ # Modern include directory management
338+ target_include_directories (executorch_core
339+ PUBLIC
340+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
341+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /runtime/core/portable_type/c10>
342+ $<INSTALL_INTERFACE:include >
343+ $<INSTALL_INTERFACE:include /executorch/runtime/core/portable_type/c10>
362344)
363- target_compile_options (executorch_core PUBLIC ${_common_compile_options} )
345+
364346if (MAX_KERNEL_NUM)
365347 target_compile_definitions (
366348 executorch_core PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM}
@@ -370,20 +352,26 @@ endif()
370352if (EXECUTORCH_BUILD_PYBIND AND APPLE )
371353 # shared version
372354 add_library (executorch_core_shared SHARED ${_executorch_core__srcs} )
373- target_link_libraries (executorch_core_shared PRIVATE program_schema)
374- if (DL_LIBRARY_EXISTS)
375- # For dladdr()
376- target_link_libraries (executorch_core_shared PRIVATE dl)
377- endif ()
378- target_include_directories (
379- executorch_core_shared PUBLIC ${_common_include_directories}
355+ target_link_libraries (executorch_core_shared
356+ PRIVATE
357+ program_schema
358+ executorch_compile_options
359+ PUBLIC
360+ executorch_base_config
380361 )
381- target_compile_definitions (
382- executorch_core_shared PUBLIC C10_USING_CUSTOM_GENERATED_MACROS
383- )
384- target_compile_options (
385- executorch_core_shared PUBLIC ${_common_compile_options}
362+
363+ if (DL_LIBRARY)
364+ target_link_libraries (executorch_core_shared PRIVATE ${DL_LIBRARY} )
365+ endif ()
366+
367+ target_include_directories (executorch_core_shared
368+ PUBLIC
369+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
370+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /runtime/core/portable_type/c10>
371+ $<INSTALL_INTERFACE:include >
372+ $<INSTALL_INTERFACE:include /executorch/runtime/core/portable_type/c10>
386373 )
374+
387375 if (MAX_KERNEL_NUM)
388376 target_compile_definitions (
389377 executorch_core_shared PRIVATE MAX_KERNEL_NUM=${MAX_KERNEL_NUM}
@@ -399,10 +387,18 @@ endif()
399387# any backends.
400388#
401389add_library (executorch ${_executorch__srcs} )
402- target_link_libraries (executorch PRIVATE executorch_core)
403- target_include_directories (executorch PUBLIC ${_common_include_directories} )
404- target_compile_definitions (executorch PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
405- target_compile_options (executorch PUBLIC ${_common_compile_options} )
390+ target_link_libraries (executorch
391+ PRIVATE
392+ executorch_core
393+ executorch_compile_options
394+ PUBLIC
395+ executorch_base_config
396+ )
397+ target_include_directories (executorch
398+ PUBLIC
399+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
400+ $<INSTALL_INTERFACE:include >
401+ )
406402target_link_options_shared_lib(executorch)
407403
408404#
@@ -430,51 +426,71 @@ endif()
430426
431427add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /configurations )
432428
433- # Install `executorch` library as well as `executorch-config.cmake` under
434- # ${CMAKE_INSTALL_PREFIX}/
429+ # Modern installation configuration
435430install (
436431 DIRECTORY runtime/core/
437- DESTINATION include /executorch/runtime/core
432+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/runtime/core
438433 FILES_MATCHING
439434 PATTERN "*.h"
440435)
441436install (
442437 DIRECTORY runtime/kernel/
443- DESTINATION include /executorch/runtime/kernel
438+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/runtime/kernel
444439 FILES_MATCHING
445440 PATTERN "*.h"
446441)
447442install (
448443 DIRECTORY runtime/platform/
449- DESTINATION include /executorch/runtime/platform
444+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/runtime/platform
450445 FILES_MATCHING
451446 PATTERN "*.h"
452447)
453448install (
454449 DIRECTORY extension/kernel_util/
455- DESTINATION include /executorch/extension/kernel_util
450+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/extension/kernel_util
456451 FILES_MATCHING
457452 PATTERN "*.h"
458453)
459454install (
460455 DIRECTORY extension/tensor/
461- DESTINATION include /executorch/extension/tensor
456+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/extension/tensor
462457 FILES_MATCHING
463458 PATTERN "*.h"
464459)
465460install (
466461 DIRECTORY extension/threadpool/
467- DESTINATION include /executorch/extension/threadpool
462+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /executorch/extension/threadpool
468463 FILES_MATCHING
469464 PATTERN "*.h"
470465)
466+
467+ # Modern target installation with export
471468install (
472- TARGETS executorch executorch_core
473- INCLUDES
474- DESTINATION ${_common_include_directories}
469+ TARGETS executorch executorch_core executorch_base_config executorch_compile_options
470+ EXPORT ExecutorchTargets
471+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
472+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
473+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
474+ INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
475475)
476- install (FILES tools/cmake/executorch-config.cmake
477- DESTINATION lib/cmake/ExecuTorch
476+
477+ install (
478+ EXPORT ExecutorchTargets
479+ FILE ExecutorchTargets.cmake
480+ NAMESPACE Executorch::
481+ DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/Executorch
482+ )
483+
484+ # Configure package config file
485+ configure_package_config_file(
486+ ${CMAKE_CURRENT_SOURCE_DIR} /tools/cmake/executorch-config.cmake
487+ ${CMAKE_CURRENT_BINARY_DIR} /executorch-config.cmake
488+ INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/Executorch
489+ )
490+
491+ install (
492+ FILES ${CMAKE_CURRENT_BINARY_DIR} /executorch-config.cmake
493+ DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/Executorch
478494)
479495
480496if (EXECUTORCH_BUILD_ARM_BAREMETAL)
@@ -622,7 +638,9 @@ if(EXECUTORCH_BUILD_PYBIND)
622638 ${CMAKE_CURRENT_SOURCE_DIR} /extension/aten_util/aten_bridge.cpp
623639 )
624640 target_include_directories (
625- util PUBLIC ${_common_include_directories} ${TORCH_INCLUDE_DIRS}
641+ util PUBLIC ${TORCH_INCLUDE_DIRS}
642+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} >
643+ $<INSTALL_INTERFACE:include >
626644 )
627645 target_compile_definitions (util PUBLIC C10_USING_CUSTOM_GENERATED_MACROS)
628646
@@ -694,19 +712,17 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)
694712 endif ()
695713
696714 add_executable (executor_runner ${_executor_runner__srcs} )
697- if (CMAKE_BUILD_TYPE STREQUAL "Release" )
698- if (APPLE )
699- target_link_options (executor_runner PRIVATE "LINKER:-dead_strip" )
700- else ()
701- target_link_options (executor_runner PRIVATE "LINKER:--gc-sections" )
702- endif ()
703- endif ()
704- target_link_libraries (executor_runner ${_executor_runner_libs} )
705- target_compile_options (executor_runner PUBLIC ${_common_compile_options} )
715+ target_link_libraries (executor_runner PRIVATE ${_executor_runner_libs} executorch_compile_options)
716+
717+ # Modern link options using generator expressions
718+ target_link_options (executor_runner PRIVATE
719+ $<$<AND :$<CONFIG:Release>,$<PLATFORM_ID:Darwin>>:LINKER:-dead_strip>
720+ $<$<AND :$<CONFIG:Release>,$<NOT :$<PLATFORM_ID:Darwin>>>:LINKER:--gc-sections>
721+ )
706722endif ()
707723
708724if (EXECUTORCH_BUILD_VULKAN)
709725 add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /backends/vulkan)
710726endif ()
711727
712- include (Test .cmake)
728+ include (Test .cmake)
0 commit comments