[CMake] Let llvm_process_sources check not only *.cpp but also *.c.
[oota-llvm.git] / cmake / modules / LLVMProcessSources.cmake
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
32 macro(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()
38 endmacro(add_header_files)
39
40
41 function(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   # Create file dependencies on the tablegenned files, if any.  Seems
46   # that this is not strictly needed, as dependencies of the .cpp
47   # sources on the tablegenned .inc files are detected and handled,
48   # but just in case...
49   foreach( s ${sources} )
50     set( f ${CMAKE_CURRENT_SOURCE_DIR}/${s} )
51     add_file_dependencies( ${f} ${TABLEGEN_OUTPUT} )
52   endforeach(s)
53   if( MSVC_IDE OR XCODE )
54     # This adds .td and .h files to the Visual Studio solution:
55     add_td_sources(sources)
56     add_header_files(sources)
57     set_source_files_properties(${ARG_ADDITIONAL_HEADERS} PROPERTIES HEADER_FILE_ONLY ON)
58     list(APPEND sources ${ARG_ADDITIONAL_HEADERS})
59   endif()
60
61   # Set common compiler options:
62   if( NOT LLVM_REQUIRES_EH )
63     if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
64       add_definitions( -fno-exceptions )
65     elseif( MSVC )
66       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHsc" "/EHs-c-")
67       add_definitions( /D_HAS_EXCEPTIONS=0 )
68     endif()
69   endif()
70   if( NOT LLVM_REQUIRES_RTTI )
71     if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
72       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti")
73     elseif( MSVC )
74       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-")
75     endif()
76   endif()
77
78   set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
79   set( ${OUT_VAR} ${sources} PARENT_SCOPE )
80 endfunction(llvm_process_sources)
81
82
83 function(llvm_check_source_file_list)
84   set(listed ${ARGN})
85   file(GLOB globbed *.c *.cpp)
86   foreach(g ${globbed})
87     get_filename_component(fn ${g} NAME)
88     list(FIND LLVM_OPTIONAL_SOURCES ${fn} idx)
89     if( idx LESS 0 )
90       list(FIND listed ${fn} idx)
91       if( idx LESS 0 )
92         message(SEND_ERROR "Found unknown source file ${g}
93 Please update ${CMAKE_CURRENT_LIST_FILE}\n")
94       endif()
95     endif()
96   endforeach()
97 endfunction(llvm_check_source_file_list)