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