Move version.h to torch/headeronly (#164381)

Differential Revision: [D83685392](https://our.internmc.facebook.com/intern/diff/D83685392)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/164381
Approved by: https://github.com/janeyx99
This commit is contained in:
Mikayla Gawarecki
2025-10-06 18:29:20 -07:00
committed by PyTorch MergeBot
parent b13cd141b3
commit f37a6523ef
12 changed files with 62 additions and 25 deletions

2
.gitignore vendored
View File

@ -88,7 +88,7 @@ torch_compile_debug/
# Listed manually because some files in this directory are not generated
torch/testing/_internal/generated/annotated_fn_args.py
torch/testing/_internal/data/*.pt
torch/csrc/api/include/torch/version.h
torch/headeronly/version.h
torch/csrc/cudnn/cuDNN.cpp
torch/csrc/generated
torch/csrc/generic/TensorMethods.cpp

View File

@ -13,6 +13,9 @@ load(":build_variables.bzl", "jit_core_sources", "lazy_tensor_ts_sources", "libt
load(":ufunc_defs.bzl", "aten_ufunc_generated_cpu_kernel_sources", "aten_ufunc_generated_cpu_sources", "aten_ufunc_generated_cuda_sources")
load("//:tools/bazel.bzl", "rules")
# Export files for use by torch/headeronly (where version.h generation now lives)
exports_files(["version.txt"])
define_targets(rules = rules)
COMMON_COPTS = [
@ -690,7 +693,9 @@ cc_library(
"torch/csrc/*/generated/*.h",
"torch/csrc/jit/serialization/mobile_bytecode_generated.h",
] + torch_cuda_headers,
) + GENERATED_AUTOGRAD_CPP + [":version_h"],
) + GENERATED_AUTOGRAD_CPP + [
"//torch/headeronly:version_h",
],
includes = [
"third_party/kineto/libkineto/include",
"torch/csrc",

View File

@ -1038,7 +1038,8 @@ def define_buck_targets(
name = "generated-version-header",
header_namespace = "torch",
exported_headers = {
"version.h": ":generate-version-header[version.h]",
"headeronly/version.h": ":generate-version-header[version.h]",
"version.h": "torch/csrc/api/include/torch/version.h"
},
labels = labels,
)
@ -1047,19 +1048,27 @@ def define_buck_targets(
fb_native.genrule(
name = "generate-version-header",
srcs = [
"torch/csrc/api/include/torch/version.h.in",
"torch/headeronly/version.h.in",
"version.txt",
],
cmd = "$(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([
cmd = "mkdir -p $OUT/torch/headeronly && $(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([
"--template-path",
"torch/csrc/api/include/torch/version.h.in",
"torch/headeronly/version.h.in",
"--version-path",
"version.txt",
"--output-path",
"$OUT/version.h",
"$OUT/torch/headeronly/version.h",
]),
cmd_exe = "md $OUT\\torch\\headeronly 2>nul & $(exe {}tools:gen-version-header) ".format(ROOT_PATH) + " ".join([
"--template-path",
"torch/headeronly/version.h.in",
"--version-path",
"version.txt",
"--output-path",
"$OUT\\torch\\headeronly\\version.h",
]),
outs = {
"version.h": ["version.h"],
"version.h": ["torch/headeronly/version.h"],
},
default_outs = ["."],
)

View File

@ -142,18 +142,6 @@ def define_targets(rules):
visibility = ["//visibility:public"],
)
rules.genrule(
name = "version_h",
srcs = [
":torch/csrc/api/include/torch/version.h.in",
":version.txt",
],
outs = ["torch/csrc/api/include/torch/version.h"],
cmd = "$(execpath //tools/setup_helpers:gen_version_header) " +
"--template-path $(location :torch/csrc/api/include/torch/version.h.in) " +
"--version-path $(location :version.txt) --output-path $@ ",
tools = ["//tools/setup_helpers:gen_version_header"],
)
#
# ATen generated code

View File

@ -243,8 +243,8 @@ configure_file("${TORCH_SRC_DIR}/_utils_internal.py"
COPYONLY)
# Generate header with version info
configure_file("${TORCH_SRC_DIR}/csrc/api/include/torch/version.h.in"
"${TORCH_SRC_DIR}/csrc/api/include/torch/version.h"
configure_file("${TORCH_SRC_DIR}/headeronly/version.h.in"
"${TORCH_SRC_DIR}/headeronly/version.h"
@ONLY)
set(GENERATED_CXX_TORCH

View File

@ -16,6 +16,7 @@ def _is_cpu_static_dispatch_build():
# build structure aims to replicate Bazel as much as possible, most of
# the rules simply forward to the Bazel definitions.
rules = struct(
alias = native.alias,
cc_binary = cc_binary,
cc_library = cc_library,
cc_test = cc_test,

View File

@ -11,5 +11,8 @@ py_binary(
py_binary(
name = "gen_version_header",
srcs = ["gen_version_header.py"],
visibility = ["//:__pkg__"],
visibility = [
"//:__pkg__",
"//torch/headeronly:__pkg__",
],
)

View File

@ -13,5 +13,8 @@ def define_targets(rules):
rules.py_binary(
name = "gen_version_header",
srcs = ["gen_version_header.py"],
visibility = ["//:__pkg__"],
visibility = [
"//:__pkg__",
"//torch/headeronly:__pkg__",
],
)

View File

@ -0,0 +1 @@
#include <torch/headeronly/version.h>

View File

@ -1,9 +1,35 @@
def define_targets(rules):
# workaround issue where open source bazel requires $(location ...)
# for filepaths but the buck conversion requires no $(location ...)
# for filepaths.
is_buck = hasattr(native, "read_config")
template_arg = "version.h.in" if is_buck else "$(location version.h.in)"
genrule_args = {
"name": "version_h",
"srcs": [
"version.h.in",
"//:version.txt",
],
"outs": ["version.h"],
"cmd": "$(execpath //tools/setup_helpers:gen_version_header) " +
"--template-path " + template_arg + " " +
"--version-path $(location //:version.txt) --output-path $@ ",
"tools": ["//tools/setup_helpers:gen_version_header"],
}
# Add visibility only for Bazel, buck genrule in fbcode.bzl does not
# support this argument
if not is_buck:
genrule_args["visibility"] = ["//visibility:public"]
rules.genrule(**genrule_args)
rules.cc_library(
name = "torch_headeronly",
hdrs = rules.glob([
"**/*.h"
]),
]) + ["version.h.in"],
visibility = ["//visibility:public"],
deps = [
"//torch/headeronly/macros",

View File

@ -40,6 +40,7 @@ def define_torch_headeronly_ovrsource(name, is_mobile):
],
exported_deps = [
":ovrsource_torch_headeronly_cmake_macros.h",
":generate-version-header",
],
)