Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | include(AddFileDependencies) |
| 2 | include(CMakeParseArguments) |
| 3 | |
| 4 | function(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 ) |
| 20 | endfunction(llvm_replace_compiler_option) |
| 21 | |
| 22 | macro(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() |
| 29 | endmacro(add_td_sources) |
| 30 | |
| 31 | function(add_header_files_for_glob hdrs_out glob) |
| 32 | file(GLOB hds ${glob}) |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 42 | endfunction(add_header_files_for_glob) |
| 43 | |
| 44 | function(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 ) |
| 56 | endfunction(find_all_header_files) |
| 57 | |
| 58 | |
| 59 | function(llvm_process_sources OUT_VAR) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 60 | cmake_parse_arguments(ARG "PARTIAL_SOURCES_INTENDED" "" "ADDITIONAL_HEADERS;ADDITIONAL_HEADER_DIRS" ${ARGN}) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | set(sources ${ARG_UNPARSED_ARGUMENTS}) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 62 | if (NOT ARG_PARTIAL_SOURCES_INTENDED) |
| 63 | llvm_check_source_file_list(${sources}) |
| 64 | endif() |
| 65 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 66 | # This adds .td and .h files to the Visual Studio solution: |
| 67 | add_td_sources(sources) |
| 68 | find_all_header_files(hdrs "${ARG_ADDITIONAL_HEADER_DIRS}") |
| 69 | if (hdrs) |
| 70 | set_source_files_properties(${hdrs} PROPERTIES HEADER_FILE_ONLY ON) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 71 | endif() |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 72 | set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON) |
| 73 | list(APPEND sources ${ARG_ADDITIONAL_HEADERS} ${hdrs}) |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 74 | |
| 75 | set( ${OUT_VAR} ${sources} PARENT_SCOPE ) |
| 76 | endfunction(llvm_process_sources) |
| 77 | |
| 78 | |
| 79 | function(llvm_check_source_file_list) |
| 80 | cmake_parse_arguments(ARG "" "SOURCE_DIR" "" ${ARGN}) |
| 81 | foreach(l ${ARG_UNPARSED_ARGUMENTS}) |
| 82 | get_filename_component(fp ${l} REALPATH) |
| 83 | list(APPEND listed ${fp}) |
| 84 | endforeach() |
| 85 | |
| 86 | if(ARG_SOURCE_DIR) |
| 87 | file(GLOB globbed |
| 88 | "${ARG_SOURCE_DIR}/*.c" "${ARG_SOURCE_DIR}/*.cpp") |
| 89 | else() |
| 90 | file(GLOB globbed *.c *.cpp) |
| 91 | endif() |
| 92 | |
| 93 | foreach(g ${globbed}) |
| 94 | get_filename_component(fn ${g} NAME) |
| 95 | if(ARG_SOURCE_DIR) |
| 96 | set(entry "${g}") |
| 97 | else() |
| 98 | set(entry "${fn}") |
| 99 | endif() |
| 100 | get_filename_component(gp ${g} REALPATH) |
| 101 | |
| 102 | # Don't reject hidden files. Some editors create backups in the |
| 103 | # same directory as the file. |
| 104 | if (NOT "${fn}" MATCHES "^\\.") |
| 105 | list(FIND LLVM_OPTIONAL_SOURCES ${entry} idx) |
| 106 | if( idx LESS 0 ) |
| 107 | list(FIND listed ${gp} idx) |
| 108 | if( idx LESS 0 ) |
| 109 | if(ARG_SOURCE_DIR) |
| 110 | set(fn_relative "${ARG_SOURCE_DIR}/${fn}") |
| 111 | else() |
| 112 | set(fn_relative "${fn}") |
| 113 | endif() |
| 114 | message(SEND_ERROR "Found unknown source file ${fn_relative} |
| 115 | Please update ${CMAKE_CURRENT_LIST_FILE}\n") |
| 116 | endif() |
| 117 | endif() |
| 118 | endif() |
| 119 | endforeach() |
| 120 | endfunction(llvm_check_source_file_list) |