1# In general, a flag is a string provided for supported functions under the
2# multi-valued option `FLAGS`.  It should be one of the following forms:
3#   FLAG_NAME
4#   FLAG_NAME__NO
5#   FLAG_NAME__ONLY
6# A target will inherit all the flags of its upstream dependency.
7#
8# When we create a target `TARGET_NAME` with a flag using (add_header_library,
9# add_object_library, ...), its behavior will depend on the flag form as follow:
10# - FLAG_NAME: The following 2 targets will be generated:
11#     `TARGET_NAME` that has `FLAG_NAME` in its `FLAGS` property.
12#     `TARGET_NAME.__NO_FLAG_NAME` that depends on `DEP.__NO_FLAG_NAME` if
13#        `TARGET_NAME` depends on `DEP` and `DEP` has `FLAG_NAME` in its `FLAGS`
14#        property.
15# - FLAG_NAME__ONLY: Only generate 1 target `TARGET_NAME` that has `FLAG_NAME`
16#     in its `FLAGS` property.
17# - FLAG_NAME__NO: Only generate 1 target `TARGET_NAME` that depends on
18# `DEP.__NO_FLAG_NAME` if `DEP` is in its DEPENDS list and `DEP` has `FLAG_NAME`
19# in its `FLAGS` property.
20#
21# To show all the targets generated, pass SHOW_INTERMEDIATE_OBJECTS=ON to cmake.
22# To show all the targets' dependency and flags, pass
23#   SHOW_INTERMEDIATE_OBJECTS=DEPS to cmake.
24#
25# To completely disable a flag FLAG_NAME expansion, set the variable
26#   SKIP_FLAG_EXPANSION_FLAG_NAME=TRUE in this file.
27
28
29function(extract_flag_modifier input_flag output_flag modifier)
30  if(${input_flag} MATCHES "__NO$")
31    string(REGEX REPLACE "__NO$" "" flag "${input_flag}")
32    set(${output_flag} ${flag} PARENT_SCOPE)
33    set(${modifier} "NO" PARENT_SCOPE)
34  elseif(${input_flag} MATCHES "__ONLY$")
35    string(REGEX REPLACE "__ONLY$" "" flag "${input_flag}")
36    set(${output_flag} ${flag} PARENT_SCOPE)
37    set(${modifier} "ONLY" PARENT_SCOPE)
38  else()
39    set(${output_flag} ${input_flag} PARENT_SCOPE)
40    set(${modifier} "" PARENT_SCOPE)
41  endif()
42endfunction(extract_flag_modifier)
43
44function(remove_duplicated_flags input_flags output_flags)
45  set(out_flags "")
46  foreach(input_flag IN LISTS input_flags)
47    if(NOT input_flag)
48      continue()
49    endif()
50
51    extract_flag_modifier(${input_flag} flag modifier)
52
53    # Check if the flag is skipped.
54    if(${SKIP_FLAG_EXPANSION_${flag}})
55      if("${SHOW_INTERMEDIATE_OBJECTS}" STREQUAL "DEPS")
56        message(STATUS "  Flag ${flag} is ignored.")
57      endif()
58      continue()
59    endif()
60
61    set(found FALSE)
62    foreach(out_flag IN LISTS out_flags)
63      extract_flag_modifier(${out_flag} o_flag o_modifier)
64      if("${flag}" STREQUAL "${o_flag}")
65        set(found TRUE)
66        break()
67      endif()
68    endforeach()
69    if(NOT found)
70      list(APPEND out_flags ${input_flag})
71    endif()
72  endforeach()
73
74  set(${output_flags} "${out_flags}" PARENT_SCOPE)
75endfunction(remove_duplicated_flags)
76
77# Collect flags from dependency list.  To see which flags come with each
78# dependence, pass `SHOW_INTERMEDIATE_OBJECTS=DEPS` to cmake.
79function(get_flags_from_dep_list output_list)
80  set(flag_list "")
81  foreach(dep IN LISTS ARGN)
82    if(NOT dep)
83      continue()
84    endif()
85
86    get_fq_dep_name(fq_dep_name ${dep})
87
88    if(NOT TARGET ${fq_dep_name})
89      continue()
90    endif()
91
92    get_target_property(flags ${fq_dep_name} "FLAGS")
93
94    if(flags AND "${SHOW_INTERMEDIATE_OBJECTS}" STREQUAL "DEPS")
95      message(STATUS "  FLAGS from dependency ${fq_dep_name} are ${flags}")
96    endif()
97
98    foreach(flag IN LISTS flags)
99      if(flag)
100        list(APPEND flag_list ${flag})
101      endif()
102    endforeach()
103  endforeach(dep)
104
105  list(REMOVE_DUPLICATES flag_list)
106
107  set(${output_list} ${flag_list} PARENT_SCOPE)
108endfunction(get_flags_from_dep_list)
109
110# Given a `flag` without modifier, scan through the list of dependency, append
111# `.__NO_flag` to any target that has `flag` in its FLAGS property.
112function(get_fq_dep_list_without_flag output_list flag)
113  set(fq_dep_no_flag_list "")
114  foreach(dep IN LISTS ARGN)
115    get_fq_dep_name(fq_dep_name ${dep})
116    if(TARGET ${fq_dep_name})
117      get_target_property(dep_flags ${fq_dep_name} "FLAGS")
118      # Only target with `flag` has `.__NO_flag` target, `flag__NO` and
119      # `flag__ONLY` do not.
120      if(${flag} IN_LIST dep_flags)
121        list(APPEND fq_dep_no_flag_list "${fq_dep_name}.__NO_${flag}")
122      else()
123        list(APPEND fq_dep_no_flag_list ${fq_dep_name})
124      endif()
125    else()
126      list(APPEND fq_dep_no_flag_list ${fq_dep_name})
127    endif()
128  endforeach(dep)
129  set(${output_list} ${fq_dep_no_flag_list} PARENT_SCOPE)
130endfunction(get_fq_dep_list_without_flag)
131
132# Special flags
133set(FMA_OPT_FLAG "FMA_OPT")
134
135# Skip FMA_OPT flag for targets that don't support fma.
136if(NOT(LIBC_TARGET_ARCHITECTURE_IS_X86 AND (LIBC_CPU_FEATURES MATCHES "FMA")))
137  set(SKIP_FLAG_EXPANSION_FMA_OPT TRUE)
138endif()
139