diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a443da..f2f5629 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,8 +93,6 @@ target_compile_definitions(mbed-core INTERFACE TARGET_NAME=${MBED_TARGET} - ${MBED_TARGET_DEFINITIONS} - ${MBED_CONFIG_DEFINITIONS} ) # Add MBED_TEST_MODE for backward compatibility with Greentea tests written for use with Mbed CLI 1 @@ -138,6 +136,10 @@ endif() endif() +# Generate target config header and include it in all files +mbed_write_target_config_header(${CMAKE_CURRENT_BINARY_DIR}/mbed-target-config.h MBED_TARGET_DEFINITIONS MBED_CONFIG_DEFINITIONS) +target_compile_options(mbed-core INTERFACE -include ${CMAKE_CURRENT_BINARY_DIR}/mbed-target-config.h) + # Include mbed.h and config from generate folder target_include_directories(mbed-core INTERFACE diff --git a/tools/cmake/app.cmake b/tools/cmake/app.cmake index e82a25a..5f78357 100644 --- a/tools/cmake/app.cmake +++ b/tools/cmake/app.cmake @@ -12,6 +12,7 @@ include(${MBED_CONFIG_PATH}/mbed_config.cmake) include(mbed_set_post_build) +include(mbed_generate_config_header) # Load toolchain file if(NOT CMAKE_TOOLCHAIN_FILE OR MBED_TOOLCHAIN_FILE_USED) diff --git a/tools/cmake/mbed_generate_config_header.cmake b/tools/cmake/mbed_generate_config_header.cmake new file mode 100644 index 0000000..92c0b8e --- /dev/null +++ b/tools/cmake/mbed_generate_config_header.cmake @@ -0,0 +1,36 @@ +# Copyright (c) 2022 ARM Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# Function to write all Mbed target defines into a header. This makes the build run more smoothly +# by removing the need to pass 100s of #define flags on the command line, which makes debugging difficult +# and can cause issues with command line length limits besides. +function(mbed_write_target_config_header HEADER_PATH) # ARGN: Lists of defines to add to the file. + + set(TARGET_HEADER_CONTENTS +" /* + Mbed OS Target Define Header. + This contains all of the #defines specific to your target and device. + It is prepended to every source file using the -include compiler option. + AUTOGENERATED by cmake. DO NOT EDIT! + */ +") + + foreach(DEFINE_LIST ${ARGN}) + string(APPEND TARGET_HEADER_CONTENTS "\n // Defines from ${DEFINE_LIST}:\n") + + foreach(DEFINE_COMMAND ${${DEFINE_LIST}}) # double dereference needed to get contents of list + + # convert defines in command-line format (VAR=value) to header format (#define VAR value) + if("${DEFINE_COMMAND}" MATCHES "^([^=]+)=(.*)$") + string(APPEND TARGET_HEADER_CONTENTS "#define ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}\n") + else() + # no value given, so follow GCC command line semantics and define it to 1 + string(APPEND TARGET_HEADER_CONTENTS "#define ${DEFINE_COMMAND} 1\n") + endif() + endforeach() + endforeach() + + # Write the file, using file(GENERATE) so that the timestamp is not updated if the contents don't change + file(GENERATE OUTPUT ${HEADER_PATH} CONTENT ${TARGET_HEADER_CONTENTS}) + +endfunction(mbed_write_target_config_header) \ No newline at end of file