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(filtered)
34  foreach(file ${hds})
35    # Explicit existence check is necessary to filter dangling symlinks
36    # out.  See https://bugs.gentoo.org/674662.
37    if(EXISTS ${file})
38      list(APPEND filtered ${file})
39    endif()
40  endforeach()
41  set(${hdrs_out} ${filtered} PARENT_SCOPE)
42endfunction(add_header_files_for_glob)
43
44function(find_all_header_files hdrs_out additional_headerdirs)
45  add_header_files_for_glob(hds *.h)
46  list(APPEND all_headers ${hds})
47
48  foreach(additional_dir ${additional_headerdirs})
49    add_header_files_for_glob(hds "${additional_dir}/*.h")
50    list(APPEND all_headers ${hds})
51    add_header_files_for_glob(hds "${additional_dir}/*.inc")
52    list(APPEND all_headers ${hds})
53  endforeach(additional_dir)
54
55  set( ${hdrs_out} ${all_headers} PARENT_SCOPE )
56endfunction(find_all_header_files)
57
58
59function(llvm_process_sources OUT_VAR)
60  cmake_parse_arguments(ARG "PARTIAL_SOURCES_INTENDED" "" "ADDITIONAL_HEADERS;ADDITIONAL_HEADER_DIRS" ${ARGN})
61  set(sources ${ARG_UNPARSED_ARGUMENTS})
62  llvm_check_source_file_list(${sources})
63
64  # This adds .td and .h files to the Visual Studio solution:
65  add_td_sources(sources)
66  find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")
67  if (hdrs)
68    set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON)
69  endif()
70  set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
71  list(APPEND sources ${ARG_ADDITIONAL_HEADERS} ${hdrs})
72
73  set( ${OUT_VAR} ${sources} PARENT_SCOPE )
74endfunction(llvm_process_sources)
75
76
77function(llvm_check_source_file_list)
78  cmake_parse_arguments(ARG "" "SOURCE_DIR" "" ${ARGN})
79  foreach(l ${ARG_UNPARSED_ARGUMENTS})
80      get_filename_component(fp ${l} REALPATH)
81      list(APPEND listed ${fp})
82  endforeach()
83
84  if(ARG_SOURCE_DIR)
85    file(GLOB globbed
86         "${ARG_SOURCE_DIR}/*.c" "${ARG_SOURCE_DIR}/*.cpp")
87  else()
88    file(GLOB globbed *.c *.cpp)
89  endif()
90
91  set_property(DIRECTORY APPEND PROPERTY LLVM_SOURCE_FILES ${listed})
92  if (ARG_PARTIAL_SOURCES_INTENDED) # llvm_process_source's scope
93    return()
94  endif()
95  get_directory_property(listed LLVM_SOURCE_FILES)
96
97  foreach(g ${globbed})
98    get_filename_component(fn ${g} NAME)
99    if(ARG_SOURCE_DIR)
100      set(entry "${g}")
101    else()
102      set(entry "${fn}")
103    endif()
104    get_filename_component(gp ${g} REALPATH)
105
106    # Don't reject hidden files. Some editors create backups in the
107    # same directory as the file.
108    if (NOT "${fn}" MATCHES "^\\.")
109      if(NOT ${entry} IN_LIST LLVM_OPTIONAL_SOURCES)
110        if(NOT ${gp} IN_LIST listed)
111          if(ARG_SOURCE_DIR)
112              set(fn_relative "${ARG_SOURCE_DIR}/${fn}")
113          else()
114              set(fn_relative "${fn}")
115          endif()
116          message(SEND_ERROR "Found erroneous configuration for source file ${fn_relative}
117LLVM's build system enforces that all source files are added to a build target, \
118that exactly one build target exists in each directory, \
119and that this target lists all files in that directory. \
120If you want multiple targets in the same directory, add \
121PARTIAL_SOURCES_INTENDED to the target specification, though it is discouraged.
122Please update ${CMAKE_CURRENT_LIST_FILE}\n")
123        endif()
124      endif()
125    endif()
126  endforeach()
127endfunction(llvm_check_source_file_list)
128