From 87c9b0affe73ca6ab41f62b94274aa8e16045ea9 Mon Sep 17 00:00:00 2001
From: Mat M <mathew1800@gmail.com>
Date: Sun, 26 Nov 2017 06:39:27 -0500
Subject: [PATCH] CMakeLists: Derive the source file listings from targets
 directly (#118)

This gets rid of the need to store to individual variables before creating
the target itself, cleaning up the variables in the surrounding scope a little bit.
---
 CMakeModules/CreateDirectoryGroups.cmake |  8 +++++---
 src/CMakeLists.txt                       | 23 +++++++++++------------
 tests/CMakeLists.txt                     | 13 ++++++-------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/CMakeModules/CreateDirectoryGroups.cmake b/CMakeModules/CreateDirectoryGroups.cmake
index e806421f..175899e7 100644
--- a/CMakeModules/CreateDirectoryGroups.cmake
+++ b/CMakeModules/CreateDirectoryGroups.cmake
@@ -1,12 +1,14 @@
-# This function should be passed a list of all files in a target. It will automatically generate
+# This function should be passed a name of an existing target. It will automatically generate
 # file groups following the directory hierarchy, so that the layout of the files in IDEs matches the
 # one in the filesystem.
-function(create_directory_groups)
+function(create_target_directory_groups target_name)
     # Place any files that aren't in the source list in a separate group so that they don't get in
     # the way.
     source_group("Other Files" REGULAR_EXPRESSION ".")
 
-    foreach(file_name ${ARGV})
+    get_target_property(target_sources "${target_name}" SOURCES)
+
+    foreach(file_name IN LISTS target_sources)
         get_filename_component(dir_name "${file_name}" PATH)
         # Group names use '\' as a separator even though the entire rest of CMake uses '/'...
         string(REPLACE "/" "\\" group_name "${dir_name}")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index dd56e860..29994a85 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,4 +1,5 @@
-set(SRCS
+add_library(dynarmic
+    # Source files
     common/memory_pool.cpp
     frontend/arm/types.cpp
     frontend/disassembler/disassembler_arm.cpp
@@ -31,9 +32,8 @@ set(SRCS
     ir_opt/dead_code_elimination_pass.cpp
     ir_opt/get_set_elimination_pass.cpp
     ir_opt/verification_pass.cpp
-    )
 
-set(HEADERS
+    # Header files
     ../include/dynarmic/callbacks.h
     ../include/dynarmic/coprocessor.h
     ../include/dynarmic/coprocessor_util.h
@@ -70,10 +70,11 @@ set(HEADERS
     frontend/translate/translate.h
     frontend/translate/translate_arm/translate_arm.h
     ir_opt/passes.h
-    )
+)
 
 if (ARCHITECTURE_x86_64)
-    list(APPEND SRCS
+    target_sources(dynarmic PRIVATE
+         # Source files
          backend_x64/abi.cpp
          backend_x64/block_of_code.cpp
          backend_x64/constant_pool.cpp
@@ -82,9 +83,8 @@ if (ARCHITECTURE_x86_64)
          backend_x64/interface_x64.cpp
          backend_x64/jitstate.cpp
          backend_x64/reg_alloc.cpp
-         )
 
-    list(APPEND HEADERS
+         # Headers
          backend_x64/abi.h
          backend_x64/block_of_code.h
          backend_x64/constant_pool.h
@@ -93,21 +93,20 @@ if (ARCHITECTURE_x86_64)
          backend_x64/jitstate.h
          backend_x64/oparg.h
          backend_x64/reg_alloc.h
-         )
+    )
 
     if (WIN32)
-        list(APPEND SRCS backend_x64/exception_handler_windows.cpp)
+        target_sources(dynarmic PRIVATE backend_x64/exception_handler_windows.cpp)
     else()
-        list(APPEND SRCS backend_x64/exception_handler_generic.cpp)
+        target_sources(dynarmic PRIVATE backend_x64/exception_handler_generic.cpp)
     endif()
 else()
     message(FATAL_ERROR "Unsupported architecture")
 endif()
 
 include(CreateDirectoryGroups)
-create_directory_groups(${SRCS} ${HEADERS})
+create_target_directory_groups(dynarmic)
 
-add_library(dynarmic ${SRCS} ${HEADERS})
 target_include_directories(dynarmic
                            PUBLIC ../include
                            PRIVATE .)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index bdcc93ca..2097f481 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,10 +1,10 @@
-set(SRCS
+add_executable(dynarmic_tests
+    # Source files
     arm/fuzz_arm.cpp
     arm/fuzz_thumb.cpp
     arm/test_arm_disassembler.cpp
     arm/test_thumb_instructions.cpp
     main.cpp
-    rand_int.h
     skyeye_interpreter/dyncom/arm_dyncom_dec.cpp
     skyeye_interpreter/dyncom/arm_dyncom_interpreter.cpp
     skyeye_interpreter/dyncom/arm_dyncom_thumb.cpp
@@ -15,9 +15,9 @@ set(SRCS
     skyeye_interpreter/skyeye_common/vfp/vfpdouble.cpp
     skyeye_interpreter/skyeye_common/vfp/vfpinstr.cpp
     skyeye_interpreter/skyeye_common/vfp/vfpsingle.cpp
-    )
 
-set(HEADERS
+    # Header files
+    rand_int.h
     skyeye_interpreter/dyncom/arm_dyncom_dec.h
     skyeye_interpreter/dyncom/arm_dyncom_interpreter.h
     skyeye_interpreter/dyncom/arm_dyncom_run.h
@@ -29,12 +29,11 @@ set(HEADERS
     skyeye_interpreter/skyeye_common/vfp/asm_vfp.h
     skyeye_interpreter/skyeye_common/vfp/vfp.h
     skyeye_interpreter/skyeye_common/vfp/vfp_helper.h
-    )
+)
 
 include(CreateDirectoryGroups)
-create_directory_groups(${SRCS} ${HEADERS})
+create_target_directory_groups(dynarmic_tests)
 
-add_executable(dynarmic_tests ${SRCS})
 target_link_libraries(dynarmic_tests PRIVATE dynarmic boost catch)
 target_include_directories(dynarmic_tests PRIVATE . ../src)
 target_compile_options(dynarmic_tests PRIVATE ${DYNARMIC_CXX_FLAGS})