From 365f2a1c0354a24ec3015b351c85f2ea09f0c15f Mon Sep 17 00:00:00 2001
From: Sergey Kosukhin <sergey.kosukhin@mpimet.mpg.de>
Date: Thu, 11 Nov 2021 14:25:11 +0100
Subject: [PATCH] Optionally (and by default) build check programs when 'make
 all'.

---
 app/Makefile.am      |  4 ++++
 configure.ac         |  9 +++++++++
 examples/Makefile.am | 13 +++++++++++--
 src/Makefile.am      |  8 +++++++-
 tests/Makefile.am    | 12 +++++++++---
 5 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/app/Makefile.am b/app/Makefile.am
index d837cdba5..67fc38e0a 100644
--- a/app/Makefile.am
+++ b/app/Makefile.am
@@ -10,7 +10,11 @@ noinst_PROGRAMS += cdi
 endif !ENABLE_CDI_LIB
 noinst_PROGRAMS += createtable
 else !ENABLE_CDI_APP
+if with_on_demand_check_programs
 check_PROGRAMS += cdi
+else !with_on_demand_check_programs
+noinst_PROGRAMS += cdi
+endif !with_on_demand_check_programs
 endif !ENABLE_CDI_APP
 
 AM_CPPFLAGS = -I$(top_srcdir)/src
diff --git a/configure.ac b/configure.ac
index 442e4a861..f478090a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -611,6 +611,15 @@ AM_MISSING_PROG([PYTHON], [python])
 AM_CONDITIONAL([ENABLE_PYTHON_INTERFACE],
   [test "x$enable_python_interface" = xyes])
 
+AC_ARG_WITH([on-demand-check-programs],
+  [AS_HELP_STRING([--with-on-demand-check-programs],
+     [only build test programs when calling 'make check' @<:@default: build ]dnl
+[during 'make all'@:>@])],
+  [test "x$withval" != xno && with_on_demand_check_programs=yes],
+  [with_on_demand_check_programs=no])
+AM_CONDITIONAL([with_on_demand_check_programs],
+  [test "x$with_on_demand_check_programs" = xyes])
+
 AC_ARG_WITH([concurrent-mpi-checks],
   [AS_HELP_STRING([--with-concurrent-mpi-checks],
      [allow for simultaneous runs of MPI tests @<:@default: run MPI test ]dnl
diff --git a/examples/Makefile.am b/examples/Makefile.am
index ad384fcd7..5b9f88f40 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -12,14 +12,23 @@ noinst_PROGRAMS = \
 
 check_PROGRAMS =
 
+# Examples that are part of the test suite:
+test_PROGRAMS_ =
+
 if ENABLE_NETCDF
-check_PROGRAMS += cdi_write_const
+test_PROGRAMS_ += cdi_write_const
 endif
 
 if ENABLE_ISOC_INTERFACE
-check_PROGRAMS += cdi_read_f2003 cdi_write_f2003
+test_PROGRAMS_ += cdi_read_f2003 cdi_write_f2003
 endif ENABLE_ISOC_INTERFACE
 
+if with_on_demand_check_programs
+check_PROGRAMS += $(test_PROGRAMS_)
+else !with_on_demand_check_programs
+noinst_PROGRAMS += $(test_PROGRAMS_)
+endif !with_on_demand_check_programs
+
 AM_CPPFLAGS = -I$(top_srcdir)/src
 
 AM_FCFLAGS =
diff --git a/src/Makefile.am b/src/Makefile.am
index 91116f24f..ebc546964 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,7 +4,7 @@ cmakedir = $(libdir)/cmake
 include_HEADERS =
 nodist_include_HEADERS =
 lib_LTLIBRARIES =
-check_LTLIBRARIES = libcdiresunpack.la
+check_LTLIBRARIES =
 noinst_LTLIBRARIES =
 EXTRA_PROGRAMS = make_fint
 EXTRA_DIST = cdilib.c
@@ -54,6 +54,12 @@ noinst_LTLIBRARIES += libcdipio.la
 endif ENABLE_MPI
 endif !ENABLE_CDI_LIB
 
+if with_on_demand_check_programs
+check_LTLIBRARIES += libcdiresunpack.la
+else !with_on_demand_check_programs
+noinst_LTLIBRARIES += libcdiresunpack.la
+endif !with_on_demand_check_programs
+
 AM_CPPFLAGS =
 if ENABLE_MPI
 # TODO: this needs a refactoring but for now we set this macro here and not in
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cf07f1500..d2651df40 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -53,7 +53,7 @@ pio_write_run_parallel.log: pio_write_deco2d_run_parallel.log
 test_resource_copy_mpi_run.log: pio_write_run_parallel.log
 endif !with_concurrent_mpi_checks
 
-check_PROGRAMS = \
+test_PROGRAMS_ = \
   calendar_test1 \
   cksum_read \
   cksum_verify \
@@ -67,18 +67,24 @@ check_PROGRAMS = \
   test_table
 
 if ENABLE_NETCDF
-check_PROGRAMS += \
+test_PROGRAMS_ += \
   test_cdf_read \
   test_cdf_write
 endif
 
 if ENABLE_MPI
-check_PROGRAMS += \
+test_PROGRAMS_ += \
   pio_write_deco2d_parallel \
   pio_write_parallel \
   test_resource_copy_mpi
 endif ENABLE_MPI
 
+if with_on_demand_check_programs
+check_PROGRAMS = $(test_PROGRAMS_)
+else !with_on_demand_check_programs
+noinst_PROGRAMS = $(test_PROGRAMS_)
+endif !with_on_demand_check_programs
+
 AM_CPPFLAGS = -I$(top_srcdir)/src
 AM_CFLAGS = $(PPM_CORE_C_INCLUDE) $(YAXT_C_INCLUDE) $(MPI_C_INCLUDE)
 
-- 
GitLab