From 3df6441a8cbc9ac710e541f35b66769277fdc831 Mon Sep 17 00:00:00 2001
From: Corentin Wallez <cwallez@chromium.org>
Date: Mon, 10 Jul 2017 22:39:04 -0400
Subject: [PATCH] Group the target sources for the VS solution explorer

---
 CMakeLists.txt | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8ebdcdc50e..4fe844ebb5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -77,8 +77,24 @@ function(NXTInternalTarget folder target)
     NXTExternalTarget("${folder}" ${target})
     set_property(TARGET ${target} APPEND PROPERTY COMPILE_OPTIONS ${NXT_INTERNAL_FLAGS})
     set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${NXT_INTERNAL_DEFS})
+
+    # Group the target sources by folder to have folders show in Visual Studio
+    if (MSVC)
+        get_target_property(targetSources ${target} SOURCES)
+        foreach(sourceFile IN ITEMS ${targetSources})
+            if (IS_ABSOLUTE "${sourceFile}")
+                file(RELATIVE_PATH sourceFile "${CMAKE_CURRENT_SOURCE_DIR}" "${sourceFile}")
+            endif()
+            get_filename_component(sourceDir "${sourceFile}" PATH)
+            string(REPLACE "/" "\\" sourceDir "${sourceDir}")
+            source_group("${sourceDir}" FILES "${sourceFile}")
+        endforeach()
+    endif()
 endfunction()
 
+# Enable the creation of folders for Visual Studio projects
+set_property(GLOBAL PROPERTY USE_FOLDERS ON)
+
 ################################################################################
 # Generate the C and C++ NXT APIs
 ################################################################################