cmake: cleanup search for devicetree compiler, dtc

Cleanup CMake code by moving devicetree compiler detection to dedicated
FindDtc.cmake module.

This allows us to re-use existing CMake find_package() functionality in
module mode and thus reduce CMake logic.

It further has the benefit of being able to directly specify dtc version
like this:
> find_package(Dtc 1.4.6)

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2022-08-22 13:08:37 +02:00 committed by Carles Cufí
parent 9a12f8b68b
commit 8f8f6cfde3
3 changed files with 51 additions and 37 deletions

View file

@ -0,0 +1,50 @@
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (c) 2022, Nordic Semiconductor ASA
# FindDTC module for locating devicetree compiler, DTC.
#
# The module defines the following variables:
#
# 'DTC'
# Path to devicetree compiler, dtc.
# Set to 'DTC-NOTFOUND' if dtc was not found.
#
# 'Dtc_FOUND', 'DTC_FOUND'
# True if the devicetree compiler, dtc, was found.
#
# 'DTC_VERSION_STRING'
# The version of devicetree compiler, dtc.
find_program(
DTC
dtc
)
if(DTC)
# Parse the 'dtc --version' output to find the installed version.
execute_process(
COMMAND
${DTC} --version
OUTPUT_VARIABLE dtc_version_output
ERROR_VARIABLE dtc_error_output
RESULT_VARIABLE dtc_status
)
set(DTC_VERSION_STRING)
if(${dtc_status} EQUAL 0)
string(REGEX MATCH "Version: DTC v?([0-9]+[.][0-9]+[.][0-9]+).*" out_var ${dtc_version_output})
set(DTC_VERSION_STRING ${CMAKE_MATCH_1})
endif()
endif()
find_package_handle_standard_args(Dtc
REQUIRED_VARS DTC
VERSION_VAR DTC_VERSION_STRING
)
if(NOT Dtc_FOUND)
# DTC was found but version requirement is not met, or dtc was not working.
# Treat it as DTC was never found by resetting the result from `find_program()`
set(DTC DTC-NOTFOUND CACHE FILEPATH "Path to a program" FORCE)
endif()

View file

@ -6,6 +6,7 @@ include(extensions)
include(python) include(python)
include(boards) include(boards)
include(generic_toolchain) include(generic_toolchain)
find_package(Dtc 1.4.6)
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated)

View file

@ -6,43 +6,6 @@ if(ZEPHYR_SDK_HOST_TOOLS)
include(${ZEPHYR_BASE}/cmake/toolchain/zephyr/host-tools.cmake) include(${ZEPHYR_BASE}/cmake/toolchain/zephyr/host-tools.cmake)
endif() endif()
# dtc is an optional dependency
find_program(
DTC
dtc
)
if(DTC)
# Parse the 'dtc --version' output to find the installed version.
set(MIN_DTC_VERSION 1.4.6)
execute_process(
COMMAND
${DTC} --version
OUTPUT_VARIABLE dtc_version_output
ERROR_VARIABLE dtc_error_output
RESULT_VARIABLE dtc_status
)
if(${dtc_status} EQUAL 0)
string(REGEX MATCH "Version: DTC v?([0-9]+[.][0-9]+[.][0-9]+).*" out_var ${dtc_version_output})
# Since it is optional, an outdated version is not an error. If an
# outdated version is discovered, print a warning and proceed as if
# DTC were not installed.
if(${CMAKE_MATCH_1} VERSION_GREATER ${MIN_DTC_VERSION})
message(STATUS "Found dtc: ${DTC} (found suitable version \"${CMAKE_MATCH_1}\", minimum required is \"${MIN_DTC_VERSION}\")")
else()
message(WARNING
"Could NOT find dtc: Found unsuitable version \"${CMAKE_MATCH_1}\", but required is at least \"${MIN_DTC_VERSION}\" (found ${DTC}). Optional devicetree error checking with dtc will not be performed.")
set(DTC DTC-NOTFOUND)
endif()
else()
message(WARNING
"Could NOT find working dtc: Found dtc (${DTC}), but failed to load with:\n ${dtc_error_output}")
set(DTC DTC-NOTFOUND)
endif()
endif()
# gperf is an optional dependency # gperf is an optional dependency
find_program( find_program(
GPERF GPERF