@@ -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 (
0 commit comments