From 4c60c81f3609fb7319965962eb38b853779d6062 Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Tue, 6 Dec 2022 23:52:30 +0100
Subject: [PATCH] Improve cmake scripts to find required utilities

- to build html doc, one requires emacs and gnuplot
- to build pdf doc, one requires additionally pdflatex and pygmentize
---
 CMakeLists.txt      | 38 ++++++++++++++++++++++++++++++++++++++
 cmake/PugsDoc.cmake | 42 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 77 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb52f0b84..c490e2259 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -669,6 +669,8 @@ else()
   endif()
 endif()
 
+message("----------- utilities ----------")
+
 if(CLANG_FORMAT)
   message(" clang-format: ${CLANG_FORMAT}")
 else()
@@ -693,5 +695,41 @@ else()
   message(" emacs: not found!")
 endif()
 
+if (GNUPLOT_FOUND)
+  message(" gnuplot: ${GNUPLOT}")
+else()
+  message(" gnuplot: not found!")
+endif()
+
+if (PYGMENTIZE)
+  message(" pygmentize: ${PYGMENTIZE}")
+else()
+  message(" pygmentize: not found!")
+endif()
+
+if (LATEX_PDFLATEX_FOUND)
+  message(" pdflatex: ${PDFLATEX_COMPILER}")
+else()
+  message(" pdflatex: not found!")
+endif()
+
+if (NOT EMACS OR NOT GNUPLOT_FOUND)
+  message(" ** Cannot build documentation: missing ")
+elseif(NOT LATEX_PDFLATEX_FOUND OR NOT PYGMENTIZE)
+  message(" ** Cannot build pdf documentation: missing")
+endif()
+if (NOT EMACS)
+  message("    - emacs")
+endif()
+if (NOT GNUPLOT_FOUND)
+  message("    - gnuplot")
+endif()
+if (NOT LATEX_PDFLATEX_FOUND)
+  message("    - pdflatex")
+endif()
+if (NOT PYGMENTIZE)
+  message("    - pygmentize")
+endif()
+
 message("================================")
 message("")
diff --git a/cmake/PugsDoc.cmake b/cmake/PugsDoc.cmake
index 7be934aba..1f8701ccc 100644
--- a/cmake/PugsDoc.cmake
+++ b/cmake/PugsDoc.cmake
@@ -6,10 +6,16 @@ find_program(EMACS emacs)
 # check for LaTeX
 find_package(LATEX COMPONENTS PDFLATEX)
 
+# check for pygmentize
+find_program(PYGMENTIZE pygmentize)
+
+# check for gnuplot
+find_package(Gnuplot)
+
 add_custom_target(userdoc)
 add_custom_target(doc DEPENDS userdoc)
 
-if (EMACS)
+if (EMACS AND GNUPLOT_FOUND)
 
   add_custom_command(
     OUTPUT "${PUGS_BINARY_DIR}/doc"
@@ -64,7 +70,7 @@ if (EMACS)
 
   add_dependencies(userdoc userdoc-html)
 
-  if (LATEX_FOUND)
+  if (LATEX_PDFLATEX_FOUND AND PYGMENTIZE)
 
     add_custom_command(
       OUTPUT "${PUGS_BINARY_DIR}/doc/userdoc.pdf"
@@ -92,7 +98,37 @@ if (EMACS)
 
     add_dependencies(userdoc userdoc-pdf)
 
+  else()
+    if (NOT LATEX_PDFLATEX_FOUND)
+      add_custom_target(userdoc-missing-latex
+	COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --no-newline "Cannot build pdf documentation: "
+	COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "pdflatex missing")
+      add_dependencies(userdoc userdoc-missing-latex)
+    endif()
+
+    if (NOT PIGMENTIZE_FOUND)
+      add_custom_target(userdoc-missing-pygmentize
+	COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --no-newline "Cannot build pdf documentation: "
+	COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "pygmentize missing")
+      add_dependencies(userdoc userdoc-missing-pygmentize)
+    endif()
+
   endif()
 
-  add_dependencies(doc userdoc)
+else()
+  if (NOT EMACS)
+    add_custom_target(userdoc-missing-emacs
+      COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --no-newline "Cannot build documentation: "
+      COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "emacs missing")
+    add_dependencies(userdoc userdoc-missing-emacs)
+  endif()
+
+  if (NOT GNUPLOT_FOUND)
+    add_custom_target(userdoc-missing-gnuplot
+      COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --no-newline "Cannot build documentation: "
+      COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --red --bold "gnuplot missing")
+    add_dependencies(userdoc userdoc-missing-gnuplot)
+  endif()
 endif()
+
+add_dependencies(doc userdoc)
-- 
GitLab