2018-01-12 02:49:13 +00:00
|
|
|
# set_default
|
|
|
|
#
|
|
|
|
# Define a variable to a default value if otherwise unset.
|
|
|
|
#
|
|
|
|
# Priority for new value is:
|
|
|
|
# - Existing cmake value (ie set with cmake -D, or already set in CMakeLists)
|
|
|
|
# - Value of any non-empty environment variable of the same name
|
|
|
|
# - Default value as provided to function
|
|
|
|
#
|
|
|
|
function(set_default variable default_value)
|
2018-02-27 04:45:30 +00:00
|
|
|
if(NOT ${variable})
|
2018-05-10 11:15:19 +00:00
|
|
|
if(DEFINED ENV{${variable}} AND NOT "$ENV{${variable}}" STREQUAL "")
|
2018-02-27 04:45:30 +00:00
|
|
|
set(${variable} $ENV{${variable}} PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(${variable} ${default_value} PARENT_SCOPE)
|
|
|
|
endif()
|
2018-01-12 02:49:13 +00:00
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# spaces2list
|
|
|
|
#
|
|
|
|
# Take a variable whose value was space-delimited values, convert to a cmake
|
|
|
|
# list (semicolon-delimited)
|
|
|
|
#
|
|
|
|
# Note: if using this for directories, keeps the issue in place that
|
|
|
|
# directories can't contain spaces...
|
2018-01-15 02:20:38 +00:00
|
|
|
#
|
|
|
|
# TODO: look at cmake separate_arguments, which is quote-aware
|
2018-01-12 02:49:13 +00:00
|
|
|
function(spaces2list variable_name)
|
2018-02-27 04:45:30 +00:00
|
|
|
string(REPLACE " " ";" tmp "${${variable_name}}")
|
|
|
|
set("${variable_name}" "${tmp}" PARENT_SCOPE)
|
2018-01-12 02:49:13 +00:00
|
|
|
endfunction()
|
|
|
|
|
2018-01-15 02:20:38 +00:00
|
|
|
|
|
|
|
# lines2list
|
|
|
|
#
|
|
|
|
# Take a variable with multiple lines of output in it, convert it
|
|
|
|
# to a cmake list (semicolon-delimited), one line per item
|
|
|
|
#
|
|
|
|
function(lines2list variable_name)
|
2018-02-27 04:45:30 +00:00
|
|
|
string(REGEX REPLACE "\r?\n" ";" tmp "${${variable_name}}")
|
|
|
|
string(REGEX REPLACE ";;" ";" tmp "${tmp}")
|
|
|
|
set("${variable_name}" "${tmp}" PARENT_SCOPE)
|
2018-01-15 02:20:38 +00:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
|
2018-01-12 02:49:13 +00:00
|
|
|
# move_if_different
|
|
|
|
#
|
|
|
|
# If 'source' has different md5sum to 'destination' (or destination
|
|
|
|
# does not exist, move it across.
|
|
|
|
#
|
|
|
|
# If 'source' has the same md5sum as 'destination', delete 'source'.
|
|
|
|
#
|
|
|
|
# Avoids timestamp updates for re-generated files where content hasn't
|
|
|
|
# changed.
|
|
|
|
function(move_if_different source destination)
|
2018-02-27 04:45:30 +00:00
|
|
|
set(do_copy 1)
|
|
|
|
file(GLOB dest_exists ${destination})
|
|
|
|
if(dest_exists)
|
|
|
|
file(MD5 ${source} source_md5)
|
|
|
|
file(MD5 ${destination} dest_md5)
|
|
|
|
if(source_md5 STREQUAL dest_md5)
|
|
|
|
set(do_copy "")
|
|
|
|
endif()
|
2018-01-12 02:49:13 +00:00
|
|
|
endif()
|
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
if(do_copy)
|
|
|
|
message("Moving ${source} -> ${destination}")
|
|
|
|
file(RENAME ${source} ${destination})
|
|
|
|
else()
|
|
|
|
message("Not moving ${source} -> ${destination}")
|
|
|
|
file(REMOVE ${source})
|
|
|
|
endif()
|
2018-01-12 02:49:13 +00:00
|
|
|
|
|
|
|
endfunction()
|
2018-01-16 00:16:14 +00:00
|
|
|
|
2018-01-17 05:25:14 +00:00
|
|
|
|
2018-01-16 00:16:14 +00:00
|
|
|
# add_compile_options variant for C++ code only
|
|
|
|
#
|
|
|
|
# This adds global options, set target properties for
|
|
|
|
# component-specific flags
|
|
|
|
function(add_cxx_compile_options)
|
2018-02-27 04:45:30 +00:00
|
|
|
foreach(option ${ARGV})
|
|
|
|
# note: the Visual Studio Generator doesn't support this...
|
|
|
|
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${option}>)
|
|
|
|
endforeach()
|
2018-01-16 00:16:14 +00:00
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# add_compile_options variant for C code only
|
|
|
|
#
|
|
|
|
# This adds global options, set target properties for
|
|
|
|
# component-specific flags
|
|
|
|
function(add_c_compile_options)
|
2018-02-27 04:45:30 +00:00
|
|
|
foreach(option ${ARGV})
|
|
|
|
# note: the Visual Studio Generator doesn't support this...
|
|
|
|
add_compile_options($<$<COMPILE_LANGUAGE:C>:${option}>)
|
|
|
|
endforeach()
|
2018-01-16 00:16:14 +00:00
|
|
|
endfunction()
|
2018-01-17 05:25:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
# target_add_binary_data adds binary data into the built target,
|
|
|
|
# by converting it to a generated source file which is then compiled
|
|
|
|
# to a binary object as part of the build
|
|
|
|
function(target_add_binary_data target embed_file embed_type)
|
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
get_filename_component(embed_file "${embed_file}" ABSOLUTE)
|
2018-01-17 05:25:14 +00:00
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
get_filename_component(name "${embed_file}" NAME)
|
2018-03-01 07:41:15 +00:00
|
|
|
set(embed_srcfile "${CMAKE_BINARY_DIR}/${name}.S")
|
2018-01-17 05:25:14 +00:00
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
add_custom_command(OUTPUT "${embed_srcfile}"
|
|
|
|
COMMAND "${CMAKE_COMMAND}"
|
|
|
|
-D "DATA_FILE=${embed_file}"
|
|
|
|
-D "SOURCE_FILE=${embed_srcfile}"
|
|
|
|
-D "FILE_TYPE=${embed_type}"
|
2018-03-01 07:41:15 +00:00
|
|
|
-P "${IDF_PATH}/tools/cmake/scripts/data_file_embed_asm.cmake"
|
2018-02-27 04:45:30 +00:00
|
|
|
MAIN_DEPENDENCY "${embed_file}"
|
2018-03-01 07:41:15 +00:00
|
|
|
DEPENDENCIES "${IDF_PATH}/tools/cmake/scripts/data_file_embed_asm.cmake"
|
2018-02-27 04:45:30 +00:00
|
|
|
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}")
|
2018-01-17 05:25:14 +00:00
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${embed_srcfile}")
|
2018-01-17 05:25:14 +00:00
|
|
|
|
2018-02-27 04:45:30 +00:00
|
|
|
target_sources("${target}" PRIVATE "${embed_srcfile}")
|
2018-01-17 05:25:14 +00:00
|
|
|
endfunction()
|
|
|
|
|
2018-01-19 04:47:49 +00:00
|
|
|
macro(include_if_exists path)
|
2018-02-27 04:45:30 +00:00
|
|
|
if(EXISTS "${path}")
|
|
|
|
include("${path}")
|
|
|
|
endif()
|
|
|
|
endmacro()
|
2018-02-01 09:45:41 +00:00
|
|
|
|
|
|
|
# Append a single line to the file specified
|
|
|
|
# The line ending is determined by the host OS
|
|
|
|
function(file_append_line file line)
|
2018-05-10 11:15:19 +00:00
|
|
|
if(DEFINED ENV{MSYSTEM} OR CMAKE_HOST_WIN32)
|
2018-02-27 04:45:30 +00:00
|
|
|
set(line_ending "\r\n")
|
|
|
|
else() # unix
|
|
|
|
set(line_ending "\n")
|
|
|
|
endif()
|
|
|
|
file(READ ${file} existing)
|
|
|
|
string(FIND ${existing} ${line_ending} last_newline REVERSE)
|
|
|
|
string(LENGTH ${existing} length)
|
|
|
|
math(EXPR length "${length}-1")
|
|
|
|
if(NOT length EQUAL last_newline) # file doesn't end with a newline
|
|
|
|
file(APPEND "${file}" "${line_ending}")
|
|
|
|
endif()
|
|
|
|
file(APPEND "${file}" "${line}${line_ending}")
|
2018-02-01 09:45:41 +00:00
|
|
|
endfunction()
|
2018-03-01 06:02:08 +00:00
|
|
|
|
2018-04-17 23:30:27 +00:00
|
|
|
# Add one or more linker scripts to the target, including a link-time dependency
|
2018-03-01 06:02:08 +00:00
|
|
|
#
|
|
|
|
# Automatically adds a -L search path for the containing directory (if found),
|
|
|
|
# and then adds -T with the filename only. This allows INCLUDE directives to be
|
|
|
|
# used to include other linker scripts in the same directory.
|
2018-04-17 23:30:27 +00:00
|
|
|
function(target_linker_script target)
|
|
|
|
foreach(scriptfile ${ARGN})
|
|
|
|
get_filename_component(abs_script "${scriptfile}" ABSOLUTE)
|
|
|
|
message(STATUS "Adding linker script ${abs_script}")
|
|
|
|
|
|
|
|
get_filename_component(search_dir "${abs_script}" DIRECTORY)
|
|
|
|
get_filename_component(scriptname "${abs_script}" NAME)
|
|
|
|
|
|
|
|
get_target_property(link_libraries "${target}" LINK_LIBRARIES)
|
|
|
|
list(FIND "${link_libraries}" "-L ${search_dir}" found_search_dir)
|
|
|
|
if(found_search_dir EQUAL "-1") # not already added as a search path
|
|
|
|
target_link_libraries("${target}" "-L ${search_dir}")
|
|
|
|
endif()
|
2018-03-01 06:02:08 +00:00
|
|
|
|
2018-04-17 23:30:27 +00:00
|
|
|
target_link_libraries("${target}" "-T ${scriptname}")
|
2018-03-01 06:02:08 +00:00
|
|
|
|
2018-04-17 23:30:27 +00:00
|
|
|
# Note: In ESP-IDF, most targets are libraries and libary LINK_DEPENDS don't propagate to
|
|
|
|
# executable(s) the library is linked to. This is done manually in components.cmake.
|
|
|
|
set_property(TARGET "${target}" APPEND PROPERTY LINK_DEPENDS "${abs_script}")
|
|
|
|
endforeach()
|
2018-03-01 06:02:08 +00:00
|
|
|
endfunction()
|
2018-04-05 07:26:28 +00:00
|
|
|
|
|
|
|
# Convert a CMake list to a JSON list and store it in a variable
|
|
|
|
function(make_json_list list variable)
|
|
|
|
string(REPLACE ";" "\", \"" result "[ \"${list}\" ]")
|
|
|
|
set("${variable}" "${result}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
2018-09-11 01:44:12 +00:00
|
|
|
|
|
|
|
# add_prefix
|
|
|
|
#
|
|
|
|
# Adds a prefix to each item in the specified list.
|
|
|
|
#
|
|
|
|
function(add_prefix var prefix)
|
|
|
|
foreach(elm ${ARGN})
|
|
|
|
list(APPEND newlist "${prefix}${elm}")
|
|
|
|
endforeach()
|
|
|
|
set(${var} "${newlist}" PARENT_SCOPE)
|
|
|
|
endfunction()
|