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

diff -r 7aebf0b30658 -r 94e56ce7436f CMakeLists.txt
--- a/CMakeLists.txt	Sat Feb 01 15:50:33 2014 +0200
+++ b/CMakeLists.txt	Wed Jan 15 14:49:02 2014 +0200
@@ -43,6 +43,100 @@
 # platform dependent information
 #######################################################################
 
+# Clang support
+set(CLANG FALSE)
+set(CLANG_BIN "")
+set(CLANG_INCLUDE "")
+
+if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
+    set(CLANG TRUE)
+
+    get_filename_component(CLANG_BIN "${CMAKE_C_COMPILER}" ABSOLUTE)
+    get_filename_component(CLANG_BIN "${CMAKE_C_COMPILER}" PATH)
+endif()
+
+# Search Clang include directory
+if(CLANG)
+    set(CLANG_INCLUDE "$ENV{CLANG_INCLUDE}")
+
+    if(CLANG_INCLUDE STREQUAL "")
+        get_filename_component(CLANG_INCLUDE "${CLANG_BIN}" PATH)
+        set(CLANG_INCLUDE "${CLANG_INCLUDE}/include")
+    endif()
+
+    include_directories("${CLANG_INCLUDE}")
+endif()
+
+set(CLANG_X32 FALSE)
+set(CLANG_X64 FALSE)
+
+if(CLANG)
+    if(CMAKE_SIZEOF_VOID_P EQUAL 4)
+        set(CLANG_X32 TRUE)
+    elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
+        set(CLANG_X64 TRUE)
+    endif()
+endif()
+
+set(CLANG_WIN32_NATIVE FALSE)
+set(CLANG_WIN32_MINGW FALSE)
+set(CLANG_WIN32_CYGWIN FALSE)
+
+# Detect platform
+if(CLANG AND WIN32)
+    if(CYGWIN)
+        set(CLANG_WIN32_CYGWIN TRUE)
+    elseif(MINGW)
+        set(CLANG_WIN32_MINGW TRUE)
+    else()
+        set(CLANG_WIN32_NATIVE TRUE)
+    endif()
+endif()
+
+if(CLANG_WIN32_NATIVE)
+    # Ability to make library file automatically.
+    set(CMAKE_AR lib)
+
+    # Does not have alternative in MS dev tools. Make it empty.
+    set(CMAKE_RANLIB)
+
+    # 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.
+    if(CMAKE_BUILD_TYPE STREQUAL "Release")
+        set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g0")
+        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g0")
+
+        add_definitions(-DNDEBUG)
+    else()
+        set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3")
+        set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3")
+
+        add_definitions(-DDEBUG)
+    endif()
+
+    if(CLANG_X64)
+        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 /subsystem:windows /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+        set(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x64 /subsystem:windows /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+    else()
+        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 /subsystem:windows /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+        SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> /nologo /machine:x32 /subsystem:windows /out:<TARGET> <LINK_FLAGS> <OBJECTS>")
+    endif()
+endif()
+#
+
 include(TestBigEndian)
 TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
 
@@ -59,10 +153,10 @@
 endif(APPLE)
 
 if(WIN32)
-   if(MSVC)
+   if(MSVC OR CLANG_WIN32_NATIVE)
       add_definitions(-DWIN32_LEAN_AND_MEAN)
       add_definitions(-D_CRT_SECURE_NO_WARNINGS)
-   endif(MSVC)
+   endif(MSVC OR CLANG_WIN32_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 +271,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_WIN32_NATIVE)
    set(HAVE_STRCASECMP 1)
-endif(MSVC)
+endif(MSVC OR CLANG_WIN32_NATIVE)
 CHECK_FUNCTION_EXISTS(strcasecmp             HAVE_STRCASECMP)
 
 CHECK_FUNCTION_EXISTS(strlcat                HAVE_STRLCAT)
@@ -421,10 +515,10 @@
 option(OPTION_LARGE_FILE "enable large file support" ON)
 
 if(OPTION_LARGE_FILE)
-   if(NOT MSVC)
+   if(NOT MSVC AND NOT CLANG_WIN32_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_WIN32_NATIVE)
 endif(OPTION_LARGE_FILE)
 
 #######################################################################
@@ -698,4 +792,3 @@
    INSTALL_MAN(sudoku 6)
 
 endif(CMAKE_HOST_UNIX)
-
diff -r 7aebf0b30658 -r 94e56ce7436f jpeg/CMakeLists.txt
--- a/jpeg/CMakeLists.txt	Sat Feb 01 15:50:33 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_WIN32_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_WIN32_NATIVE)
 
 install(TARGETS fltk_jpeg
    EXPORT fltk-install
diff -r 7aebf0b30658 -r 94e56ce7436f png/CMakeLists.txt
--- a/png/CMakeLists.txt	Sat Feb 01 15:50:33 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_WIN32_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_WIN32_NATIVE)
 
 target_link_libraries(fltk_png ${FLTK_ZLIB_LIBRARIES})
 
diff -r 7aebf0b30658 -r 94e56ce7436f zlib/CMakeLists.txt
--- a/zlib/CMakeLists.txt	Sat Feb 01 15:50:33 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_WIN32_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_WIN32_NATIVE)
 
 install(TARGETS fltk_z
    EXPORT fltk-install
