From 32257403b04e437280379c7fba91dad213c73887 Mon Sep 17 00:00:00 2001 From: Torsten Tejlmand Rasmussen Date: Tue, 16 Feb 2021 20:30:01 +0100 Subject: [PATCH] [cmake] using cmake -E echo instead of shell echo command (#6175) The echo command of the host system may have slight variations between shell built-in echo and the echo command provided by the OS. Especially the Windows OS echo command is different from practical any other echo command. This means that the currently used `COMMAND echo -e ""` does not work universally. This commit introduces `etc/cmake/print.cmake` which allow to print a CMake list where each item is printed on a new line, to get the desired behavior on the `print-ot-config` target. Using `cmake -E echo ` ensures identical behaviour across different systems. Signed-off-by: Torsten Rasmussen --- CMakeLists.txt | 6 +++++- etc/cmake/print.cmake | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 etc/cmake/print.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index dc47efb9e..e676babb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -194,4 +194,8 @@ endif() add_subdirectory(tests) -add_custom_target(print-ot-config ALL COMMAND echo -e "$,\"\\n\">") +add_custom_target(print-ot-config ALL + COMMAND ${CMAKE_COMMAND} + -DLIST="$" + -P ${PROJECT_SOURCE_DIR}/etc/cmake/print.cmake +) diff --git a/etc/cmake/print.cmake b/etc/cmake/print.cmake new file mode 100644 index 000000000..13fcd641d --- /dev/null +++ b/etc/cmake/print.cmake @@ -0,0 +1,42 @@ +# +# Copyright (c) 2021, The OpenThread Authors. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of the copyright holder nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + + +# Purpose of this CMake script is to support printing of properties fetched +# using a generator expression. +# +# Depending on the generator in use, Ninja, Makefile, other, it is not possible +# to always ensure proper new line on all platforms when calling echo. +# The print.cmake handles this issue by taking a CMake list and prints each item +# in the list on a new line. +# +# This script can be invoked as: `cmake -DLIST="itemA;itemB;..." -P print.cmake` + +foreach(item ${LIST}) + execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${item}) +endforeach()