3 # Define a variable to a default value if otherwise unset.
5 # Priority for new value is:
6 # - Existing cmake value (ie set with cmake -D, or already set in CMakeLists)
7 # - Value of any non-empty environment variable of the same name
8 # - Default value as provided to function
10 function(set_default variable default_value)
12 if(DEFINED ENV{${variable}} AND NOT "$ENV{${variable}}" STREQUAL "")
13 set(${variable} $ENV{${variable}} PARENT_SCOPE)
15 set(${variable} ${default_value} PARENT_SCOPE)
22 # Take a variable whose value was space-delimited values, convert to a cmake
23 # list (semicolon-delimited)
25 # Note: if using this for directories, keeps the issue in place that
26 # directories can't contain spaces...
28 # TODO: look at cmake separate_arguments, which is quote-aware
29 function(spaces2list variable_name)
30 string(REPLACE " " ";" tmp "${${variable_name}}")
31 set("${variable_name}" "${tmp}" PARENT_SCOPE)
36 # Take a variable with multiple lines of output in it, convert it
37 # to a cmake list (semicolon-delimited), one line per item
39 function(lines2list variable_name)
40 string(REGEX REPLACE "\r?\n" ";" tmp "${${variable_name}}")
41 string(REGEX REPLACE ";;" ";" tmp "${tmp}")
42 set("${variable_name}" "${tmp}" PARENT_SCOPE)
48 # If 'source' has different md5sum to 'destination' (or destination
49 # does not exist, move it across.
51 # If 'source' has the same md5sum as 'destination', delete 'source'.
53 # Avoids timestamp updates for re-generated files where content hasn't
55 function(move_if_different source destination)
57 file(GLOB dest_exists ${destination})
59 file(MD5 ${source} source_md5)
60 file(MD5 ${destination} dest_md5)
61 if(source_md5 STREQUAL dest_md5)
67 message("Moving ${source} -> ${destination}")
68 file(RENAME ${source} ${destination})
70 message("Not moving ${source} -> ${destination}")
71 file(REMOVE ${source})
77 # add_compile_options variant for C++ code only
79 # This adds global options, set target properties for
80 # component-specific flags
81 function(add_cxx_compile_options)
82 foreach(option ${ARGV})
83 # note: the Visual Studio Generator doesn't support this...
84 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
88 # add_compile_options variant for C code only
90 # This adds global options, set target properties for
91 # component-specific flags
92 function(add_c_compile_options)
93 foreach(option ${ARGV})
94 # note: the Visual Studio Generator doesn't support this...
95 add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
100 # target_add_binary_data adds binary data into the built target,
101 # by converting it to a generated source file which is then compiled
102 # to a binary object as part of the build
103 function(target_add_binary_data target embed_file embed_type)
105 get_filename_component(embed_file "${embed_file}" ABSOLUTE)
107 get_filename_component(name "${embed_file}" NAME)
108 set(embed_srcfile "${CMAKE_BINARY_DIR}/${name}.S")
110 add_custom_command(OUTPUT "${embed_srcfile}"
111 COMMAND "${CMAKE_COMMAND}"
112 -D "DATA_FILE=${embed_file}"
113 -D "SOURCE_FILE=${embed_srcfile}"
114 -D "FILE_TYPE=${embed_type}"
115 -P "${IDF_PATH}/tools/cmake/scripts/data_file_embed_asm.cmake"
116 MAIN_DEPENDENCY "${embed_file}"
117 DEPENDENCIES "${IDF_PATH}/tools/cmake/scripts/data_file_embed_asm.cmake"
118 WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
120 set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${embed_srcfile}")
122 target_sources("${target}" PRIVATE "${embed_srcfile}")
125 macro(include_if_exists path)
131 # Append a single line to the file specified
132 # The line ending is determined by the host OS
133 function(file_append_line file line)
134 if(DEFINED ENV{MSYSTEM} OR CMAKE_HOST_WIN32)
135 set(line_ending "\r\n")
137 set(line_ending "\n")
139 file(READ ${file} existing)
140 string(FIND ${existing} ${line_ending} last_newline REVERSE)
141 string(LENGTH ${existing} length)
142 math(EXPR length "${length}-1")
143 if(NOT length EQUAL last_newline) # file doesn't end with a newline
144 file(APPEND "${file}" "${line_ending}")
146 file(APPEND "${file}" "${line}${line_ending}")
149 # Add one or more linker scripts to the target, including a link-time dependency
151 # Automatically adds a -L search path for the containing directory (if found),
152 # and then adds -T with the filename only. This allows INCLUDE directives to be
153 # used to include other linker scripts in the same directory.
154 function(target_linker_script target)
155 foreach(scriptfile ${ARGN})
156 get_filename_component(abs_script "${scriptfile}" ABSOLUTE)
157 message(STATUS "Adding linker script ${abs_script}")
159 get_filename_component(search_dir "${abs_script}" DIRECTORY)
160 get_filename_component(scriptname "${abs_script}" NAME)
162 get_target_property(link_libraries "${target}" LINK_LIBRARIES)
163 list(FIND "${link_libraries}" "-L ${search_dir}" found_search_dir)
164 if(found_search_dir EQUAL "-1") # not already added as a search path
165 target_link_libraries("${target}" "-L ${search_dir}")
168 target_link_libraries("${target}" "-T ${scriptname}")
170 # Note: In ESP-IDF, most targets are libraries and libary LINK_DEPENDS don't propagate to
171 # executable(s) the library is linked to. This is done manually in components.cmake.
172 set_property(TARGET "${target}" APPEND PROPERTY LINK_DEPENDS "${abs_script}")
176 # Convert a CMake list to a JSON list and store it in a variable
177 function(make_json_list list variable)
178 string(REPLACE ";" "\", \"" result "[ \"${list}\" ]")
179 set("${variable}" "${result}" PARENT_SCOPE)
184 # Adds a prefix to each item in the specified list.
186 function(add_prefix var prefix)
188 list(APPEND newlist "${prefix}${elm}")
190 set(${var} "${newlist}" PARENT_SCOPE)