@@ -9,7 +9,7 @@ To verify the processed cargo cc_args, we use cc_args_and_env_analysis_test().
99
1010load ("@bazel_skylib//lib:unittest.bzl" , "analysistest" , "asserts" )
1111load ("@rules_cc//cc:action_names.bzl" , "ACTION_NAMES" , "ACTION_NAME_GROUPS" )
12- load ("@rules_cc//cc:cc_toolchain_config_lib.bzl" , "action_config" , "feature" , "flag_group" , "flag_set" , "tool" , "tool_path" )
12+ load ("@rules_cc//cc:cc_toolchain_config_lib.bzl" , "action_config" , "env_entry" , "env_set" , " feature" , "flag_group" , "flag_set" , "tool" , "tool_path" )
1313load ("@rules_cc//cc:defs.bzl" , "cc_toolchain" )
1414load ("@rules_cc//cc/common:cc_common.bzl" , "cc_common" )
1515load ("//cargo:defs.bzl" , "cargo_build_script" )
@@ -35,6 +35,17 @@ def _test_cc_config_impl(ctx):
3535 ]),
3636 ),
3737 ],
38+ env_sets = [
39+ env_set (
40+ actions = ACTION_NAME_GROUPS .all_cc_compile_actions ,
41+ env_entries = [
42+ env_entry (
43+ key = "INCLUDE" ,
44+ value = ctx .attr .extra_include_paths ,
45+ ),
46+ ],
47+ ),
48+ ] if ctx .attr .extra_include_paths else [],
3849 ),
3950 feature (
4051 name = "default_cpp_flags" ,
@@ -155,6 +166,7 @@ test_cc_config = rule(
155166 "extra_ar_flags" : attr .string_list (),
156167 "extra_cc_compile_flags" : attr .string_list (),
157168 "extra_cxx_compile_flags" : attr .string_list (),
169+ "extra_include_paths" : attr .string (default = "" ),
158170 "legacy_cc_toolchain" : attr .bool (default = False ),
159171 },
160172 provides = [CcToolchainConfigInfo ],
@@ -240,6 +252,15 @@ def _cc_args_and_env_analysis_test_impl(ctx):
240252 "set up by most Bazel C/C++ toolchains is extremely error-prone." ,
241253 )
242254
255+ if ctx .attr .expected_include :
256+ actual_include = cargo_action .env .get ("INCLUDE" )
257+ asserts .equals (
258+ env ,
259+ ctx .attr .expected_include ,
260+ actual_include ,
261+ "error: expected INCLUDE '{}' but got '{}'" .format (ctx .attr .expected_include , actual_include ),
262+ )
263+
243264 return analysistest .end (env )
244265
245266cc_args_and_env_analysis_test = analysistest .make (
@@ -248,6 +269,7 @@ cc_args_and_env_analysis_test = analysistest.make(
248269 attrs = {
249270 "expected_cflags" : attr .string_list (default = ["-Wall" ]),
250271 "expected_cxxflags" : attr .string_list (default = ["-fno-rtti" ]),
272+ "expected_include" : attr .string (default = "" ),
251273 "legacy_cc_toolchain" : attr .bool (default = False ),
252274 },
253275)
@@ -258,6 +280,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
258280 extra_cc_compile_flags = ["-Wall" ],
259281 extra_cxx_compile_flags = ["-fno-rtti" ],
260282 extra_ar_flags = ["-x" ],
283+ extra_include_paths = "" ,
261284 legacy_cc_toolchain = False ):
262285 """Produces a test cargo_build_script target that's set up to use a custom cc_toolchain with the extra_cc_compile_flags.
263286
@@ -272,6 +295,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
272295 extra_cc_compile_flags: Extra C/C++ args for the cc_toolchain.
273296 extra_cxx_compile_flags: Extra C++-specific args for the cc_toolchain.
274297 extra_ar_flags: Extra archiver args for the cc_toolchain.
298+ extra_include_paths: INCLUDE environment variable value for the cc_toolchain.
275299 legacy_cc_toolchain: Enables legacy tool_path configuration of the cc
276300 cc toolchain.
277301 """
@@ -281,6 +305,7 @@ def cargo_build_script_with_extra_cc_compile_flags(
281305 extra_cc_compile_flags = extra_cc_compile_flags ,
282306 extra_cxx_compile_flags = extra_cxx_compile_flags ,
283307 extra_ar_flags = extra_ar_flags ,
308+ extra_include_paths = extra_include_paths ,
284309 legacy_cc_toolchain = legacy_cc_toolchain ,
285310 )
286311 cc_toolchain (
@@ -466,3 +491,54 @@ def resource_dir_absolute_test(name):
466491 target_under_test = "%s/cargo_build_script" % name ,
467492 expected_cflags = ["-resource-dir" , "/test/absolute/resources" , "-resource-dir=/test/absolute/resources2" , "-resource-dir=" , "some_unrelated_arg" ],
468493 )
494+
495+ def include_relative_test (name ):
496+ cargo_build_script_with_extra_cc_compile_flags (
497+ name = "%s/cargo_build_script" % name ,
498+ extra_include_paths = select ({
499+ "@platforms//os:windows" : "test/relative/include;another/relative/path" ,
500+ "//conditions:default" : "test/relative/include:another/relative/path" ,
501+ }),
502+ )
503+ cc_args_and_env_analysis_test (
504+ name = name ,
505+ target_under_test = "%s/cargo_build_script" % name ,
506+ expected_include = select ({
507+ "@platforms//os:windows" : "${pwd}/test/relative/include;${pwd}/another/relative/path" ,
508+ "//conditions:default" : "${pwd}/test/relative/include:${pwd}/another/relative/path" ,
509+ }),
510+ )
511+
512+ def include_absolute_test (name ):
513+ cargo_build_script_with_extra_cc_compile_flags (
514+ name = "%s/cargo_build_script" % name ,
515+ extra_include_paths = select ({
516+ "@platforms//os:windows" : "/test/absolute/include;/another/absolute/path" ,
517+ "//conditions:default" : "/test/absolute/include:/another/absolute/path" ,
518+ }),
519+ )
520+ cc_args_and_env_analysis_test (
521+ name = name ,
522+ target_under_test = "%s/cargo_build_script" % name ,
523+ expected_include = select ({
524+ "@platforms//os:windows" : "/test/absolute/include;/another/absolute/path" ,
525+ "//conditions:default" : "/test/absolute/include:/another/absolute/path" ,
526+ }),
527+ )
528+
529+ def include_mixed_test (name ):
530+ cargo_build_script_with_extra_cc_compile_flags (
531+ name = "%s/cargo_build_script" % name ,
532+ extra_include_paths = select ({
533+ "@platforms//os:windows" : "/test/absolute/include;test/relative/path" ,
534+ "//conditions:default" : "/test/absolute/include:test/relative/path" ,
535+ }),
536+ )
537+ cc_args_and_env_analysis_test (
538+ name = name ,
539+ target_under_test = "%s/cargo_build_script" % name ,
540+ expected_include = select ({
541+ "@platforms//os:windows" : "/test/absolute/include;${pwd}/test/relative/path" ,
542+ "//conditions:default" : "/test/absolute/include:${pwd}/test/relative/path" ,
543+ }),
544+ )
0 commit comments