154568cb3Sopenharmony_ci# Date: 2019-11-11 254568cb3Sopenharmony_ci# Copyright © Huawei Technologies Co., Ltd. 2010-2020. All rights reserved. 354568cb3Sopenharmony_ci########################################################################### 454568cb3Sopenharmony_ci 554568cb3Sopenharmony_ci 654568cb3Sopenharmony_ci############################## 公共函数定义区域 ############################## 754568cb3Sopenharmony_ci## 1. import defconfig 854568cb3Sopenharmony_ci##把.config文件转换为cmake命名空间的变量,比如把CONFIG_OS_ARCH_ARM32VX=y 转化为 cmake变量 954568cb3Sopenharmony_cifunction(import_kconfig config_file)##.config文件转换为cmake命名空间的变量 1054568cb3Sopenharmony_ci #### 读取Config文件 1154568cb3Sopenharmony_ci file(STRINGS ${config_file} config_list REGEX "^CONFIG_" ENCODING "UTF-8")#### 读取Config文件 1254568cb3Sopenharmony_ci 1354568cb3Sopenharmony_ci ##处理各个匹配行 1454568cb3Sopenharmony_ci foreach (config ${config_list})##处理各个匹配行 1554568cb3Sopenharmony_ci ##获取变量值 1654568cb3Sopenharmony_ci ##字符串匹配‘=’号 1754568cb3Sopenharmony_ci string(REGEX MATCH "=(.+$)" conf_value ${config})##字符串匹配‘=’号 1854568cb3Sopenharmony_ci ## 获取匹配值 1954568cb3Sopenharmony_ci set(conf_value ${CMAKE_MATCH_1})##获取匹配值 2054568cb3Sopenharmony_ci ##匹配CONFIG_OS_HARDWARE_PLATFORM="OS_ARM7" 这种有""的情形 2154568cb3Sopenharmony_ci if("${conf_value}" MATCHES "^\"(.*)\"$") 2254568cb3Sopenharmony_ci ##设置环境变量值 2354568cb3Sopenharmony_ci set(conf_value ${CMAKE_MATCH_1})##设置环境变量值 2454568cb3Sopenharmony_ci ##定义结束 2554568cb3Sopenharmony_ci endif()##定义结束 2654568cb3Sopenharmony_ci ##获取变量名 2754568cb3Sopenharmony_ci ##字符串匹配‘=’号 2854568cb3Sopenharmony_ci string(REGEX MATCH "[^=]+" conf_name ${config})##字符串匹配‘=’号 2954568cb3Sopenharmony_ci ##声明cmake全局变量 3054568cb3Sopenharmony_ci ##设置环境变量值 3154568cb3Sopenharmony_ci set("${conf_name}" "${conf_value}" PARENT_SCOPE)##设置环境变量值 3254568cb3Sopenharmony_ci ##message("${conf_name}=${conf_value}") 3354568cb3Sopenharmony_ci ##遍历结束符 3454568cb3Sopenharmony_ci endforeach()##遍历结束符 3554568cb3Sopenharmony_ci##功能结束符 3654568cb3Sopenharmony_ciendfunction()##功能结束符 3754568cb3Sopenharmony_ci 3854568cb3Sopenharmony_ci 3954568cb3Sopenharmony_ci 4054568cb3Sopenharmony_ci 4154568cb3Sopenharmony_ci 4254568cb3Sopenharmony_ci 4354568cb3Sopenharmony_cifunction(cc_object target sources cppincs cflags component_name working_directory) 4454568cb3Sopenharmony_ci # @target Object: the name of the target 4554568cb3Sopenharmony_ci # @sources List[str]: the code source path of list 4654568cb3Sopenharmony_ci # @cppincs List[str]: the include directories path of list 4754568cb3Sopenharmony_ci # @cppdefines List[str]: the defines of list 4854568cb3Sopenharmony_ci # @cflags List[str]: the c compile flags of list 4954568cb3Sopenharmony_ci # @component_name str: the component name of the atrget 5054568cb3Sopenharmony_ci # @working_directory str: the directory of command execute 5154568cb3Sopenharmony_ci list(JOIN ${cppincs} "\t-I" target_incflags) 5254568cb3Sopenharmony_ci set(obj_install_dir ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${component_name}) 5354568cb3Sopenharmony_ci file(MAKE_DIRECTORY ${obj_install_dir}) 5454568cb3Sopenharmony_ci 5554568cb3Sopenharmony_ci 5654568cb3Sopenharmony_ci 5754568cb3Sopenharmony_ci foreach(source_path IN LISTS ${sources}) 5854568cb3Sopenharmony_ci get_filename_component(source_name ${source_path} NAME) 5954568cb3Sopenharmony_ci # .c替换为.eln 6054568cb3Sopenharmony_ci string(REGEX REPLACE \\.c \.o target_name ${source_name}) 6154568cb3Sopenharmony_ci string(REPLACE ${working_directory}/ "" new_source_path ${source_path}) 6254568cb3Sopenharmony_ci string(REGEX REPLACE .*/ "" current_directory ${working_directory}) 6354568cb3Sopenharmony_ci add_custom_command( 6454568cb3Sopenharmony_ci OUTPUT ${obj_install_dir}/${target_name} 6554568cb3Sopenharmony_ci COMMAND ${cc} ${${cflags}} -I${target_incflags} -o ${obj_install_dir}/${target_name} -c ${new_source_path} 6654568cb3Sopenharmony_ci DEPENDS ${source_path} 6754568cb3Sopenharmony_ci WORKING_DIRECTORY ${working_directory} 6854568cb3Sopenharmony_ci ) 6954568cb3Sopenharmony_ci add_custom_target( 7054568cb3Sopenharmony_ci ${target_name}_custom 7154568cb3Sopenharmony_ci ALL 7254568cb3Sopenharmony_ci DEPENDS ${obj_install_dir}/${target_name} 7354568cb3Sopenharmony_ci ) 7454568cb3Sopenharmony_ci 7554568cb3Sopenharmony_ci list(APPEND ${target}_list_dep ${target_name}_custom ) 7654568cb3Sopenharmony_ci list(APPEND ${target}_list__ ${obj_install_dir}/${target_name} ) 7754568cb3Sopenharmony_ci endforeach() 7854568cb3Sopenharmony_ci set(${target}_PATH_LIST ${${target}_list__} PARENT_SCOPE) 7954568cb3Sopenharmony_ci set(${target}_NAME_LIST ${${target}_list_dep} PARENT_SCOPE) 8054568cb3Sopenharmony_ciendfunction() 8154568cb3Sopenharmony_ci 8254568cb3Sopenharmony_ci 8354568cb3Sopenharmony_cifunction(asm_object target sources cflags component_name working_directory) 8454568cb3Sopenharmony_ci # @target Object: the name of the target 8554568cb3Sopenharmony_ci # @sources List[str]: the code source path of list 8654568cb3Sopenharmony_ci # @cppincs List[str]: the include directories path of list 8754568cb3Sopenharmony_ci # @cppdefines List[str]: the defines of list 8854568cb3Sopenharmony_ci # @cflags List[str]: the c compile flags of list 8954568cb3Sopenharmony_ci # @component_name str: the component name of the atrget 9054568cb3Sopenharmony_ci # @working_directory str: the directory of command execute 9154568cb3Sopenharmony_ci set(obj_install_dir ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${component_name}) 9254568cb3Sopenharmony_ci file(MAKE_DIRECTORY ${obj_install_dir}) 9354568cb3Sopenharmony_ci 9454568cb3Sopenharmony_ci 9554568cb3Sopenharmony_ci 9654568cb3Sopenharmony_ci foreach(source_path IN LISTS ${sources}) 9754568cb3Sopenharmony_ci get_filename_component(source_name ${source_path} NAME) 9854568cb3Sopenharmony_ci string(REGEX REPLACE \\.asm \.eln target_name ${source_name}) 9954568cb3Sopenharmony_ci string(REPLACE ${working_directory}/ "" new_source_path ${source_path}) 10054568cb3Sopenharmony_ci string(REGEX REPLACE .*/ "" current_directory ${working_directory}) 10154568cb3Sopenharmony_ci add_custom_command( 10254568cb3Sopenharmony_ci OUTPUT ${obj_install_dir}/${target_name} 10354568cb3Sopenharmony_ci COMMAND ${asm} ${${cflags}} -o ${obj_install_dir}/${target_name} -c ${new_source_path} 10454568cb3Sopenharmony_ci DEPENDS ${source_path} 10554568cb3Sopenharmony_ci WORKING_DIRECTORY ${working_directory} 10654568cb3Sopenharmony_ci ) 10754568cb3Sopenharmony_ci add_custom_target( 10854568cb3Sopenharmony_ci ${target_name}_custom 10954568cb3Sopenharmony_ci ALL 11054568cb3Sopenharmony_ci DEPENDS ${obj_install_dir}/${target_name} 11154568cb3Sopenharmony_ci ) 11254568cb3Sopenharmony_ci 11354568cb3Sopenharmony_ci list(APPEND ${target}_list_dep ${target_name}_custom ) 11454568cb3Sopenharmony_ci list(APPEND ${target}_list__ ${obj_install_dir}/${target_name} ) 11554568cb3Sopenharmony_ci endforeach() 11654568cb3Sopenharmony_ci set(${target}_PATH_LIST ${${target}_list__} PARENT_SCOPE) 11754568cb3Sopenharmony_ci set(${target}_NAME_LIST ${${target}_list_dep} PARENT_SCOPE) 11854568cb3Sopenharmony_ciendfunction() 11954568cb3Sopenharmony_ci 12054568cb3Sopenharmony_ci 12154568cb3Sopenharmony_ci 12254568cb3Sopenharmony_ci 12354568cb3Sopenharmony_ci 12454568cb3Sopenharmony_cifunction(ar_library target_name target_suffix target_path ar_flag object_name object_path) 12554568cb3Sopenharmony_ci # @target Object: the name of the target 12654568cb3Sopenharmony_ci # @sources List[str]: the code source path of list 12754568cb3Sopenharmony_ci # @cppincs List[str]: the include directories path of list 12854568cb3Sopenharmony_ci # @cppdefines List[str]: the defines of list 12954568cb3Sopenharmony_ci # @cflags List[str]: the c compile flags of list 13054568cb3Sopenharmony_ci # @component_name str: the component name of the atrget 13154568cb3Sopenharmony_ci # @working_directory str: the directory of command execute 13254568cb3Sopenharmony_ci 13354568cb3Sopenharmony_ci add_custom_command( 13454568cb3Sopenharmony_ci OUTPUT ${target_path}/${target_name}.${target_suffix} 13554568cb3Sopenharmony_ci COMMAND ${ar} ${ar_flag} ${target_path}/${target_name}.${target_suffix} ${object_path} 13654568cb3Sopenharmony_ci WORKING_DIRECTORY ${target_path} 13754568cb3Sopenharmony_ci ) 13854568cb3Sopenharmony_ci add_custom_target( 13954568cb3Sopenharmony_ci ${target_name} 14054568cb3Sopenharmony_ci ALL 14154568cb3Sopenharmony_ci DEPENDS ${target_path}/${target_name}.${target_suffix} 14254568cb3Sopenharmony_ci ) 14354568cb3Sopenharmony_ci foreach(object_target ${object_name}) 14454568cb3Sopenharmony_ci add_dependencies(${target_name} ${object_target}) 14554568cb3Sopenharmony_ci endforeach() 14654568cb3Sopenharmony_ciendfunction() 14754568cb3Sopenharmony_ci 14854568cb3Sopenharmony_ci 14954568cb3Sopenharmony_cifunction(link_library target_name target_path link_flag object_name object_path) 15054568cb3Sopenharmony_ci # @target Object: the name of the target 15154568cb3Sopenharmony_ci # @sources List[str]: the code source path of list 15254568cb3Sopenharmony_ci # @cppincs List[str]: the include directories path of list 15354568cb3Sopenharmony_ci # @cppdefines List[str]: the defines of list 15454568cb3Sopenharmony_ci # @cflags List[str]: the c compile flags of list 15554568cb3Sopenharmony_ci # @component_name str: the component name of the atrget 15654568cb3Sopenharmony_ci # @working_directory str: the directory of command execute 15754568cb3Sopenharmony_ci 15854568cb3Sopenharmony_ci add_custom_command( 15954568cb3Sopenharmony_ci OUTPUT ${target_path}/${target_name} 16054568cb3Sopenharmony_ci COMMAND ${link} ${${link_flag}} -o ${target_path}/${target_name} ${object_path} 16154568cb3Sopenharmony_ci WORKING_DIRECTORY ${target_path} 16254568cb3Sopenharmony_ci ) 16354568cb3Sopenharmony_ci add_custom_target( 16454568cb3Sopenharmony_ci ${target_name} 16554568cb3Sopenharmony_ci ALL 16654568cb3Sopenharmony_ci DEPENDS ${target_path}/${target_name} 16754568cb3Sopenharmony_ci ) 16854568cb3Sopenharmony_ci foreach(object_target ${object_name}) 16954568cb3Sopenharmony_ci add_dependencies(${target_name} ${object_target}) 17054568cb3Sopenharmony_ci endforeach() 17154568cb3Sopenharmony_ciendfunction() 172