From 7b81903756451281bd1f07d8e9a2dceeff79df08 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Sat, 27 May 2017 18:26:55 -0700
Subject: [PATCH] CMake: Correct inter-module dependencies and library
 visibility

Modules didn't correctly define their dependencies before, which relied
on the frontends implicitly including every module for linking to
succeed.

Also changed every target_link_libraries call to specify visibility of
dependencies to avoid leaking definitions to dependents when not
necessary.
---
 src/audio_core/CMakeLists.txt   |  7 ++++---
 src/citra/CMakeLists.txt        |  8 ++++----
 src/citra_qt/CMakeLists.txt     |  6 +++---
 src/common/CMakeLists.txt       |  2 +-
 src/core/CMakeLists.txt         |  4 ++--
 src/input_common/CMakeLists.txt |  6 +++---
 src/tests/CMakeLists.txt        |  5 +++--
 src/video_core/CMakeLists.txt   | 12 +++++++-----
 8 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index a72a907ef3..c571213fc0 100644
--- a/src/audio_core/CMakeLists.txt
+++ b/src/audio_core/CMakeLists.txt
@@ -38,9 +38,10 @@ endif()
 create_directory_groups(${SRCS} ${HEADERS})
 
 add_library(audio_core STATIC ${SRCS} ${HEADERS})
-target_link_libraries(audio_core SoundTouch)
+target_link_libraries(audio_core PUBLIC common core)
+target_link_libraries(audio_core PRIVATE SoundTouch)
 
 if(SDL2_FOUND)
-    target_link_libraries(audio_core ${SDL2_LIBRARY})
-    set_property(TARGET audio_core APPEND PROPERTY COMPILE_DEFINITIONS HAVE_SDL2)
+    target_link_libraries(audio_core PRIVATE ${SDL2_LIBRARY})
+    target_compile_definitions(audio_core PRIVATE HAVE_SDL2)
 endif()
diff --git a/src/citra/CMakeLists.txt b/src/citra/CMakeLists.txt
index 47231ba719..9eddb342be 100644
--- a/src/citra/CMakeLists.txt
+++ b/src/citra/CMakeLists.txt
@@ -18,12 +18,12 @@ create_directory_groups(${SRCS} ${HEADERS})
 include_directories(${SDL2_INCLUDE_DIR})
 
 add_executable(citra ${SRCS} ${HEADERS})
-target_link_libraries(citra core video_core audio_core common input_common)
-target_link_libraries(citra ${SDL2_LIBRARY} ${OPENGL_gl_LIBRARY} inih glad)
+target_link_libraries(citra PRIVATE common core input_common)
+target_link_libraries(citra PRIVATE ${SDL2_LIBRARY} ${OPENGL_gl_LIBRARY} inih glad)
 if (MSVC)
-    target_link_libraries(citra getopt)
+    target_link_libraries(citra PRIVATE getopt)
 endif()
-target_link_libraries(citra ${PLATFORM_LIBRARIES} Threads::Threads)
+target_link_libraries(citra PRIVATE ${PLATFORM_LIBRARIES} Threads::Threads)
 
 if(UNIX AND NOT APPLE)
     install(TARGETS citra RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt
index 4e837668ef..809e0b9385 100644
--- a/src/citra_qt/CMakeLists.txt
+++ b/src/citra_qt/CMakeLists.txt
@@ -91,9 +91,9 @@ if (APPLE)
 else()
     add_executable(citra-qt ${SRCS} ${HEADERS} ${UI_HDRS})
 endif()
-target_link_libraries(citra-qt core video_core audio_core common input_common)
-target_link_libraries(citra-qt ${OPENGL_gl_LIBRARY} ${CITRA_QT_LIBS})
-target_link_libraries(citra-qt ${PLATFORM_LIBRARIES} Threads::Threads)
+target_link_libraries(citra-qt PRIVATE audio_core common core input_common video_core)
+target_link_libraries(citra-qt PRIVATE ${OPENGL_gl_LIBRARY} ${CITRA_QT_LIBS} glad)
+target_link_libraries(citra-qt PRIVATE ${PLATFORM_LIBRARIES} Threads::Threads)
 
 if(UNIX AND NOT APPLE)
     install(TARGETS citra-qt RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 546a145004..a33a8cdbeb 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -96,5 +96,5 @@ create_directory_groups(${SRCS} ${HEADERS})
 
 add_library(common STATIC ${SRCS} ${HEADERS})
 if (ARCHITECTURE_x86_64)
-    target_link_libraries(common xbyak)
+    target_link_libraries(common PRIVATE xbyak)
 endif()
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index cbfd1299cf..7aa81e8850 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -380,5 +380,5 @@ include_directories(../../externals/cryptopp)
 create_directory_groups(${SRCS} ${HEADERS})
 
 add_library(core STATIC ${SRCS} ${HEADERS})
-
-target_link_libraries(core dynarmic cryptopp)
+target_link_libraries(core PUBLIC common PRIVATE audio_core video_core)
+target_link_libraries(core PRIVATE cryptopp dynarmic)
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt
index cfe5caaa39..5b306e42e7 100644
--- a/src/input_common/CMakeLists.txt
+++ b/src/input_common/CMakeLists.txt
@@ -19,9 +19,9 @@ endif()
 create_directory_groups(${SRCS} ${HEADERS})
 
 add_library(input_common STATIC ${SRCS} ${HEADERS})
-target_link_libraries(input_common common core)
+target_link_libraries(input_common PUBLIC core PRIVATE common)
 
 if(SDL2_FOUND)
-    target_link_libraries(input_common ${SDL2_LIBRARY})
-    set_property(TARGET input_common APPEND PROPERTY COMPILE_DEFINITIONS HAVE_SDL2)
+    target_link_libraries(input_common PRIVATE ${SDL2_LIBRARY})
+    target_compile_definitions(input_common PRIVATE HAVE_SDL2)
 endif()
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index d1144ba77c..85f2f29857 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -13,7 +13,8 @@ create_directory_groups(${SRCS} ${HEADERS})
 include_directories(../../externals/catch/single_include/)
 
 add_executable(tests ${SRCS} ${HEADERS})
-target_link_libraries(tests core video_core audio_core common)
-target_link_libraries(tests ${PLATFORM_LIBRARIES} Threads::Threads)
+target_link_libraries(tests PRIVATE common core)
+target_link_libraries(tests PRIVATE glad) # To support linker work-around
+target_link_libraries(tests PRIVATE ${PLATFORM_LIBRARIES} Threads::Threads)
 
 add_test(NAME tests COMMAND $<TARGET_FILE:tests>)
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index e00b88f718..e455f03bd1 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -79,13 +79,15 @@ endif()
 create_directory_groups(${SRCS} ${HEADERS})
 
 add_library(video_core STATIC ${SRCS} ${HEADERS})
-target_link_libraries(video_core glad)
+target_link_libraries(video_core PUBLIC common core)
+target_link_libraries(video_core PRIVATE glad)
+
 if (ARCHITECTURE_x86_64)
-    target_link_libraries(video_core xbyak)
+    target_link_libraries(video_core PRIVATE xbyak)
 endif()
 
 if (PNG_FOUND)
-    target_link_libraries(video_core ${PNG_LIBRARIES})
-    include_directories(${PNG_INCLUDE_DIRS})
-    add_definitions(${PNG_DEFINITIONS})
+    target_link_libraries(video_core PRIVATE ${PNG_LIBRARIES})
+    target_include_directories(video_core PRIVATE ${PNG_INCLUDE_DIRS})
+    target_compile_definitions(video_core PRIVATE ${PNG_DEFINITIONS})
 endif()