From ff9fce63f9f23e2a348a7dcb09c0dffd08e40676 Mon Sep 17 00:00:00 2001
From: Tomas Pecka <peckato1@fit.cvut.cz>
Date: Sun, 18 Nov 2018 10:34:32 +0100
Subject: [PATCH] Build: CMake: Move find_packages from main CMakeLists to
 subprojects. -DALIB_NOGUI is not needed now

---
 CMake/CMakeLists_bin.txt           |  6 ++++--
 CMake/CMakeLists_lib.txt           |  5 +++++
 CMake/CMakeLists_root.txt          | 22 +---------------------
 CMake/generate.conf                |  9 ++++++++-
 CMake/generate.py                  | 20 ++++++++++++--------
 extra/docker/master-cli/Dockerfile |  3 +--
 6 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/CMake/CMakeLists_bin.txt b/CMake/CMakeLists_bin.txt
index d67320e18c..ee9fac590f 100644
--- a/CMake/CMakeLists_bin.txt
+++ b/CMake/CMakeLists_bin.txt
@@ -2,6 +2,9 @@ project({project_name})
 
 set(PROJECT_NAME {project_name})
 
+find_package(tclap REQUIRED)
+{find_packages}
+
 set(SOURCE_FILES
         {source_files}
         )
@@ -13,11 +16,10 @@ add_executable(${{PROJECT_NAME}} ${{SOURCE_FILES}})
 target_include_directories(${{PROJECT_NAME}}
         PUBLIC ${{CMAKE_CURRENT_SOURCE_DIR}}/src # anything that depends on this should include src also, hence public
         {include_paths}
-        {include_test_paths}
         )
 
 # Target link libraries
-target_link_libraries(${{PROJECT_NAME}} {target_test_libs} {target_libs})
+target_link_libraries(${{PROJECT_NAME}} {target_libs})
 
 set_target_properties(${{PROJECT_NAME}} PROPERTIES
         CXX_STANDARD 14
diff --git a/CMake/CMakeLists_lib.txt b/CMake/CMakeLists_lib.txt
index 1531ae5497..4db78d92f1 100644
--- a/CMake/CMakeLists_lib.txt
+++ b/CMake/CMakeLists_lib.txt
@@ -4,6 +4,8 @@ set(PROJECT_NAME {project_name})
 
 {cmake_options}
 
+{find_packages}
+
 set(SOURCE_FILES
         {source_files}
         )
@@ -52,6 +54,8 @@ set(CMAKE_AUTOUIC OFF)
 
 set(PROJECT_NAME_TEST test-{project_name})
 
+{find_packages_tests}
+
 set(SOURCE_FILES_TEST
         {source_files_test}
         )
@@ -62,6 +66,7 @@ set_target_properties(${{PROJECT_NAME_TEST}} PROPERTIES
         CXX_STANDARD_REQUIRED ON
         )
 
+find_package(cppunit REQUIRED)
 target_link_libraries(${{PROJECT_NAME_TEST}} ${{PROJECT_NAME}} {target_test_libs} ${{CPPUNIT_LIBRARY}})
 
 # Include dependencies directories
diff --git a/CMake/CMakeLists_root.txt b/CMake/CMakeLists_root.txt
index 42b6da6219..75b0affb77 100644
--- a/CMake/CMakeLists_root.txt
+++ b/CMake/CMakeLists_root.txt
@@ -72,24 +72,9 @@ elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
     add_compile_options(${{ALIB_BUILD_FLAGS_RELEASE}})
 endif ()
 
-######################################
-# check required external dependencies
-# - we will deal with includes and library links in each project explicitly
-find_package(LibXml2 REQUIRED)
-find_package(cppunit REQUIRED)
-find_package(tclap REQUIRED)
-find_package(readline REQUIRED)
-
-if(NOT ALIB_NOGUI)
-    find_package(Threads)
-    find_package(Graphviz)
-    find_package(Qt5 REQUIRED COMPONENTS Core Widgets Xml)
-    find_package(jsoncpp REQUIRED)
-endif()
-
 
 # Cpp version
-set(CMAKE_CXX_STANDARD {{cpp_version}})
+set(CMAKE_CXX_STANDARD 17)
 
 # TODO set CMAKE_INSTALL_PREFIX in release only
 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
@@ -107,11 +92,6 @@ set(ALIB_MODULES_EXE
         {alib_modules_exe}
         )
 
-if (ALIB_NOGUI)
-    list (REMOVE_ITEM ALIB_MODULES_LIB "alib2gui")
-    list (REMOVE_ITEM ALIB_MODULES_EXE "agui2")
-endif ()
-
 ##################
 # configure version
 configure_file (
diff --git a/CMake/generate.conf b/CMake/generate.conf
index 12f1c3c30c..6b84c2afe5 100644
--- a/CMake/generate.conf
+++ b/CMake/generate.conf
@@ -29,26 +29,33 @@ TestSourcesDir: test-src
 [CMake:Deps:xml2]
 include: PUBLIC ${LIBXML2_INCLUDE_DIR}
 link: ${LIBXML2_LIBRARIES}
+find: LibXml2 REQUIRED
 
 [CMake:Deps:threads]
 link: ${CMAKE_THREAD_LIBS_INIT}
+find: Threads REQUIRED
 
 [CMake:Deps:Qt5Widgets]
 link: Qt5::Widgets
 include: ${Qt5Widgets_INCLUDE_DIRS}
+find: Qt5 REQUIRED COMPONENTS Core Widgets
 
 [CMake:Deps:Qt5Xml]
 link: Qt5::Xml
 include:${Qt5Xml_INCLUDE_DIRS}
+find: Qt5 REQUIRED COMPONENTS Core Xml
 
 [CMake:Deps:graphviz]
 link: ${GRAPHVIZ_GVC_LIBRARY} ${GRAPHVIZ_CGRAPH_LIBRARY}
 include: ${GRAPHVIZ_INCLUDE_DIR}
+find: Graphviz
 
 [CMake:Deps:json]
 link: ${JSONCPP_LIBRARIES}
 include: ${jsoncpp_INCLUDE_DIRS}
+find: jsoncpp REQUIRED
 
 [CMake:Deps:readline]
 link: ${Readline_LIBRARY}
-include: ${Readline_INCLUDE_DIR}
\ No newline at end of file
+include: ${Readline_INCLUDE_DIR}
+find: readline REQUIRED
\ No newline at end of file
diff --git a/CMake/generate.py b/CMake/generate.py
index 4d449b38f9..f5c62520b7 100755
--- a/CMake/generate.py
+++ b/CMake/generate.py
@@ -199,8 +199,8 @@ class Generator:
 
     @classmethod
     def generate_library(cls, project, dry_run):
-        sys_includes, sys_libs = cls._handle_system_deps(project.system_deps)
-        test_sys_includes, test_sys_libs = cls._handle_system_deps(project.system_deps)
+        sys_includes, sys_libs, finds = cls._handle_system_deps(project.system_deps)
+        test_sys_includes, test_sys_libs, test_finds = cls._handle_system_deps(project.system_deps_test)
 
         with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
             f.write(CATEGORIES[project.category].format(
@@ -213,32 +213,36 @@ class Generator:
                 source_files=Helpers.join(project.find_sources()),
                 source_files_test=Helpers.join(project.find_sources_test()),
                 include_files=Helpers.join(project.find_install_sources()),
+                find_packages=Helpers.join(map(lambda p: "find_package({})".format(p), finds), "\n"),
+                find_packages_tests=Helpers.join(map(lambda p: "find_package({})".format(p), test_finds), "\n"),
             ))
 
     @classmethod
     def generate_executable(cls, project, dry_run):
-        sys_includes, sys_libs = cls._handle_system_deps(project.system_deps)
-        test_sys_includes, test_sys_libs = cls._handle_system_deps(project.system_deps)
+        sys_includes, sys_libs, finds = cls._handle_system_deps(project.system_deps)
 
         with SmartOpen("CMakeLists.txt", 'w', directory=project.path, dry_run=dry_run) as f:
             f.write(CATEGORIES[project.category].format(
                 project_name=project.name,
                 target_libs=Helpers.join(project.dependencies + sys_libs, ' '),
-                target_test_libs=Helpers.join(project.dependencies_test + test_sys_libs, ' '),
                 include_paths=Helpers.join([project.name] + sys_includes, ' '),
-                include_test_paths=Helpers.join([project.name] + test_sys_includes, ' '),
                 cmake_options=project.cmake_additional_set,
                 source_files=Helpers.join(project.find_sources()),
+                find_packages=Helpers.join(map(lambda p: "find_package({})".format(p), finds), "\n"),
             ))
 
     @staticmethod
     def _handle_system_deps(system_deps):
-        libs, incl = [], []
+        libs, incl, find = [], [], []
         for dep in system_deps:
             incl = incl + Config.get_array(CONFIG, 'CMake:Deps:{}'.format(dep), 'include', fallback=[])
             libs = libs + Config.get_array(CONFIG, 'CMake:Deps:{}'.format(dep), 'link', fallback=[])
+            try:
+                find.append(Config.get(CONFIG, 'CMake:Deps:{}'.format(dep), 'find'))
+            except ConfigException:
+                pass
 
-        return incl, libs
+        return incl, libs, find
 
 
 # ----------------------------------------------------------------------------------------------------------------------
diff --git a/extra/docker/master-cli/Dockerfile b/extra/docker/master-cli/Dockerfile
index 8d483e5b82..16dba3061a 100644
--- a/extra/docker/master-cli/Dockerfile
+++ b/extra/docker/master-cli/Dockerfile
@@ -19,13 +19,12 @@ RUN apk add \
 
 ADD     . $BASEDIR
 WORKDIR $BASEDIR
-RUN	CMake/generate.py -wm
+RUN	CMake/generate.py -wm -p aql2
 
 WORKDIR	$BASEDIR/release
 RUN	cmake \
 		-DCMAKE_BUILD_TYPE=Release \
 		-DCMAKE_INSTALL_PREFIX=/usr/local \
-		-DALIB_NOGUI=1 \
 		.. && \
 	make -j $(grep -c processor /proc/cpuinfo) && \
 	make test && \
-- 
GitLab