diff --git a/cmake/modules/FindDtc.cmake b/cmake/modules/FindDtc.cmake new file mode 100644 index 00000000000..249fc2e9ca2 --- /dev/null +++ b/cmake/modules/FindDtc.cmake @@ -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() diff --git a/cmake/modules/dts.cmake b/cmake/modules/dts.cmake index e84bd0e2ee1..a641e0c68f8 100644 --- a/cmake/modules/dts.cmake +++ b/cmake/modules/dts.cmake @@ -6,6 +6,7 @@ include(extensions) include(python) include(boards) include(generic_toolchain) +find_package(Dtc 1.4.6) file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated) diff --git a/cmake/modules/host-tools.cmake b/cmake/modules/host-tools.cmake index abe153837cf..afacef77f0f 100644 --- a/cmake/modules/host-tools.cmake +++ b/cmake/modules/host-tools.cmake @@ -6,43 +6,6 @@ if(ZEPHYR_SDK_HOST_TOOLS) include(${ZEPHYR_BASE}/cmake/toolchain/zephyr/host-tools.cmake) 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 find_program( GPERF