From d2b11174f3f9c2b40d6049ed370bee1004c70e0a Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Thu, 16 Dec 2021 14:21:10 +0100 Subject: [PATCH] cmake: create a user_cache.cmake CMake module The user_cache.cmake CMake module remove boilerplate code and place it inside a dedicated user_cache.cmake CMake module. The user cache functions has been moved to the new Zephyr CMake module. This is part of a general CMake overhaul to allow better modularization and reuse of the Zephyr build system. Signed-off-by: Torsten Rasmussen --- cmake/app/boilerplate.cmake | 8 +--- cmake/extensions.cmake | 75 ----------------------------- cmake/user_cache.cmake | 94 +++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 82 deletions(-) create mode 100644 cmake/user_cache.cmake diff --git a/cmake/app/boilerplate.cmake b/cmake/app/boilerplate.cmake index ee39172ae6..d13de44bb2 100644 --- a/cmake/app/boilerplate.cmake +++ b/cmake/app/boilerplate.cmake @@ -269,13 +269,7 @@ alternate .overlay file using this parameter. These settings will override the \ settings in the board's .dts file. Multiple files may be listed, e.g. \ DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"") -# 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}") - +include(${ZEPHYR_BASE}/cmake/user_cache.cmake) include(${ZEPHYR_BASE}/cmake/verify-toolchain.cmake) include(${ZEPHYR_BASE}/cmake/host-tools.cmake) diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index 9800a7ddee..24f283010e 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -2062,81 +2062,6 @@ macro(assert_exists var) endmacro() # 3.5. File system management -function(check_if_directory_is_writeable dir ok) - execute_process( - COMMAND - ${PYTHON_EXECUTABLE} - ${ZEPHYR_BASE}/scripts/dir_is_writeable.py - ${dir} - RESULT_VARIABLE ret - ) - - if("${ret}" STREQUAL "0") - # The directory is write-able - set(${ok} 1 PARENT_SCOPE) - else() - set(${ok} 0 PARENT_SCOPE) - endif() -endfunction() - -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}}) - - check_if_directory_is_writeable(${test_user_dir} ok) - if(${ok}) - # 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() - function(generate_unique_target_name_from_filename filename target_name) get_filename_component(basename ${filename} NAME) string(REPLACE "." "_" x ${basename}) diff --git a/cmake/user_cache.cmake b/cmake/user_cache.cmake new file mode 100644 index 0000000000..8139e57e29 --- /dev/null +++ b/cmake/user_cache.cmake @@ -0,0 +1,94 @@ +# 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. + +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}")