From a75e615a90b74e1ea40c119f095940ed44d26fe9 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Thu, 14 Oct 2021 15:32:47 -0700 Subject: [PATCH] Centralize C++ compiler flags in rn_defs.bzl Summary: Centralize C++ compiler flags in rn_defs.bzl. There is really no reason for these Cxx libraries to specify their own compiler flags: nearly 100% of them are identical, and the copypasta makes it difficult to make repo-wide changes (like upgrading C++ versions, etc). This is now causing build failures until everything is migrated properly, and there are two flags (enable_rtti and enable_exceptions) that MUST have the same value and can be configured per-module, as needed. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D31631767 fbshipit-source-id: 84f0441eb0ad09219e97d13babe0707d25f08472 --- tools/build_defs/oss/rn_defs.bzl | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tools/build_defs/oss/rn_defs.bzl b/tools/build_defs/oss/rn_defs.bzl index 7064150c9d0..45a732449da 100644 --- a/tools/build_defs/oss/rn_defs.bzl +++ b/tools/build_defs/oss/rn_defs.bzl @@ -80,17 +80,39 @@ def get_react_native_preprocessor_flags(): return [] # Building is not supported in OSS right now -def rn_xplat_cxx_library(name, **kwargs): - new_kwargs = { +def rn_xplat_cxx_library(name, compiler_flags_enable_exceptions = True, compiler_flags_enable_rtti = True, **kwargs): + visibility = kwargs.get("visibility", []) + kwargs = { k: v for k, v in kwargs.items() if k.startswith("exported_") } + # RTTI and exceptions must either be both on, or both off + if compiler_flags_enable_exceptions != compiler_flags_enable_rtti: + fail("Must enable or disable both exceptions and RTTI; they cannot be mismatched. See this post for details: https://fb.workplace.com/groups/iosappsize/permalink/2277094415672494/") + + # These are the default compiler flags for ALL React Native Cxx targets. + # For all of these, we PREPEND to compiler_flags: if these are already set + # or being overridden in compiler_flags, it's very likely that the flag is set + # app-wide or that we're otherwise in some special mode. + kwargs["compiler_flags"] = kwargs.get("compiler_flags", []) + kwargs["compiler_flags"] = ["-std=c++17"] + kwargs["compiler_flags"] + kwargs["compiler_flags"] = ["-Wall"] + kwargs["compiler_flags"] + kwargs["compiler_flags"] = ["-Werror"] + kwargs["compiler_flags"] + if compiler_flags_enable_exceptions: + kwargs["compiler_flags"] = ["-fexceptions"] + kwargs["compiler_flags"] + else: + kwargs["compiler_flags"] = ["-fno-exceptions"] + kwargs["compiler_flags"] + if compiler_flags_enable_rtti: + kwargs["compiler_flags"] = ["-frtti"] + kwargs["compiler_flags"] + else: + kwargs["compiler_flags"] = ["-fno-rtti"] + kwargs["compiler_flags"] + native.cxx_library( name = name, - visibility = kwargs.get("visibility", []), - **new_kwargs + visibility = visibility, + **kwargs ) rn_xplat_cxx_library2 = rn_xplat_cxx_library