Skip to content

Commit 029486e

Browse files
Add FindPythonPackage and use it to detect memap dependencies
1 parent 7135c65 commit 029486e

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ endfunction()
198198
# Parse toolchain generated map file of `target` and display a readable table format.
199199
#
200200
function(mbed_generate_map_file target)
201-
find_package (Python3)
202201
add_custom_command(
203202
TARGET
204203
${target}
@@ -234,7 +233,10 @@ endfunction()
234233
function(mbed_set_post_build target)
235234
mbed_validate_application_profile(${target})
236235
mbed_generate_bin_hex(${target})
237-
mbed_generate_map_file(${target})
236+
237+
if(HAVE_MEMAP_DEPS)
238+
mbed_generate_map_file(${target})
239+
endif()
238240
endfunction()
239241

240242
# Ninja requires to be forced for response files

tools/cmake/CheckPythonPackage.cmake

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright (c) 2020 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# CMake functions for checking for Python packages
5+
# Requires PYTHON_EXECUTABLE to be defined. Call FindPythonInterp first!
6+
7+
# NOTE: if moving this file, be sure to also move python_packagecheck.py
8+
9+
# must evaluate this now since CMAKE_CURRENT_LIST_DIR doesn't work in function scope
10+
set(PYTHON_PACKAGECHECK_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/python_packagecheck.py)
11+
12+
# set OUTPUT_VAR to whether PACKAGENAME was found
13+
function(check_python_package PACKAGENAME OUTPUT_VAR)
14+
15+
# can't have Python packages without Python!
16+
if(NOT Python3_FOUND)
17+
set(${OUTPUT_VAR} FALSE PARENT_SCOPE)
18+
return()
19+
endif()
20+
21+
set(NEED_TO_RUN_CHECK TRUE)
22+
23+
if(DEFINED ${OUTPUT_VAR})
24+
if(${OUTPUT_VAR})
25+
26+
# if the python interpreter changed, we need to recheck
27+
if("${PY_INTERP_FOR_${OUTPUT_VAR}}" STREQUAL "${Python3_EXECUTABLE}")
28+
set(NEED_TO_RUN_CHECK FALSE)
29+
endif()
30+
31+
endif()
32+
endif()
33+
34+
if(NEED_TO_RUN_CHECK)
35+
set(PY_INTERP_FOR_${OUTPUT_VAR} ${Python3_EXECUTABLE} CACHE INTERNAL "The python interpreter used to run the ${OUTPUT_VAR} check" FORCE)
36+
37+
execute_process(COMMAND ${Python3_EXECUTABLE} ${PYTHON_PACKAGECHECK_SCRIPT} ${PACKAGENAME}
38+
RESULT_VARIABLE PACKAGECHECK_RESULT)
39+
40+
if(${PACKAGECHECK_RESULT} EQUAL 0)
41+
set(HAVE_PACKAGE TRUE)
42+
else()
43+
set(HAVE_PACKAGE FALSE)
44+
endif()
45+
46+
if(HAVE_PACKAGE)
47+
message(STATUS "Checking for Python package ${PACKAGENAME} -- found")
48+
else()
49+
message(STATUS "Checking for Python package ${PACKAGENAME} -- not found")
50+
endif()
51+
52+
set(${OUTPUT_VAR} ${HAVE_PACKAGE} CACHE BOOL "Whether the Python package ${PACKAGENAME} was found" FORCE)
53+
mark_as_advanced(${OUTPUT_VAR})
54+
55+
endif()
56+
endfunction(check_python_package)
57+
58+
# check that PACKAGENAME can be imported, and print an error if not
59+
function(verify_python_package PACKAGENAME)
60+
61+
# we can just generate our own variable name
62+
string(TOUPPER "HAVE_${PACKAGENAME}" HAVE_VAR_NAME)
63+
64+
check_python_package(${PACKAGENAME} ${HAVE_VAR_NAME})
65+
66+
if(NOT ${HAVE_VAR_NAME})
67+
message(FATAL_ERROR "The required Python package ${PACKAGENAME} was not found in ${Python3_EXECUTABLE}. Please install it.")
68+
endif()
69+
endfunction(verify_python_package)

tools/cmake/app.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,16 @@ enable_language(C CXX ASM)
2323
# set executable suffix (has to be done after enabling languages)
2424
# Note: This is nice to have, but is also required because STM32Cube will only work on files with a .elf extension
2525
set(CMAKE_EXECUTABLE_SUFFIX .elf)
26+
27+
# Find Python and needed packages
28+
find_package(Python3)
29+
include(${CMAKE_CURRENT_LIST_DIR}/CheckPythonPackage.cmake)
30+
check_python_package(intelhex HAVE_INTELHEX)
31+
check_python_package(prettytable HAVE_PRETTYTABLE)
32+
33+
if(Python3_FOUND AND HAVE_INTELHEX AND HAVE_PRETTYTABLE)
34+
set(HAVE_MEMAP_DEPS TRUE)
35+
else()
36+
set(HAVE_MEMAP_DEPS FALSE)
37+
message(STATUS "Missing Python dependencies (python3, intelhex, prettytable) so the memory map cannot be printed")
38+
endif()

tools/cmake/python_packagecheck.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
mbed SDK
3+
Copyright (c) 2020 ARM Limited
4+
SPDX-License-Identifier: Apache-2.0
5+
"""
6+
7+
#file which is invoked by the cmake build system to check if all necessary python packages are installed.
8+
9+
import sys
10+
11+
try:
12+
__import__(sys.argv[1])
13+
except ImportError:
14+
exit(1)
15+
16+
exit(0)

0 commit comments

Comments
 (0)