• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Date:   2019-11-11
2# Copyright © Huawei Technologies Co., Ltd. 2010-2020. All rights reserved.
3###########################################################################
4
5
6############################## 公共函数定义区域 ##############################
7## 1. import defconfig
8##把.config文件转换为cmake命名空间的变量,比如把CONFIG_OS_ARCH_ARM32VX=y 转化为 cmake变量
9function(import_kconfig config_file)##.config文件转换为cmake命名空间的变量
10    #### 读取Config文件
11    file(STRINGS ${config_file} config_list REGEX "^CONFIG_" ENCODING "UTF-8")#### 读取Config文件
12
13    ##处理各个匹配行
14    foreach (config ${config_list})##处理各个匹配行
15      ##获取变量值
16      ##字符串匹配‘=’号
17      string(REGEX MATCH "=(.+$)" conf_value ${config})##字符串匹配‘=’号
18      ## 获取匹配值
19      set(conf_value ${CMAKE_MATCH_1})##获取匹配值
20      ##匹配CONFIG_OS_HARDWARE_PLATFORM="OS_ARM7" 这种有""的情形
21      if("${conf_value}" MATCHES "^\"(.*)\"$")
22        ##设置环境变量值
23        set(conf_value ${CMAKE_MATCH_1})##设置环境变量值
24      ##定义结束
25      endif()##定义结束
26      ##获取变量名
27      ##字符串匹配‘=’号
28      string(REGEX MATCH "[^=]+" conf_name ${config})##字符串匹配‘=’号
29      ##声明cmake全局变量
30      ##设置环境变量值
31      set("${conf_name}" "${conf_value}" PARENT_SCOPE)##设置环境变量值
32      ##message("${conf_name}=${conf_value}")
33    ##遍历结束符
34    endforeach()##遍历结束符
35##功能结束符
36endfunction()##功能结束符
37
38
39
40
41
42
43function(cc_object target sources cppincs  cflags component_name working_directory)
44    # @target Object: the name of the target
45    # @sources List[str]: the code source path of list
46    # @cppincs List[str]: the include directories path of list
47    # @cppdefines List[str]: the defines of list
48    # @cflags List[str]: the c compile flags of list
49    # @component_name str: the component name of the atrget
50    # @working_directory str: the directory of command execute
51    list(JOIN ${cppincs} "\t-I" target_incflags)
52    set(obj_install_dir ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${component_name})
53    file(MAKE_DIRECTORY ${obj_install_dir})
54
55
56
57    foreach(source_path IN LISTS ${sources})
58        get_filename_component(source_name ${source_path} NAME)
59        # .c替换为.eln
60        string(REGEX REPLACE \\.c \.o target_name ${source_name})
61        string(REPLACE ${working_directory}/ "" new_source_path ${source_path})
62        string(REGEX REPLACE .*/ "" current_directory ${working_directory})
63        add_custom_command(
64            OUTPUT ${obj_install_dir}/${target_name}
65            COMMAND ${cc} ${${cflags}} -I${target_incflags}  -o ${obj_install_dir}/${target_name} -c ${new_source_path}
66            DEPENDS ${source_path}
67            WORKING_DIRECTORY ${working_directory}
68        )
69        add_custom_target(
70            ${target_name}_custom
71            ALL
72            DEPENDS ${obj_install_dir}/${target_name}
73        )
74
75        list(APPEND ${target}_list_dep ${target_name}_custom )
76        list(APPEND ${target}_list__ ${obj_install_dir}/${target_name} )
77    endforeach()
78    set(${target}_PATH_LIST ${${target}_list__} PARENT_SCOPE)
79    set(${target}_NAME_LIST ${${target}_list_dep} PARENT_SCOPE)
80endfunction()
81
82
83function(asm_object target sources   cflags component_name working_directory)
84    # @target Object: the name of the target
85    # @sources List[str]: the code source path of list
86    # @cppincs List[str]: the include directories path of list
87    # @cppdefines List[str]: the defines of list
88    # @cflags List[str]: the c compile flags of list
89    # @component_name str: the component name of the atrget
90    # @working_directory str: the directory of command execute
91    set(obj_install_dir ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${component_name})
92    file(MAKE_DIRECTORY ${obj_install_dir})
93
94
95
96    foreach(source_path IN LISTS ${sources})
97        get_filename_component(source_name ${source_path} NAME)
98        string(REGEX REPLACE \\.asm \.eln target_name ${source_name})
99        string(REPLACE ${working_directory}/ "" new_source_path ${source_path})
100        string(REGEX REPLACE .*/ "" current_directory ${working_directory})
101        add_custom_command(
102            OUTPUT ${obj_install_dir}/${target_name}
103            COMMAND ${asm} ${${cflags}}   -o ${obj_install_dir}/${target_name} -c ${new_source_path}
104            DEPENDS ${source_path}
105            WORKING_DIRECTORY ${working_directory}
106        )
107        add_custom_target(
108            ${target_name}_custom
109            ALL
110            DEPENDS ${obj_install_dir}/${target_name}
111        )
112
113        list(APPEND ${target}_list_dep ${target_name}_custom )
114        list(APPEND ${target}_list__ ${obj_install_dir}/${target_name} )
115    endforeach()
116    set(${target}_PATH_LIST ${${target}_list__} PARENT_SCOPE)
117    set(${target}_NAME_LIST ${${target}_list_dep} PARENT_SCOPE)
118endfunction()
119
120
121
122
123
124function(ar_library target_name target_suffix target_path  ar_flag object_name object_path)
125    # @target Object: the name of the target
126    # @sources List[str]: the code source path of list
127    # @cppincs List[str]: the include directories path of list
128    # @cppdefines List[str]: the defines of list
129    # @cflags List[str]: the c compile flags of list
130    # @component_name str: the component name of the atrget
131    # @working_directory str: the directory of command execute
132
133    add_custom_command(
134        OUTPUT ${target_path}/${target_name}.${target_suffix}
135        COMMAND ${ar} ${ar_flag} ${target_path}/${target_name}.${target_suffix} ${object_path}
136        WORKING_DIRECTORY ${target_path}
137    )
138    add_custom_target(
139        ${target_name}
140        ALL
141        DEPENDS ${target_path}/${target_name}.${target_suffix}
142    )
143    foreach(object_target  ${object_name})
144        add_dependencies(${target_name} ${object_target})
145    endforeach()
146endfunction()
147
148
149function(link_library target_name  target_path  link_flag object_name object_path)
150    # @target Object: the name of the target
151    # @sources List[str]: the code source path of list
152    # @cppincs List[str]: the include directories path of list
153    # @cppdefines List[str]: the defines of list
154    # @cflags List[str]: the c compile flags of list
155    # @component_name str: the component name of the atrget
156    # @working_directory str: the directory of command execute
157
158    add_custom_command(
159        OUTPUT ${target_path}/${target_name}
160        COMMAND ${link} ${${link_flag}} -o ${target_path}/${target_name} ${object_path}
161        WORKING_DIRECTORY ${target_path}
162    )
163    add_custom_target(
164        ${target_name}
165        ALL
166        DEPENDS ${target_path}/${target_name}
167    )
168    foreach(object_target  ${object_name})
169        add_dependencies(${target_name} ${object_target})
170    endforeach()
171endfunction()
172