Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions crosstool/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,24 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
flag_groups = [
flag_group(
flags = [
# Note: This treats all headers as C++ headers, which may lead to
# parsing failures for C headers that are not valid C++.
# For such headers, use features = ["-parse_headers"] to selectively
# disable parsing.
"-xc++-header",
"-fsyntax-only",
],
),
],
with_features = [with_feature_set(not_features = ["parse_headers_as_c"])],
),
flag_set(
actions = [ACTION_NAMES.cpp_header_parsing],
flag_groups = [
flag_group(
flags = [
"-xc-header",
"-fsyntax-only",
],
),
],
with_features = [with_feature_set(features = ["parse_headers_as_c"])],
),
],
env_sets = [
Expand Down Expand Up @@ -1929,11 +1938,19 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
),
] if ctx.attr.conly_flags else []),
),
flag_set(
actions = [ACTION_NAMES.cpp_header_parsing],
flag_groups = ([
flag_group(
flags = ctx.attr.conly_flags,
),
] if ctx.attr.conly_flags else []),
with_features = [with_feature_set(features = ["parse_headers_as_c"])],
),
flag_set(
actions = [
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
],
flag_groups = ([
Expand All @@ -1942,6 +1959,15 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
),
] if ctx.attr.cxx_flags else []),
),
flag_set(
actions = [ACTION_NAMES.cpp_header_parsing],
flag_groups = ([
flag_group(
flags = ctx.attr.cxx_flags,
),
] if ctx.attr.cxx_flags else []),
with_features = [with_feature_set(not_features = ["parse_headers_as_c"])],
),
flag_set(
actions = [
ACTION_NAMES.assemble,
Expand Down Expand Up @@ -2294,6 +2320,7 @@ please file an issue at https://github.com/bazelbuild/apple_support/issues/new
feature(name = "only_doth_headers_in_module_maps"),
feature(name = "opt"),
feature(name = "parse_headers"),
feature(name = "parse_headers_as_c"),
feature(name = "no_dotd_file"),
feature(name = "sanitize_pwd", enabled = True),
feature(name = "set_soname", enabled = True),
Expand Down
34 changes: 34 additions & 0 deletions test/compiling_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default_test = make_action_command_line_test_rule()
copt_order_test = make_action_command_line_test_rule(
config_settings = {
"//command_line_option:compilation_mode": "opt",
"//command_line_option:conlyopt": ["-DFROM_CONLY_FLAG=1"],
"//command_line_option:copt": ["-DFROM_COPTS_FLAG=1"],
"//command_line_option:cxxopt": ["-DFROM_CXX_FLAG=1"],
"//command_line_option:objccopt": ["-DFROM_OBJCCOPTS_FLAG=1"],
Expand Down Expand Up @@ -395,19 +396,52 @@ def compiling_test_suite(name):
"-O2", # From --compilation_mode=opt
"-isysroot",
"__BAZEL_XCODE_SDKROOT__",
"-DCOPTS_ENV=1",
"-std=c++17",
"-DCXXOPTS_ENV=1",
"-DFROM_COPTS_FLAG=1",
"-DFROM_CXX_FLAG=1",
"-D__DATE__=\"redacted\"",
"test/header_parsing/valid_header.h",
"-o",
"$(BIN_DIR)/test/header_parsing/_objs/valid_header/valid_header.h.processed",
],
not_expected_argv = [
"-c", # Produces a clang warning since we don't compile anything in this action
"-DCONLY_ENV=1",
"-DFROM_CONLY_FLAG=1",
],
mnemonic = "CppCompile",
target_under_test = "//test/header_parsing:valid_header",
)

copt_order_test(
name = "{}_header_parsing_as_c_copt_order_test".format(name),
tags = [name],
expected_argv = [
"-xc-header",
"-fsyntax-only",
"-O2", # From --compilation_mode=opt
"-isysroot",
"__BAZEL_XCODE_SDKROOT__",
"-DCOPTS_ENV=1",
"-DCONLY_ENV=1",
"-DFROM_COPTS_FLAG=1",
"-D__DATE__=\"redacted\"",
"test/header_parsing/c_only_header.h",
"-o",
"$(BIN_DIR)/test/header_parsing/_objs/c_only_header/c_only_header.h.processed",
],
not_expected_argv = [
"-c", # Produces a clang warning since we don't compile anything in this action
"-xc++-header",
"-std=c++17",
"-DCXXOPTS_ENV=1",
],
mnemonic = "CppCompile",
target_under_test = "//test/header_parsing:c_only_header",
)

default_test(
name = "{}_dependency_file_test".format(name),
tags = [name],
Expand Down
14 changes: 14 additions & 0 deletions test/header_parsing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ cc_library(
features = ["-parse_headers"],
)

cc_library(
name = "c_only_header_without_parse_headers_as_c",
hdrs = ["c_only_header.h"],
tags = ["manual"],
)

cc_library(
name = "c_only_header",
hdrs = ["c_only_header.h"],
features = ["parse_headers_as_c"],
visibility = ["//test:__subpackages__"],
)

cc_library(
name = "valid_header",
hdrs = ["valid_header.h"],
Expand All @@ -35,6 +48,7 @@ objc_library(
build_test(
name = "test",
targets = [
":c_only_header",
":invalid_header_feature_disabled",
":valid_header",
":valid_header_objc",
Expand Down
5 changes: 5 additions & 0 deletions test/header_parsing/c_only_header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#ifdef __cplusplus
#error "expected C header parsing"
#endif

void c_only_header(void);
9 changes: 9 additions & 0 deletions test/shell/header_parsing_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ function test_bad_header_parsing_objc() {
expect_log "test/header_parsing/invalid_header.h:2:1: error: unknown type name 'uint8_t'"
}

function test_header_parsing_as_c() {
bazel_cmd build --process_headers_in_dependencies -- //test/header_parsing:c_only_header &>"$TEST_log"
}

function test_c_header_without_parse_headers_as_c() {
! bazel_cmd build --process_headers_in_dependencies -- //test/header_parsing:c_only_header_without_parse_headers_as_c &> "$TEST_log" || fail "Expected build failure"
expect_log 'test/header_parsing/c_only_header.h:2:2: error: "expected C header parsing"'
}

run_suite "header_parsing tests"
146 changes: 143 additions & 3 deletions test/test_data/toolchain_configs/darwin_arm64.json
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,38 @@
}
],
"type_name": "env_set",
"with_features": []
"with_features": [
{
"features": [],
"not_features": [
"parse_headers_as_c"
],
"type_name": "with_feature_set"
}
]
},
{
"actions": [
"c++-header-parsing"
],
"env_entries": [
{
"expand_if_available": "output_file",
"key": "HEADER_PARSING_OUTPUT",
"type_name": "env_entry",
"value": "%{output_file}"
}
],
"type_name": "env_set",
"with_features": [
{
"features": [
"parse_headers_as_c"
],
"not_features": [],
"type_name": "with_feature_set"
}
]
}
],
"flag_sets": [
Expand All @@ -877,7 +908,46 @@
}
],
"type_name": "flag_set",
"with_features": []
"with_features": [
{
"features": [],
"not_features": [
"parse_headers_as_c"
],
"type_name": "with_feature_set"
}
]
},
{
"actions": [
"c++-header-parsing"
],
"flag_groups": [
{
"expand_if_available": "output_file",
"expand_if_equal": null,
"expand_if_false": null,
"expand_if_not_available": null,
"expand_if_true": null,
"flag_groups": [],
"flags": [
"-xc-header",
"-fsyntax-only"
],
"iterate_over": null,
"type_name": "flag_group"
}
],
"type_name": "flag_set",
"with_features": [
{
"features": [
"parse_headers_as_c"
],
"not_features": [],
"type_name": "with_feature_set"
}
]
}
],
"implies": [],
Expand Down Expand Up @@ -3720,10 +3790,39 @@
"type_name": "flag_set",
"with_features": []
},
{
"actions": [
"c++-header-parsing"
],
"flag_groups": [
{
"expand_if_available": null,
"expand_if_equal": null,
"expand_if_false": null,
"expand_if_not_available": null,
"expand_if_true": null,
"flag_groups": [],
"flags": [
"-DCONLY_ENV=1"
],
"iterate_over": null,
"type_name": "flag_group"
}
],
"type_name": "flag_set",
"with_features": [
{
"features": [
"parse_headers_as_c"
],
"not_features": [],
"type_name": "with_feature_set"
}
]
},
{
"actions": [
"c++-compile",
"c++-header-parsing",
"c++-module-compile",
"linkstamp-compile"
],
Expand All @@ -3745,6 +3844,37 @@
],
"type_name": "flag_set",
"with_features": []
},
{
"actions": [
"c++-header-parsing"
],
"flag_groups": [
{
"expand_if_available": null,
"expand_if_equal": null,
"expand_if_false": null,
"expand_if_not_available": null,
"expand_if_true": null,
"flag_groups": [],
"flags": [
"-std=c++17",
"-DCXXOPTS_ENV=1"
],
"iterate_over": null,
"type_name": "flag_group"
}
],
"type_name": "flag_set",
"with_features": [
{
"features": [],
"not_features": [
"parse_headers_as_c"
],
"type_name": "with_feature_set"
}
]
}
],
"implies": [],
Expand Down Expand Up @@ -4966,6 +5096,16 @@
"requires": [],
"type_name": "feature"
},
{
"enabled": false,
"env_sets": [],
"flag_sets": [],
"implies": [],
"name": "parse_headers_as_c",
"provides": [],
"requires": [],
"type_name": "feature"
},
{
"enabled": false,
"env_sets": [],
Expand Down
Loading