From 81007172a4795ac97715f1eb421fbd2e78b53981 Mon Sep 17 00:00:00 2001 From: Torsten Rasmussen Date: Wed, 12 Feb 2020 14:55:36 +0100 Subject: [PATCH] cmake: adding ZephyrConfig.cmake to allow an easy way to locate Zephyr Adding ZephyrConfig.cmake and ZephyrConfigVersion.cmake allows projects to use find_package to locate Zephyr. This means that it will be possible to allow users to run CMake without the need to source zephyr-env.sh or run zephyr-env.cmd. This is especially useful for IDEs such as Eclipse or SES, where it will no longer be required to source the above files before launching the IDE. Signed-off-by: Torsten Rasmussen --- CODEOWNERS | 1 + share/zephyr-package/cmake/CMakeLists.txt | 27 +++++++++++ share/zephyr-package/cmake/ZephyrConfig.cmake | 30 ++++++++++++ .../cmake/ZephyrConfigVersion.cmake | 47 +++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 share/zephyr-package/cmake/CMakeLists.txt create mode 100644 share/zephyr-package/cmake/ZephyrConfig.cmake create mode 100644 share/zephyr-package/cmake/ZephyrConfigVersion.cmake diff --git a/CODEOWNERS b/CODEOWNERS index 99501cba1c0..c6220adb316 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -404,6 +404,7 @@ /scripts/west-commands.yml @mbolivar-nordic /scripts/zephyr_module.py @tejlmand /scripts/valgrind.supp @aescolar @daor-oti +/share/zephyr-package/ @tejlmand /subsys/bluetooth/ @joerchan @jhedberg @Vudentz /subsys/bluetooth/controller/ @carlescufi @cvinayak @thoh-ot /subsys/bluetooth/mesh/ @jhedberg @trond-snekvik @joerchan @Vudentz diff --git a/share/zephyr-package/cmake/CMakeLists.txt b/share/zephyr-package/cmake/CMakeLists.txt new file mode 100644 index 00000000000..911b8e53c42 --- /dev/null +++ b/share/zephyr-package/cmake/CMakeLists.txt @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Purpose of this CMake file is to install a ZephyrConfig package reference in: +# Unix/Linux/MacOS: ~/.cmake/packages/Zephyr +# Windows : HKEY_CURRENT_USER +# +# Having ZephyrConfig package allows for find_package(Zephyr) to work when ZEPHYR_BASE is not defined. +# +# Create the reference by running `cmake .` in this directory. + +cmake_minimum_required(VERSION 3.13.1) +project(ZephyrPackageConfig NONE) + +if(NOT (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_LIST_DIR)) + message(WARNING "\$\{CMAKE_BINARY_DIR\} is different from \$\{CMAKE_CURRENT_LIST_DIR\}, Zephyr config package may not work as expected.") +endif() + +message("Zephyr (${CMAKE_CURRENT_LIST_DIR})") +message("has been added to the user package registry in:") +if(WIN32) + message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr") +else() + message("~/.cmake/packages/Zephyr") +endif() + +export(PACKAGE Zephyr) +export(PACKAGE ZephyrUnittest) diff --git a/share/zephyr-package/cmake/ZephyrConfig.cmake b/share/zephyr-package/cmake/ZephyrConfig.cmake new file mode 100644 index 00000000000..71df4d52c4f --- /dev/null +++ b/share/zephyr-package/cmake/ZephyrConfig.cmake @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: Apache-2.0 + +# This file provides Zephyr Config Package functionality. +# +# The purpose of this files is to allow users to decide if they want to: +# - Use ZEPHYR_BASE environment setting for explicitly set select a zephyr installation +# - Support automatic Zephyr installation lookup through the use of find_package(ZEPHYR) + +# First check to see if user has provided a Zephyr base manually. +# Set Zephyr base to environment setting. +# It will be empty if not set in environment. +set(ZEPHYR_BASE $ENV{ZEPHYR_BASE}) + +# Find out the current Zephyr base. +get_filename_component(CURRENT_ZEPHYR_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) + +if (ZEPHYR_BASE) + # Get rid of any double folder string before comparison, as example, user provides + # ZEPHYR_BASE=//path/to//zephyr_base/ + # must also work. + get_filename_component(ZEPHYR_BASE ${ZEPHYR_BASE} ABSOLUTE) +else() + # Zephyr base is not set in environment but currently used Zephyr is located within the same tree + # as the caller (sample/test/application) code of find_package(Zephyr). + # Thus we set a Zephyr base for looking up boilerplate.cmake faster. + set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR}) +endif() + +message("Including boilerplate: ${ZEPHYR_BASE}/cmake/app/boilerplate.cmake") +include(${ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) diff --git a/share/zephyr-package/cmake/ZephyrConfigVersion.cmake b/share/zephyr-package/cmake/ZephyrConfigVersion.cmake new file mode 100644 index 00000000000..aca4713272d --- /dev/null +++ b/share/zephyr-package/cmake/ZephyrConfigVersion.cmake @@ -0,0 +1,47 @@ +# SPDX-License-Identifier: Apache-2.0 + +# This file provides Zephyr Config Package version information. +# +# The purpose of the version file is to ensure that CMake find_package can correctly locate a +# usable Zephyr installation for building of applications. + +# First check to see if user has provided a Zephyr base manually. +set(ZEPHYR_BASE $ENV{ZEPHYR_BASE}) + +if (ZEPHYR_BASE) + # ZEPHYR_BASE was set in environment, meaning the package version must be ignored and the Zephyr + # pointed to by ZEPHYR_BASE is to be used regardless of version + + # Get rid of any double folder string before comparison, as example, user provides + # ZEPHYR_BASE=//path/to//zephyr_base/ + # must also work. + get_filename_component(ZEPHYR_BASE ${ZEPHYR_BASE} ABSOLUTE) + if (${ZEPHYR_BASE}/zephyr-package/cmake STREQUAL ${CMAKE_CURRENT_LIST_DIR}) + # We are the Zephyr to be used + set(PACKAGE_VERSION_COMPATIBLE TRUE) + set(PACKAGE_VERSION_EXACT TRUE) + else() + # User has pointed to a different Zephyr installation, so don't use this version + set(PACKAGE_VERSION_COMPATIBLE FALSE) + endif() + return() +endif() + +# Temporary set local Zephyr base to allow using version.cmake to find this Zephyr tree current version +set(ZEPHYR_BASE ${CMAKE_CURRENT_LIST_DIR}/../../..) +include(${ZEPHYR_BASE}/cmake/version.cmake) +set(ZEPHYR_BASE) + +# Zephyr uses project version, but CMake package uses PACKAGE_VERSION +set(PACKAGE_VERSION ${PROJECT_VERSION}) +if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION) + set(PACKAGE_VERSION_COMPATIBLE FALSE) +else() + # Currently, this version is capable of handling on prior versions. + # In future, in case version 3.0.0 cannot be used for project requiring + # version 2.x.x, then add such check here. + set(PACKAGE_VERSION_COMPATIBLE TRUE) + if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION) + set(PACKAGE_VERSION_EXACT TRUE) + endif() +endif()