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
31
32macro(add_header_files srcs)
33  file(GLOB hds *.h)
34  if( hds )
35    set_source_files_properties(${hds} PROPERTIES HEADER_FILE_ONLY ON)
36    list(APPEND ${srcs} ${hds})
37  endif()
38endmacro(add_header_files)
39
40
41function(llvm_process_sources OUT_VAR)
42  cmake_parse_arguments(ARG "" "" "ADDITIONAL_HEADERS" ${ARGN})
43  set(sources ${ARG_UNPARSED_ARGUMENTS})
44  llvm_check_source_file_list( ${sources} )
45  if( MSVC_IDE OR XCODE )
46    # This adds .td and .h files to the Visual Studio solution:
47    add_td_sources(sources)
48    add_header_files(sources)
49    set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
50    list(APPEND sources ${ARG_ADDITIONAL_HEADERS})
51  endif()
52
53  set( ${OUT_VAR} ${sources} PARENT_SCOPE )
54endfunction(llvm_process_sources)
55
56
57function(llvm_check_source_file_list)
58  set(listed ${ARGN})
59  file(GLOB globbed *.c *.cpp)
60  foreach(g ${globbed})
61    get_filename_component(fn ${g} NAME)
62
63    # Don't reject hidden files. Some editors create backups in the
64    # same directory as the file.
65    if (NOT "${fn}" MATCHES "^\\.")
66      list(FIND LLVM_OPTIONAL_SOURCES ${fn} idx)
67      if( idx LESS 0 )
68        list(FIND listed ${fn} idx)
69        if( idx LESS 0 )
70          message(SEND_ERROR "Found unknown source file ${g}
71Please update ${CMAKE_CURRENT_LIST_FILE}\n")
72        endif()
73      endif()
74    endif()
75  endforeach()
76endfunction(llvm_check_source_file_list)
77