nuttx/cmake/nuttx_toolchain.cmake
shichunma 0afb8c7ad5 cmake/nuttx_toolchain.cmake: track preprocessed include deps
Make preprocess-generated outputs depend on included files by
emitting depfiles for GNU/Clang Ninja/Makefile builds.

This fixes stale generated rc.sysinit and ROMFS images when a
board-specific included fragment such as rc.sysinit.ap changes,
because the previous custom command only depended on the top-level
source file.

Signed-off-by: Jerry Ma <shichunma@bestechnic.com>
2026-06-04 17:22:43 +08:00

151 lines
4.8 KiB
CMake

# ##############################################################################
# cmake/nuttx_toolchain.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.
#
# ##############################################################################
# This file is used to set the parts that need common settings in the Toolchain
# file. Such as preprocessing process, link command, toolchain library method
# search. If the manual of the newly supported toolchain is different, you can
# override these methods in the toolchain
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
# force color for gcc > 4.9
add_compile_options(-fdiagnostics-color=always)
endif()
endif()
# Cmake build provide absolute paths to compile files. If __FILE__ macros are
# used in the source code(ASSERT), the binary will contain many invalid paths.
# This saves some memory, stops exposing build systems locations in binaries,
# make failure logs more deterministic and most importantly makes builds more
# failure logs more deterministic and most importantly makes builds more
# deterministic. Debuggers usually have a path mapping feature to ensure the
# files are still found.
if((NOT MSVC) AND (NOT CONFIG_ARCH_TOOLCHAIN_GHS))
if(CONFIG_OUTPUT_STRIP_PATHS)
add_compile_options(-fmacro-prefix-map=${NUTTX_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_APPS_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_BOARD_ABS_DIR}=)
add_compile_options(-fmacro-prefix-map=${NUTTX_CHIP_ABS_DIR}=)
endif()
endif()
# Support CMake to define additional configuration options
if(EXTRA_FLAGS)
separate_arguments(EXTRA_FLAGS_LIST UNIX_COMMAND "${EXTRA_FLAGS}")
add_compile_options(${EXTRA_FLAGS_LIST})
endif()
# ~~~
# nuttx_generate_preprocess_target
#
# Description:
# because different toolchains have different preprocessing instructions,
# we define the COMMON preprocessing target here.
#
# Prototype:
# nuttx_generate_preprocess_target(
# SOURCE_FILE
# ${single_source_file}
# TARGET_FILE
# ${single_target_output}
# DEPENDS
# ${option_depned_target})
# ~~~
#
if(NOT NUTTX_TOOLCHAIN_PREPROCESS_DEFINED)
function(nuttx_generate_preprocess_target)
set(DEFINES -D__NuttX__)
# parse arguments into variables
nuttx_parse_function_args(
FUNC
nuttx_generate_preprocess_target
ONE_VALUE
SOURCE_FILE
TARGET_FILE
MULTI_VALUE
DEPENDS
REQUIRED
SOURCE_FILE
TARGET_FILE
ARGN
${ARGN})
set(depfile_args)
set(preprocess_depflags)
if((CMAKE_GENERATOR MATCHES "Ninja|Makefiles")
AND (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES
"Clang"))
set(preprocess_depfile ${TARGET_FILE}.d)
list(
APPEND
preprocess_depflags
-MMD
-MT
${TARGET_FILE}
-MF
${preprocess_depfile})
list(APPEND depfile_args DEPFILE ${preprocess_depfile})
endif()
add_custom_command(
OUTPUT ${TARGET_FILE}
COMMAND
${PREPROCESS} ${DEFINES} ${preprocess_depflags}
-I${CMAKE_BINARY_DIR}/include -I${NUTTX_DIR}/include
-I${NUTTX_CHIP_ABS_DIR} ${SOURCE_FILE} > ${TARGET_FILE}
DEPENDS ${SOURCE_FILE} ${DEPENDS} ${depfile_args}
COMMAND_EXPAND_LISTS)
endfunction()
endif()
# ~~~
# nuttx_find_toolchain_lib
#
# Description:
# this is general function for finding toolchain libraries.
#
# Prototype:
# nuttx_find_toolchain_lib(${single_toolchain_lib})
# ~~~
if(NOT NUTTX_FIND_TOOLCHAIN_LIB_DEFINED)
function(nuttx_find_toolchain_lib)
if(NOT ARGN)
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-libgcc-file-name
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_lib_path)
else()
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-file-name=${ARGN}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_lib_path)
endif()
nuttx_add_extra_library(${extra_lib_path})
endfunction()
endif()