Skip to content

Commit 947c804

Browse files
Parse requirements.txt in CMake
1 parent c667747 commit 947c804

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

tools/cmake/app.cmake

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,39 @@ set(CMAKE_EXECUTABLE_SUFFIX .elf)
2626

2727
# Find Python and needed packages
2828
find_package(Python3)
29+
# Find Python
30+
find_package(Python3 COMPONENTS Interpreter)
2931
include(${CMAKE_CURRENT_LIST_DIR}/CheckPythonPackage.cmake)
30-
check_python_package(intelhex HAVE_INTELHEX)
31-
check_python_package(prettytable HAVE_PRETTYTABLE)
3232

33-
if(Python3_FOUND AND HAVE_INTELHEX AND HAVE_PRETTYTABLE)
33+
# Check python packages from requirements.txt
34+
file(STRINGS ${CMAKE_CURRENT_LIST_DIR}/requirements.txt PYTHON_REQUIREMENTS)
35+
foreach(REQUIREMENT ${PYTHON_REQUIREMENTS})
36+
37+
# Look for a string from the start of each line that does not contain "<", ">", "=", or " ".
38+
if(REQUIREMENT MATCHES "^([^<>= ]+)")
39+
40+
set(PACKAGE_NAME ${CMAKE_MATCH_1})
41+
string(TOUPPER ${PACKAGE_NAME} PACKAGE_NAME_UCASE) # Ucase name needed for CMake variable
42+
string(TOLOWER ${PACKAGE_NAME} PACKAGE_NAME_LCASE) # Lcase name needed for import statement
43+
44+
check_python_package(${PACKAGE_NAME_LCASE} HAVE_PYTHON_${PACKAGE_NAME_UCASE})
45+
if(NOT HAVE_PYTHON_${PACKAGE_NAME_UCASE})
46+
message(WARNING "Missing Python dependency ${PACKAGE_NAME}")
47+
endif()
48+
49+
else()
50+
51+
message(FATAL_ERROR "Cannot parse line \"${REQUIREMENT}\" in requirements.txt")
52+
53+
endif()
54+
55+
endforeach()
56+
57+
# Check deps for memap
58+
if(Python3_FOUND AND HAVE_PYTHON_INTELHEX AND HAVE_PYTHON_PRETTYTABLE)
3459
set(HAVE_MEMAP_DEPS TRUE)
3560
else()
3661
set(HAVE_MEMAP_DEPS FALSE)
3762
message(STATUS "Missing Python dependencies (python3, intelhex, prettytable) so the memory map cannot be printed")
38-
endif()
63+
endif()
64+

0 commit comments

Comments
 (0)