xref: /llvm-project/llvm/cmake/modules/LLVMProcessSources.cmake (revision b07e7b76c5d532a611cf19ae083a94d44bfe0c5d)
1function(llvm_replace_compiler_option var old new)
2  # Replaces a compiler option or switch `old' in `var' by `new'.
3  # If `old' is not in `var', appends `new' to `var'.
4  # Example: llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
5  # If the option already is on the variable, don't add it:
6  if( "${${var}}" MATCHES "(^| )${new}($| )" )
7    set(n "")
8  else()
9    set(n "${new}")
10  endif()
11  if( "${${var}}" MATCHES "(^| )${old}($| )" )
12    string( REGEX REPLACE "(^| )${old}($| )" " ${n} " ${var} "${${var}}" )
13  else()
14    set( ${var} "${${var}} ${n}" )
15  endif()
16  set( ${var} "${${var}}" PARENT_SCOPE )
17endfunction(llvm_replace_compiler_option)
18
19macro(add_td_sources srcs)
20  file(GLOB tds *.td)
21  if( tds )
22    source_group("TableGen descriptions" FILES ${tds})
23    set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
24    list(APPEND ${srcs} ${tds})
25  endif()
26endmacro(add_td_sources)
27
28function(add_header_files_for_glob hdrs_out glob)
29  file(GLOB hds ${glob})
30  set(filtered)
31  foreach(file ${hds})
32    # Explicit existence check is necessary to filter dangling symlinks
33    # out.  See https://bugs.gentoo.org/674662.
34    if(EXISTS ${file})
35      list(APPEND filtered ${file})
36    endif()
37  endforeach()
38  set(${hdrs_out} ${filtered} PARENT_SCOPE)
39endfunction(add_header_files_for_glob)
40
41function(find_all_header_files hdrs_out additional_headerdirs)
42  add_header_files_for_glob(hds *.h)
43  list(APPEND all_headers ${hds})
44
45  foreach(additional_dir ${additional_headerdirs})
46    add_header_files_for_glob(hds "${additional_dir}/*.h")
47    list(APPEND all_headers ${hds})
48    add_header_files_for_glob(hds "${additional_dir}/*.inc")
49    list(APPEND all_headers ${hds})
50  endforeach(additional_dir)
51
52  set( ${hdrs_out} ${all_headers} PARENT_SCOPE )
53endfunction(find_all_header_files)
54
55
56function(llvm_process_sources OUT_VAR)
57  cmake_parse_arguments(ARG "PARTIAL_SOURCES_INTENDED" "" "ADDITIONAL_HEADERS;ADDITIONAL_HEADER_DIRS" ${ARGN})
58  set(sources ${ARG_UNPARSED_ARGUMENTS})
59  llvm_check_source_file_list(${sources})
60
61  # This adds .td and .h files to the Visual Studio solution:
62  add_td_sources(sources)
63  find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}")
64  if (hdrs)
65    set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON)
66  endif()
67  set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
68  list(APPEND sources ${ARG_ADDITIONAL_HEADERS} ${hdrs})
69
70  set( ${OUT_VAR} ${sources} PARENT_SCOPE )
71endfunction(llvm_process_sources)
72
73
74function(llvm_check_source_file_list)
75  cmake_parse_arguments(ARG "" "SOURCE_DIR" "" ${ARGN})
76  foreach(l ${ARG_UNPARSED_ARGUMENTS})
77      get_filename_component(fp ${l} REALPATH)
78      list(APPEND listed ${fp})
79  endforeach()
80
81  if(ARG_SOURCE_DIR)
82    file(GLOB globbed
83         "${ARG_SOURCE_DIR}/*.c" "${ARG_SOURCE_DIR}/*.cpp")
84  else()
85    file(GLOB globbed *.c *.cpp)
86  endif()
87
88  set_property(DIRECTORY APPEND PROPERTY LLVM_SOURCE_FILES ${listed})
89  if (ARG_PARTIAL_SOURCES_INTENDED) # llvm_process_source's scope
90    return()
91  endif()
92  get_directory_property(listed LLVM_SOURCE_FILES)
93
94  foreach(g ${globbed})
95    get_filename_component(fn ${g} NAME)
96    if(ARG_SOURCE_DIR)
97      set(entry "${g}")
98    else()
99      set(entry "${fn}")
100    endif()
101    get_filename_component(gp ${g} REALPATH)
102
103    # Don't reject hidden files. Some editors create backups in the
104    # same directory as the file.
105    if (NOT "${fn}" MATCHES "^\\.")
106      if(NOT ${entry} IN_LIST LLVM_OPTIONAL_SOURCES)
107        if(NOT ${gp} IN_LIST listed)
108          if(ARG_SOURCE_DIR)
109              set(fn_relative "${ARG_SOURCE_DIR}/${fn}")
110          else()
111              set(fn_relative "${fn}")
112          endif()
113          message(SEND_ERROR "Found erroneous configuration for source file ${fn_relative}
114LLVM's build system enforces that all source files are added to a build target, \
115that exactly one build target exists in each directory, \
116and that this target lists all files in that directory. \
117If you want multiple targets in the same directory, add \
118PARTIAL_SOURCES_INTENDED to the target specification, though it is discouraged.
119Please update ${CMAKE_CURRENT_LIST_FILE}\n")
120        endif()
121      endif()
122    endif()
123  endforeach()
124endfunction(llvm_check_source_file_list)
125