Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit edde894

Browse files
tejlmandnashif
authored andcommitted
cmake: Zephyr CMake package clean-up and minor fix
Fixes: zephyrproject-rtos#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 zephyrproject-rtos#27375. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent c67a0cd commit edde894

File tree

8 files changed

+91
-131
lines changed

8 files changed

+91
-131
lines changed

doc/guides/zephyr_cmake_package.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ Zephyr CMake package is exported to the CMake user package registry using the fo
5858

5959
.. code-block:: bash
6060
61-
cmake -S <PATH-TO-ZEPHYR>/share/zephyr-package/cmake -B <PATH-TO-ZEPHYR>/share/zephyr-package/cmake
62-
cmake --build <PATH-TO-ZEPHYR>/share/zephyr-package/cmake --target pristine
61+
cmake -P <PATH-TO-ZEPHYR>/share/zephyr-package/cmake/zephyr_export.cmake
62+
63+
This will export the current Zephyr to the CMake user package registry.
64+
65+
To also export the Zephyr Unittest CMake package, run the following command in addition:
66+
67+
.. code-block:: bash
68+
69+
cmake -P <PATH-TO-ZEPHYR>/share/zephyrunittest-package/cmake/zephyr_export.cmake
6370
64-
This will export the current Zephyr to the CMake user package registry and remove the temporary
65-
files generated by CMake during export.
6671
6772
.. _zephyr_cmake_package_zephyr_base:
6873

scripts/west_commands/export.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import argparse
66
from pathlib import Path
77
from shutil import rmtree
8-
from subprocess import CalledProcessError
98

109
from west.commands import WestCommand
1110
from west import log
@@ -45,35 +44,20 @@ def do_run(self, args, unknown_args):
4544
# The 'share' subdirectory of the top level zephyr repository.
4645
share = Path(__file__).parents[2] / 'share'
4746

48-
run_cmake_and_clean_up(share / 'zephyr-package' / 'cmake')
49-
run_cmake_and_clean_up(share / 'zephyrunittest-package' / 'cmake')
47+
run_cmake_export(share / 'zephyr-package' / 'cmake')
48+
run_cmake_export(share / 'zephyrunittest-package' / 'cmake')
5049

51-
def run_cmake_and_clean_up(path):
52-
# Run a package installation script, cleaning up afterwards.
50+
def run_cmake_export(path):
51+
# Run a package installation script.
5352
#
5453
# Filtering out lines that start with -- ignores the normal
5554
# CMake status messages and instead only prints the important
5655
# information.
5756

58-
try:
59-
lines = run_cmake(['-S', str(path), '-B', str(path)],
60-
capture_output=True)
61-
finally:
62-
msg = [line for line in lines if not line.startswith('-- ')]
63-
log.inf('\n'.join(msg))
64-
clean_up(path)
65-
66-
def clean_up(path):
67-
try:
68-
run_cmake(['-P', str(path / 'pristine.cmake')],
69-
capture_output=True)
70-
except CalledProcessError:
71-
# Do our best to clean up even though CMake failed.
72-
log.wrn(f'Failed to make {path} pristine; '
73-
'removing known generated files...')
74-
for subpath in ['CMakeCache.txt', 'CMakeFiles', 'build.ninja',
75-
'cmake_install.cmake', 'rules.ninja']:
76-
remove_if_exists(Path(path) / subpath)
57+
lines = run_cmake(['-P', str(path / 'zephyr_export.cmake')],
58+
capture_output=True)
59+
msg = [line for line in lines if not line.startswith('-- ')]
60+
log.inf('\n'.join(msg))
7761

7862
def remove_if_exists(pathobj):
7963
if pathobj.is_file():

share/zephyr-package/cmake/CMakeLists.txt

Lines changed: 0 additions & 30 deletions
This file was deleted.

share/zephyr-package/cmake/pristine.cmake

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# Purpose of this CMake file is to install a ZephyrConfig package reference in:
4+
# Unix/Linux/MacOS: ~/.cmake/packages/Zephyr
5+
# Windows : HKEY_CURRENT_USER
6+
#
7+
# Having ZephyrConfig package allows for find_package(Zephyr) to work when ZEPHYR_BASE is not defined.
8+
#
9+
# Create the reference by running `cmake -P zephyr_export.cmake` in this directory.
10+
11+
set(MD5_INFILE "current_path.txt")
12+
13+
# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation.
14+
# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry.
15+
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR})
16+
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE}
17+
OUTPUT_VARIABLE MD5_SUM
18+
)
19+
string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM)
20+
if(WIN32)
21+
execute_process(COMMAND ${CMAKE_COMMAND}
22+
-E write_regv
23+
"HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}"
24+
)
25+
else()
26+
file(WRITE $ENV{HOME}/.cmake/packages/Zephyr/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR})
27+
endif()
28+
29+
message("Zephyr (${CMAKE_CURRENT_LIST_DIR})")
30+
message("has been added to the user package registry in:")
31+
if(WIN32)
32+
message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\Zephyr\n")
33+
else()
34+
message("~/.cmake/packages/Zephyr\n")
35+
endif()
36+
37+
file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/ZephyrPackagePath.txt)

share/zephyrunittest-package/cmake/CMakeLists.txt

Lines changed: 0 additions & 30 deletions
This file was deleted.

share/zephyrunittest-package/cmake/pristine.cmake

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# Purpose of this CMake file is to install a ZephyrUnittestConfig package reference in:
4+
# Unix/Linux/MacOS: ~/.cmake/packages/ZephyrUnittest
5+
# Windows : HKEY_CURRENT_USER
6+
#
7+
# Having ZephyrUnittestConfig package allows for find_package(ZephyrUnittest) to work when ZEPHYR_BASE is not defined.
8+
#
9+
# Create the reference by running `cmake -P zephyr_export.cmake` in this directory.
10+
11+
set(MD5_INFILE "current_path.txt")
12+
13+
# We write CMAKE_CURRENT_LIST_DIR into MD5_INFILE, as the content of that file will be used for MD5 calculation.
14+
# This means we effectively get the MD5 of CMAKE_CURRENT_LIST_DIR which must be used for CMake user package registry.
15+
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE} ${CMAKE_CURRENT_LIST_DIR})
16+
execute_process(COMMAND ${CMAKE_COMMAND} -E md5sum ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE}
17+
OUTPUT_VARIABLE MD5_SUM
18+
)
19+
string(SUBSTRING ${MD5_SUM} 0 32 MD5_SUM)
20+
if(WIN32)
21+
execute_process(COMMAND ${CMAKE_COMMAND}
22+
-E write_regv
23+
"HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\;${MD5_SUM}" "${CMAKE_CURRENT_LIST_DIR}"
24+
)
25+
else()
26+
file(WRITE $ENV{HOME}/.cmake/packages/ZephyrUnittest/${MD5_SUM} ${CMAKE_CURRENT_LIST_DIR})
27+
endif()
28+
29+
message("ZephyrUnittest (${CMAKE_CURRENT_LIST_DIR})")
30+
message("has been added to the user package registry in:")
31+
if(WIN32)
32+
message("HKEY_CURRENT_USER\\Software\\Kitware\\CMake\\Packages\\ZephyrUnittest\n")
33+
else()
34+
message("~/.cmake/packages/ZephyrUnittest\n")
35+
endif()
36+
37+
file(REMOVE ${CMAKE_CURRENT_LIST_DIR}/${MD5_INFILE})

0 commit comments

Comments
 (0)