From 6c422f3208ead82d3d709f18badd80b68a56340f Mon Sep 17 00:00:00 2001
From: Matej Ferencevic <matej.ferencevic@memgraph.io>
Date: Fri, 31 Jan 2020 16:27:10 +0100
Subject: [PATCH] Remove global include directories in CMake

Summary:
All external libraries now automatically include their include directories. It
is necessary only to link to the external library using
`target_link_libraries(target library)` and the include directory of the
library will be automatically available for the binary.

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D2654
---
 CMakeLists.txt                 | 10 ----------
 libs/CMakeLists.txt            | 20 +++++++++++---------
 tests/CMakeLists.txt           |  2 --
 tests/benchmark/CMakeLists.txt |  1 -
 4 files changed, 11 insertions(+), 22 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0854782a3..bb1bf6ff9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -148,16 +148,6 @@ find_package(OpenSSL REQUIRED)
 set(libs_dir ${CMAKE_SOURCE_DIR}/libs)
 add_subdirectory(libs EXCLUDE_FROM_ALL)
 
-include_directories(SYSTEM ${GFLAGS_INCLUDE_DIR})
-include_directories(SYSTEM ${GLOG_INCLUDE_DIR})
-include_directories(SYSTEM ${FMT_INCLUDE_DIR})
-include_directories(SYSTEM ${ANTLR4_INCLUDE_DIR})
-include_directories(SYSTEM ${BZIP2_INCLUDE_DIR})
-include_directories(SYSTEM ${ZLIB_INCLUDE_DIR})
-include_directories(SYSTEM ${ROCKSDB_INCLUDE_DIR})
-include_directories(SYSTEM ${LIBRDKAFKA_INCLUDE_DIR})
-
-
 # Optional subproject configuration -------------------------------------------
 option(TEST_COVERAGE "Generate coverage reports from running memgraph" OFF)
 option(TOOLS "Build tools binaries" ON)
diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt
index 6507257aa..6d78924cd 100644
--- a/libs/CMakeLists.txt
+++ b/libs/CMakeLists.txt
@@ -17,7 +17,7 @@ function(import_header_library name include_dir)
   mark_as_advanced(${_upper_name}_INCLUDE_DIR)
 endfunction(import_header_library)
 
-function(import_library name type location)
+function(import_library name type location include_dir)
   add_library(${name} ${type} IMPORTED GLOBAL)
   if (${ARGN})
     # Optional argument is the name of the external project that we need to
@@ -26,10 +26,12 @@ function(import_library name type location)
   else()
     add_dependencies(${name} ${name}-proj)
   endif()
-  # Unfortunately, we cannot use include_dir before it is built.
-  # set_property(TARGET ${name} PROPERTY
-  #              INTERFACE_INCLUDE_DIRECTORIES ${include_dir})
   set_property(TARGET ${name} PROPERTY IMPORTED_LOCATION ${location})
+  # We need to create the include directory first in order to be able to add it
+  # as an include directory. The header files in the include directory will be
+  # generated later during the build process.
+  file(MAKE_DIRECTORY ${include_dir})
+  target_include_directories(${name} INTERFACE ${include_dir})
 endfunction(import_library)
 
 # Calls `ExternalProject_Add(${name}-proj` with default arguments for cmake
@@ -74,7 +76,7 @@ macro(import_external_library name type library_location include_dir)
   set(${_upper_name}_INCLUDE_DIR ${include_dir} CACHE FILEPATH
       "Path to ${name} include directory" FORCE)
   mark_as_advanced(${_upper_name}_LIBRARY ${_upper_name}_INCLUDE_DIR)
-  import_library(${name} ${type} ${${_upper_name}_LIBRARY})
+  import_library(${name} ${type} ${${_upper_name}_LIBRARY} ${${_upper_name}_INCLUDE_DIR})
 endmacro(import_external_library)
 
 # setup antlr
@@ -131,10 +133,10 @@ set(GTEST_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest.a
 set(GTEST_MAIN_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/googletest/lib/libgtest_main.a
     CACHE FILEPATH "Path to gtest_main library" FORCE)
 mark_as_advanced(GTEST_INCLUDE_DIR GMOCK_LIBRARY GMOCK_MAIN_LIBRARY GTEST_LIBRARY GTEST_MAIN_LIBRARY)
-import_library(gtest STATIC ${GTEST_LIBRARY} gtest-proj)
-import_library(gtest_main STATIC ${GTEST_MAIN_LIBRARY} gtest-proj)
-import_library(gmock STATIC ${GMOCK_LIBRARY} gtest-proj)
-import_library(gmock_main STATIC ${GMOCK_MAIN_LIBRARY} gtest-proj)
+import_library(gtest STATIC ${GTEST_LIBRARY} ${GTEST_INCLUDE_DIR} gtest-proj)
+import_library(gtest_main STATIC ${GTEST_MAIN_LIBRARY} ${GTEST_INCLUDE_DIR} gtest-proj)
+import_library(gmock STATIC ${GMOCK_LIBRARY} ${GTEST_INCLUDE_DIR} gtest-proj)
+import_library(gmock_main STATIC ${GMOCK_MAIN_LIBRARY} ${GTEST_INCLUDE_DIR} gtest-proj)
 
 # setup google flags
 set(GFLAGS_NO_FILENAMES "0")
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index eda246100..aae04e368 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,5 +1,3 @@
-include_directories(${GTEST_INCLUDE_DIR})
-
 # benchmark test binaries
 add_subdirectory(benchmark)
 
diff --git a/tests/benchmark/CMakeLists.txt b/tests/benchmark/CMakeLists.txt
index c9b64e137..e6f083b84 100644
--- a/tests/benchmark/CMakeLists.txt
+++ b/tests/benchmark/CMakeLists.txt
@@ -12,7 +12,6 @@ function(add_benchmark test_cpp)
   # requires unique logical target names
   set_target_properties(${target_name} PROPERTIES OUTPUT_NAME ${exec_name})
   target_link_libraries(${target_name} benchmark)
-  target_include_directories(${target_name} PRIVATE ${BENCHMARK_INCLUDE_DIR})
   # register test
   add_test(${target_name} ${exec_name})
   add_dependencies(memgraph__benchmark ${target_name})