[CMAKE]
[reactos.git] / msc.cmake
index 5304db0..864eea0 100644 (file)
--- a/msc.cmake
+++ b/msc.cmake
@@ -1,28 +1,46 @@
 \r
-if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86")\r
-    add_definitions(-D__i386__)\r
+if(0) # Fixme redefinition warning\r
+if(OPTIMIZE STREQUAL "1")\r
+    add_definitions(/O1)\r
+elseif(OPTIMIZE STREQUAL "2")\r
+    add_definitions(/O2)\r
+elseif(OPTIMIZE STREQUAL "3")\r
+    add_definitions(/Ot /Ox /GS-)\r
+elseif(OPTIMIZE STREQUAL "4")\r
+    add_definitions(/Os /Ox /GS-)\r
+elseif(OPTIMIZE STREQUAL "5")\r
+    add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)\r
+endif()\r
 endif()\r
 \r
-add_definitions(-Dinline=__inline)\r
-\r
-if(NOT CMAKE_CROSSCOMPILING)\r
-\r
+add_definitions(/X /GS- /Zl /Zi)\r
+add_definitions(-Dinline=__inline -D__STDC__=1)\r
 \r
+if(${_MACHINE_ARCH_FLAG} MATCHES X86)\r
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")\r
+  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")\r
+  set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")\r
+endif()\r
 \r
-else()\r
+if(${ARCH} MATCHES amd64)\r
+    add_definitions(-D__x86_64)\r
+endif()\r
 \r
-add_definitions(/GS- /Zl /Zi)\r
-add_definitions(-Dinline=__inline -D__STDC__=1)\r
+link_directories("${REACTOS_BINARY_DIR}/importlibs" ${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)\r
 \r
 set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")\r
 \r
+macro(add_pch _target_name _header_filename _src_list)\r
+    get_filename_component(FILE ${_header_filename} NAME)\r
+    set(_gch_filename "${_target_name}_${FILE}.gch")\r
+    add_custom_command(\r
+        OUTPUT ${_gch_filename}\r
+        COMMAND echo Ignoring precompiled header\r
+        DEPENDS ${_header_filename})\r
+endmacro()\r
 \r
-macro(set_entrypoint MODULE ENTRYPOINT)\r
-    if(${ENTRYPOINT} STREQUAL "0")\r
-        set(NEW_LINKER_FLAGS "/ENTRY:0")\r
-    else()\r
-        set(NEW_LINKER_FLAGS "/ENTRY:${ENTRYPOINT}")\r
-    endif()\r
+macro(add_linkerflag MODULE _flag)\r
+    set(NEW_LINKER_FLAGS ${_flag})\r
     get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)\r
     if(LINKER_FLAGS)\r
         set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")\r
@@ -30,46 +48,85 @@ macro(set_entrypoint MODULE ENTRYPOINT)
     set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})\r
 endmacro()\r
 \r
-macro(set_subsystem MODULE SUBSYSTEM)\r
-    set(NEW_LINKER_FLAGS "/subsystem:${SUBSYSTEM}")\r
-    get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)\r
-    if(LINKER_FLAGS)\r
-        set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")\r
+macro(set_entrypoint MODULE ENTRYPOINT)\r
+    if(${ENTRYPOINT} STREQUAL "0")\r
+        add_linkerflag(${MODULE} "/NOENTRY")\r
+    else()\r
+        add_linkerflag(${MODULE} "/ENTRY:${ENTRYPOINT}")\r
     endif()\r
-    set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})\r
 endmacro()\r
 \r
-macro(set_image_base MODULE IMAGE_BASE)\r
-    set(NEW_LINKER_FLAGS "/BASE:${IMAGE_BASE}")\r
-    get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)\r
-    if(LINKER_FLAGS)\r
-        set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")\r
-    endif()\r
-    set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})\r
+macro(set_subsystem MODULE SUBSYSTEM)\r
+    add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")\r
 endmacro()\r
 \r
-macro(add_importlibs MODULE)\r
-    foreach(LIB ${ARGN})\r
-        target_link_libraries(${MODULE} ${LIB}.LIB)\r
-    endforeach()\r
+macro(set_image_base MODULE IMAGE_BASE)\r
+    add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")\r
 endmacro()\r
 \r
 macro(set_module_type MODULE TYPE)\r
     add_dependencies(${MODULE} psdk buildno_header)\r
     if(${TYPE} MATCHES nativecui)\r
         set_subsystem(${MODULE} native)\r
-        add_importlibs(${MODULE} ntdll)\r
+        set_entrypoint(${MODULE} NtProcessStartup@4)\r
     endif()\r
     if (${TYPE} MATCHES win32gui)\r
         set_subsystem(${MODULE} windows)\r
+        set_entrypoint(${MODULE} WinMainCRTStartup)\r
+        if(IS_UNICODE)\r
+            target_link_libraries(${MODULE} mingw_wmain)\r
+        else()\r
+            target_link_libraries(${MODULE} mingw_main)\r
+        endif()\r
+        target_link_libraries(${MODULE} mingw_common msvcsup)\r
     endif ()\r
     if (${TYPE} MATCHES win32cui)\r
         set_subsystem(${MODULE} console)\r
+        set_entrypoint(${MODULE} mainCRTStartup)\r
+        if(IS_UNICODE)\r
+            target_link_libraries(${MODULE} mingw_wmain)\r
+        else()\r
+            target_link_libraries(${MODULE} mingw_main)\r
+        endif()\r
+        target_link_libraries(${MODULE} mingw_common msvcsup)\r
     endif ()\r
+    if(${TYPE} MATCHES win32dll)\r
+        # Need this only because mingw library is broken\r
+        set_entrypoint(${MODULE} DllMainCRTStartup@12)\r
+        if(DEFINED baseaddress_${MODULE})\r
+            set_image_base(${MODULE} ${baseaddress_${MODULE}})\r
+        else()\r
+            message(STATUS "${MODULE} has no base address")\r
+        endif()\r
+        target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)\r
+        add_linkerflag(${MODULE} "/DLL")\r
+    endif()\r
+    if(${TYPE} MATCHES win32ocx)\r
+        set_entrypoint(${MODULE} DllMainCRTStartup@12)\r
+        set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")\r
+        target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)\r
+        add_linkerflag(${MODULE} "/DLL")\r
+    endif()\r
+    if(${TYPE} MATCHES cpl)\r
+        set_entrypoint(${MODULE} DllMainCRTStartup@12)\r
+        set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")\r
+        target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)\r
+        add_linkerflag(${MODULE} "/DLL")\r
+    endif()\r
+    if(${TYPE} MATCHES kernelmodedriver)\r
+        set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")\r
+        set_entrypoint(${MODULE} DriverEntry@8)\r
+        set_subsystem(${MODULE} native)\r
+        set_image_base(${MODULE} 0x00010000)\r
+        add_linkerflag(${MODULE} "/DRIVER")\r
+        add_dependencies(${MODULE} bugcodes)\r
+        target_link_libraries(${MODULE} msvcsup)\r
+    endif()\r
 endmacro()\r
 \r
 macro(set_unicode)\r
-    add_definitions(-DUNICODE -D_UNICODE)\r
+   add_definitions(-DUNICODE -D_UNICODE)\r
+   set(IS_UNICODE 1)\r
 endmacro()\r
 \r
 set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi  /Ob0 /Od")\r
@@ -79,12 +136,75 @@ macro(set_rc_compiler)
 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake\r
 endmacro()\r
 \r
-#idl files support\r
-set(IDL_COMPILER midl)\r
-set(IDL_FLAGS /win32)\r
-set(IDL_HEADER_ARG /h) #.h\r
-set(IDL_TYPELIB_ARG /tlb) #.tlb\r
-set(IDL_SERVER_ARG /sstub) #.c for stub server library\r
-set(IDL_CLIENT_ARG /cstub) #.c for stub client library\r
+# Thanks MS for creating a stupid linker\r
+macro(add_importlib_target _exports_file)\r
+    get_filename_component(_name ${_exports_file} NAME_WE)\r
+    get_target_property(_suffix ${_name} SUFFIX)\r
+    if(${_suffix} STREQUAL "_suffix-NOTFOUND")\r
+        get_target_property(_type ${_name} TYPE)\r
+        if(${_type} MATCHES EXECUTABLE)\r
+            set(_suffix ".exe")\r
+        else()\r
+            set(_suffix ".dll")\r
+        endif()\r
+    endif()\r
+\r
+    # Generate the asm stub file and the export def file\r
+    add_custom_command(\r
+        OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def\r
+        COMMAND native-spec2def --ms --kill-at -r -n=${_name}${_suffix} -d=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def -l=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}\r
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file})\r
+\r
+    # Assemble the stub file\r
+    add_custom_command(\r
+        OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj\r
+        COMMAND ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj /c /Ta ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm\r
+        DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")\r
+\r
+    # Add neccessary importlibs for redirections\r
+    set(_libraries "")\r
+    foreach(_lib ${ARGN})\r
+        list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")\r
+        list(APPEND _dependencies ${_lib})\r
+    endforeach()\r
+\r
+    # Build the importlib\r
+    add_custom_command(\r
+        OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib\r
+        COMMAND LINK /LIB /NOLOGO /MACHINE:X86 /DEF:${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def /OUT:${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${_libraries}\r
+        DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)\r
+\r
+    # Add the importlib target\r
+    add_custom_target(\r
+        lib${_name}\r
+        DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)\r
+\r
+    add_dependencies(lib${_name} asm ${_dependencies})\r
+endmacro()\r
+\r
+macro(add_importlibs MODULE)\r
+    foreach(LIB ${ARGN})\r
+        target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.lib)\r
+        add_dependencies(${MODULE} lib${LIB})\r
+    endforeach()\r
+endmacro()\r
+\r
+macro(spec2def _dllname _spec_file)\r
+    get_filename_component(_file ${_spec_file} NAME_WE)\r
+    add_custom_command(\r
+        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c\r
+        COMMAND native-spec2def --ms --kill-at -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}\r
+        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})\r
+    set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c\r
+        PROPERTIES GENERATED TRUE)\r
+endmacro()\r
+\r
+macro(macro_mc FILE)\r
+    set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)\r
+endmacro()\r
+\r
+file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)\r
+\r
+#pseh workaround\r
+set(PSEH_LIB "pseh")\r
 \r
-endif()
\ No newline at end of file