zephyr/cmake/modules/user_cache.cmake
Torsten Rasmussen 61453e4a58 cmake: Zephyr CMake package and CMake modules
Create a cmake/modules folder containing all Zephyr CMake modules.
All Zephyr cmake files that are included from boilerplate are now
converted into CMake modules which can be individually loaded.

The Zephyr CMake package is updated to support loading of individual
CMake modules using the COMPONENTS argument to `find_package(Zephyr)`.
If the COMPONENTS argument is not specified, the default Zephyr build
system will load.
If COMPONENTS is specified then, only those components and the
dependencies will be loaded.

If a Zephyr CMake module depends on another CMake module which has not
been loaded, it will automatically be loaded.

This allows us to modularize and reuse individual parts of the Zephyr
CMake build system in a more flexible way in future.

Such usage could be:
- Higher livel multi image build system
- Invocation of individual components, for example dts processing by
  twister without loading all build code
- Doc build
- Unittesting

With this new CMake package and CMake module scheme then direct
sourcing of boilerplate.cmake has been deprecated.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2022-02-22 10:02:39 -08:00

99 lines
3 KiB
CMake

# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2021, Nordic Semiconductor ASA
# Configure user cache directory.
#
# The user cache can be used for caching of data that should be persistent
# across builds to speed up CMake configure / build system generation and/or
# compilation.
#
# Only data that can be safely re-generated should be placed in this cache.
#
# Zephyr build system uses this user cache to store Zephyr compiler check
# results which significantly improve toolchain testing performance.
# See https://github.com/zephyrproject-rtos/zephyr/pull/7102 for details.
#
# Outcome:
# The following variables will be defined when this CMake module completes:
#
# - USER_CACHE_DIR: User cache directory in use.
#
# If the above variable is already set when this CMake module is loaded,
# then no changes to the variable will happen.
#
# Variables set by this module and not mentioned above are considered internal
# use only and may be removed, renamed, or re-purposed without prior notice.
include_guard(GLOBAL)
include(python)
function(find_appropriate_cache_directory dir)
set(env_suffix_LOCALAPPDATA .cache)
if(CMAKE_HOST_APPLE)
# On macOS, ~/Library/Caches is the preferred cache directory.
set(env_suffix_HOME Library/Caches)
else()
set(env_suffix_HOME .cache)
endif()
# Determine which env vars should be checked
if(CMAKE_HOST_APPLE)
set(dirs HOME)
elseif(CMAKE_HOST_WIN32)
set(dirs LOCALAPPDATA)
else()
# Assume Linux when we did not detect 'mac' or 'win'
#
# On Linux, freedesktop.org recommends using $XDG_CACHE_HOME if
# that is defined and defaulting to $HOME/.cache otherwise.
set(dirs
XDG_CACHE_HOME
HOME
)
endif()
foreach(env_var ${dirs})
if(DEFINED ENV{${env_var}})
set(env_dir $ENV{${env_var}})
set(test_user_dir ${env_dir}/${env_suffix_${env_var}})
execute_process(COMMAND ${PYTHON_EXECUTABLE}
${ZEPHYR_BASE}/scripts/dir_is_writeable.py ${test_user_dir}
RESULT_VARIABLE writable_result
)
if("${writable_result}" STREQUAL "0")
# The directory is write-able
set(user_dir ${test_user_dir})
break()
else()
# The directory was not writeable, keep looking for a suitable
# directory
endif()
endif()
endforeach()
# Populate local_dir with a suitable directory for caching
# files. Prefer a directory outside of the git repository because it
# is good practice to have clean git repositories.
if(DEFINED user_dir)
# Zephyr's cache files go in the "zephyr" subdirectory of the
# user's cache directory.
set(local_dir ${user_dir}/zephyr)
else()
set(local_dir ${ZEPHYR_BASE}/.cache)
endif()
set(${dir} ${local_dir} PARENT_SCOPE)
endfunction()
# Populate USER_CACHE_DIR with a directory that user applications may
# write cache files to.
if(NOT DEFINED USER_CACHE_DIR)
find_appropriate_cache_directory(USER_CACHE_DIR)
endif()
message(STATUS "Cache files will be written to: ${USER_CACHE_DIR}")