From ccaee8ce880cf3c8f810b27cdc76f92ca7daabe3 Mon Sep 17 00:00:00 2001 From: Omar Chebib Date: Tue, 24 Nov 2020 12:24:39 +0800 Subject: [PATCH] buildsystem: flash binary to a named partition Add support for CMake version less than 3.8. These versions don't provide a way to have ternary expressions in expressions generator. --- components/esptool_py/project_include.cmake | 61 ++++++++++++++++++--- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/components/esptool_py/project_include.cmake b/components/esptool_py/project_include.cmake index 0f33f732e7..253367748e 100644 --- a/components/esptool_py/project_include.cmake +++ b/components/esptool_py/project_include.cmake @@ -278,9 +278,45 @@ function(esptool_py_flash_target_image target_name image_name offset image) set_property(TARGET encrypted-${target_name} APPEND PROPERTY NON_ENCRYPTED_IMAGES "${offset} ${image}") endif() endif() - endfunction() +# Use this function to generate a ternary expression that will be evaluated. +# - retexpr is the expression returned by the function +# - condition is the expression evaluated to a boolean +# - condtrue is the expression to evaluate if condition is true +# - condfalse is the expression to evaluate if condition is false +# This function can be summarized as: +# retexpr = condition ? condtrue : condfalse +function(if_expr_generator retexpr condition condtrue condfalse) + # CMake version 3.8 and above provide a ternary operator for expression + # generator. For version under, we must simulate this behaviour + if(${CMAKE_VERSION} VERSION_LESS "3.8.0") + + # If condtrue is not empty, then we have to do something in case the + # condition is true. Generate the expression that will be used in that + # case + if(condtrue) + set(iftrue "$<${condition}:${condtrue}>") + endif() + + # Same for condfalse. If it is empty, it is useless to create an expression + # that will be evaluated later + if(condfalse) + set(iffalse "$<$:${condfalse}>") + endif() + + # Concatenate the previously generated expressions. If one of them was not + # initialized (because of empty condtrue/condfalse) it will be replace by + # an empty string + set(${retexpr} "${iftrue}${iffalse}" PARENT_SCOPE) + + else() + # CMake 3.8 and above implement what we want, making the expression simpler + set(${retexpr} "$" PARENT_SCOPE) + endif() +endfunction() + + function(esptool_py_flash_target target_name main_args sub_args) set(single_value OFFSET IMAGE) # template file to use to be able to # flash the image individually using esptool @@ -356,21 +392,30 @@ $,\n>") # The variable has_non_encrypted_image will be evaluated to "1" if some # images must not be encrypted. This variable will be used in the next # expression - set(has_non_encrypted_images "$>") + set(has_non_encrypted_images "$>") # Prepare esptool arguments (--encrypt or --encrypt-files) - set_target_properties(encrypted-${target_name} PROPERTIES SUB_ARGS "${sub_args};\ -$") + if_expr_generator(if_non_enc_expr ${has_non_encrypted_images} + "" "--encrypt") + set_target_properties(encrypted-${target_name} PROPERTIES SUB_ARGS + "${sub_args}; ${if_non_enc_expr}") # Generate the list of files to pass to esptool - set(encrypted_files "$,\n>") - set(non_encrypted_files "$,\n>") + set(encrypted_files "$,\n>") + set(non_encrypted_files "$,\n>") # Put both lists together, use --encrypted-files if we do also have # plain images to flash - set(flash_args_content "$, >\ + + if_expr_generator(if_enc_expr ${has_non_encrypted_images} + "--encrypt-files\n" "") + set(flash_args_content "$, >\ ${non_encrypted_files}\n\ -$\ +${if_enc_expr}\ ${encrypted_files}") # The expression is ready to be geenrated, write it to the file which