nuttx/cmake/nuttx_toolchain.cmake
Alan Carvalho de Assis 87260499e1
Some checks are pending
MemBrowse Memory Report / changes-filter (push) Waiting to run
MemBrowse Memory Report / load-targets (push) Waiting to run
MemBrowse Memory Report / identical (push) Blocked by required conditions
MemBrowse Memory Report / analyze (push) Blocked by required conditions
cmake: Use NUTTX(_DIR/_BIN_DIR) instead CMAKE(_SRC_DIR/_BIN_DIR)
This change fixes NuttX’s CMake support when NuttX is embedded
in another project via add_subdirectory(). CMake’s CMAKE_SOURCE_DIR
and CMAKE_BINARY_DIR refer to the outermost project, causing NuttX
to access its .config, generated files, host tools, and build artifacts
in the parent project’s directories. The fix introduces NUTTX_DIR and
NUTTX_BINARY_DIR, based on CMAKE_CURRENT_SOURCE_DIR and
CMAKE_CURRENT_BINARY_DIR, and consistently uses them for NuttX
self-references while preserving existing standalone builds. It fixes
the Kconfig initialization failure reported in #19697 and allows an
embedded sim:nsh build to configure, build, and boot successfully.
The change affects only the CMake build system (not Make or Kconfig
defaults), requires the corresponding nuttx-apps change, and does not
extend add_subdirectory() support to cross-compiled non-sim boards due
to CMake’s toolchain-file limitation.

Fixes #19697.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Alan Carvalho de Assis <acassis@gmail.com>
2026-08-09 11:13:08 -03: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${NUTTX_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()