Skip to content

Commit dd33463

Browse files
authored
Merge pull request #14205 from multiplemonomials/master
CMake: better detection of memap dependencies
2 parents d3eaca5 + 7b03aea commit dd33463

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ endfunction()
184184
# Parse toolchain generated map file of `target` and display a readable table format.
185185
#
186186
function(mbed_generate_map_file target)
187-
find_package (Python3)
188187
add_custom_command(
189188
TARGET
190189
${target}
@@ -220,7 +219,10 @@ endfunction()
220219
function(mbed_set_post_build target)
221220
mbed_validate_application_profile(${target})
222221
mbed_generate_bin_hex(${target})
223-
mbed_generate_map_file(${target})
222+
223+
if(HAVE_MEMAP_DEPS)
224+
mbed_generate_map_file(${target})
225+
endif()
224226
endfunction()
225227

226228
# Ninja requires to be forced for response files

tools/cmake/CheckPythonPackage.cmake

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 FindPython first!
6+
7+
# set OUTPUT_VAR to whether PACKAGENAME was found
8+
function(check_python_package PACKAGENAME OUTPUT_VAR)
9+
# can't have Python packages without Python!
10+
if(NOT Python3_FOUND)
11+
set(${OUTPUT_VAR} FALSE PARENT_SCOPE)
12+
return()
13+
endif()
14+
15+
set(NEED_TO_RUN_CHECK TRUE)
16+
17+
if(DEFINED ${OUTPUT_VAR})
18+
if(${OUTPUT_VAR})
19+
# if the python interpreter changed, we need to recheck
20+
if("${PY_INTERP_FOR_${OUTPUT_VAR}}" STREQUAL "${Python3_EXECUTABLE}")
21+
set(NEED_TO_RUN_CHECK FALSE)
22+
endif()
23+
endif()
24+
endif()
25+
26+
if(NEED_TO_RUN_CHECK)
27+
set(PY_INTERP_FOR_${OUTPUT_VAR} ${Python3_EXECUTABLE} CACHE INTERNAL "The python interpreter used to run the ${OUTPUT_VAR} check" FORCE)
28+
29+
execute_process(
30+
COMMAND ${Python3_EXECUTABLE} -c "import ${PACKAGENAME}" RESULT_VARIABLE PACKAGECHECK_RESULT
31+
)
32+
33+
if(${PACKAGECHECK_RESULT} EQUAL 0)
34+
set(HAVE_PACKAGE TRUE)
35+
else()
36+
set(HAVE_PACKAGE FALSE)
37+
endif()
38+
39+
if(HAVE_PACKAGE)
40+
message(STATUS "Checking for Python package ${PACKAGENAME} -- found")
41+
else()
42+
message(STATUS "Checking for Python package ${PACKAGENAME} -- not found")
43+
endif()
44+
45+
set(${OUTPUT_VAR} ${HAVE_PACKAGE} CACHE BOOL "Whether the Python package ${PACKAGENAME} was found" FORCE)
46+
mark_as_advanced(${OUTPUT_VAR})
47+
endif()
48+
endfunction(check_python_package)
49+
50+
# check that PACKAGENAME can be imported, and print an error if not
51+
function(verify_python_package PACKAGENAME)
52+
53+
# we can just generate our own variable name
54+
string(TOUPPER "HAVE_${PACKAGENAME}" HAVE_VAR_NAME)
55+
56+
check_python_package(${PACKAGENAME} ${HAVE_VAR_NAME})
57+
58+
if(NOT ${HAVE_VAR_NAME})
59+
message(FATAL_ERROR "The required Python package ${PACKAGENAME} was not found in ${Python3_EXECUTABLE}. Please install it.")
60+
endif()
61+
62+
endfunction(verify_python_package)

tools/cmake/app.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,35 @@ 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
28+
find_package(Python3 COMPONENTS Interpreter)
29+
include(${CMAKE_CURRENT_LIST_DIR}/CheckPythonPackage.cmake)
30+
31+
# Check python packages from requirements.txt
32+
file(STRINGS ${CMAKE_CURRENT_LIST_DIR}/requirements.txt PYTHON_REQUIREMENTS)
33+
foreach(REQUIREMENT ${PYTHON_REQUIREMENTS})
34+
# Look for a string from the start of each line that does not contain "<", ">", "=", or " ".
35+
if(REQUIREMENT MATCHES "^([^<>= ]+)")
36+
set(PACKAGE_NAME ${CMAKE_MATCH_1})
37+
string(TOUPPER ${PACKAGE_NAME} PACKAGE_NAME_UCASE) # Ucase name needed for CMake variable
38+
string(TOLOWER ${PACKAGE_NAME} PACKAGE_NAME_LCASE) # Lcase name needed for import statement
39+
40+
check_python_package(${PACKAGE_NAME_LCASE} HAVE_PYTHON_${PACKAGE_NAME_UCASE})
41+
if(NOT HAVE_PYTHON_${PACKAGE_NAME_UCASE})
42+
message(WARNING "Missing Python dependency ${PACKAGE_NAME}")
43+
endif()
44+
else()
45+
message(FATAL_ERROR "Cannot parse line \"${REQUIREMENT}\" in requirements.txt")
46+
endif()
47+
48+
endforeach()
49+
50+
# Check deps for memap
51+
if(Python3_FOUND AND HAVE_PYTHON_INTELHEX AND HAVE_PYTHON_PRETTYTABLE)
52+
set(HAVE_MEMAP_DEPS TRUE)
53+
else()
54+
set(HAVE_MEMAP_DEPS FALSE)
55+
message(STATUS "Missing Python dependencies (python3, intelhex, prettytable) so the memory map cannot be printed")
56+
endif()
57+

tools/cmake/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
prettytable==0.7.2
22
future==0.16.0
33
Jinja2>=2.10.1,<2.11
4+
intelhex>=2.3.0,<3.0.0

0 commit comments

Comments
 (0)