Commit 81007172 authored by Torsten Rasmussen's avatar Torsten Rasmussen Committed by Carles Cufi
Browse files

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: default avatarTorsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
parent 261636ce
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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
+27 −0
Original line number Diff line number Diff line
# 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)
+30 −0
Original line number Diff line number Diff line
# 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)
+47 −0
Original line number Diff line number Diff line
# 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()