Repository: mesonbuild/meson

Issue:
Should meson specifiy -release for DLang projects with the `plain` buildtype?
Right now meson doesn't specify `-release` on the compiler command line when compiling with the `plain` buildtype. This is problematic, since the `-release` flag doesn't do unwanted optimizations distros want to avoid, it disables asserts, quoting the LDC docs: `-release | Disables asserts, invariants, contracts and boundscheck` [0]. Since distros usually use the `plain` buildtype, but don't want debug asserts, contracts (input validation for functions, also via asserts) etc. in their binaries it'd make sense to specify `-release` even with the `plain` buildtype IMHO.




Discussion:
It sounds like `-release` should be tied t the badly named `b_ndebug` flag then.
Ah yes, that's probably the right course of action.

Resolving patch:
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -186,7 +186,7 @@ def is_known_suffix(fname):
 d_gdc_buildtype_args = {'plain': [],
                         'debug': [],
                         'debugoptimized': ['-finline-functions'],
-                        'release': ['-frelease', '-finline-functions'],
+                        'release': ['-finline-functions'],
                         'minsize': [],
                         'custom': [],
                         }
@@ -194,7 +194,7 @@ def is_known_suffix(fname):
 d_ldc_buildtype_args = {'plain': [],
                         'debug': [],
                         'debugoptimized': ['-enable-inlining', '-Hkeep-all-bodies'],
-                        'release': ['-release', '-enable-inlining', '-Hkeep-all-bodies'],
+                        'release': ['-enable-inlining', '-Hkeep-all-bodies'],
                         'minsize': [],
                         'custom': [],
                         }
@@ -202,7 +202,7 @@ def is_known_suffix(fname):
 d_dmd_buildtype_args = {'plain': [],
                         'debug': [],
                         'debugoptimized': ['-inline'],
-                        'release': ['-release', '-inline'],
+                        'release': ['-inline'],
                         'minsize': [],
                         'custom': [],
                         }
@@ -320,7 +320,7 @@ def get_base_compile_args(options, compiler):
         if (options['b_ndebug'].value == 'true' or
                 (options['b_ndebug'].value == 'if-release' and
                  options['buildtype'].value in {'release', 'plain'})):
-            args += ['-DNDEBUG']
+            args += compiler.get_disable_assert_args()
     except KeyError:
         pass
     # This does not need a try...except
@@ -1204,6 +1204,9 @@ def use_linker_args(cls, linker: str) -> T.List[str]:
     def get_coverage_link_args(self) -> T.List[str]:
         return self.linker.get_coverage_args()
 
+    def get_disable_assert_args(self) -> T.List[str]:
+        return []
+
 
 def get_largefile_args(compiler):
     '''
diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py
--- a/mesonbuild/compilers/d.py
+++ b/mesonbuild/compilers/d.py
@@ -647,7 +647,7 @@ def __init__(self, exelist, version, for_machine: MachineChoice,
                           '2': default_warn_args + ['-Wextra'],
                           '3': default_warn_args + ['-Wextra', '-Wpedantic']}
         self.base_options = ['b_colorout', 'b_sanitize', 'b_staticpic',
-                             'b_vscrt', 'b_coverage', 'b_pgo']
+                             'b_vscrt', 'b_coverage', 'b_pgo', 'b_ndebug']
 
         self._has_color_support = version_compare(self.version, '>=4.9')
         # dependencies were implemented before, but broken - support was fixed in GCC 7.1+
@@ -686,6 +686,9 @@ def get_linker_always_args(self) -> T.List[str]:
             return args
         return args + ['-shared-libphobos']
 
+    def get_disable_assert_args(self):
+        return ['-frelease']
+
 
 class LLVMDCompiler(DmdLikeCompilerMixin, DCompiler):
 
@@ -693,7 +696,7 @@ def __init__(self, exelist, version, for_machine: MachineChoice,
                  info: 'MachineInfo', arch, **kwargs):
         DCompiler.__init__(self, exelist, version, for_machine, info, arch, False, None, **kwargs)
         self.id = 'llvm'
-        self.base_options = ['b_coverage', 'b_colorout', 'b_vscrt']
+        self.base_options = ['b_coverage', 'b_colorout', 'b_vscrt', 'b_ndebug']
 
     def get_colorout_args(self, colortype):
         if colortype == 'always':
@@ -735,6 +738,9 @@ def get_linker_always_args(self) -> T.List[str]:
             return args
         return args + ['-link-defaultlib-shared']
 
+    def get_disable_assert_args(self) -> T.List[str]:
+        return ['--release']
+
 
 class DmdDCompiler(DmdLikeCompilerMixin, DCompiler):
 
@@ -742,7 +748,7 @@ def __init__(self, exelist, version, for_machine: MachineChoice,
                  info: 'MachineInfo', arch, **kwargs):
         DCompiler.__init__(self, exelist, version, for_machine, info, arch, False, None, **kwargs)
         self.id = 'dmd'
-        self.base_options = ['b_coverage', 'b_colorout', 'b_vscrt']
+        self.base_options = ['b_coverage', 'b_colorout', 'b_vscrt', 'b_ndebug']
 
     def get_colorout_args(self, colortype):
         if colortype == 'always':
@@ -805,3 +811,6 @@ def get_linker_always_args(self) -> T.List[str]:
         if self.info.is_windows():
             return args
         return args + ['-defaultlib=phobos2', '-debuglib=phobos2']
+
+    def get_disable_assert_args(self) -> T.List[str]:
+        return ['-release']
diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py
--- a/mesonbuild/compilers/mixins/clike.py
+++ b/mesonbuild/compilers/mixins/clike.py
@@ -1137,3 +1137,6 @@ def has_func_attribute(self, name, env):
 
         return self.compiles(self.attribute_check_func(name), env,
                              extra_args=self.get_has_func_attribute_extra_args(name))
+
+    def get_disable_assert_args(self) -> T.List[str]:
+        return ['-DNDEBUG']


Test patch: