[CMAKE]
[reactos.git] / reactos / cmake / gcc.cmake
index 669ffe6..2dc7d48 100644 (file)
@@ -1,6 +1,6 @@
 
 # Show a note about ccache build
-if(CCACHE STREQUAL "ccache")
+if(ENABLE_CCACHE)
     message("-- Enabling ccache build - done")
 endif()
 
@@ -10,19 +10,35 @@ if(NOT DEFINED SEPARATE_DBG)
 endif()
 
 if(NOT DEFINED USE_PSEH3)
-    set(USE_PSEH3 0)
+    set(USE_PSEH3 1)
 endif()
 
 if(USE_PSEH3)
     add_definitions(-D_USE_PSEH3=1)
 endif()
 
+if(NOT DEFINED USE_DUMMY_PSEH)
+    set(USE_DUMMY_PSEH 0)
+endif()
+
+if(USE_DUMMY_PSEH)
+    add_definitions(-D_USE_DUMMY_PSEH=1)
+endif()
+
 # Compiler Core
 add_compile_flags("-pipe -fms-extensions -fno-strict-aliasing")
 if(GCC_VERSION VERSION_GREATER 4.7)
     add_compile_flags("-mstackrealign")
 endif()
 
+if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
+    add_compile_flags_language("-std=gnu89" "C")
+    add_compile_flags("-Wno-microsoft")
+endif()
+
+if(DBG)
+    add_compile_flags_language("-Wold-style-declaration -Wdeclaration-after-statement" "C")
+endif()
 add_compile_flags_language("-fno-rtti -fno-exceptions" "CXX")
 
 #bug
@@ -32,13 +48,19 @@ set(REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
  if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
 string(REPLACE "/" "\\" REACTOS_SOURCE_DIR_NATIVE ${REACTOS_SOURCE_DIR})
 endif()
-add_compile_flags("-fdebug-prefix-map=\"${REACTOS_SOURCE_DIR_NATIVE}\"=ReactOS")
+
+if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+    add_compile_flags("-fdebug-prefix-map=\"${REACTOS_SOURCE_DIR_NATIVE}\"=ReactOS")
+endif()
 
 # Debugging
 if(SEPARATE_DBG)
     add_compile_flags("-gdwarf-2 -g2")
 else()
-    add_compile_flags("-gdwarf-2 -gstrict-dwarf -femit-struct-debug-detailed=none -feliminate-unused-debug-symbols")
+    add_compile_flags("-gdwarf-2 -gstrict-dwarf")
+    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+        add_compile_flags("-femit-struct-debug-detailed=none -feliminate-unused-debug-symbols")
+    endif()
 endif()
 
 # For some reason, cmake sets -fPIC, and we don't want it
@@ -53,15 +75,21 @@ else()
     add_compile_flags("-march=${OARCH}")
 endif()
 
-# Warnings
-add_compile_flags("-Werror -Wall -Wno-char-subscripts -Wpointer-arith -Wno-multichar -Wno-unused-value")
+# Warnings, errors
+if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+    add_compile_flags("-Werror")
+endif()
+
+add_compile_flags("-Wall -Wpointer-arith")
+add_compile_flags("-Wno-char-subscripts -Wno-multichar -Wno-unused-value")
 
-if(GCC_VERSION VERSION_LESS 4.7)
-    add_compile_flags("-Wno-error=uninitialized")
-elseif(GCC_VERSION VERSION_EQUAL 4.7 OR GCC_VERSION VERSION_GREATER 4.7)
-    add_compile_flags("-Wno-error=unused-but-set-variable -Wno-maybe-uninitialized -Wno-error=narrowing")
+if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+    add_compile_flags("-Wno-maybe-uninitialized")
+    add_compile_flags("-Wno-error=unused-but-set-variable")
 endif()
 
+add_compile_flags("-Wno-error=type-limits")
+
 if(ARCH STREQUAL "amd64")
     add_compile_flags("-Wno-format")
 elseif(ARCH STREQUAL "arm")
@@ -74,7 +102,7 @@ if(OPTIMIZE STREQUAL "1")
 elseif(OPTIMIZE STREQUAL "2")
     add_compile_flags("-Os")
 elseif(OPTIMIZE STREQUAL "3")
-    add_compile_flags("-O1 -fno-inline-functions-called-once -fno-tree-sra")
+    add_compile_flags("-Og")
 elseif(OPTIMIZE STREQUAL "4")
     add_compile_flags("-O1")
 elseif(OPTIMIZE STREQUAL "5")
@@ -87,11 +115,15 @@ endif()
 
 # Link-time code generation
 if(LTCG)
-    add_compile_flags("-flto -Wno-error=clobbered")
+    add_compile_flags("-flto -fno-fat-lto-objects")
 endif()
 
 if(ARCH STREQUAL "i386")
-    add_compile_flags("-mpreferred-stack-boundary=3 -fno-set-stack-executable -fno-optimize-sibling-calls -fno-omit-frame-pointer")
+    add_compile_flags("-fno-optimize-sibling-calls -fno-omit-frame-pointer")
+    if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+        add_compile_flags("-mpreferred-stack-boundary=3 -fno-set-stack-executable")
+    endif()
+    # FIXME: this doesn't work. CMAKE_BUILD_TYPE is always "Debug"
     if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
         add_compile_flags("-momit-leaf-frame-pointer")
     endif()
@@ -109,7 +141,7 @@ endif()
 
 add_definitions(-D_inline=__inline)
 
-# alternative arch name
+# Alternative arch name
 if(ARCH STREQUAL "amd64")
     set(ARCH2 x86_64)
 else()
@@ -168,9 +200,13 @@ endif()
 set(CMAKE_EXE_LINKER_FLAGS "-nostdlib -Wl,--enable-auto-image-base,--disable-auto-import,--disable-stdcall-fixup")
 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS_INIT} -Wl,--disable-stdcall-fixup")
 
-SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> -Wa,--compress-debug-sections <FLAGS> -o <OBJECT> -c <SOURCE>")
+if(NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
+    set(_compress_debug_sections_flag "-Wa,--compress-debug-sections")
+endif()
+
+SET(CMAKE_C_COMPILE_OBJECT "${CCACHE} <CMAKE_C_COMPILER> <DEFINES> ${_compress_debug_sections_flag} <FLAGS> -o <OBJECT> -c <SOURCE>")
 SET(CMAKE_CXX_COMPILE_OBJECT "${CCACHE} <CMAKE_CXX_COMPILER>  <DEFINES> <FLAGS> -o <OBJECT> -c <SOURCE>")
-set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -Wa,--compress-debug-sections -x assembler-with-cpp -o <OBJECT> -I${REACTOS_SOURCE_DIR}/include/asm -I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
+set(CMAKE_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> ${_compress_debug_sections_flag} -x assembler-with-cpp -o <OBJECT> -I${REACTOS_SOURCE_DIR}/include/asm -I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> -D__ASM__ -c <SOURCE>")
 
 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> -O coff <FLAGS> -DRC_INVOKED -D__WIN32__=1 -D__FLAT__=1 ${I18N_DEFS} <DEFINES> <SOURCE> <OBJECT>")
 set(CMAKE_DEPFILE_FLAGS_RC "--preprocessor \"${MINGW_TOOLCHAIN_PREFIX}gcc${MINGW_TOOLCHAIN_SUFFIX} -E -xc-header -MMD -MF <DEPFILE> -MT <OBJECT>\" ")
@@ -234,18 +270,18 @@ if(NOT ARCH STREQUAL "i386")
 endif()
 
 function(generate_import_lib _libname _dllname _spec_file)
-    # generate the def for the import lib
+    # Generate the def for the import lib
     add_custom_command(
         OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def
-        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
+        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} --implib -d=${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
     set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def PROPERTIES EXTERNAL_OBJECT TRUE)
 
-    #create normal importlib
+    # Create normal importlib
     _add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
     set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB" PREFIX "")
 
-    #create delayed importlib
+    # Create delayed importlib
     _add_library(${_libname}_delayed STATIC EXCLUDE_FROM_ALL ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
     set_target_properties(${_libname}_delayed PROPERTIES LINKER_LANGUAGE "IMPLIB_DELAYED" PREFIX "")
 endfunction()
@@ -254,7 +290,7 @@ endfunction()
 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-lib=<TARGET>")
 set(CMAKE_IMPLIB_DELAYED_CREATE_STATIC_LIBRARY "${CMAKE_DLLTOOL} --def <OBJECTS> --kill-at --output-delaylib=<TARGET>")
 function(spec2def _dllname _spec_file)
-    # do we also want to add importlib targets?
+    # Do we also want to add importlib targets?
     if(${ARGC} GREATER 2)
         if(${ARGN} STREQUAL "ADD_IMPORTLIB")
             set(__add_importlib TRUE)
@@ -263,18 +299,18 @@ function(spec2def _dllname _spec_file)
         endif()
     endif()
 
-    # get library basename
+    # Get library basename
     get_filename_component(_file ${_dllname} NAME_WE)
 
-    # error out on anything else than spec
+    # Error out on anything else than spec
     if(NOT ${_spec_file} MATCHES ".*\\.spec")
         message(FATAL_ERROR "spec2def only takes spec files as input.")
     endif()
 
-    # generate exports def and stubs C file for the module
+    # Generate exports def and C stubs file for the DLL
     add_custom_command(
         OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
-        COMMAND native-spec2def -n=${_dllname} --kill-at -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
+        COMMAND native-spec2def -n=${_dllname} -a=${ARCH2} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
         DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
 
     if(__add_importlib)
@@ -286,75 +322,45 @@ macro(macro_mc FLAG FILE)
     set(COMMAND_MC ${CMAKE_MC_COMPILER} ${FLAG} -b ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos)
 endmacro()
 
-#pseh lib, needed with mingw
+# PSEH lib, needed with mingw
 set(PSEH_LIB "pseh")
 
 # Macros
 if(PCH)
-    macro(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
-        # Add the precompiled header to the build
-        get_filename_component(_FILE ${_header_filename} NAME)
-        set(_gch_filename "${_FILE}.gch")
-        list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
-
-        # This gets us our includes
-        get_directory_property(DIRINC INCLUDE_DIRECTORIES)
-        foreach(item ${DIRINC})
-            list(APPEND ${_out_compile_flags} -I${item})
-        endforeach()
-
-        # This our definitions
-        get_directory_property(_compiler_flags DEFINITIONS)
-        list(APPEND ${_out_compile_flags} ${_compiler_flags})
-
-        # This gets any specific definitions that were added with set-target-property
-        get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
-        if(_target_defs)
-            foreach(item ${_target_defs})
-                list(APPEND ${_out_compile_flags} -D${item})
-            endforeach()
-        endif()
+    add_compile_flags("-Winvalid-pch -Werror=invalid-pch")
+    macro(add_pch _target _pch _sources)
+        # When including x.h GCC looks for x.h.gch first
+        set(_pch_final_name "${_target}_pch.h")
+        set(_gch ${CMAKE_CURRENT_BINARY_DIR}/${_pch_final_name}.gch)
 
         if(IS_CPP)
-            list(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
+            set(_pch_language CXX)
         else()
-            list(APPEND ${_out_compile_flags} ${CMAKE_C_FLAGS})
+            set(_pch_language C)
         endif()
 
-        separate_arguments(${_out_compile_flags})
-    endmacro()
-
-    macro(add_pch _target_name _FILE)
-        set(_header_filename ${CMAKE_CURRENT_SOURCE_DIR}/${_FILE})
-        get_filename_component(_basename ${_FILE} NAME)
-        set(_gch_filename ${_basename}.gch)
-        _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
-
-        if(IS_CPP)
-            set(__lang CXX)
-            set(__compiler ${CCACHE} ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1})
-        else()
-            set(__lang C)
-            set(__compiler ${CCACHE} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
+        # Build the precompiled header
+        # HEADER_FILE_ONLY FALSE: force compiling the header
+        # EXTERNAL_SOURCE TRUE: don't use the .gch file when linking
+        set_source_files_properties(${_pch} PROPERTIES
+            HEADER_FILE_ONLY FALSE
+            LANGUAGE ${_pch_language}
+            EXTERNAL_SOURCE TRUE
+            OBJECT_LOCATION ${_gch})
+
+        if(ENABLE_CCACHE)
+            set(_ccache_flag "-fpch-preprocess")
         endif()
 
-        add_custom_command(OUTPUT ${_gch_filename}
-            COMMAND ${__compiler} ${_args}
-            IMPLICIT_DEPENDS ${__lang} ${_header_filename}
-            DEPENDS ${_header_filename} ${ARGN})
-        get_target_property(_src_files ${_target_name} SOURCES)
-        add_target_compile_flags(${_target_name} "-fpch-preprocess -Winvalid-pch -Wno-error=invalid-pch")
-        foreach(_item in ${_src_files})
-            get_source_file_property(__src_lang ${_item} LANGUAGE)
-            if(__src_lang STREQUAL __lang)
-                set_source_files_properties(${_item} PROPERTIES OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_gch_filename})
-            endif()
+        # Include the gch in the specified source files, skipping the pch file itself
+        list(REMOVE_ITEM ${_sources} ${_pch})
+        foreach(_src ${${_sources}})
+            set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_ccache_flag} -include ${_pch_final_name}")
+            set_property(SOURCE ${_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
         endforeach()
-        #set dependency checking : depends on precompiled header only which already depends on deeper header
-        set_target_properties(${_target_name} PROPERTIES IMPLICIT_DEPENDS_INCLUDE_TRANSFORM "\"${_basename}\"=;<${_basename}>=")
     endmacro()
 else()
-    macro(add_pch _target_name _FILE)
+    macro(add_pch _target _pch _sources)
     endmacro()
 endif()
 
@@ -385,4 +391,3 @@ endfunction()
 macro(add_asm_files _target)
     list(APPEND ${_target} ${ARGN})
 endmacro()
-