xref: /aosp_15_r20/external/llvm/cmake/modules/LLVMProcessSources.cmake (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Workerinclude(AddFileDependencies)
2*9880d681SAndroid Build Coastguard Workerinclude(CMakeParseArguments)
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Workerfunction(llvm_replace_compiler_option var old new)
5*9880d681SAndroid Build Coastguard Worker  # Replaces a compiler option or switch `old' in `var' by `new'.
6*9880d681SAndroid Build Coastguard Worker  # If `old' is not in `var', appends `new' to `var'.
7*9880d681SAndroid Build Coastguard Worker  # Example: llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
8*9880d681SAndroid Build Coastguard Worker  # If the option already is on the variable, don't add it:
9*9880d681SAndroid Build Coastguard Worker  if( "${${var}}" MATCHES "(^| )${new}($| )" )
10*9880d681SAndroid Build Coastguard Worker    set(n "")
11*9880d681SAndroid Build Coastguard Worker  else()
12*9880d681SAndroid Build Coastguard Worker    set(n "${new}")
13*9880d681SAndroid Build Coastguard Worker  endif()
14*9880d681SAndroid Build Coastguard Worker  if( "${${var}}" MATCHES "(^| )${old}($| )" )
15*9880d681SAndroid Build Coastguard Worker    string( REGEX REPLACE "(^| )${old}($| )" " ${n} " ${var} "${${var}}" )
16*9880d681SAndroid Build Coastguard Worker  else()
17*9880d681SAndroid Build Coastguard Worker    set( ${var} "${${var}} ${n}" )
18*9880d681SAndroid Build Coastguard Worker  endif()
19*9880d681SAndroid Build Coastguard Worker  set( ${var} "${${var}}" PARENT_SCOPE )
20*9880d681SAndroid Build Coastguard Workerendfunction(llvm_replace_compiler_option)
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Workermacro(add_td_sources srcs)
23*9880d681SAndroid Build Coastguard Worker  file(GLOB tds *.td)
24*9880d681SAndroid Build Coastguard Worker  if( tds )
25*9880d681SAndroid Build Coastguard Worker    source_group("TableGen descriptions" FILES ${tds})
26*9880d681SAndroid Build Coastguard Worker    set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
27*9880d681SAndroid Build Coastguard Worker    list(APPEND ${srcs} ${tds})
28*9880d681SAndroid Build Coastguard Worker  endif()
29*9880d681SAndroid Build Coastguard Workerendmacro(add_td_sources)
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Workerfunction(add_header_files_for_glob hdrs_out glob)
32*9880d681SAndroid Build Coastguard Worker  file(GLOB hds ${glob})
33*9880d681SAndroid Build Coastguard Worker  set(${hdrs_out} ${hds} PARENT_SCOPE)
34*9880d681SAndroid Build Coastguard Workerendfunction(add_header_files_for_glob)
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Workerfunction(find_all_header_files hdrs_out additional_headerdirs)
37*9880d681SAndroid Build Coastguard Worker  add_header_files_for_glob(hds *.h)
38*9880d681SAndroid Build Coastguard Worker  list(APPEND all_headers ${hds})
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker  foreach(additional_dir ${additional_headerdirs})
41*9880d681SAndroid Build Coastguard Worker    add_header_files_for_glob(hds "${additional_dir}/*.h")
42*9880d681SAndroid Build Coastguard Worker    list(APPEND all_headers ${hds})
43*9880d681SAndroid Build Coastguard Worker    add_header_files_for_glob(hds "${additional_dir}/*.inc")
44*9880d681SAndroid Build Coastguard Worker    list(APPEND all_headers ${hds})
45*9880d681SAndroid Build Coastguard Worker  endforeach(additional_dir)
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker  set( ${hdrs_out} ${all_headers} PARENT_SCOPE )
48*9880d681SAndroid Build Coastguard Workerendfunction(find_all_header_files)
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Workerfunction(llvm_process_sources OUT_VAR)
52*9880d681SAndroid Build Coastguard Worker  cmake_parse_arguments(ARG "" "" "ADDITIONAL_HEADERS;ADDITIONAL_HEADER_DIRS" ${ARGN})
53*9880d681SAndroid Build Coastguard Worker  set(sources ${ARG_UNPARSED_ARGUMENTS})
54*9880d681SAndroid Build Coastguard Worker  llvm_check_source_file_list( ${sources} )
55*9880d681SAndroid Build Coastguard Worker  if( MSVC_IDE OR XCODE )
56*9880d681SAndroid Build Coastguard Worker    # This adds .td and .h files to the Visual Studio solution:
57*9880d681SAndroid Build Coastguard Worker    add_td_sources(sources)
58*9880d681SAndroid Build Coastguard Worker    find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")
59*9880d681SAndroid Build Coastguard Worker    if (hdrs)
60*9880d681SAndroid Build Coastguard Worker      set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON)
61*9880d681SAndroid Build Coastguard Worker    endif()
62*9880d681SAndroid Build Coastguard Worker    set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
63*9880d681SAndroid Build Coastguard Worker    list(APPEND sources ${ARG_ADDITIONAL_HEADERS} ${hdrs})
64*9880d681SAndroid Build Coastguard Worker  endif()
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker  set( ${OUT_VAR} ${sources} PARENT_SCOPE )
67*9880d681SAndroid Build Coastguard Workerendfunction(llvm_process_sources)
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Workerfunction(llvm_check_source_file_list)
71*9880d681SAndroid Build Coastguard Worker  set(listed ${ARGN})
72*9880d681SAndroid Build Coastguard Worker  file(GLOB globbed *.c *.cpp)
73*9880d681SAndroid Build Coastguard Worker  foreach(g ${globbed})
74*9880d681SAndroid Build Coastguard Worker    get_filename_component(fn ${g} NAME)
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker    # Don't reject hidden files. Some editors create backups in the
77*9880d681SAndroid Build Coastguard Worker    # same directory as the file.
78*9880d681SAndroid Build Coastguard Worker    if (NOT "${fn}" MATCHES "^\\.")
79*9880d681SAndroid Build Coastguard Worker      list(FIND LLVM_OPTIONAL_SOURCES ${fn} idx)
80*9880d681SAndroid Build Coastguard Worker      if( idx LESS 0 )
81*9880d681SAndroid Build Coastguard Worker        list(FIND listed ${fn} idx)
82*9880d681SAndroid Build Coastguard Worker        if( idx LESS 0 )
83*9880d681SAndroid Build Coastguard Worker          message(SEND_ERROR "Found unknown source file ${g}
84*9880d681SAndroid Build Coastguard WorkerPlease update ${CMAKE_CURRENT_LIST_FILE}\n")
85*9880d681SAndroid Build Coastguard Worker        endif()
86*9880d681SAndroid Build Coastguard Worker      endif()
87*9880d681SAndroid Build Coastguard Worker    endif()
88*9880d681SAndroid Build Coastguard Worker  endforeach()
89*9880d681SAndroid Build Coastguard Workerendfunction(llvm_check_source_file_list)
90