From a852cec30a6540b5c1ea2947195454eef6269944 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Fri, 27 Aug 2021 15:12:12 +0100
Subject: [PATCH] tests: Factor out some common Makefile flags

We can use AM_CPPFLAGS, AM_CFLAGS etc to factor out some common flags
in the tests.  Note the rules here are complicated, see:

https://www.gnu.org/software/automake/manual/html_node/Flag-Variables-Ordering.html

and for unclear reasons there is no AM_LDADD nor any workaround:

https://stackoverflow.com/questions/29252969/automake-am-ldadd-workaround

This commit is mostly pure refactoring but it also tries to make the
flags usage more consistent across tests so it may have side-effects
like enabling more warnings.

(cherry picked from commit 5fd648f821e9ab3ee08bf360348d1fb01537a267)
(cherry picked from commit 6cb1f74b09beca1ddaef794136f221bfb7bb4faa)
---
 interop/Makefile.am |  57 ++++++-------
 tests/Makefile.am   | 190 ++++++++++++++++++--------------------------
 2 files changed, 104 insertions(+), 143 deletions(-)

diff --git a/interop/Makefile.am b/interop/Makefile.am
index 9787c26e..9432ad43 100644
--- a/interop/Makefile.am
+++ b/interop/Makefile.am
@@ -28,6 +28,16 @@ LOG_COMPILER = $(top_builddir)/run
 check_PROGRAMS =
 TESTS =
 
+# Common flags.
+# Note there is no such thing as "AM_LDADD".
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/tests \
+	$(NULL)
+AM_CFLAGS = \
+	$(WARNINGS_CFLAGS) \
+	$(NULL)
+
 if HAVE_NBD_SERVER
 
 check_PROGRAMS += \
@@ -41,22 +51,20 @@ TESTS += \
 
 interop_nbd_server_SOURCES = interop.c
 interop_nbd_server_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBD_SERVER)\" \
 	-DSERVER_PARAMS='"-d", "-C", "/dev/null", "0", tmpfile' \
 	-DEXPORT_NAME='""'
-interop_nbd_server_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbd_server_LDADD = $(top_builddir)/lib/libnbd.la
 
 list_exports_nbd_server_SOURCES = list-exports.c
 list_exports_nbd_server_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBD_SERVER)\" \
 	-DSERVER_PARAMS='"-C", "$(srcdir)/list-exports-nbd-config", "-d", "0"' \
 	-DEXPORTS='"disk1", "disk2"' \
 	-DDESCRIPTIONS='"", ""' \
 	$(NULL)
-list_exports_nbd_server_CFLAGS = $(WARNINGS_CFLAGS)
 list_exports_nbd_server_LDADD = $(top_builddir)/lib/libnbd.la
 
 endif HAVE_NBD_SERVER
@@ -104,19 +112,18 @@ endif
 
 interop_qemu_nbd_SOURCES = interop.c
 interop_qemu_nbd_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSOCKET_ACTIVATION=1 \
 	-DSERVER=\"$(QEMU_NBD)\" \
 	-DSERVER_PARAMS='"-f", "raw", "-x", "/", tmpfile' \
 	-DEXPORT_NAME='"/"' \
 	$(NULL)
-interop_qemu_nbd_CFLAGS = $(WARNINGS_CFLAGS)
 interop_qemu_nbd_LDADD = $(top_builddir)/lib/libnbd.la
 
 # qemu-nbd requires absolute path to dir
 interop_qemu_nbd_tls_certs_SOURCES = interop.c
 interop_qemu_nbd_tls_certs_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSOCKET_ACTIVATION=1 \
 	-DSERVER=\"$(QEMU_NBD)\" \
 	-DSERVER_PARAMS='"--object", "tls-creds-x509,id=tls0,endpoint=server,dir=$(abs_top_builddir)/tests/pki", "--tls-creds", "tls0", "-f", "raw", "-x", "/", tmpfile' \
@@ -124,13 +131,12 @@ interop_qemu_nbd_tls_certs_CPPFLAGS = \
 	-DCERTS=1 \
 	-DTLS_MODE=LIBNBD_TLS_REQUIRE \
 	$(NULL)
-interop_qemu_nbd_tls_certs_CFLAGS = $(WARNINGS_CFLAGS)
 interop_qemu_nbd_tls_certs_LDADD = $(top_builddir)/lib/libnbd.la
 
 # qemu-nbd requires absolute path to dir
 interop_qemu_nbd_tls_psk_SOURCES = interop.c
 interop_qemu_nbd_tls_psk_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSOCKET_ACTIVATION=1 \
 	-DSERVER=\"$(QEMU_NBD)\" \
 	-DSERVER_PARAMS='"--object", "tls-creds-psk,id=tls0,endpoint=server,dir=$(abs_top_builddir)/tests", "--tls-creds", "tls0", "-f", "raw", "-x", "/", tmpfile' \
@@ -138,7 +144,6 @@ interop_qemu_nbd_tls_psk_CPPFLAGS = \
 	-DPSK=1 \
 	-DTLS_MODE=LIBNBD_TLS_REQUIRE \
 	$(NULL)
-interop_qemu_nbd_tls_psk_CFLAGS = $(WARNINGS_CFLAGS)
 interop_qemu_nbd_tls_psk_LDADD = $(top_builddir)/lib/libnbd.la
 
 dirty_bitmap_SOURCES = dirty-bitmap.c
@@ -148,28 +153,24 @@ dirty_bitmap_LDADD = $(top_builddir)/lib/libnbd.la
 
 list_exports_qemu_nbd_SOURCES = list-exports.c
 list_exports_qemu_nbd_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSOCKET_ACTIVATION=1 \
 	-DSERVER=\"$(QEMU_NBD)\" \
 	-DSERVER_PARAMS='"-f", "raw", "-x", "testing", "-D", "data", tmpfile' \
 	-DEXPORTS='"testing"' \
 	-DDESCRIPTIONS='"data"' \
 	$(NULL)
-list_exports_qemu_nbd_CFLAGS = $(WARNINGS_CFLAGS)
 list_exports_qemu_nbd_LDADD = $(top_builddir)/lib/libnbd.la
 
 socket_activation_qemu_nbd_SOURCES = socket-activation.c
 socket_activation_qemu_nbd_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(QEMU_NBD)\" \
 	-DSERVER_PARAMS='"-f", "raw", "-x", "", tmpfile' \
 	$(NULL)
-socket_activation_qemu_nbd_CFLAGS = $(WARNINGS_CFLAGS)
 socket_activation_qemu_nbd_LDADD = $(top_builddir)/lib/libnbd.la
 
 structured_read_SOURCES = structured-read.c
-structured_read_CPPFLAGS = -I$(top_srcdir)/include
-structured_read_CFLAGS = $(WARNINGS_CFLAGS)
 structured_read_LDADD = $(top_builddir)/lib/libnbd.la
 
 endif HAVE_QEMU_NBD
@@ -215,88 +216,80 @@ endif
 
 interop_nbdkit_SOURCES = interop.c
 interop_nbdkit_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"-s", "--exit-with-parent", "file", tmpfile' \
 	$(NULL)
-interop_nbdkit_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_certs_SOURCES = interop.c
 interop_nbdkit_tls_certs_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=require", "--tls-certificates=../tests/pki", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DCERTS=1 \
 	-DTLS_MODE=LIBNBD_TLS_REQUIRE \
 	$(NULL)
-interop_nbdkit_tls_certs_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_certs_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_certs_allow_enabled_SOURCES = interop.c
 interop_nbdkit_tls_certs_allow_enabled_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=require", "--tls-certificates=../tests/pki", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DCERTS=1 \
 	-DTLS_MODE=LIBNBD_TLS_ALLOW \
 	$(NULL)
-interop_nbdkit_tls_certs_allow_enabled_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_certs_allow_enabled_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_certs_allow_fallback_SOURCES = interop.c
 interop_nbdkit_tls_certs_allow_fallback_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=off", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DCERTS=1 \
 	-DTLS_MODE=LIBNBD_TLS_ALLOW \
 	-DTLS_FALLBACK=1 \
 	$(NULL)
-interop_nbdkit_tls_certs_allow_fallback_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_certs_allow_fallback_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_psk_SOURCES = interop.c
 interop_nbdkit_tls_psk_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=require", "--tls-psk=../tests/keys.psk", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DPSK=1 \
 	-DTLS_MODE=LIBNBD_TLS_REQUIRE \
 	$(NULL)
-interop_nbdkit_tls_psk_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_psk_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_psk_allow_enabled_SOURCES = interop.c
 interop_nbdkit_tls_psk_allow_enabled_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=require", "--tls-psk=../tests/keys.psk", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DPSK=1 \
 	-DTLS_MODE=LIBNBD_TLS_ALLOW \
 	$(NULL)
-interop_nbdkit_tls_psk_allow_enabled_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_psk_allow_enabled_LDADD = $(top_builddir)/lib/libnbd.la
 
 interop_nbdkit_tls_psk_allow_fallback_SOURCES = interop.c
 interop_nbdkit_tls_psk_allow_fallback_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"--tls=off", "-s", "--exit-with-parent", "file", tmpfile' \
 	-DPSK=1 \
 	-DTLS_MODE=LIBNBD_TLS_ALLOW \
 	-DTLS_FALLBACK=1 \
 	$(NULL)
-interop_nbdkit_tls_psk_allow_fallback_CFLAGS = $(WARNINGS_CFLAGS)
 interop_nbdkit_tls_psk_allow_fallback_LDADD = $(top_builddir)/lib/libnbd.la
 
 socket_activation_nbdkit_SOURCES = socket-activation.c
 socket_activation_nbdkit_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER=\"$(NBDKIT)\" \
 	-DSERVER_PARAMS='"file", tmpfile' \
 	$(NULL)
-socket_activation_nbdkit_CFLAGS = $(WARNINGS_CFLAGS)
 socket_activation_nbdkit_LDADD = $(top_builddir)/lib/libnbd.la
 
 endif HAVE_NBDKIT
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 64320cad..436e1c10 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -52,6 +52,18 @@ TESTS_ENVIRONMENT = srcdir=$(srcdir) LIBNBD_DEBUG=1
 # Use the ./run script so we're always using the local library and tools.
 LOG_COMPILER = $(top_builddir)/run
 
+# Common flags.
+# Note there is no such thing as "AM_LDADD".
+AM_CPPFLAGS = \
+	-I$(top_srcdir)/include \
+	$(NULL)
+AM_CFLAGS = \
+	$(WARNINGS_CFLAGS) \
+	$(NULL)
+AM_CXXFLAGS = \
+	$(WARNINGS_CFLAGS) \
+	$(NULL)
+
 #----------------------------------------------------------------------
 # The following tests do not need an NBD server.
 
@@ -81,45 +93,30 @@ TESTS += \
 .PHONY: compile
 
 compile_header_only_SOURCES = compile-header-only.c
-compile_header_only_CPPFLAGS = -I$(top_srcdir)/include
-compile_header_only_CFLAGS = $(WARNINGS_CFLAGS)
 compile_header_only_LDADD = $(top_builddir)/lib/libnbd.la
 
 compile_c_SOURCES = compile.c
-compile_c_CPPFLAGS = -I$(top_srcdir)/include
-compile_c_CFLAGS = $(WARNINGS_CFLAGS)
 compile_c_LDADD = $(top_builddir)/lib/libnbd.la
 
 compile_ansi_c_SOURCES = compile-ansi-c.c
 compile_ansi_c_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-std=c90 -pedantic
-compile_ansi_c_CFLAGS = $(WARNINGS_CFLAGS)
 compile_ansi_c_LDADD = $(top_builddir)/lib/libnbd.la
 
 close_null_SOURCES = close-null.c
-close_null_CPPFLAGS = -I$(top_srcdir)/include
-close_null_CFLAGS = $(WARNINGS_CFLAGS)
 close_null_LDADD = $(top_builddir)/lib/libnbd.la
 
 debug_SOURCES = debug.c
-debug_CPPFLAGS = -I$(top_srcdir)/include
-debug_CFLAGS = $(WARNINGS_CFLAGS)
 debug_LDADD = $(top_builddir)/lib/libnbd.la
 
 debug_environment_SOURCES = debug-environment.c
-debug_environment_CPPFLAGS = -I$(top_srcdir)/include
-debug_environment_CFLAGS = $(WARNINGS_CFLAGS)
 debug_environment_LDADD = $(top_builddir)/lib/libnbd.la
 
 version_SOURCES = version.c
-version_CPPFLAGS = -I$(top_srcdir)/include
-version_CFLAGS = $(WARNINGS_CFLAGS)
 version_LDADD = $(top_builddir)/lib/libnbd.la
 
 export_name_SOURCES = export-name.c
-export_name_CPPFLAGS = -I$(top_srcdir)/include
-export_name_CFLAGS = $(WARNINGS_CFLAGS)
 export_name_LDADD = $(top_builddir)/lib/libnbd.la
 
 if HAVE_CXX
@@ -128,8 +125,6 @@ check_PROGRAMS += compile-cxx
 TESTS += compile-cxx
 
 compile_cxx_SOURCES = compile-cxx.cpp
-compile_cxx_CPPFLAGS = -I$(top_srcdir)/include
-compile_cxx_CXXFLAGS = $(WARNINGS_CFLAGS)
 compile_cxx_LDADD = $(top_builddir)/lib/libnbd.la
 
 endif HAVE_CXX
@@ -220,243 +215,208 @@ TESTS += \
 	$(NULL)
 
 errors_SOURCES = errors.c
-errors_CPPFLAGS = -I$(top_srcdir)/include
-errors_CFLAGS = $(WARNINGS_CFLAGS)
 errors_LDADD = $(top_builddir)/lib/libnbd.la
 
 server_death_SOURCES = server-death.c
-server_death_CPPFLAGS = -I$(top_srcdir)/include
-server_death_CFLAGS = $(WARNINGS_CFLAGS)
 server_death_LDADD = $(top_builddir)/lib/libnbd.la
 
 shutdown_flags_SOURCES = shutdown-flags.c
-shutdown_flags_CPPFLAGS = -I$(top_srcdir)/include
-shutdown_flags_CFLAGS = $(WARNINGS_CFLAGS)
 shutdown_flags_LDADD = $(top_builddir)/lib/libnbd.la
 
 get_size_SOURCES = get-size.c
-get_size_CPPFLAGS = -I$(top_srcdir)/include
-get_size_CFLAGS = $(WARNINGS_CFLAGS)
 get_size_LDADD = $(top_builddir)/lib/libnbd.la
 
 read_only_flag_SOURCES = read-only-flag.c
-read_only_flag_CPPFLAGS = -I$(top_srcdir)/include
-read_only_flag_CFLAGS = $(WARNINGS_CFLAGS)
 read_only_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 read_write_flag_SOURCES = read-write-flag.c
-read_write_flag_CPPFLAGS = -I$(top_srcdir)/include
-read_write_flag_CFLAGS = $(WARNINGS_CFLAGS)
 read_write_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_flush_flag_SOURCES = eflags.c
 can_flush_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_flush \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_flush \
 	$(NULL)
-can_flush_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_flush_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_flush_flag_SOURCES = eflags.c
 can_not_flush_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_flush -Dvalue=false \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_flush -Dvalue=false \
 	$(NULL)
-can_not_flush_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_flush_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_fua_flag_SOURCES = eflags.c
 can_fua_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_fua -Dvalue=native \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_fua -Dvalue=native \
 	$(NULL)
-can_fua_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_fua_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_fua_flag_SOURCES = eflags.c
 can_not_fua_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_fua -Dvalue=none \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_fua -Dvalue=none \
 	$(NULL)
-can_not_fua_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_fua_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 is_rotational_flag_SOURCES = eflags.c
 is_rotational_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=is_rotational \
+	$(AM_CPPFLAGS) \
+	-Dflag=is_rotational \
 	$(NULL)
-is_rotational_flag_CFLAGS = $(WARNINGS_CFLAGS)
 is_rotational_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 is_not_rotational_flag_SOURCES = eflags.c
 is_not_rotational_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=is_rotational -Dvalue=false \
+	$(AM_CPPFLAGS) \
+	-Dflag=is_rotational -Dvalue=false \
 	$(NULL)
-is_not_rotational_flag_CFLAGS = $(WARNINGS_CFLAGS)
 is_not_rotational_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_trim_flag_SOURCES = eflags.c
 can_trim_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_trim \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_trim \
 	$(NULL)
-can_trim_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_trim_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_trim_flag_SOURCES = eflags.c
 can_not_trim_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_trim -Dvalue=false \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_trim -Dvalue=false \
 	$(NULL)
-can_not_trim_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_trim_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_zero_flag_SOURCES = eflags.c
 can_zero_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_zero \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_zero \
 	$(NULL)
-can_zero_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_zero_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_zero_flag_SOURCES = eflags.c
 can_not_zero_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_zero -Dvalue=false \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_zero -Dvalue=false \
 	-Dfilter='"--filter=nozero"' \
 	$(NULL)
-can_not_zero_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_zero_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_fast_zero_flag_SOURCES = eflags.c
 can_fast_zero_flag_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/include -Dflag=can_fast_zero \
 	-Drequire='"has_can_fast_zero=1"' \
 	$(NULL)
-can_fast_zero_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_fast_zero_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_fast_zero_flag_SOURCES = eflags.c
 can_not_fast_zero_flag_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/include -Dflag=can_fast_zero -Dvalue=false \
 	-Drequire='"has_can_fast_zero=1"' \
 	$(NULL)
-can_not_fast_zero_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_fast_zero_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_df_flag_SOURCES = eflags.c
 can_df_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_df \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_df \
 	$(NULL)
-can_df_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_df_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_df_flag_SOURCES = eflags.c
 can_not_df_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_df -Dvalue=false -Dno_sr \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_df -Dvalue=false -Dno_sr \
 	$(NULL)
-can_not_df_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_df_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_multi_conn_flag_SOURCES = eflags.c
 can_multi_conn_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_multi_conn \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_multi_conn \
 	$(NULL)
-can_multi_conn_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_multi_conn_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_multi_conn_flag_SOURCES = eflags.c
 can_not_multi_conn_flag_CPPFLAGS = \
-	-I$(top_srcdir)/include -Dflag=can_multi_conn -Dvalue=false \
+	$(AM_CPPFLAGS) \
+	-Dflag=can_multi_conn -Dvalue=false \
 	$(NULL)
-can_not_multi_conn_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_multi_conn_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_cache_flag_SOURCES = eflags.c
 can_cache_flag_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/include -Dflag=can_cache -Dvalue=native \
 	-Drequire='"has_can_cache=1"' \
 	$(NULL)
-can_cache_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_cache_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 can_not_cache_flag_SOURCES = eflags.c
 can_not_cache_flag_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/include -Dflag=can_cache -Dvalue=none \
 	-Drequire='"has_can_cache=1"' \
 	$(NULL)
-can_not_cache_flag_CFLAGS = $(WARNINGS_CFLAGS)
 can_not_cache_flag_LDADD = $(top_builddir)/lib/libnbd.la
 
 oldstyle_SOURCES = oldstyle.c
-oldstyle_CPPFLAGS = -I$(top_srcdir)/include
-oldstyle_CFLAGS = $(WARNINGS_CFLAGS)
 oldstyle_LDADD = $(top_builddir)/lib/libnbd.la
 
 newstyle_limited_SOURCES = newstyle-limited.c
-newstyle_limited_CPPFLAGS = -I$(top_srcdir)/include
-newstyle_limited_CFLAGS = $(WARNINGS_CFLAGS)
 newstyle_limited_LDADD = $(top_builddir)/lib/libnbd.la
 
 opt_abort_SOURCES = opt-abort.c
-opt_abort_CPPFLAGS = -I$(top_srcdir)/include
-opt_abort_CFLAGS = $(WARNINGS_CFLAGS)
 opt_abort_LDADD = $(top_builddir)/lib/libnbd.la
 
 opt_list_SOURCES = opt-list.c
 opt_list_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSCRIPT='"$(abs_srcdir)/opt-list.sh"' \
 	$(NULL)
-opt_list_CFLAGS = $(WARNINGS_CFLAGS)
 opt_list_LDADD = $(top_builddir)/lib/libnbd.la
 
 opt_info_SOURCES = opt-info.c
 opt_info_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSCRIPT='"$(abs_srcdir)/opt-info.sh"' \
 	$(NULL)
-opt_info_CFLAGS = $(WARNINGS_CFLAGS)
 opt_info_LDADD = $(top_builddir)/lib/libnbd.la
 
 opt_list_meta_SOURCES = opt-list-meta.c
-opt_list_meta_CPPFLAGS = \
-	-I$(top_srcdir)/include \
-	$(NULL)
-opt_list_meta_CFLAGS = $(WARNINGS_CFLAGS)
 opt_list_meta_LDADD = $(top_builddir)/lib/libnbd.la
 
 connect_unix_SOURCES = connect-unix.c
-connect_unix_CPPFLAGS = -I$(top_srcdir)/include
-connect_unix_CFLAGS = $(WARNINGS_CFLAGS)
 connect_unix_LDADD = $(top_builddir)/lib/libnbd.la
 
 connect_tcp_SOURCES = connect-tcp.c
-connect_tcp_CPPFLAGS = -I$(top_srcdir)/include
-connect_tcp_CFLAGS = $(WARNINGS_CFLAGS)
 connect_tcp_LDADD = $(top_builddir)/lib/libnbd.la
 
 aio_parallel_SOURCES = aio-parallel.c
 aio_parallel_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/common/include \
 	$(NULL)
-aio_parallel_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
 aio_parallel_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 aio_parallel_load_SOURCES = aio-parallel-load.c
-aio_parallel_load_CPPFLAGS = -I$(top_srcdir)/include
-aio_parallel_load_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
 aio_parallel_load_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 synch_parallel_SOURCES = synch-parallel.c
 synch_parallel_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/common/include \
 	$(NULL)
-synch_parallel_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
+synch_parallel_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
 synch_parallel_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 meta_base_allocation_SOURCES = meta-base-allocation.c
-meta_base_allocation_CPPFLAGS = -I$(top_srcdir)/include
-meta_base_allocation_CFLAGS = $(WARNINGS_CFLAGS)
 meta_base_allocation_LDADD = $(top_builddir)/lib/libnbd.la
 
 closure_lifetimes_SOURCES = closure-lifetimes.c
-closure_lifetimes_CPPFLAGS = -I$(top_srcdir)/include
-closure_lifetimes_CFLAGS = $(WARNINGS_CFLAGS)
 closure_lifetimes_LDADD = $(top_builddir)/lib/libnbd.la
 
 #----------------------------------------------------------------------
@@ -470,8 +430,10 @@ check_DATA += pki/stamp-pki
 TESTS += connect-tls-certs
 
 connect_tls_certs_SOURCES = connect-tls.c
-connect_tls_certs_CPPFLAGS = -I$(top_srcdir)/include -DCERTS=1
-connect_tls_certs_CFLAGS = $(WARNINGS_CFLAGS)
+connect_tls_certs_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
+	-DCERTS=1 \
+	$(NULL)
 connect_tls_certs_LDADD = $(top_builddir)/lib/libnbd.la
 
 pki/stamp-pki: $(srcdir)/make-pki.sh
@@ -499,31 +461,36 @@ TESTS += \
 check_DATA += keys.psk
 
 connect_tls_psk_SOURCES = connect-tls.c
-connect_tls_psk_CPPFLAGS = -I$(top_srcdir)/include -DPSK=1
-connect_tls_psk_CFLAGS = $(WARNINGS_CFLAGS)
+connect_tls_psk_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
+	-DPSK=1 \
+	$(NULL)
 connect_tls_psk_LDADD = $(top_builddir)/lib/libnbd.la
 
 aio_parallel_tls_SOURCES = aio-parallel.c
 aio_parallel_tls_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/common/include \
 	-DTLS=1 \
 	$(NULL)
-aio_parallel_tls_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
+aio_parallel_tls_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
 aio_parallel_tls_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 aio_parallel_load_tls_SOURCES = aio-parallel-load.c
-aio_parallel_load_tls_CPPFLAGS = -I$(top_srcdir)/include -DTLS=1
-aio_parallel_load_tls_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
+aio_parallel_load_tls_CPPFLAGS = \
+	$(AM_CPPFLAGS) \
+	-DTLS=1 \
+	$(NULL)
+aio_parallel_load_tls_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
 aio_parallel_load_tls_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 synch_parallel_tls_SOURCES = synch-parallel.c
 synch_parallel_tls_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-I$(top_srcdir)/common/include \
 	-DTLS=1 \
 	$(NULL)
-synch_parallel_tls_CFLAGS = $(WARNINGS_CFLAGS) $(PTHREAD_CFLAGS)
+synch_parallel_tls_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
 synch_parallel_tls_LDADD = $(top_builddir)/lib/libnbd.la $(PTHREAD_LIBS)
 
 keys.psk:
@@ -550,18 +517,19 @@ TESTS += \
 RANDOM1 := $(shell bash -c "echo $$(( 32768 + (RANDOM & 16383) ))")
 connect_uri_nbd_SOURCES = connect-uri.c
 connect_uri_nbd_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER_PARAMS='"-p", "$(RANDOM1)"' \
 	-DPIDFILE='"connect-uri-nbd.pid"' \
-	-DURI='"nbd://localhost:$(RANDOM1)/"'
-connect_uri_nbd_CFLAGS = $(WARNINGS_CFLAGS)
+	-DURI='"nbd://localhost:$(RANDOM1)/"' \
+	$(NULL)
+connect_uri_nbd_CFLAGS = $(AM_CFLAGS)
 connect_uri_nbd_LDADD = $(top_builddir)/lib/libnbd.la
 
 CONNECT_URI_NBD_UNIX_SOCKET := \
 	$(shell mktemp /tmp/connect-uri-nbd-unix-socket-XXXXXX)
 connect_uri_nbd_unix_SOURCES = connect-uri.c
 connect_uri_nbd_unix_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER_PARAMS='"-U", SOCKET' \
 	-DSOCKET='"$(CONNECT_URI_NBD_UNIX_SOCKET)"' \
 	-DPIDFILE='"connect-uri-nbd-unix.pid"' \
@@ -584,18 +552,18 @@ TESTS += \
 RANDOM2 := $(shell bash -c "echo $$(( 32768 + (RANDOM & 16383) ))")
 connect_uri_nbds_SOURCES = connect-uri.c
 connect_uri_nbds_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER_PARAMS='"-p", "$(RANDOM2)", "--tls=require", "--tls-certificates=pki"' \
 	-DPIDFILE='"connect-uri-nbds.pid"' \
-	-DURI='"nbds://localhost:$(RANDOM2)/"'
-connect_uri_nbds_CFLAGS = $(WARNINGS_CFLAGS)
+	-DURI='"nbds://localhost:$(RANDOM2)/"' \
+	$(NULL)
 connect_uri_nbds_LDADD = $(top_builddir)/lib/libnbd.la
 
 CONNECT_URI_NBDS_UNIX_SOCKET := \
 	$(shell mktemp /tmp/connect-uri-nbds-unix-socket-XXXXXX)
 connect_uri_nbds_unix_SOURCES = connect-uri.c
 connect_uri_nbds_unix_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER_PARAMS='"-U", SOCKET, "--tls=require", "--tls-certificates=pki"' \
 	-DSOCKET='"$(CONNECT_URI_NBDS_UNIX_SOCKET)"' \
 	-DPIDFILE='"connect-uri-nbds-unix.pid"' \
@@ -617,11 +585,11 @@ TESTS += \
 RANDOM3 := $(shell bash -c "echo $$(( 32768 + (RANDOM & 16383) ))")
 connect_uri_nbds_psk_SOURCES = connect-uri.c
 connect_uri_nbds_psk_CPPFLAGS = \
-	-I$(top_srcdir)/include \
+	$(AM_CPPFLAGS) \
 	-DSERVER_PARAMS='"-p", "$(RANDOM3)", "--tls=require", "--tls-psk=keys.psk"' \
 	-DPIDFILE='"connect-uri-nbds-psk.pid"' \
-	-DURI='"nbds://alice@localhost:$(RANDOM3)/?tls-psk-file=keys.psk"'
-connect_uri_nbds_psk_CFLAGS = $(WARNINGS_CFLAGS)
+	-DURI='"nbds://alice@localhost:$(RANDOM3)/?tls-psk-file=keys.psk"' \
+	$(NULL)
 connect_uri_nbds_psk_LDADD = $(top_builddir)/lib/libnbd.la
 
 endif HAVE_PSKTOOL
-- 
2.43.0