1# SPDX-License-Identifier: BSD-3-Clause 2 3# Reads configs from kconfig file and set them as cmake variables. 4# Each config is in format CONFIG_<NAME>=<VALUE>. 5# Configs are added to parent scope with CONFIG_ prefix (as written in file). 6function(read_kconfig_config config_file) 7 file( 8 STRINGS 9 ${config_file} 10 configs_list 11 REGEX "^CONFIG_" 12 ENCODING "UTF-8" 13 ) 14 15 foreach(config ${configs_list}) 16 string(REGEX MATCH "^([^=]+)=(.*)$" ignored ${config}) 17 set(config_name ${CMAKE_MATCH_1}) 18 set(config_value ${CMAKE_MATCH_2}) 19 20 if("${config_value}" MATCHES "^\"(.*)\"$") 21 set(config_value ${CMAKE_MATCH_1}) 22 endif() 23 24 set("${config_name}" "${config_value}" PARENT_SCOPE) 25 endforeach() 26endfunction() 27 28# create optimization flags based on cmake variables set from Kconfig 29function(get_optimization_flag OUT_VAR) 30 if(CONFIG_OPTIMIZE_FOR_PERFORMANCE) 31 set(${OUT_VAR} "O2" PARENT_SCOPE) 32 elseif(CONFIG_OPTIMIZE_FOR_SIZE) 33 set(${OUT_VAR} "Os" PARENT_SCOPE) 34 elseif(CONFIG_OPTIMIZE_FOR_DEBUG) 35 set(${OUT_VAR} "Og" PARENT_SCOPE) 36 elseif(CONFIG_OPTIMIZE_FOR_NONE) 37 set(${OUT_VAR} "O0" PARENT_SCOPE) 38 else() 39 message(FATAL_ERROR "no CONFIG_OPTIMIZE_ found") 40 endif() 41endfunction() 42 43# Adds sources to target like target_sources, but assumes that 44# paths are relative to subdirectory. 45# Works like: 46# Cmake >= 3.13: 47# target_sources(<target> PRIVATE <sources>) 48# Cmake < 3.13: 49# target_sources(<target> PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/<sources>) 50function(add_local_sources target) 51 foreach(arg ${ARGN}) 52 if(IS_ABSOLUTE ${arg}) 53 set(path ${arg}) 54 else() 55 set(path ${CMAKE_CURRENT_SOURCE_DIR}/${arg}) 56 endif() 57 58 target_sources(${target} PRIVATE ${path}) 59 # -imacros${CONFIG_H_PATH} escapes regular .h dep scanning 60 # add_dependencies(${target} genconfig) # has no effect? 61 set_source_files_properties(${path} 62 PROPERTIES 63 OBJECT_DEPENDS ${CONFIG_H_PATH} 64 ) 65 endforeach() 66endfunction() 67 68# Declares new static lib with given name and path that will be linked 69# to sof binary. 70function(sof_add_static_library lib_name lib_path) 71 # we need libs to be visible in the root CMakeLists, so use GLOBAL 72 add_library(${lib_name} STATIC IMPORTED GLOBAL) 73 74 if(IS_ABSOLUTE ${lib_path}) 75 set(lib_abs_path ${lib_path}) 76 else() 77 set(lib_abs_path ${CMAKE_CURRENT_SOURCE_DIR}/${lib_path}) 78 endif() 79 80 set_target_properties(${lib_name} PROPERTIES IMPORTED_LOCATION ${lib_abs_path}) 81 target_link_libraries(sof_static_libraries INTERFACE ${lib_name}) 82endfunction() 83 84# Appends literal with path of the source file relative to the project root 85# It is useful if sources in given target need deterministic relative path 86# to the actually compiled file. 87# __FILE is not always suitable as C standard states that __FILE__ expands to 88# input file name, that usually is absolute path what will cause f.e. .rodata 89# size to be dependent on where project is physically located on the disk. 90function(sof_append_relative_path_definitions target) 91 get_target_property(sources ${target} SOURCES) 92 foreach(src ${sources}) 93 get_filename_component(ABS_PATH ${src} ABSOLUTE) 94 file(RELATIVE_PATH rel ${PROJECT_SOURCE_DIR} ${ABS_PATH}) 95 set_property( 96 SOURCE ${src} 97 APPEND 98 PROPERTY COMPILE_DEFINITIONS 99 RELATIVE_FILE="${rel}") 100 endforeach() 101endfunction() 102