Don't use -O3 on Mingw, as people report it as unreliable. Use -O2
[oota-llvm.git] / cmake / modules / LLVMProcessSources.cmake
1 include(AddFileDependencies)
2
3 macro(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( "${${var}}" MATCHES "(^| )${old}($| )" )
8     string( REGEX REPLACE "(^| )${old}($| )" " ${new}  " ${var} "${${var}}" )
9   else()
10     set( ${var} "${${var}} ${new}" )
11   endif()
12 endmacro(llvm_replace_compiler_option)
13
14 macro(add_td_sources srcs)
15   file(GLOB tds *.td)
16   if( tds )
17     source_group("TableGen descriptions" FILES ${tds})
18     set_source_files_properties(${tds} PROPERTIES HEADER_FILE_ONLY ON)
19     list(APPEND ${srcs} ${tds})
20   endif()
21 endmacro(add_td_sources)
22
23
24 macro(add_header_files srcs)
25   file(GLOB hds *.h *.def)
26   if( hds )
27     set_source_files_properties(${hds} PROPERTIES HEADER_FILE_ONLY ON)
28     list(APPEND ${srcs} ${hds})
29   endif()
30 endmacro(add_header_files)
31
32
33 function(llvm_process_sources OUT_VAR)
34   set( sources ${ARGN} )
35   llvm_check_source_file_list( ${sources} )
36   # Create file dependencies on the tablegenned files, if any.  Seems
37   # that this is not strictly needed, as dependencies of the .cpp
38   # sources on the tablegenned .inc files are detected and handled,
39   # but just in case...
40   foreach( s ${sources} )
41     set( f ${CMAKE_CURRENT_SOURCE_DIR}/${s} )
42     add_file_dependencies( ${f} ${TABLEGEN_OUTPUT} )
43   endforeach(s)
44   if( MSVC_IDE )
45     # This adds .td and .h files to the Visual Studio solution:
46     add_td_sources(sources)
47     add_header_files(sources)
48   endif()
49
50   # Set common compiler options:
51   if( NOT LLVM_REQUIRES_EH )
52     if( CMAKE_COMPILER_IS_GNUCXX )
53       add_definitions( -fno-exceptions )
54     elseif( MSVC )
55       string( REGEX REPLACE "[ ^]/EHsc ?" " /EHs-c- " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
56       add_definitions( /D_HAS_EXCEPTIONS=0 )
57     endif()
58   endif()
59   if( NOT LLVM_REQUIRES_RTTI )
60     if( CMAKE_COMPILER_IS_GNUCXX )
61       add_definitions( -fno-rtti )
62     elseif( MSVC )
63       string( REGEX REPLACE "[ ^]/GR ?" " /GR- " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
64     endif()
65   endif()
66
67   set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
68   set( ${OUT_VAR} ${sources} PARENT_SCOPE )
69 endfunction(llvm_process_sources)
70
71
72 function(llvm_check_source_file_list)
73   set(listed ${ARGN})
74   file(GLOB globbed *.cpp)
75   foreach(g ${globbed})
76     get_filename_component(fn ${g} NAME)
77     list(FIND listed ${fn} idx)
78     if( idx LESS 0 )
79       message(SEND_ERROR "Found unknown source file ${g}
80 Please update ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt\n")
81     endif()
82   endforeach()
83 endfunction(llvm_check_source_file_list)