# HG changeset patch
# User AnyCPU
# Date 1389790142 -7200
#      Wed Jan 15 14:49:02 2014 +0200
# Node ID d553c78f77c222d2e823b923a9048eb5bacdf7dd
# Parent  1e1d12f4323c33b279207635af6a94bc01a0bf4e
Build FLTK via Clang compiler and native Windows tools: CMake, NMake.

diff -r 1e1d12f4323c -r d553c78f77c2 CMakeLists.txt
--- a/CMakeLists.txt	Sun Jan 26 14:31:14 2014 +0200
+++ b/CMakeLists.txt	Wed Jan 15 14:49:02 2014 +0200
@@ -43,6 +43,66 @@
 # platform dependent information
 #######################################################################
 
+# Clang and native tools on Windows.
+SET(CLANG_WIN_NATIVE FALSE)
+SET(CLANG_WIN_NATIVE32 FALSE)
+SET(CLANG_WIN_NATIVE64 FALSE)
+
+if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+    if(WIN32)
+        SET(CMAKE_SYSTEM_NAME Windows)
+        # Ability to make library file automatically.
+        SET(CMAKE_AR lib)
+        # Does not have alternative in MS dev tools. Make it empty.
+        SET(CMAKE_RANLIB)
+
+        SET(CLANG_WIN_NATIVE TRUE)
+
+        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+            SET(CLANG_WIN_NATIVE64 TRUE)
+        elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
+            SET(CLANG_WIN_NATIVE32 TRUE)
+        endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
+
+        get_filename_component(CLANG_BIN "${CMAKE_C_COMPILER}" PATH)
+        get_filename_component(CLANG_INC "${CLANG_BIN}" PATH)
+        set(CLANG_INC "${CLANG_INC}/include")
+    endif(WIN32) 
+endif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+
+if(CLANG_WIN_NATIVE)
+    # Clang's std include path.
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I${CLANG_INC}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CLANG_INC}")
+
+    # Maximum compatibility with MSVC ABI.
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fms-extensions -fms-compatibility -fno-rtti -fno-exceptions -fno-omit-frame-pointer")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -x c++ -fms-extensions -fms-compatibility -fno-rtti -fno-exceptions -fno-cxx-exceptions -fno-omit-frame-pointer")
+
+    # Release mode.
+    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g0 -DNDEBUG")
+    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g0 -DNDEBUG")
+
+    if(CLANG_WIN_NATIVE64)
+        add_definitions(-DWIN32 -DWIN64)
+
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -target x86_64-pc-win32")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -target x86_64-pc-win32")
+
+        SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x64 /ltcg /subsystem:windows  /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+        SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x64 /ltcg /subsystem:windows  /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+    elseif(CLANG_WIN_NATIVE32)
+        add_definitions(-DWIN32)
+
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32 -target i386-pc-win32")
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -target i386-pc-win32")
+
+        SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x32 /ltcg /subsystem:windows  /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+        SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x32 /ltcg /subsystem:windows  /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+    endif(CLANG_WIN_NATIVE64)
+endif(CLANG_WIN_NATIVE)
+#
+
 include(TestBigEndian)
 TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
 
@@ -59,10 +119,10 @@
 endif(APPLE)
 
 if(WIN32)
-   if(MSVC)
+   if(MSVC OR CLANG_WIN_NATIVE)
       add_definitions(-DWIN32_LEAN_AND_MEAN)
       add_definitions(-D_CRT_SECURE_NO_WARNINGS)
-   endif(MSVC)
+   endif(MSVC OR CLANG_WIN_NATIVE)
    if(CMAKE_C_COMPILER_ID STREQUAL GNU)
       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-subsystem,windows")
    endif(CMAKE_C_COMPILER_ID STREQUAL GNU)
@@ -177,9 +237,9 @@
 CHECK_FUNCTION_EXISTS(snprintf               HAVE_SNPRINTF)
 
 # not really true but we convert strcasecmp calls to _stricmp calls in flstring.h
-if(MSVC)
+if(MSVC OR CLANG_WIN_NATIVE)
    set(HAVE_STRCASECMP 1)
-endif(MSVC)
+endif(MSVC OR CLANG_WIN_NATIVE)
 CHECK_FUNCTION_EXISTS(strcasecmp             HAVE_STRCASECMP)
 
 CHECK_FUNCTION_EXISTS(strlcat                HAVE_STRLCAT)
@@ -421,10 +481,10 @@
 option(OPTION_LARGE_FILE "enable large file support" ON)
 
 if(OPTION_LARGE_FILE)
-   if(NOT MSVC)
+   if(NOT MSVC AND NOT CLANG_WIN_NATIVE)
       add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE)
       list(APPEND FLTK_CFLAGS -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE)
-   endif(NOT MSVC)
+   endif(NOT MSVC AND NOT CLANG_WIN_NATIVE)
 endif(OPTION_LARGE_FILE)
 
 #######################################################################
diff -r 1e1d12f4323c -r d553c78f77c2 jpeg/CMakeLists.txt
--- a/jpeg/CMakeLists.txt	Sun Jan 26 14:31:14 2014 +0200
+++ b/jpeg/CMakeLists.txt	Wed Jan 15 14:49:02 2014 +0200
@@ -34,7 +34,7 @@
 #######################################################################
 add_library(fltk_jpeg ${systemdependent_SRCS} ${common_SRCS} ${compression_SRCS} ${decompression_SRCS})
 
-if(MSVC)
+if(MSVC OR CLANG_WIN_NATIVE)
    set_target_properties(fltk_jpeg
       PROPERTIES
          OUTPUT_NAME fltkjpeg
@@ -43,7 +43,7 @@
    if(OPTION_LARGE_FILE)
       set_target_properties(fltk_jpeg PROPERTIES LINK_FLAGS /LARGEADDRESSAWARE)
    endif(OPTION_LARGE_FILE)
-endif(MSVC)
+endif(MSVC OR CLANG_WIN_NATIVE)
 
 install(TARGETS fltk_jpeg
    EXPORT fltk-install
diff -r 1e1d12f4323c -r d553c78f77c2 png/CMakeLists.txt
--- a/png/CMakeLists.txt	Sun Jan 26 14:31:14 2014 +0200
+++ b/png/CMakeLists.txt	Wed Jan 15 14:49:02 2014 +0200
@@ -18,14 +18,14 @@
 #######################################################################
 add_library(fltk_png ${PNG_SRCS})
 
-if(MSVC)
+if(MSVC OR CLANG_WIN_NATIVE)
    set_target_properties(fltk_png PROPERTIES OUTPUT_NAME fltkpng)
    if(OPTION_LARGE_FILE)
       set_target_properties(fltk_png PROPERTIES LINK_FLAGS /LARGEADDRESSAWARE)
    endif(OPTION_LARGE_FILE)
 else()
    set_target_properties(fltk_png PROPERTIES OUTPUT_NAME fltk_png)
-endif(MSVC)
+endif(MSVC OR CLANG_WIN_NATIVE)
 
 target_link_libraries(fltk_png ${FLTK_ZLIB_LIBRARIES})
 
diff -r 1e1d12f4323c -r d553c78f77c2 zlib/CMakeLists.txt
--- a/zlib/CMakeLists.txt	Sun Jan 26 14:31:14 2014 +0200
+++ b/zlib/CMakeLists.txt	Wed Jan 15 14:49:02 2014 +0200
@@ -15,7 +15,7 @@
 #######################################################################
 add_library(fltk_z ${ZLIB_SRCS})
 
-if(MSVC)
+if(MSVC OR CLANG_WIN_NATIVE)
    set_target_properties(fltk_z
       PROPERTIES
          OUTPUT_NAME zlib
@@ -24,7 +24,7 @@
    if(OPTION_LARGE_FILE)
       set_target_properties(fltk_z PROPERTIES LINK_FLAGS /LARGEADDRESSAWARE)
    endif(OPTION_LARGE_FILE)
-endif(MSVC)
+endif(MSVC OR CLANG_WIN_NATIVE)
 
 install(TARGETS fltk_z
    EXPORT fltk-install
