Skip to content

Commit

Permalink
cmake:Remove Unix-specific code and replace it with cmake script to s…
Browse files Browse the repository at this point in the history
…upport cross-platform

the Unix-specific commands used in the CMake implementation of savedefconfig are not
supported on the Windows platform, which will cause compatibility issues.

replace it with CMake implementation, which can be cross-platform and has been verified on Windows.

Signed-off-by: xuxin19 <[email protected]>
  • Loading branch information
xuxin930 authored and xiaoxiang781216 committed Aug 14, 2024
1 parent af10e1b commit cd46d27
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 33 deletions.
35 changes: 2 additions & 33 deletions cmake/menuconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,41 +68,10 @@ add_custom_target(
savedefconfig
COMMAND ${CMAKE_COMMAND} -E env ${KCONFIG_ENV} savedefconfig --out
${CMAKE_BINARY_DIR}/defconfig.tmp
COMMAND grep "CONFIG_ARCH=" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp
COMMAND grep "^CONFIG_ARCH_CHIP_" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp || true
COMMAND grep "CONFIG_ARCH_CHIP=" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp || true
COMMAND grep "CONFIG_ARCH_BOARD=" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp || true
COMMAND grep "^CONFIG_ARCH_CUSTOM" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp || true
COMMAND grep "^CONFIG_ARCH_BOARD_CUSTOM" ${CMAKE_BINARY_DIR}/.config >>
${CMAKE_BINARY_DIR}/defconfig.tmp || true
COMMAND export LC_ALL=C && cat ${CMAKE_BINARY_DIR}/defconfig.tmp | sort | uniq
> ${CMAKE_BINARY_DIR}/sortedconfig.tmp || true
COMMAND echo "\\#" > ${CMAKE_BINARY_DIR}/warning.tmp || true
COMMAND echo "\\# This file is autogenerated: PLEASE DO NOT EDIT IT." >>
${CMAKE_BINARY_DIR}/warning.tmp
COMMAND echo "\\#" >> ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND
echo
"\\# You can use \\\"make menuconfig\\\" to make any modifications to the installed .config file."
>> ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND
echo
"\\# You can then do \\\"make savedefconfig\\\" to generate a new defconfig file that includes your"
>> ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND echo "\\# modifications." >> ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND echo "\\#" >> ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND cat ${CMAKE_BINARY_DIR}/warning.tmp
${CMAKE_BINARY_DIR}/sortedconfig.tmp > ${CMAKE_BINARY_DIR}/defconfig
COMMAND ${CMAKE_COMMAND} -P ${NUTTX_DIR}/cmake/savedefconfig.cmake
${CMAKE_BINARY_DIR}/.config ${CMAKE_BINARY_DIR}/defconfig.tmp
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/defconfig
${NUTTX_DEFCONFIG}
COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_BINARY_DIR}/warning.tmp
COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_BINARY_DIR}/defconfig.tmp
COMMAND ${CMAKE_COMMAND} -E remove -f ${CMAKE_BINARY_DIR}/sortedconfig.tmp
WORKING_DIRECTORY ${NUTTX_DIR})

# utility target to restore .config from board's defconfig
Expand Down
72 changes: 72 additions & 0 deletions cmake/savedefconfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ##############################################################################
# cmake/savedefconfig.cmake
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
# ##############################################################################

include(cmake/nuttx_kconfig.cmake)

set(SOURCE_FILE ${CMAKE_ARGV3})
set(TARGET_FILE ${CMAKE_ARGV4})

file(STRINGS ${SOURCE_FILE} ConfigContents)
encode_brackets(ConfigContents)

foreach(NameAndValue ${ConfigContents})
decode_brackets(NameAndValue)
encode_semicolon(NameAndValue)
if("${NameAndValue}" MATCHES "CONFIG_ARCH="
OR "${NameAndValue}" MATCHES "^CONFIG_ARCH_CHIP_"
OR "${NameAndValue}" MATCHES "CONFIG_ARCH_CHIP="
OR "${NameAndValue}" MATCHES "CONFIG_ARCH_BOARD="
OR "${NameAndValue}" MATCHES "^CONFIG_ARCH_CUSTOM"
OR "${NameAndValue}" MATCHES "^CONFIG_ARCH_BOARD_CUSTOM")
decode_semicolon(Value)
file(APPEND ${TARGET_FILE} "${NameAndValue}\n")
endif()
endforeach()

get_filename_component(BINARY_DIR "${TARGET_FILE}" DIRECTORY)

set(OUTPUT_FILE ${BINARY_DIR}/defconfig)

# cmake-format: off
file(WRITE ${OUTPUT_FILE} "")
file(APPEND ${OUTPUT_FILE} "\#\n")
file(APPEND ${OUTPUT_FILE} "\# This file is autogenerated: PLEASE DO NOT EDIT IT.\n")
file(APPEND ${OUTPUT_FILE} "\#\n")
file(APPEND ${OUTPUT_FILE} "\# You can use \"make menuconfig\" to make any modifications to the installed .config file.\n")
file(APPEND ${OUTPUT_FILE} "\# You can then do \"make savedefconfig\" to generate a new defconfig file that includes your\n")
file(APPEND ${OUTPUT_FILE} "\# modifications.\n")
file(APPEND ${OUTPUT_FILE} "\#\n")
file(READ ${TARGET_FILE} CONTENTS)
# cmake-format: on
encode_brackets(CONTENTS)
encode_semicolon(CONTENTS)

string(REGEX MATCHALL "[^\n]+" LINES ${CONTENTS})

list(REMOVE_DUPLICATES LINES)
list(SORT LINES)

foreach(LINE IN LISTS LINES)
decode_brackets(LINE)
decode_semicolon(LINE)
file(APPEND ${OUTPUT_FILE} "${LINE}\n")
endforeach()

execute_process(COMMAND ${CMAKE_COMMAND} -E remove ${TARGET_FILE})

0 comments on commit cd46d27

Please sign in to comment.