diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 8424f3b629..651c3b3d98 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -2,10 +2,6 @@ // See script/api_protobuf/api_protobuf.py #pragma once -#include "esphome/core/defines.h" -#ifdef USE_BLUETOOTH_PROXY -#define USE_API_VARINT64 -#endif #include "esphome/core/string_ref.h" #include "proto.h" diff --git a/esphome/components/api/api_pb2_defines.h b/esphome/components/api/api_pb2_defines.h new file mode 100644 index 0000000000..6f60f24f99 --- /dev/null +++ b/esphome/components/api/api_pb2_defines.h @@ -0,0 +1,8 @@ +// This file was automatically generated with a tool. +// See script/api_protobuf/api_protobuf.py +#pragma once + +#include "esphome/core/defines.h" +#ifdef USE_BLUETOOTH_PROXY +#define USE_API_VARINT64 +#endif diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index 69b0440e38..ce3f9e1808 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -1,5 +1,6 @@ #pragma once +#include "api_pb2_defines.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 2324708324..5d3c61fcda 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2593,22 +2593,28 @@ def main() -> None: ) # Find the ifdef guard for 64-bit varint fields (int64/uint64/sint64). - # Emitted before proto.h so parse_wide_() and 64-bit accessors are available. + # Generated into api_pb2_defines.h so proto.h can include it, ensuring + # consistent ProtoVarInt layout across all translation units. has_varint64, varint64_guard = get_varint64_ifdef(file, message_ifdef_map) + # Generate api_pb2_defines.h — included by proto.h to ensure all translation + # units see USE_API_VARINT64 consistently (avoids ODR violations in ProtoVarInt). + defines_content = FILE_HEADER + defines_content += "#pragma once\n\n" + defines_content += '#include "esphome/core/defines.h"\n' + if has_varint64: + defines_content += "\n".join( + wrap_with_ifdef(["#define USE_API_VARINT64"], varint64_guard) + ) + defines_content += "\n" + + with open(root / "api_pb2_defines.h", "w", encoding="utf-8") as f: + f.write(defines_content) + content = FILE_HEADER content += """\ #pragma once -#include "esphome/core/defines.h" -""" - if has_varint64: - content += "\n".join( - wrap_with_ifdef(["#define USE_API_VARINT64"], varint64_guard) - ) - content += "\n" - - content += """\ #include "esphome/core/string_ref.h" #include "proto.h"