Home | History | Annotate | Download | only in modules
      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 *.def)
     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   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     add_td_sources(sources)
     54     add_header_files(sources)
     55   endif()
     56 
     57   # Set common compiler options:
     58   if( NOT LLVM_REQUIRES_EH )
     59     if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
     60       add_definitions( -fno-exceptions )
     61     elseif( MSVC )
     62       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/EHsc" "/EHs-c-")
     63       add_definitions( /D_HAS_EXCEPTIONS=0 )
     64     endif()
     65   endif()
     66   if( NOT LLVM_REQUIRES_RTTI )
     67     if( LLVM_COMPILER_IS_GCC_COMPATIBLE )
     68       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "-frtti" "-fno-rtti")
     69     elseif( MSVC )
     70       llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/GR" "/GR-")
     71     endif()
     72   endif()
     73 
     74   set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE )
     75   set( ${OUT_VAR} ${sources} PARENT_SCOPE )
     76 endfunction(llvm_process_sources)
     77 
     78 
     79 function(llvm_check_source_file_list)
     80   set(listed ${ARGN})
     81   file(GLOB globbed *.cpp)
     82   foreach(g ${globbed})
     83     get_filename_component(fn ${g} NAME)
     84     list(FIND listed ${fn} idx)
     85     if( idx LESS 0 )
     86       message(SEND_ERROR "Found unknown source file ${g}
     87 Please update ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt\n")
     88     endif()
     89   endforeach()
     90 endfunction(llvm_check_source_file_list)
     91