Bug #105464 MySQL 8.0.27 don't build on macOS 12.0.1 due to fragile cmake code
Submitted: 4 Nov 2021 23:08 Modified: 20 Jan 2022 21:00
Reporter: Christopher Chavez (OCA) Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:8.0.27 OS:MacOS (12.0.1)
Assigned to: CPU Architecture:Any

[4 Nov 2021 23:08] Christopher Chavez
Description:
MySQL 8.0.27 (and likely since 8.0.24) encounters a fatal error when configuring on macOS 12.0.1:

CMake Error at cmake/package_name.cmake:92 (MESSAGE):
  Could not run sw_vers
Call Stack (most recent call first):
  cmake/package_name.cmake:150 (GET_PACKAGE_FILE_NAME)
  CMakeLists.txt:754 (INCLUDE)
-- Configuring incomplete, errors occurred!

The cause was introduced by a change made to cmake/package_name.cmake in 8.0.24: https://github.com/mysql/mysql-server/commit/83b87ae7f8b0 (Bug #32349239 Change-Id: I97984bdaa09d025422709fc15569e12f3ee2ff11). Part of the change is to check whether a STRING(REGEX MATCH …) command failed based on whether either of CMAKE_MATCH_1 or CMAKE_MATCH_2 are false. On macOS x.y or macOS x.y.z where y is 0, CMAKE_MATCH_2 is set to "0" which is considered false, and the "Could not run sw_vers" error is incorrectly output.

How to repeat:
Attempt to configure MySQL 8.0.27 on macOS 12.0.1 (or likely any other macOS x.y or macOS x.y.z where y is 0, such as macOS 11.0).

Suggested fix:
In cmake/package_name.cmake: check CMAKE_MATCH_COUNT EQUAL 2 instead to know whether STRING(REGEX MATCH …) succeeded and that CMAKE_MATCH_1 and CMAKE_MATCH_2 are correctly set. This works correctly even in cases when CMAKE_MATCH_2 is "0".
[5 Nov 2021 0:10] Christopher Chavez
Here is the change I intend to attach to "Contributions" if my OCA is approved:

diff --git a/cmake/package_name.cmake b/cmake/package_name.cmake
index 058b0bcbd8cb..be1af896d515 100644
--- a/cmake/package_name.cmake
+++ b/cmake/package_name.cmake
@@ -88,7 +88,7 @@ MACRO(GET_PACKAGE_FILE_NAME Var)
 
       STRING(REGEX MATCH
         "ProductVersion:[\n\t ]*([0-9]+)\\.([0-9]+)" UNUSED ${SW_VERS_PRODUCTVERSION})
-      IF(NOT CMAKE_MATCH_1 OR NOT CMAKE_MATCH_2)
+      IF(NOT CMAKE_MATCH_COUNT EQUAL 2)
         MESSAGE(FATAL_ERROR "Could not run sw_vers")
       ENDIF()
[5 Nov 2021 13:37] MySQL Verification Team
Hi Mr. Chavez,

Thank you for your bug report.

However, we are not able to repeat the problem.

We have built 8.0.27 several times on macOS Monterey, without any problems. The only thing that we had to do is to use latest stable CMake program , which is 3.21.3 and latest XCode. We also had to choose the options carefully, so that they use only the resources that are available on macOS and not those that are running only on Linux.

Hence, we can't repeat the problem.
[5 Nov 2021 13:52] MySQL Verification Team
One more info from us .....

We can run program `sw_vers` without problem on our Monterey. Make sure you have it available and in the PATH.
[5 Nov 2021 22:33] Christopher Chavez
Thank you for verifying this report.

The person I am reporting this issue on behalf of was using Xcode 13.1 and CMake 3.21.4. I provided them this debugging patch which prints relevant CMake variables and truth values: https://github.com/chrstphrchvz/mysql-server/commit/f9af863dabd5.patch

This is the output they received (https://trac.macports.org/ticket/63726#comment:10):

-- SW_VERS_OUTPUT_LIST: 'ProductName:	macOS;ProductVersion:	12.0.1;BuildVersion:	21A559'
-- SW_VERS_PRODUCTNAME: 'ProductName:	macOS'
-- SW_VERS_PRODUCTVERSION: 'ProductVersion:	12.0.1'
-- CMAKE_MATCH_1: '12'
-- CMAKE_MATCH_2: '0'
-- CMAKE_MATCH_1: TRUE
-- CMAKE_MATCH_2: FALSE
CMake Error at cmake/package_name.cmake:107 (MESSAGE):
  Could not run sw_vers

So the problem did not appear to be with running the sw_vers command itself, but rather how the parsed output from it is being handled.

The code in cmake/package_name.cmake relevant to this issue is specific to macOS, so I did not have the impression the error could be due to MySQL being configured requiring resources (I’m not sure if this refers to system commands or MySQL features) not compatible with macOS.

Someone also suggested it would be simpler for the code to run `sw_vers -productVersion` (an option supported even by older macOS versions) and rely less on regex parsing.
[6 Nov 2021 16:14] Terje Røsten
Hi!

Thanks for your report. This issue has been resolved upstream already.
The patch used is:

iff --git a/cmake/package_name.cmake b/cmake/package_name.cmake
index 058b0bcbd8c..7983475221c 100644
--- a/cmake/package_name.cmake
+++ b/cmake/package_name.cmake
@@ -88,7 +88,7 @@ MACRO(GET_PACKAGE_FILE_NAME Var)

       STRING(REGEX MATCH
         "ProductVersion:[\n\t ]*([0-9]+)\\.([0-9]+)" UNUSED ${SW_VERS_PRODUCTVERSION})
-      IF(NOT CMAKE_MATCH_1 OR NOT CMAKE_MATCH_2)
+      IF(NOT DEFINED CMAKE_MATCH_1 OR NOT DEFINED CMAKE_MATCH_2)
         MESSAGE(FATAL_ERROR "Could not run sw_vers")
       ENDIF()

Can you please try this patch and verify this fix works for you?
[7 Nov 2021 11:34] Christopher Chavez
Someone else opened a pull request on GitHub suggesting the exact same fix as in Terje Røsten’s comment: https://github.com/mysql/mysql-server/pull/379

The GitHub pull request author confirms that the upstream (MySQL internal development?) fix works. I’ve not heard from the person I reported this issue on behalf of regarding the results of trying the upstream fix, however I am confident it will work as I believe it is functionally equivalent to the CMAKE_MATCH_COUNT fix I proposed.
[8 Nov 2021 12:17] MySQL Verification Team
Thank you, Terje .....

For the info, we use XCode 13.1 and CMake 3.21.3.
[10 Nov 2021 7:46] Christopher Chavez
The person that reported the issue to me has verified that the upstream fix works.
[10 Nov 2021 12:28] MySQL Verification Team
Thank you , Mr. Chavez.
[1 Dec 2021 7:20] MySQL Verification Team
Bug #105760 marked as duplicate of this one
[14 Dec 2021 15:45] MySQL Verification Team
Bug #105893 marked as duplicate of this one.
[20 Jan 2022 21:00] Christopher Chavez
MySQL 8.0.28 includes the fix for this issue.
[21 Jan 2022 13:37] MySQL Verification Team
Yes, that is true .......

It is a single line fix ......