1include(AddFileDependencies)
2
3function(llvm_replace_compiler_option var old new)
4  # Replaces a compiler option or switch `old' in `var' by `new'.
5  # If `old' is not in `var', appends `new' to `var'.
6  # Example: llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
7  # If the option already is on the variable, don't add it:
8  if( "${${var}}" MATCHES "(^| )${new}($| )" )
9    set(n "")
10  else()
11    set(n "${new}")
12  endif()
13  if( "${${var}}" MATCHES "(^| )${old}($| )" )
14    string( REGEX REPLACE "(^| )${old}($| )" " ${n} " ${var} "${${var}}" )
15  else()
16    set( ${var} "${${var}} ${n}" )
17  endif()
18  set( ${var} "${${var}}" PARENT_SCOPE )
19endfunction(llvm_replace_compiler_option)
20
21macro(add_td_sources srcs)
22  file(GLOB tds *.td)
23  if( tds )
24    source_group("TableGen descriptions" FILES ${tds})
25    set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
26    list(APPEND ${srcs} ${tds})
27  endif()
28endmacro(add_td_sources)
29
30
31macro(add_header_files srcs)
32  file(GLOB hds *.h)
33  if( hds )
34    set_source_files_properties(${hds} PROPERTIES HEADER_FILE_ONLY ON)
35    list(APPEND ${srcs} ${hds})
36  endif()
37endmacro(add_header_files)
38
39
40function(llvm_process_sources OUT_VAR)
41  set( sources ${ARGN} )
42  llvm_check_source_file_list( ${sources} )
43  # Create file dependencies on the tablegenned files, if any.  Seems
44  # that this is not strictly needed, as dependencies of the .cpp
45  # sources on the tablegenned .inc files are detected and handled,
46  # but just in case...
47  foreach( s ${sources} )
48    set( f ${CMAKE_CURRENT_SOURCE_DIR}/${s} )
49    add_file_dependencies( ${f} ${TABLEGEN_OUTPUT} )
50  endforeach(s)
51  if( MSVC_IDE )
52    # This adds .td and .h files to the Visual Studio solution:
53    # FIXME: Shall we handle *.def here?
54    add_td_sources(sources)
55    add_header_files(sources)
56  endif()
57
58  # Set common compiler options:
59  if( NOT LLVM_REQUIRES_EH )
60    if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
61      add_definitions( -fno-exceptions )
62    elseif( MSVC )
63      llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHsc" "/EHs-c-")
64      add_definitions( /D_HAS_EXCEPTIONS=0 )
65    endif()
66  endif()
67  if( NOT LLVM_REQUIRES_RTTI )
68    if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
69      llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti")
70    elseif( MSVC )
71      llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-")
72    endif()
73  endif()
74
75  set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
76  set( ${OUT_VAR} ${sources} PARENT_SCOPE )
77endfunction(llvm_process_sources)
78
79
80function(llvm_check_source_file_list)
81  set(listed ${ARGN})
82  file(GLOB globbed *.cpp)
83  foreach(g ${globbed})
84    get_filename_component(fn ${g} NAME)
85    list(FIND LLVM_OPTIONAL_SOURCES ${fn} idx)
86    if( idx LESS 0 )
87      list(FIND listed ${fn} idx)
88      if( idx LESS 0 )
89        message(SEND_ERROR "Found unknown source file ${g}
90Please update ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt\n")
91      endif()
92    endif()
93  endforeach()
94endfunction(llvm_check_source_file_list)
95