From 4579b8be19c0afae66a78691b705b7fcb8d6a935 Mon Sep 17 00:00:00 2001
From: Patrick Gansterer <paroga@paroga.com>
Date: Sat, 14 Jul 2012 07:12:23 +0200
Subject: [PATCH] Added very basic Windows CE Makefile support

This patch enables CMake to create Makefiles targeting
Windows CE devices.
CMake needs to be run within an cross compile command prompt
and requires a toolchain file which sets CMAKE_SYSTEM_NAME
to "WindowsCE" and CMAKE_SYSTEM_PROCESSOR to the target
machine architecture (e.g. THUMB).
---
 Modules/Platform/Windows-cl.cmake   |   29 ++++++++++++++++++++++-------
 Modules/Platform/WindowsCE-cl.cmake |   13 +++++++++++++
 Modules/Platform/WindowsCE.cmake    |    1 +
 3 files changed, 36 insertions(+), 7 deletions(-)
 create mode 100644 Modules/Platform/WindowsCE-cl.cmake
 create mode 100644 Modules/Platform/WindowsCE.cmake

diff --git a/Modules/Platform/Windows-cl.cmake b/Modules/Platform/Windows-cl.cmake
index be6abb6..691ac78 100644
--- a/Modules/Platform/Windows-cl.cmake
+++ b/Modules/Platform/Windows-cl.cmake
@@ -7,8 +7,13 @@ SET(WIN32 1)
 
 INCLUDE(Platform/cl)
 
-SET(CMAKE_CREATE_WIN32_EXE /subsystem:windows)
-SET(CMAKE_CREATE_CONSOLE_EXE /subsystem:console)
+IF(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
+  SET(CMAKE_CREATE_WIN32_EXE "/subsystem:windowsce /entry:WinMainCRTStartup")
+  SET(CMAKE_CREATE_CONSOLE_EXE "/subsystem:windowsce /entry:mainACRTStartup")
+ELSE(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
+  SET(CMAKE_CREATE_WIN32_EXE "/subsystem:windows")
+  SET(CMAKE_CREATE_CONSOLE_EXE "/subsystem:console")
+ENDIF(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
 
 IF(CMAKE_GENERATOR MATCHES "Visual Studio 6")
    SET (CMAKE_NO_BUILD_TYPE 1)
@@ -152,18 +157,28 @@ IF(MSVC_VERSION GREATER 1310)
   SET(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_COMMAND> -E vs_link_exe ${CMAKE_CXX_LINK_EXECUTABLE}")
   SET(CMAKE_C_LINK_EXECUTABLE "<CMAKE_COMMAND> -E vs_link_exe ${CMAKE_C_LINK_EXECUTABLE}")
 
+  IF(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
+    STRING(TOUPPER ${MSVC_ARCHITECTURE_FAMILY} MSVC_ARCHITECTURE_FAMILY_UPPER)
+    SET(CMAKE_PLATFORM_DEFINES "/D_WIN32_WCE=0x500 /DUNDER_CE /D${MSVC_ARCHITECTURE_FAMILY} /D_${MSVC_ARCHITECTURE_FAMILY_UPPER}_")
+    SET(CMAKE_C_STANDARD_LIBRARIES_INIT "coredll.lib corelibc.lib ole32.lib oleaut32.lib uuid.lib commctrl.lib ")
+    SET(CMAKE_EXE_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT} /NODEFAULTLIB:oldnames.lib")
+  ELSE(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
+    SET(CMAKE_PLATFORM_DEFINES "/DWIN32")
+    SET(CMAKE_DEBUG_PLATFORM_DEFINES "/RTC1")
+    SET(CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib ")
+  ENDIF(CMAKE_SYSTEM_NAME MATCHES "WindowsCE")
+
   SET(CMAKE_BUILD_TYPE_INIT Debug)
-  SET (CMAKE_CXX_FLAGS_INIT "/DWIN32 /D_WINDOWS /W3 /Zm1000 /EHsc /GR")
-  SET (CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1")
+  SET (CMAKE_CXX_FLAGS_INIT "${CMAKE_PLATFORM_DEFINES} /D_WINDOWS /W3 /Zm1000 /EHsc /GR")
+  SET (CMAKE_CXX_FLAGS_DEBUG_INIT "${CMAKE_DEBUG_PLATFORM_DEFINES} /D_DEBUG /MDd /Zi /Ob0 /Od")
   SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT "/MD /O1 /Ob1 /D NDEBUG")
   SET (CMAKE_CXX_FLAGS_RELEASE_INIT "/MD /O2 /Ob2 /D NDEBUG")
   SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "/MD /Zi /O2 /Ob1 /D NDEBUG")
-  SET (CMAKE_C_FLAGS_INIT "/DWIN32 /D_WINDOWS /W3 /Zm1000")
-  SET (CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi  /Ob0 /Od /RTC1")
+  SET (CMAKE_C_FLAGS_INIT "${CMAKE_PLATFORM_DEFINES} /D_WINDOWS /W3 /Zm1000")
+  SET (CMAKE_C_FLAGS_DEBUG_INIT "${CMAKE_DEBUG_PLATFORM_DEFINES} /D_DEBUG /MDd /Zi  /Ob0 /Od")
   SET (CMAKE_C_FLAGS_MINSIZEREL_INIT "/MD /O1 /Ob1 /D NDEBUG")
   SET (CMAKE_C_FLAGS_RELEASE_INIT "/MD /O2 /Ob2 /D NDEBUG")
   SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MD /Zi /O2 /Ob1 /D NDEBUG")
-  SET (CMAKE_C_STANDARD_LIBRARIES_INIT "kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib ")
   SET (CMAKE_EXE_LINKER_FLAGS_INIT "${CMAKE_EXE_LINKER_FLAGS_INIT}")
 ELSE(MSVC_VERSION GREATER 1310)
   IF(CMAKE_USING_VC_FREE_TOOLS)
diff --git a/Modules/Platform/WindowsCE-cl.cmake b/Modules/Platform/WindowsCE-cl.cmake
new file mode 100644
index 0000000..690b2ad
--- /dev/null
+++ b/Modules/Platform/WindowsCE-cl.cmake
@@ -0,0 +1,13 @@
+if (CMAKE_SYSTEM_PROCESSOR MATCHES "SH4")
+  set (MSVC_ARCHITECTURE_FAMILY "SHx")
+elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "THUMB")
+  set (MSVC_ARCHITECTURE_FAMILY "ARM")
+elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "X86")
+  set (MSVC_ARCHITECTURE_FAMILY "x86")
+else ()
+  set (MSVC_ARCHITECTURE_FAMILY ${CMAKE_SYSTEM_PROCESSOR})
+endif ()
+
+set (MSVC_C_ARCHITECTURE_ID ${CMAKE_SYSTEM_PROCESSOR})
+set (MSVC_CXX_ARCHITECTURE_ID ${CMAKE_SYSTEM_PROCESSOR})
+include (Platform/Windows-cl)
diff --git a/Modules/Platform/WindowsCE.cmake b/Modules/Platform/WindowsCE.cmake
new file mode 100644
index 0000000..151301e
--- /dev/null
+++ b/Modules/Platform/WindowsCE.cmake
@@ -0,0 +1 @@
+include (Platform/Windows)
-- 
1.7.10.msysgit.1

