[gtk/mcatanzaro/build-best-practices: 1/2] build: fix check for debug vs. release builds




commit b34683d9910bcf63e164181a4eaf426c38182526
Author: Michael Catanzaro <mcatanzaro redhat com>
Date:   Thu Aug 4 11:09:16 2022 -0500

    build: fix check for debug vs. release builds
    
    Confusingly, Meson's debug option indicates whether debuginfo is
    enabled, not whether we have a debug build. We should consider the
    optimization level as well when deciding whether to enable debug extras.
    
    I've outlined an argument for why we should make these changes here:
    
    https://blogs.gnome.org/mcatanzaro/2022/07/15/best-practices-for-build-options/
    
    The main take-away is this rule:
    
    “You have a non-production (debug) build if debug is true and if
    optimization is 0 or g; otherwise, you have a production build.”
    
    This rule actually fails on Windows, where Meson's debug option functions
    completely differently than everywhere else, but in the next commit I
    will add a safety hatch to explicitly select whether debug is enabled or
    not.
    
    This commit additionally removes guidance related to manually
    controlling preprocessor definitions in plain builds, which is
    controversial and violates Rule 6 from my blog post. I will add build
    options to handle things manually in the next commit.

 docs/reference/gtk/building.md | 33 ++++++++-------------------------
 meson.build                    | 15 ++++++---------
 2 files changed, 14 insertions(+), 34 deletions(-)
---
diff --git a/docs/reference/gtk/building.md b/docs/reference/gtk/building.md
index 31073d1d04..e12f0e33ed 100644
--- a/docs/reference/gtk/building.md
+++ b/docs/reference/gtk/building.md
@@ -83,35 +83,18 @@ configuration option. GTK enables and disables functionality
 depending on the build type used when calling *meson* to
 configure the build.
 
-### Debug builds
-
-GTK will enable debugging code paths in both the `debug` and
-`debugoptimized` build types. Builds with `buildtype` set to
-`debug` will additionally enable consistency checks on the
-internal state of the toolkit.
-
-It is recommended to use the `debug` or `debugoptimized` build
-types when developing GTK itself. Additionally, `debug` builds of
-GTK are recommended for profiling and debugging GTK applications,
-as they include additional validation of the internal state.
+GTK will enable debugging code paths, including
+consistency checks on the internal state of the toolkit,
+when using the `debug` build type. Other build types will
+disable debugging code paths and additional run time safeties,
+like checked casts for object instances. It is recommended to
+use the `debug` build type when developing GTK itself. Additionally,
+`debug` builds of GTK are recommended for profiling and debugging GTK
+applications, as they include additional validation of the internal state.
 
 The `debugoptimized` build type is the default for GTK if no build
 type is specified when calling *meson*.
 
-### Release builds
-
-The `release` build type will disable debugging code paths and
-additional run time safeties, like checked casts for object
-instances.
-
-The `plain` build type provided by Meson should only be used when
-packaging GTK, and it's expected that packagers will provide their
-own compiler flags when building GTK. See the previous section for
-the list of environment variables to be used to define compiler and
-linker flags. Note that with the plain build type, you are also
-responsible for controlling the debugging features of GTK with
-`-DG_ENABLE_DEBUG` and `-DG_DISABLE_CAST_CHECKS`.
-
 ## Dependencies
 
 Before you can compile the GTK widget toolkit, you need to have
diff --git a/meson.build b/meson.build
index 2ab7290628..2f5670321d 100644
--- a/meson.build
+++ b/meson.build
@@ -58,16 +58,13 @@ add_project_arguments('-DGTK_VERSION="@0@"'.format(meson.project_version()), lan
 add_project_arguments('-D_GNU_SOURCE', language: 'c')
 
 # Use debug/optimization flags to determine whether to enable debug or disable
-# cast checks
+# cast checks. We have a non-production (debug) build if debug is true and if
+# optimization is 0 or g; otherwise, we have a production build.
 gtk_debug_cflags = []
-debug = get_option('debug')
-optimization = get_option('optimization')
-if debug
-  gtk_debug_cflags += '-DG_ENABLE_DEBUG'
-  if optimization in ['0', 'g']
-    gtk_debug_cflags += '-DG_ENABLE_CONSISTENCY_CHECKS'
-  endif
-elif optimization in ['2', '3', 's']
+optimized_build = get_option('optimization') not in [ '0', 'g' ]
+if get_option('debug') and not optimized_build
+  gtk_debug_cflags += ['-DG_ENABLE_DEBUG', '-DG_ENABLE_CONSISTENCY_CHECKS']
+elif optimized_build
   gtk_debug_cflags += ['-DG_DISABLE_CAST_CHECKS', '-DG_DISABLE_ASSERT']
 endif
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]