Bug #110226 Build fails when CMAKE_CXX_FLAGS_RELEASE is set because -DNDEBUG isn't set
Submitted: 27 Feb 2023 18:39 Modified: 28 Feb 2023 6:11
Reporter: Mark Callaghan Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:8.0.31 OS:Any
Assigned to: CPU Architecture:Any

[27 Feb 2023 18:39] Mark Callaghan
Description:
I appreciate the docs here:
https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options.html#cmake-compiler-f...

... with respect to advice on changing the optimizer level. In my case I want a Release build that uses -O2 and -O3 will be used by default. But when I set -DCMAKE_C_FLAGS_RELEASE and -DCMAKE_CXX_FLAGS_RELEASE the build for 8.0.31 and 8.0.32 fails.

How to repeat:
This is on Ubuntu 22.04 that uses gcc 11.3.0

I start with this cmake command line based on the advice at:
https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options.html#cmake-compiler-f...

The advice was:
"If you set flags that affect optimization (-Onumber), you must set the CMAKE_C_FLAGS_build_type and/or CMAKE_CXX_FLAGS_build_type options, where build_type corresponds to the CMAKE_BUILD_TYPE value."

And then has an example, and the example doesn't use -DNDEBUG.

BF=" -g1 "
CF=" $BF "
CXXF=" $BF "

cmake .. -DCMAKE_BUILD_TYPE=Release \
-DWITH_SSL=system -DWITH_ZLIB=bundled \
-DMYSQL_MAINTAINER_MODE=0 -DENABLED_LOCAL_INFILE=1 \
-DCMAKE_INSTALL_PREFIX=$1 -DWITH_BOOST=$PWD/../boost \
-DCMAKE_CXX_FLAGS="$CXXF" -DCMAKE_C_FLAGS="$CF" \
-DCMAKE_C_FLAGS_RELEASE="-O2" \
-DCMAKE_CXX_FLAGS_RELEASE="-O2" \
-DWITH_NUMA=ON -DWITH_ROUTER=OFF -DWITH_MYSQLX=OFF -DWITH_UNIT_TESTS=OFF

The build with the above cmake command line fails. Some errors are:
/home/mdcallag/b/mysql-8.0.31/storage/perfschema/pfs_variable.cc: In member function ‘bool PFS_system_variable_cache::init_show_var_array(enum_var_type, bool)’:
/home/mdcallag/b/mysql-8.0.31/storage/perfschema/pfs_variable.cc:95:24: warning: unused variable ‘LOCK_plugin’ [-Wunused-variable]
   95 |   extern mysql_mutex_t LOCK_plugin;
      |                        ^~~~~~~~~~~
In file included from /home/mdcallag/b/mysql-8.0.31/include/my_alloc.h:42,
                 from /home/mdcallag/b/mysql-8.0.31/include/sql_string.h:44,
                 from /home/mdcallag/b/mysql-8.0.31/sql/basic_ostream.h:28,
                 from /home/mdcallag/b/mysql-8.0.31/sql/binlog_ostream.h:27,
                 from /home/mdcallag/b/mysql-8.0.31/include/mysql/group_replication_priv.h:32,
                 from /home/mdcallag/b/mysql-8.0.31/plugin/replication_observers_example/replication_observers_example.cc:33:
/home/mdcallag/b/mysql-8.0.31/plugin/replication_observers_example/replication_observers_example.cc: In function ‘int trans_after_commit(Trans_param*)’:
/home/mdcallag/b/mysql-8.0.31/plugin/replication_observers_example/replication_observers_example.cc:327:13: error: ‘debug_sync_set_action’ was not declared in this scope
  327 |     assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
      |             ^~~~~~~~~~~~~~~~~~~~~
/home/mdcallag/b/mysql-8.0.31/include/my_dbug.h:173:7: note: in definition of macro ‘DBUG_EXECUTE_IF’
  173 |       a1                                 \

I can avoid build failures by adding -DNDEBUG as in ...

BF=" -g1 "
CF=" $BF "
CXXF=" $BF "

cmake .. -DCMAKE_BUILD_TYPE=Release \
-DWITH_SSL=system -DWITH_ZLIB=bundled \
-DMYSQL_MAINTAINER_MODE=0 -DENABLED_LOCAL_INFILE=1 \
-DCMAKE_INSTALL_PREFIX=$1 -DWITH_BOOST=$PWD/../boost \
-DCMAKE_CXX_FLAGS="$CXXF" -DCMAKE_C_FLAGS="$CF" \
-DCMAKE_C_FLAGS_RELEASE="-O2 -DNDEBUG" \
-DCMAKE_CXX_FLAGS_RELEASE="-O2 -DNDEBUG" \
-DWITH_NUMA=ON -DWITH_ROUTER=OFF -DWITH_MYSQLX=OFF -DWITH_UNIT_TESTS=OFF

Suggested fix:
Either the cmake files or the docs should be updated. The doc update should be easy, just add -DNDEBUG. I have limited cmake skills so I don't have an opinion on whether this is a bug in the cmake support.
[27 Feb 2023 20:16] Mark Callaghan
Based on this comment, I assume the docs need to be updated.

https://github.com/mysql/mysql-server/blob/1bfe02bdad6604d54913c62614bde57a055c8332/CMakeL...

# So, if you want to use '-O3' for a RELWITHDEBINFO build, you should do:
# cmake -DCMAKE_CXX_FLAGS_RELWITHDEBINFO="-O3 -g -DNDEBUG"
[27 Feb 2023 20:27] Mark Callaghan
The 5.7 docs have similar advice, but also don't work because compiler_options.cmake clobbers whatever I pass via the command line. See:
https://github.com/mysql/mysql-server/commit/f23927c82896ab353a3ca2d051b3a79502a16627
[28 Feb 2023 6:11] MySQL Verification Team
Hello Mark,

Thank you for the report and feedback.

Sincerely,
Umesh
[28 Feb 2023 6:46] Laurynas Biveinis
I am having trouble imagining a use case were one would want Release configuration without NDEBUG (which is a language standard macro) defined. Thus, it should be added to Release config flags unconditionally regardless of the user-set flags.