all: add "nm-std-aux/nm-default-std.h" as replacement for "nm-default.h"

autotools projects commonly should include "config.h" as first header.
Also, commonly we need more headers, like glib.h or our nm_auto macros.
Hence, almost all our sources should as first include "nm-default.h".

However, as we build different parts, "nm-default.h" gets controlled
by the NETWORKMANAGER_COMPILATION define which autotools/meson needs
to specify in the build options.

That is confusing.

One advantage of that was, that theoretically the same sources can
be built twice, with different behavior. However, we should avoid doing
that altogether and build static libraries (once) that we link multiple
times.

Another advantage was that if NETWORKMANAGER_COMPILATION is for example
set to build a DAEMON source, there is a check that we don't include
private headers from libnm-core. However, that should be better solved
by not having public, internal and private headers in the same
directory.

Instead, introduce different "nm-default-*.h" headers that don't require
special defines and behave in a consistent way. This way, we require
fewer CFLAGS and it's immediately clear by looking at the source alone
which headers are included. Also, you will be easier see when a wrong
nm-default-*.h header gets included.

Introduce the first replacement. The others will follow.
This commit is contained in:
Thomas Haller 2021-02-04 07:34:17 +01:00
parent 021e87c084
commit dbdac49b81
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
5 changed files with 142 additions and 4 deletions

View file

@ -354,7 +354,6 @@ shared_nm_std_aux_libnm_std_aux_la_CPPFLAGS = \
$(CODE_COVERAGE_CFLAGS) \
$(SANITIZER_LIB_CFLAGS) \
-DG_LOG_DOMAIN=\""libnm"\" \
-DNETWORKMANAGER_COMPILATION='0' \
$(NULL)
shared_nm_std_aux_libnm_std_aux_la_SOURCES = \
@ -362,6 +361,7 @@ shared_nm_std_aux_libnm_std_aux_la_SOURCES = \
shared/nm-std-aux/c-list-util.c \
shared/nm-std-aux/c-list-util.h \
shared/nm-std-aux/nm-dbus-compat.h \
shared/nm-std-aux/nm-default-std.h \
shared/nm-std-aux/nm-networkmanager-compilation.h \
shared/nm-std-aux/nm-std-aux.h \
shared/nm-std-aux/nm-std-utils.c \

View file

@ -114,8 +114,7 @@ libnm_std_aux = static_library(
],
include_directories: top_inc,
c_args: [
'-DG_LOG_DOMAIN="@0@"'.format(libnm_name),
'-DNETWORKMANAGER_COMPILATION=0',
'-DG_LOG_DOMAIN="libnm"',
],
)

View file

@ -22,6 +22,10 @@
#error Define NETWORKMANAGER_COMPILATION accordingly
#endif
#if NETWORKMANAGER_COMPILATION < NM_NETWORKMANAGER_COMPILATION_WITH_GLIB
#error Dont include this header with such NETWORKMANAGER_COMPILATION
#endif
#ifndef G_LOG_DOMAIN
#if defined(NETWORKMANAGER_COMPILATION_TEST)
#define G_LOG_DOMAIN "test"

View file

@ -0,0 +1,135 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2015 Red Hat, Inc.
*/
#ifndef __NM_DEFAULT_STD_H__
#define __NM_DEFAULT_STD_H__
#include "nm-networkmanager-compilation.h"
#ifdef NETWORKMANAGER_COMPILATION
#error Dont define NETWORKMANAGER_COMPILATION
#endif
#ifdef NETWORKMANAGER_COMPILATION_TEST
#error Dont define NETWORKMANAGER_COMPILATION_TEST
#endif
#ifndef G_LOG_DOMAIN
#error Define G_LOG_DOMAIN
#endif
/*****************************************************************************/
#define NETWORKMANAGER_COMPILATION 0
/*****************************************************************************/
/* always include these headers for our internal source files. */
#ifndef ___CONFIG_H__
#define ___CONFIG_H__
#include <config.h>
#endif
#include "config-extra.h"
/* for internal compilation we don't want the deprecation macros
* to be in effect. Define the widest range of versions to effectively
* disable deprecation checks */
#define NM_VERSION_MIN_REQUIRED NM_VERSION_0_9_8
#ifndef NM_MORE_ASSERTS
#define NM_MORE_ASSERTS 0
#endif
#if NM_MORE_ASSERTS == 0
/* The cast macros like NM_TYPE() are implemented via G_TYPE_CHECK_INSTANCE_CAST()
* and _G_TYPE_CIC(). The latter, by default performs runtime checks of the type
* by calling g_type_check_instance_cast().
* This check has a certain overhead without being helpful.
*
* Example 1:
* static void foo (NMType *obj)
* {
* access_obj_without_check (obj);
* }
* foo ((NMType *) obj);
* // There is no runtime check and passing an invalid pointer
* // leads to a crash.
*
* Example 2:
* static void foo (NMType *obj)
* {
* access_obj_without_check (obj);
* }
* foo (NM_TYPE (obj));
* // There is a runtime check which prints a g_warning(), but that doesn't
* // avoid the crash as NM_TYPE() cannot do anything then passing on the
* // invalid pointer.
*
* Example 3:
* static void foo (NMType *obj)
* {
* g_return_if_fail (NM_IS_TYPE (obj));
* access_obj_without_check (obj);
* }
* foo ((NMType *) obj);
* // There is a runtime check which prints a g_critical() which also avoids
* // the crash. That is actually helpful to catch bugs and avoid crashes.
*
* Example 4:
* static void foo (NMType *obj)
* {
* g_return_if_fail (NM_IS_TYPE (obj));
* access_obj_without_check (obj);
* }
* foo (NM_TYPE (obj));
* // The runtime check is performed twice, with printing a g_warning() and
* // a g_critical() and avoiding the crash.
*
* Example 3 is how it should be done. Type checks in NM_TYPE() are pointless.
* Disable them for our production builds.
*/
#ifndef G_DISABLE_CAST_CHECKS
#define G_DISABLE_CAST_CHECKS
#endif
#endif
#if NM_MORE_ASSERTS == 0
#ifndef G_DISABLE_CAST_CHECKS
/* Unless compiling with G_DISABLE_CAST_CHECKS, glib performs type checking
* during G_VARIANT_TYPE() via g_variant_type_checked_(). This is not necessary
* because commonly this cast is needed during something like
*
* g_variant_builder_init (&props, G_VARIANT_TYPE ("a{sv}"));
*
* Note that in if the variant type would be invalid, the check still
* wouldn't make the buggy code magically work. Instead of passing a
* bogus type string (bad), it would pass %NULL to g_variant_builder_init()
* (also bad).
*
* Also, a function like g_variant_builder_init() already validates
* the input type via something like
*
* g_return_if_fail (g_variant_type_is_container (type));
*
* So, by having G_VARIANT_TYPE() also validate the type, we validate
* twice, whereas the first validation is rather pointless because it
* doesn't prevent the function to be called with invalid arguments.
*
* Just patch G_VARIANT_TYPE() to perform no check.
*/
#undef G_VARIANT_TYPE
#define G_VARIANT_TYPE(type_string) ((const GVariantType *) (type_string))
#endif
#endif
/*****************************************************************************/
#include <stdlib.h>
/*****************************************************************************/
#endif /* __NM_DEFAULT_STD_H__ */

View file

@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "nm-default.h"
#include "nm-default-std.h"
#include "nm-std-utils.h"