Skip to content

Commit eef91dc

Browse files
dvyukovcopybara-github
authored andcommitted
Fix language standard used for the clang_generator tool
Use -std=gnu++17 instead of -std=c++17 when possible. See the added comment for details. Without the fix the build fails with: replacement_library.sapi.guest.h:21:67: error: unknown type name '_Bool' 21 | int64_t a3, char a4, _Bool a5, | ^ I did not find any other test that does not use C and uses a header file for the sandboxed library. So used the replacement_library test. PiperOrigin-RevId: 792096242 Change-Id: I0c34dbfc0207deba405e01928da8c45f858f0f8b
1 parent db89d4b commit eef91dc

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

sandboxed_api/bazel/sapi.bzl

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,26 @@ def sort_deps(deps):
7373
other_deps = [x for x in deps if not x.startswith(":")]
7474
return sorted(colon_deps) + sorted(other_deps)
7575

76-
def _clang_generator_flags(cc_ctx, cpp_toolchain):
76+
def _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths):
7777
flags = []
7878

79-
# TODO(cblichmann): Get language standard from the toolchain
80-
flags.append("--extra-arg=-std=c++17")
79+
# The gnu compiler is what frequently used in practice,
80+
# and it allows to use more langauge extensions (e.g. _Bool type in C).
81+
# However, compiling C code with -std=gnu++ does not work,
82+
# -std=c++ somehow works for C, and is required to compile mixed C/C++ inputs.
83+
# So this is the best we can do.
84+
std = "gnu++"
85+
for f in input_files_paths:
86+
if f.endswith(".c"):
87+
std = "c++"
88+
break
89+
90+
# TODO(cblichmann): use the same language standard as the toolchain.
91+
# Note: it may be different for different files, but at least we could
92+
# infer the max standard version (year) used in the actual compilation.
93+
std += "17"
94+
95+
flags.append("--extra-arg=-std=" + std)
8196

8297
# Disable warnings in parsed code
8398
flags.append("--extra-arg=-Wno-everything")
@@ -148,15 +163,6 @@ def _sapi_interface_impl(ctx):
148163
# Append all headers as dependencies
149164
input_files += cc_ctx.headers.to_list()
150165

151-
if use_clang_generator:
152-
input_files += cpp_toolchain.all_files.to_list()
153-
extra_flags += _clang_generator_flags(cc_ctx, cpp_toolchain)
154-
else:
155-
append_all(extra_flags, "-D", cc_ctx.defines.to_list())
156-
append_all(extra_flags, "-isystem", cc_ctx.system_includes.to_list())
157-
append_all(extra_flags, "-iquote", cc_ctx.quote_includes.to_list())
158-
append_all(extra_flags, "-I", cc_ctx.includes.to_list())
159-
160166
if ctx.attr.input_files:
161167
for f in ctx.files.input_files:
162168
input_files.append(f)
@@ -165,6 +171,15 @@ def _sapi_interface_impl(ctx):
165171
# Try to find files automatically
166172
input_files_paths += _lib_direct_headers(ctx.attr.lib, cc_ctx)
167173

174+
if use_clang_generator:
175+
input_files += cpp_toolchain.all_files.to_list()
176+
extra_flags += _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths)
177+
else:
178+
append_all(extra_flags, "-D", cc_ctx.defines.to_list())
179+
append_all(extra_flags, "-isystem", cc_ctx.system_includes.to_list())
180+
append_all(extra_flags, "-iquote", cc_ctx.quote_includes.to_list())
181+
append_all(extra_flags, "-I", cc_ctx.includes.to_list())
182+
168183
if use_clang_generator:
169184
args += extra_flags + input_files_paths
170185
else:
@@ -603,8 +618,9 @@ def _sandboxed_library_gen_impl(ctx):
603618
args.append("--host_src_out={}".format(ctx.outputs.host_src_out.path))
604619
args.append("--sapi_out={}".format(ctx.attr.sapi_hdr))
605620
args.append("--sapi_limit_scan_depth")
606-
args += _clang_generator_flags(cc_ctx, cpp_toolchain)
607-
args += _lib_direct_headers(ctx.attr.lib, cc_ctx)
621+
input_files_paths = _lib_direct_headers(ctx.attr.lib, cc_ctx)
622+
args += _clang_generator_flags(cc_ctx, cpp_toolchain, input_files_paths)
623+
args += input_files_paths
608624

609625
progress_msg = "Generating sandboxed library {}.".format(ctx.attr.lib_name)
610626
ctx.actions.run(

sandboxed_api/testcases/replaced_library.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414

1515
#include "sandboxed_api/testcases/replaced_library.h"
1616

17+
#include <cstddef>
18+
#include <cstdint>
1719
#include <string>
1820

1921
#include "absl/strings/string_view.h"
2022

23+
void mylib_scalar_types(int a0, float a1, double a2, int64_t a3, char a4,
24+
bool a5, size_t a6) {}
25+
2126
std::string mylib_copy(const std::string& src) { return src; }
2227

2328
void mylib_copy(absl::string_view src, std::string& dst) {

sandboxed_api/testcases/replaced_library.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
#ifndef SANDBOXED_API_SANDBOX2_TESTCASES_REPLACED_LIBRARY_H_
2121
#define SANDBOXED_API_SANDBOX2_TESTCASES_REPLACED_LIBRARY_H_
2222

23+
#include <cstddef>
24+
#include <cstdint>
2325
#include <string>
2426

2527
#include "absl/strings/string_view.h"
2628

29+
void mylib_scalar_types(int a0, float a1, double a2, int64_t a3, char a4,
30+
bool a5, size_t a6);
2731
int mylib_add(int x, int y);
2832
std::string mylib_copy(const std::string& src);
2933
void mylib_copy(absl::string_view src, std::string& dst);

0 commit comments

Comments
 (0)