cmake: Zephyr CMake package clean-up and minor fix

Fixes: #27375

This is a cleanup of the Zephyr CMake package export.
The code has been simplified so that the export now happens through a
CMake script. This avoids several generated CMake build files compared
to previous export mode, and thus removes the need for a CMake pristine
script.

A benefit of this cleanup is that it also fixes #27375.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2020-08-06 10:41:39 +02:00 committed by Anas Nashif
parent c67a0cd098
commit edde894d04
8 changed files with 91 additions and 131 deletions

View file

@ -58,11 +58,16 @@ Zephyr CMake package is exported to the CMake user package registry using the fo
.. code-block:: bash
cmake -S <PATH-TO-ZEPHYR>/share/zephyr-package/cmake -B <PATH-TO-ZEPHYR>/share/zephyr-package/cmake
cmake --build <PATH-TO-ZEPHYR>/share/zephyr-package/cmake --target pristine
cmake -P <PATH-TO-ZEPHYR>/share/zephyr-package/cmake/zephyr_export.cmake
This will export the current Zephyr to the CMake user package registry.
To also export the Zephyr Unittest CMake package, run the following command in addition:
.. code-block:: bash
cmake -P <PATH-TO-ZEPHYR>/share/zephyrunittest-package/cmake/zephyr_export.cmake
This will export the current Zephyr to the CMake user package registry and remove the temporary
files generated by CMake during export.
.. _zephyr_cmake_package_zephyr_base:

View file

@ -5,7 +5,6 @@
import argparse
from pathlib import Path
from shutil import rmtree
from subprocess import CalledProcessError
from west.commands import WestCommand
from west import log
@ -45,35 +44,20 @@ class ZephyrExport(WestCommand):
# The 'share' subdirectory of the top level zephyr repository.
share = Path(__file__).parents[2] / 'share'
run_cmake_and_clean_up(share / 'zephyr-package' / 'cmake')
run_cmake_and_clean_up(share / 'zephyrunittest-package' / 'cmake')
run_cmake_export(share / 'zephyr-package' / 'cmake')
run_cmake_export(share / 'zephyrunittest-package' / 'cmake')
def run_cmake_and_clean_up(path):
# Run a package installation script, cleaning up afterwards.
def run_cmake_export(path):
# Run a package installation script.
#
# Filtering out lines that start with -- ignores the normal
# CMake status messages and instead only prints the important
# information.
try:
lines = run_cmake(['-S', str(path), '-B', str(path)],
capture_output=True)
finally:
msg = [line for line in lines if not line.startswith('-- ')]
log.inf('\n'.join(msg))
clean_up(path)
def clean_up(path):
try:
run_cmake(['-P', str(path / 'pristine.cmake')],
capture_output=True)
except CalledProcessError:
# Do our best to clean up even though CMake failed.
log.wrn(f'Failed to make {path} pristine; '
'removing known generated files...')
for subpath in ['CMakeCache.txt', 'CMakeFiles', 'build.ninja',
'cmake_install.cmake', 'rules.ninja']:
remove_if_exists(Path(path) / subpath)
lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')],
capture_output=True)
msg = [line for line in lines if not line.startswith('-- ')]
log.inf('\n'.join(msg))
def remove_if_exists(pathobj):
if pathobj.is_file():

View file

@ -1,30 +0,0 @@
# 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\n")
else()
message("~/.cmake/packages/Zephyr\n")
endif()
add_custom_target(pristine
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/pristine.cmake
)
export(PACKAGE Zephyr)

View file

@ -1,22 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# Purpose of this CMake file is to clean all CMake files generated by CMake when
# exporting Zephry to CMake user package registry.
# Get a list of all files.
file(GLOB_RECURSE GENERATED_FILES
LIST_DIRECTORIES true
${CMAKE_CURRENT_LIST_DIR}/*
)
# Remove the files that is used be Zephyr from the list.
list(REMOVE_ITEM GENERATED_FILES
"${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt"
"${CMAKE_CURRENT_LIST_DIR}/pristine.cmake"
"${CMAKE_CURRENT_LIST_DIR}/zephyr_package_search.cmake"
"${CMAKE_CURRENT_LIST_DIR}/ZephyrConfigVersion.cmake"
"${CMAKE_CURRENT_LIST_DIR}/ZephyrConfig.cmake"
)
# Delete everything else, as those files are created by CMake.
file(REMOVE_RECURSE ${GENERATED_FILES})

View file

@ -0,0 +1,37 @@
# 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 -P zephyr_export.cmake` in this directory.
set(MD5_INFILE "current_path.txt")
# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation.
# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry.
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE}
OUTPUT_VARIABLE MD5_SUM
)
string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM)
if(WIN32)
execute_process(COMMAND ${CMAKE_COMMAND}
-E write_regv
"HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}"
)
else()
file(WRITE $ENV{HOME}/.cmake/packages/Zephyr/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR})
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\n")
else()
message("~/.cmake/packages/Zephyr\n")
endif()
file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/ZephyrPackagePath.txt)

View file

@ -1,30 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# Purpose of this CMake file is to install a ZephyrUnittestConfig package reference in:
# Unix/Linux/MacOS: ~/.cmake/packages/ZephyrUnittest
# Windows : HKEY_CURRENT_USER
#
# Having ZephyrUnittestConfig package allows for find_package(ZephyrUnittest) 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(ZephyrUnittestPackageConfig NONE)
if(NOT (CMAKE_BINARY_DIR STREQUAL CMAKE_CURRENT_LIST_DIR))
message(WARNING "\$\{CMAKE_BINARY_DIR\} is different from \$\{CMAKE_CURRENT_LIST_DIR\}, ZephyrUnittest config package may not work as expected.")
endif()
message("ZephyrUnittest (${CMAKE_CURRENT_LIST_DIR})")
message("has been added to the user package registry in:")
if(WIN32)
message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\n")
else()
message("~/.cmake/packages/ZephyrUnittest\n")
endif()
add_custom_target(pristine
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_DIR}/pristine.cmake
)
export(PACKAGE ZephyrUnittest)

View file

@ -1,21 +0,0 @@
# SPDX-License-Identifier: Apache-2.0
# Purpose of this CMake file is to clean all CMake files generated by CMake when
# exporting Zephry to CMake user package registry.
# Get a list of all files.
file(GLOB_RECURSE GENERATED_FILES
LIST_DIRECTORIES true
${CMAKE_CURRENT_LIST_DIR}/*
)
# Remove the files that is used be Zephyr from the list.
list(REMOVE_ITEM GENERATED_FILES
"${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt"
"${CMAKE_CURRENT_LIST_DIR}/pristine.cmake"
"${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfigVersion.cmake"
"${CMAKE_CURRENT_LIST_DIR}/ZephyrUnittestConfig.cmake"
)
# Delete everything else, as those files are created by CMake.
file(REMOVE_RECURSE ${GENERATED_FILES})

View file

@ -0,0 +1,37 @@
# SPDX-License-Identifier: Apache-2.0
# Purpose of this CMake file is to install a ZephyrUnittestConfig package reference in:
# Unix/Linux/MacOS: ~/.cmake/packages/ZephyrUnittest
# Windows : HKEY_CURRENT_USER
#
# Having ZephyrUnittestConfig package allows for find_package(ZephyrUnittest) to work when ZEPHYR_BASE is not defined.
#
# Create the reference by running `cmake -P zephyr_export.cmake` in this directory.
set(MD5_INFILE "current_path.txt")
# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation.
# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry.
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE}
OUTPUT_VARIABLE MD5_SUM
)
string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM)
if(WIN32)
execute_process(COMMAND ${CMAKE_COMMAND}
-E write_regv
"HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}"
)
else()
file(WRITE $ENV{HOME}/.cmake/packages/ZephyrUnittest/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR})
endif()
message("ZephyrUnittest (${CMAKE_CURRENT_LIST_DIR})")
message("has been added to the user package registry in:")
if(WIN32)
message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\n")
else()
message("~/.cmake/packages/ZephyrUnittest\n")
endif()
file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE})