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