Newer
Older
mbed-os / tools / cmake / mbed_generate_config_header.cmake
# 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)