From 6577acf650863f48a83496b9d3a11260fc01330e Mon Sep 17 00:00:00 2001 From: Andrew Shao Date: Fri, 7 Feb 2025 16:44:05 -0800 Subject: [PATCH] Add patches and modify flags to allow compilation on Apple Silicon --- WORKSPACE | 29 +++++++++++++++++-- ml_metadata/mysql_configure.bzl | 5 +++- ml_metadata/postgresql.BUILD | 4 ++- .../third_party/libmysqlclient_2.patch | 19 ++++++++++++ ml_metadata/third_party/rules_cc.patch | 11 +++++++ ml_metadata/third_party/rules_cc_2.patch | 11 +++++++ ml_metadata/third_party/upb.patch | 11 +++++++ ml_metadata/third_party/zetasql.patch | 24 +++++++++++++++ ml_metadata/utils.bzl | 9 ++++++ setup.py | 6 ++-- 10 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 ml_metadata/third_party/libmysqlclient_2.patch create mode 100644 ml_metadata/third_party/rules_cc.patch create mode 100644 ml_metadata/third_party/rules_cc_2.patch create mode 100644 ml_metadata/third_party/upb.patch create mode 100644 ml_metadata/third_party/zetasql.patch create mode 100644 ml_metadata/utils.bzl diff --git a/WORKSPACE b/WORKSPACE index 246515185..954fdec46 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,8 +1,20 @@ workspace(name = "ml_metadata") load("//ml_metadata:repo.bzl", "clean_dep") +load("//ml_metadata:utils.bzl", "github_archive") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +github_archive( + name = "rules_cc", + repo = "https://github.com/bazelbuild/rules_cc", + commit = "818289e5613731ae410efb54218a4077fb9dbb03", + sha256 = "0adbd6f567291ad526e82c765e15aed33cea5e256eeba129f1501142c2c56610", + patches = [ + "//ml_metadata/third_party:rules_cc.patch", + "//ml_metadata/third_party:rules_cc_2.patch" + ] +) + http_archive( name = "postgresql", build_file = "//ml_metadata:postgresql.BUILD", @@ -39,7 +51,10 @@ http_archive( ) load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") -rules_foreign_cc_dependencies() +rules_foreign_cc_dependencies( + register_preinstalled_tools = True, + register_built_tools = False, +) http_archive( name = "com_google_absl", @@ -106,6 +121,14 @@ http_archive( build_file = "//ml_metadata/third_party:six.BUILD" ) +github_archive( + name = "upb", + repo = "https://github.com/protocolbuffers/upb", + commit = "230d502d6ea96fc9d40f02ad26c20fe85a627648", + sha256 = "28488db7f638ed5a433f7ac3b67e6b62932c155c2cd9987a2c534a3591f3b2dd", + patches = ["//ml_metadata/third_party:upb.patch",], +) + http_archive( name = "com_google_protobuf", sha256 = "1add10f9bd92775b91f326da259f243881e904dd509367d5031d4c782ba82810", @@ -252,7 +275,9 @@ http_archive( name = "com_google_zetasql", urls = ["https://github.com/google/zetasql/archive/%s.zip" % ZETASQL_COMMIT], strip_prefix = "zetasql-%s" % ZETASQL_COMMIT, - #patches = ["//ml_metadata/third_party:zetasql.patch"], + patches = [ + "//ml_metadata/third_party:zetasql_2.patch", + ], sha256 = '651a768cd51627f58aa6de7039aba9ddab22f4b0450521169800555269447840' ) diff --git a/ml_metadata/mysql_configure.bzl b/ml_metadata/mysql_configure.bzl index 457f650e5..4679305fa 100644 --- a/ml_metadata/mysql_configure.bzl +++ b/ml_metadata/mysql_configure.bzl @@ -27,5 +27,8 @@ def mysql_configure(): workspace_file = "//ml_metadata:libmysqlclient.WORKSPACE", remote = "https://github.com/MariaDB/mariadb-connector-c.git", tag = "v3.0.8-release", - patches = ["//ml_metadata/third_party:libmysqlclient.patch"], + patches = [ + "//ml_metadata/third_party:libmysqlclient.patch", + "//ml_metadata/third_party:libmysqlclient_2.patch", + ], ) diff --git a/ml_metadata/postgresql.BUILD b/ml_metadata/postgresql.BUILD index 42e2f811b..46cd33cd1 100644 --- a/ml_metadata/postgresql.BUILD +++ b/ml_metadata/postgresql.BUILD @@ -1146,7 +1146,9 @@ genrule( "/* #undef HAVE__CPUID */", "", "/* Define to 1 if you have __get_cpuid. */", - "#define HAVE__GET_CPUID 1", + "#ifndef __APPLE__", + "# define HAVE__GET_CPUID 1", + "#endif", "", "/* Define to 1 if your compiler understands _Static_assert. */", "#define HAVE__STATIC_ASSERT 1", diff --git a/ml_metadata/third_party/libmysqlclient_2.patch b/ml_metadata/third_party/libmysqlclient_2.patch new file mode 100644 index 000000000..e96de33db --- /dev/null +++ b/ml_metadata/third_party/libmysqlclient_2.patch @@ -0,0 +1,19 @@ +--- cmake/FindIconv.cmake 2025-02-07 16:18:14 ++++ cmake/FindIconv.cmake.new 2025-02-07 16:21:47 +@@ -18,14 +18,14 @@ + set(ICONV_FIND_QUIETLY TRUE) + endif (ICONV_INCLUDE_DIR AND ICONV_LIBRARIES) + +-find_path(ICONV_INCLUDE_DIR iconv.h) ++find_path(ICONV_INCLUDE_DIR iconv.h ${ICONV_PATH}/include) + + IF(CMAKE_SYSTEM_NAME MATCHES "SunOS") + # There is some libiconv.so in /usr/local that must + # be avoided, iconv routines are in libc + ELSEIF(APPLE) + find_library(ICONV_LIBRARIES NAMES iconv libiconv PATHS +- /usr/lib/ ++ ${ICONV_PATH}/lib + NO_CMAKE_SYSTEM_PATH) + SET(ICONV_EXTERNAL TRUE) + ELSE() diff --git a/ml_metadata/third_party/rules_cc.patch b/ml_metadata/third_party/rules_cc.patch new file mode 100644 index 000000000..e1f2a46f0 --- /dev/null +++ b/ml_metadata/third_party/rules_cc.patch @@ -0,0 +1,11 @@ +--- ./cc/private/toolchain/cc_toolchain_config.bzl 2020-05-13 07:24:06 ++++ ./cc/private/toolchain/cc_toolchain_config.bzl.new 2025-02-06 12:52:31 +@@ -385,8 +385,6 @@ + flag_group( + flags = [ + "-lstdc++", +- "-undefined", +- "dynamic_lookup", + "-headerpad_max_install_names", + "-no-canonical-prefixes", + ], diff --git a/ml_metadata/third_party/rules_cc_2.patch b/ml_metadata/third_party/rules_cc_2.patch new file mode 100644 index 000000000..858ebd323 --- /dev/null +++ b/ml_metadata/third_party/rules_cc_2.patch @@ -0,0 +1,11 @@ +--- cc/private/toolchain/unix_cc_configure.bzl 2025-02-06 15:38:22 ++++ cc/private/toolchain/unix_cc_configure.bzl.new 2025-02-06 15:39:08 +@@ -521,8 +521,6 @@ + "-z", + ) + ( + [ +- "-undefined", +- "dynamic_lookup", + "-headerpad_max_install_names", + ] if darwin else bin_search_flag + [ + # Gold linker only? Can we enable this by default? diff --git a/ml_metadata/third_party/upb.patch b/ml_metadata/third_party/upb.patch new file mode 100644 index 000000000..4290b3713 --- /dev/null +++ b/ml_metadata/third_party/upb.patch @@ -0,0 +1,11 @@ +--- bazel/build_defs.bzl 2022-10-17 09:59:30 ++++ bazel/build_defs.bzl.new 2025-02-06 11:28:56 +@@ -34,7 +34,7 @@ + _DEFAULT_CPPOPTS.extend([ + "-Wextra", + # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang) +- "-Werror", ++ # "-Werror", + "-Wno-long-long", + ]) + _DEFAULT_COPTS.extend([ diff --git a/ml_metadata/third_party/zetasql.patch b/ml_metadata/third_party/zetasql.patch new file mode 100644 index 000000000..310b8f55f --- /dev/null +++ b/ml_metadata/third_party/zetasql.patch @@ -0,0 +1,24 @@ +--- bazel/BUILD 2022-08-10 22:20:30 ++++ bazel/BUILD.new 2025-02-07 14:49:24 +@@ -40,7 +40,11 @@ + configure_make( + name = "m4", + env = select({ +- "@platforms//os:macos": {"AR": ""}, ++ "@platforms//os:macos": { ++ "AR": "", ++ "CFLAGS": "-Wno-incompatible-function-pointer-types", ++ "CXXFLAGS": "-Wno-incompatible-function-pointer-types", ++ }, + "//conditions:default": {}, + }), + lib_source = "@m4//:all", +@@ -63,6 +67,8 @@ + env = select({ + "@platforms//os:macos": { + "AR": "", ++ "CFLAGS": "-Wno-incompatible-function-pointer-types", ++ "CXXFLAGS": "-Wno-incompatible-function-pointer-types", + "M4": "$(execpath :m4_bin)", + }, + "//conditions:default": {"M4": "$(execpath :m4_bin)"}, diff --git a/ml_metadata/utils.bzl b/ml_metadata/utils.bzl new file mode 100644 index 000000000..c3e1e8f6f --- /dev/null +++ b/ml_metadata/utils.bzl @@ -0,0 +1,9 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +def github_archive(repo, commit, **kwargs): + repo_name = repo.split("/")[-1] + http_archive( + urls = [repo + "/archive/" + commit + ".zip"], + strip_prefix = repo_name + "-" + commit, + **kwargs + ) diff --git a/setup.py b/setup.py index 5b1ad7415..a401a1b2a 100644 --- a/setup.py +++ b/setup.py @@ -99,10 +99,10 @@ def finalize_options(self): else: self._additional_build_options = ['--macos_minimum_os=10.14'] - if 'ICONV_LIBRARIES' in os.environ: + if 'ICONV_PATH' in os.environ: self._additional_build_options.append( - '--action_env=CMAKE_ICONV_FLAG=-DICONV_LIBRARIES=' + - os.environ['ICONV_LIBRARIES']) + '--action_env=CMAKE_ICONV_FLAG=-DICONV_PATH=' + + os.environ['ICONV_PATH']) def run(self): subprocess.check_call(