Skip to content

Commit d0089ab

Browse files
zhuhan0facebook-github-bot
authored andcommitted
Extract types from compile.bzl into compile_types.bzl
Summary: NFC. In D69757706 I need to use several types from `compile.bzl` in `cuda.bzl`, and `cuda.bzl` is included in `compile.bzl`. So extracting some types out of `compile.bzl` to avoid a dependency cycle. Reviewed By: get9 Differential Revision: D69757707 fbshipit-source-id: e92fde8cf17f4f95d860f4c10e018dc3712f0ad4
1 parent 3fc9b01 commit d0089ab

File tree

6 files changed

+189
-163
lines changed

6 files changed

+189
-163
lines changed

prelude/apple/apple_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ load(
3838
"CompileArgsfiles",
3939
)
4040
load(
41-
"@prelude//cxx:compile.bzl",
41+
"@prelude//cxx:compile_types.bzl",
4242
"AsmExtensions",
4343
"CxxSrcCompileCommand", # @unused Used as a type
4444
)

prelude/cxx/comp_db.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ load("@prelude//:paths.bzl", "paths")
99
load("@prelude//cxx:cxx_toolchain_types.bzl", "CxxPlatformInfo", "CxxToolchainInfo")
1010
load("@prelude//utils:argfile.bzl", "at_argfile")
1111
load(
12-
":compile.bzl",
12+
":compile_types.bzl",
1313
"CxxSrcCompileCommand", # @unused Used as a type
1414
)
1515
load(":cxx_context.bzl", "get_cxx_toolchain_info")

prelude/cxx/compile.bzl

Lines changed: 21 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@
66
# of this source tree.
77

88
load("@prelude//:paths.bzl", "paths")
9+
load(
10+
"@prelude//cxx:compile_types.bzl",
11+
"AsmExtensions",
12+
"CxxCompileCommand",
13+
"CxxCompileCommandOutput",
14+
"CxxCompileFlavor",
15+
"CxxCompileOutput",
16+
"CxxExtension",
17+
"CxxSrcCompileCommand",
18+
"CxxSrcPrecompileCommand",
19+
"DepFileType",
20+
"HeaderExtension",
21+
"HeadersDepFiles",
22+
)
923
load("@prelude//cxx:cxx_toolchain_types.bzl", "CxxToolchainInfo")
1024
load("@prelude//cxx:cxx_utility.bzl", "cxx_attrs_get_allow_cache_upload")
1125
load(
@@ -47,155 +61,6 @@ load(
4761
"get_flags_for_compiler_type",
4862
)
4963

50-
# Supported assembly extensions
51-
AsmExtensions = enum(
52-
".s",
53-
".sx",
54-
".S",
55-
".asm",
56-
".asmpp",
57-
)
58-
59-
# Supported Cxx file extensions
60-
CxxExtension = enum(
61-
".cpp",
62-
".cc",
63-
".cl",
64-
".cxx",
65-
".c++",
66-
".c",
67-
".m",
68-
".mm",
69-
".cu",
70-
".hip",
71-
".h",
72-
".hpp",
73-
".hh",
74-
".h++",
75-
".hxx",
76-
".bc",
77-
*AsmExtensions.values()
78-
)
79-
80-
# Header files included in compilation databases
81-
HeaderExtension = enum(
82-
".h",
83-
".hpp",
84-
".hh",
85-
".h++",
86-
".hxx",
87-
".cuh",
88-
)
89-
90-
# File types for dep files
91-
DepFileType = enum(
92-
"cpp",
93-
"c",
94-
"cuda",
95-
"asm",
96-
)
97-
98-
_HeadersDepFiles = record(
99-
# An executable to wrap the actual command with for post-processing of dep
100-
# files into the format that Buck2 recognizes (i.e. one artifact per line).
101-
processor = field(cmd_args),
102-
# The tag that was added to headers.
103-
tag = field(ArtifactTag),
104-
# A function that produces new cmd_args to append to the compile command to
105-
# get it to emit the dep file. This will receive the output dep file as an
106-
# input.
107-
mk_flags = field(typing.Callable),
108-
# Dependency tracking mode to know how to generate dep file
109-
dep_tracking_mode = field(DepTrackingMode),
110-
)
111-
112-
# Information about how to compile a source file of particular extension.
113-
_CxxCompileCommand = record(
114-
# The compiler and any args which are independent of the rule.
115-
base_compile_cmd = field(cmd_args),
116-
# The argsfile of arguments from the rule and it's dependencies.
117-
argsfile = field(CompileArgsfile),
118-
# The argsfile to use for Xcode integration.
119-
xcode_argsfile = field(CompileArgsfile),
120-
# The argsfile containing header units args.
121-
header_units_argsfile = field(CompileArgsfile | None),
122-
headers_dep_files = field([_HeadersDepFiles, None]),
123-
compiler_type = field(str),
124-
# The action category
125-
category = field(str),
126-
allow_cache_upload = field(bool),
127-
)
128-
129-
# Information about how to compile a source file.
130-
CxxSrcCompileCommand = record(
131-
# Source file to compile.
132-
src = field(Artifact),
133-
# If we have multiple source entries with same files but different flags,
134-
# specify an index so we can differentiate them. Otherwise, use None.
135-
index = field([int, None], None),
136-
# The CxxCompileCommand to use to compile this file.
137-
cxx_compile_cmd = field(_CxxCompileCommand),
138-
# Arguments specific to the source file.
139-
args = field(list[typing.Any]),
140-
# Is this a header file?
141-
is_header = field(bool, False),
142-
# The index store factory to use to generate index store for this source file.
143-
index_store_factory = field(typing.Callable | None, None),
144-
error_handler = field([typing.Callable, None], None),
145-
)
146-
147-
_CxxSrcPrecompileCommand = record(
148-
# Source file to compile.
149-
src = field(Artifact),
150-
# The CxxCompileCommand to use to compile this file.
151-
cxx_compile_cmd = field(_CxxCompileCommand),
152-
# Arguments specific to the source file.
153-
args = field(list[typing.Any]),
154-
# Extra argsfile to include after any other header units argsfile but before the
155-
# main argsfiles.
156-
extra_argsfile = field([CompileArgsfile, None], None),
157-
)
158-
159-
# Output of creating compile commands for Cxx source files.
160-
CxxCompileCommandOutput = record(
161-
# List of compile commands for each source file.
162-
src_compile_cmds = field(list[CxxSrcCompileCommand], default = []),
163-
# Base compile commands for each source file extension.
164-
base_compile_cmds = field(dict[CxxExtension, _CxxCompileCommand], default = {}),
165-
# Argsfiles generated for compiling these source files.
166-
argsfiles = field(CompileArgsfiles, default = CompileArgsfiles()),
167-
# List of compile commands for use in compilation database generation.
168-
comp_db_compile_cmds = field(list[CxxSrcCompileCommand], default = []),
169-
)
170-
171-
CxxCompileOutput = record(
172-
# The compiled `.o` file.
173-
object = field(Artifact),
174-
object_format = field(CxxObjectFormat, CxxObjectFormat("native")),
175-
object_has_external_debug_info = field(bool, False),
176-
# Externally referenced debug info, which doesn't get linked with the
177-
# object (e.g. the above `.o` when using `-gsplit-dwarf=single` or the
178-
# the `.dwo` when using `-gsplit-dwarf=split`).
179-
external_debug_info = field(Artifact | None, None),
180-
clang_remarks = field(Artifact | None, None),
181-
clang_trace = field(Artifact | None, None),
182-
gcno_file = field(Artifact | None, None),
183-
index_store = field(Artifact | None, None),
184-
assembly = field(Artifact | None, None),
185-
diagnostics = field(Artifact | None, None),
186-
preproc = field(Artifact | None, None),
187-
)
188-
189-
CxxCompileFlavor = enum(
190-
# Default compilation witout alterations
191-
"default",
192-
# Produces position independent compile outputs
193-
"pic",
194-
# Produces position independent compile outputs
195-
# using optimization flags from toolchain
196-
"pic_optimized",
197-
)
198-
19964
def get_source_extension_for_header(header_extension: str, default: CxxExtension) -> CxxExtension:
20065
"""
20166
Which source file extension to use to get compiler flags for the header.
@@ -347,7 +212,7 @@ def create_compile_cmds(
347212

348213
src_compile_cmds = []
349214
hdr_compile_cmds = []
350-
cxx_compile_cmd_by_ext = {} # type: dict[CxxExtension, _CxxCompileCommand]
215+
cxx_compile_cmd_by_ext = {} # type: dict[CxxExtension, CxxCompileCommand]
351216
argsfile_by_ext = {} # type: dict[str, CompileArgsfile]
352217
xcode_argsfile_by_ext = {} # type: dict[str, CompileArgsfile]
353218

@@ -775,7 +640,7 @@ def _create_precompile_cmd(
775640
header_group: str | None,
776641
group_name: str,
777642
extra_preprocessors: list[CPreprocessor],
778-
cmd: _CxxCompileCommand) -> _CxxSrcPrecompileCommand:
643+
cmd: CxxCompileCommand) -> CxxSrcPrecompileCommand:
779644
include_dirs = flatten([x.include_dirs for x in preprocessors])
780645
converted_headers = [
781646
_convert_raw_header(ctx, raw_header, include_dirs)
@@ -852,7 +717,7 @@ module "{}" {{
852717
args.extend(["-Xclang", cmd_args(input_header, format = "-fmodules-embed-file={}")])
853718
args.extend(["--precompile", input_header])
854719

855-
return _CxxSrcPrecompileCommand(
720+
return CxxSrcPrecompileCommand(
856721
src = src_dir,
857722
cxx_compile_cmd = cmd,
858723
args = args,
@@ -863,7 +728,7 @@ def _precompile_single_cxx(
863728
ctx: AnalysisContext,
864729
impl_params: CxxRuleConstructorParams,
865730
group_name: str,
866-
src_compile_cmd: _CxxSrcPrecompileCommand) -> HeaderUnit:
731+
src_compile_cmd: CxxSrcPrecompileCommand) -> HeaderUnit:
867732
identifier = src_compile_cmd.src.short_path
868733

869734
filename = "{}.pcm".format(identifier)
@@ -1287,7 +1152,7 @@ def _generate_base_compile_command(
12871152
pre: CPreprocessorInfo,
12881153
headers_tag: ArtifactTag,
12891154
ext: CxxExtension,
1290-
filename_prefix: str = "") -> _CxxCompileCommand:
1155+
filename_prefix: str = "") -> CxxCompileCommand:
12911156
"""
12921157
Generate a common part of a compile command that is shared by all sources
12931158
with a given extension.
@@ -1303,7 +1168,7 @@ def _generate_base_compile_command(
13031168
tracking_mode = _get_dep_tracking_mode(toolchain, dep_file_file_type_hint)
13041169
mk_dep_files_flags = get_headers_dep_files_flags_factory(tracking_mode)
13051170
if mk_dep_files_flags:
1306-
headers_dep_files = _HeadersDepFiles(
1171+
headers_dep_files = HeadersDepFiles(
13071172
processor = cmd_args(toolchain.internal_tools.dep_file_processor),
13081173
mk_flags = mk_dep_files_flags,
13091174
tag = headers_tag,
@@ -1315,7 +1180,7 @@ def _generate_base_compile_command(
13151180
header_units_argsfile = _mk_header_units_argsfile(ctx, compiler_info, pre, ext, filename_prefix)
13161181

13171182
allow_cache_upload = cxx_attrs_get_allow_cache_upload(ctx.attrs, default = compiler_info.allow_cache_upload)
1318-
return _CxxCompileCommand(
1183+
return CxxCompileCommand(
13191184
base_compile_cmd = base_compile_cmd,
13201185
argsfile = argsfile,
13211186
xcode_argsfile = xcode_argsfile,

0 commit comments

Comments
 (0)