From 6192c42682b47ae5707f69946a9c40173a1845b8 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 23 Nov 2022 14:32:48 -0700 Subject: [PATCH 1/6] Add macro for constructing local include paths --- cc/local_include_prefix.bzl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 cc/local_include_prefix.bzl diff --git a/cc/local_include_prefix.bzl b/cc/local_include_prefix.bzl new file mode 100644 index 00000000..7a00c9e9 --- /dev/null +++ b/cc/local_include_prefix.bzl @@ -0,0 +1,16 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# This source is subject to the license found in the file 'LICENSE' which must +# be be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + + +def local_include_prefix(): + root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." + root = root + "/" + print(root) + return root From 1d0afa6b1909f38858f232ab184c43ef3265e0e4 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 23 Nov 2022 14:49:12 -0700 Subject: [PATCH 2/6] Add package path --- cc/local_include_prefix.bzl | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cc/local_include_prefix.bzl b/cc/local_include_prefix.bzl index 7a00c9e9..68c97877 100644 --- a/cc/local_include_prefix.bzl +++ b/cc/local_include_prefix.bzl @@ -8,9 +8,7 @@ # EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. - def local_include_prefix(): root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." - root = root + "/" - print(root) - return root + package = native.package_name() + return root + "/" + package + "/" From 69462a092d051c439f27f0b59d7ad3935e6e13b4 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 23 Nov 2022 17:55:56 -0700 Subject: [PATCH 3/6] Init local includes impl --- cc/defs.bzl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cc/defs.bzl b/cc/defs.bzl index 44f62752..0115d5eb 100644 --- a/cc/defs.bzl +++ b/cc/defs.bzl @@ -10,6 +10,8 @@ """Swift wrappers for native cc rules.""" +load(":local_include_prefix.bzl", "local_include_prefix") + # Name for a unit test UNIT = "unit" @@ -74,6 +76,9 @@ def _common_c_opts(nocopts, pedantic = False): return copts +def _construct_local_includes(local_includes): + return ["-I" + local_include_prefix() + include for include in local_includes] + def swift_cc_library(**kwargs): """Wraps cc_library to enforce standards for a production library. @@ -89,10 +94,12 @@ def swift_cc_library(**kwargs): attribute takes a list of flags to remove from the default compiler options. Use judiciously. """ + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + nocopts = kwargs.pop("nocopts", []) # pop because nocopts is a deprecated cc* attr. copts = _common_c_opts(nocopts, pedantic = True) - kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts + kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + local_includes + copts native.cc_library(**kwargs) @@ -112,6 +119,8 @@ def swift_cc_tool_library(**kwargs): attribute takes a list of flags to remove from the default compiler options. Use judiciously. """ + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + nocopts = kwargs.pop("nocopts", []) copts = _common_c_opts(nocopts, pedantic = False) From 72abad46512a70bd37378fdcce58fd095e633b1a Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 30 Nov 2022 13:44:27 -0700 Subject: [PATCH 4/6] Working impl --- cc/defs.bzl | 73 +++++++++++++++++++++++++++++-------- cc/local_include_prefix.bzl | 14 ------- cc/utils.bzl | 35 ++++++++++++++++++ 3 files changed, 93 insertions(+), 29 deletions(-) delete mode 100644 cc/local_include_prefix.bzl create mode 100644 cc/utils.bzl diff --git a/cc/defs.bzl b/cc/defs.bzl index 0115d5eb..72f7a0d7 100644 --- a/cc/defs.bzl +++ b/cc/defs.bzl @@ -10,7 +10,7 @@ """Swift wrappers for native cc rules.""" -load(":local_include_prefix.bzl", "local_include_prefix") +load(":utils.bzl", "construct_local_include") # Name for a unit test UNIT = "unit" @@ -77,7 +77,7 @@ def _common_c_opts(nocopts, pedantic = False): return copts def _construct_local_includes(local_includes): - return ["-I" + local_include_prefix() + include for include in local_includes] + return [construct_local_include(path) for path in local_includes] def swift_cc_library(**kwargs): """Wraps cc_library to enforce standards for a production library. @@ -90,16 +90,22 @@ def swift_cc_library(**kwargs): Args: **kwargs: See https://bazel.build/reference/be/c-cpp#cc_library - An additional attribute nocopts is supported. This - attribute takes a list of flags to remove from the - default compiler options. Use judiciously. + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. + + nocopts: List of flags to remove from the default compile + options. Use judiciously. """ local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) nocopts = kwargs.pop("nocopts", []) # pop because nocopts is a deprecated cc* attr. copts = _common_c_opts(nocopts, pedantic = True) - kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + local_includes + copts + copts = local_includes + copts + kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts native.cc_library(**kwargs) @@ -115,15 +121,21 @@ def swift_cc_tool_library(**kwargs): Args: **kwargs: See https://bazel.build/reference/be/c-cpp#cc_library - An additional attribute nocopts is supported. This - attribute takes a list of flags to remove from the - default compiler options. Use judiciously. + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. + + nocopts: List of flags to remove from the default compile + options. Use judiciously. """ local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) nocopts = kwargs.pop("nocopts", []) copts = _common_c_opts(nocopts, pedantic = False) + copts = local_includes + copts kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts native.cc_library(**kwargs) @@ -139,13 +151,21 @@ def swift_cc_binary(**kwargs): Args: **kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary - An additional attribute nocopts is supported. This - attribute takes a list of flags to remove from the - default compiler options. Use judiciously. + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. + + nocopts: List of flags to remove from the default compile + options. Use judiciously. """ + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + nocopts = kwargs.pop("nocopts", []) copts = _common_c_opts(nocopts, pedantic = True) + copts = local_includes + copts kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + copts native.cc_binary(**kwargs) @@ -162,9 +182,14 @@ def swift_cc_tool(**kwargs): Args: **kwargs: See https://bazel.build/reference/be/c-cpp#cc_binary - An additional attribute nocopts is supported. This - attribute takes a list of flags to remove from the - default compiler options. Use judiciously. + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. + + nocopts: List of flags to remove from the default compile + options. Use judiciously. """ nocopts = kwargs.pop("nocopts", []) @@ -178,7 +203,16 @@ def swift_cc_test_library(**kwargs): Args: **kwargs: See https://bazel.build/reference/be/c-cpp#cc_test + + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. """ + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + + kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + local_includes native.cc_library(**kwargs) def swift_cc_test(name, type, **kwargs): @@ -192,11 +226,20 @@ def swift_cc_test(name, type, **kwargs): these test types seperately: `bazel test --test_tag_filters=unit //...` **kwargs: See https://bazel.build/reference/be/c-cpp#cc_test + + The following additional attributes are supported: + + local_includes: List of local (non-public) include paths. Prefer + this to passing local includes using copts. Paths are expected to + be relative to the package this macro is called from. """ if not (type == UNIT or type == INTEGRATION): fail("The 'type' attribute must be either UNIT or INTEGRATION") + local_includes = _construct_local_includes(kwargs.pop("local_includes", [])) + + kwargs["copts"] = (kwargs["copts"] if "copts" in kwargs else []) + local_includes kwargs["name"] = name kwargs["tags"] = (kwargs["tags"] if "tags" in kwargs else []) + [type] native.cc_test(**kwargs) diff --git a/cc/local_include_prefix.bzl b/cc/local_include_prefix.bzl deleted file mode 100644 index 68c97877..00000000 --- a/cc/local_include_prefix.bzl +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (C) 2022 Swift Navigation Inc. -# Contact: Swift Navigation -# -# This source is subject to the license found in the file 'LICENSE' which must -# be be distributed together with this source. All other rights reserved. -# -# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, -# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED -# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. - -def local_include_prefix(): - root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." - package = native.package_name() - return root + "/" + package + "/" diff --git a/cc/utils.bzl b/cc/utils.bzl new file mode 100644 index 00000000..5fe3e656 --- /dev/null +++ b/cc/utils.bzl @@ -0,0 +1,35 @@ +# Copyright (C) 2022 Swift Navigation Inc. +# Contact: Swift Navigation +# +# This source is subject to the license found in the file 'LICENSE' which must +# be be distributed together with this source. All other rights reserved. +# +# THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, +# EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. + +def construct_local_include(path): + """Helper to correctly set up local (non-public) include paths. + + When a bazel workspace is consumed externally, (i.e. via local_repository), + its sources are placed under /external//. This + typically breaks local include paths defined using -I. + + This macro ensures that the include path is constructed correctly both when + building a workpace standalone, and externally. + + Args: + path: The include path relative to the package this macro is called from + + Use the special argument $(GENDIR) to construct an include path for + any generated files the build depends on. + + """ + root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." + package = native.package_name() + + # Generated files are placed in $(GENDIR)/external/ + if path == "$(GENDIR)": + return "-I" + path + "/" + root + "/" + package + "/" + else: + return "-I" + root + "/" + package + "/" + path + "/" From dc448288af87c52bf3fe650c7d3590321e04b9e8 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 30 Nov 2022 14:04:03 -0700 Subject: [PATCH 5/6] Add more doc --- cc/utils.bzl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cc/utils.bzl b/cc/utils.bzl index 5fe3e656..07332c93 100644 --- a/cc/utils.bzl +++ b/cc/utils.bzl @@ -22,7 +22,8 @@ def construct_local_include(path): path: The include path relative to the package this macro is called from Use the special argument $(GENDIR) to construct an include path for - any generated files the build depends on. + any generated files the build depends on. Assumes these files are + not generated into a subdirectory. """ root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." From 725df2674ff61e337e358f09af8eafb80793a205 Mon Sep 17 00:00:00 2001 From: Isaac Torres Date: Wed, 30 Nov 2022 14:04:45 -0700 Subject: [PATCH 6/6] tidy --- cc/utils.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/cc/utils.bzl b/cc/utils.bzl index 07332c93..b31bf8ab 100644 --- a/cc/utils.bzl +++ b/cc/utils.bzl @@ -24,7 +24,6 @@ def construct_local_include(path): Use the special argument $(GENDIR) to construct an include path for any generated files the build depends on. Assumes these files are not generated into a subdirectory. - """ root = Label(native.repository_name() + "//:WORKSPACE").workspace_root or "." package = native.package_name()