@@ -39,10 +39,29 @@ extern "C" unsigned char halide_c_template_CodeGen_C_vectors[];
3939
4040namespace {
4141
42+ // On Windows, raw string literals and binary2cpp-generated arrays can
43+ // contain \r\n line endings (from git checkout with core.autocrlf).
44+ // Normalize to \n so that generated source files have consistent,
45+ // platform-independent line endings.
46+ string normalize_line_endings (const char *s) {
47+ string result;
48+ result.reserve (strlen (s));
49+ for (; *s; ++s) {
50+ if (*s != ' \r ' ) {
51+ result += *s;
52+ }
53+ }
54+ return result;
55+ }
56+
57+ string normalize_line_endings (const unsigned char *s) {
58+ return normalize_line_endings (reinterpret_cast <const char *>(s));
59+ }
60+
4261// HALIDE_MUST_USE_RESULT defined here is intended to exactly
4362// duplicate the definition in HalideRuntime.h (so that either or
4463// both can be present, in any order).
45- const char * const kDefineMustUseResult = R"INLINE_CODE( #ifndef HALIDE_MUST_USE_RESULT
64+ const string kDefineMustUseResult = normalize_line_endings( R"INLINE_CODE( #ifndef HALIDE_MUST_USE_RESULT
4665#ifdef __has_attribute
4766#if __has_attribute(nodiscard)
4867#define HALIDE_MUST_USE_RESULT [[nodiscard]]
@@ -55,9 +74,9 @@ const char *const kDefineMustUseResult = R"INLINE_CODE(#ifndef HALIDE_MUST_USE_R
5574#define HALIDE_MUST_USE_RESULT
5675#endif
5776#endif
58- )INLINE_CODE" ;
77+ )INLINE_CODE" ) ;
5978
60- const char * const constexpr_argument_info_docs = R"INLINE_CODE(
79+ const string constexpr_argument_info_docs = normalize_line_endings( R"INLINE_CODE(
6180/**
6281 * This function returns a constexpr array of information about a Halide-generated
6382 * function's argument signature (e.g., number of arguments, type of each, etc).
@@ -88,7 +107,7 @@ const char *const constexpr_argument_info_docs = R"INLINE_CODE(
88107 * impact aside from the numerical value of the constant.
89108 */
90109
91- )INLINE_CODE" ;
110+ )INLINE_CODE" ) ;
92111
93112class TypeInfoGatherer : public IRGraphVisitor {
94113private:
@@ -194,7 +213,7 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
194213 // If it's a header, emit an include guard.
195214 stream << " #ifndef HALIDE_FUNCTION_INFO_" << c_print_name (guard) << " \n "
196215 << " #define HALIDE_FUNCTION_INFO_" << c_print_name (guard) << " \n " ;
197- stream << R"INLINE_CODE(
216+ stream << normalize_line_endings ( R"INLINE_CODE(
198217/* MACHINE GENERATED By Halide. */
199218
200219#if !(__cplusplus >= 201703L || _MSVC_LANG >= 201703L)
@@ -203,7 +222,7 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
203222
204223#include "HalideRuntime.h"
205224
206- )INLINE_CODE" ;
225+ )INLINE_CODE" ) ;
207226
208227 return ;
209228 }
@@ -247,9 +266,9 @@ CodeGen_C::CodeGen_C(ostream &s, const Target &t, OutputKind output_kind, const
247266 } else {
248267 // Include declarations of everything generated C source might want
249268 stream
250- << halide_c_template_CodeGen_C_prologue << " \n "
251- << halide_internal_runtime_header_HalideRuntime_h << " \n "
252- << halide_internal_initmod_inlined_c << " \n " ;
269+ << normalize_line_endings ( halide_c_template_CodeGen_C_prologue) << " \n "
270+ << normalize_line_endings ( halide_internal_runtime_header_HalideRuntime_h) << " \n "
271+ << normalize_line_endings ( halide_internal_initmod_inlined_c) << " \n " ;
253272 stream << " \n " ;
254273 }
255274
@@ -294,24 +313,24 @@ CodeGen_C::~CodeGen_C() {
294313 << " // use the -r flag with any Halide generator binary, e.g.:\n "
295314 << " // $ ./my_generator -r halide_runtime -o . target=host\n "
296315 << " \n "
297- << halide_internal_runtime_header_HalideRuntime_h << " \n " ;
316+ << normalize_line_endings ( halide_internal_runtime_header_HalideRuntime_h) << " \n " ;
298317 if (target.has_feature (Target::CUDA)) {
299- stream << halide_internal_runtime_header_HalideRuntimeCuda_h << " \n " ;
318+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeCuda_h) << " \n " ;
300319 }
301320 if (target.has_feature (Target::HVX)) {
302- stream << halide_internal_runtime_header_HalideRuntimeHexagonHost_h << " \n " ;
321+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeHexagonHost_h) << " \n " ;
303322 }
304323 if (target.has_feature (Target::Metal)) {
305- stream << halide_internal_runtime_header_HalideRuntimeMetal_h << " \n " ;
324+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeMetal_h) << " \n " ;
306325 }
307326 if (target.has_feature (Target::OpenCL)) {
308- stream << halide_internal_runtime_header_HalideRuntimeOpenCL_h << " \n " ;
327+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeOpenCL_h) << " \n " ;
309328 }
310329 if (target.has_feature (Target::D3D12Compute)) {
311- stream << halide_internal_runtime_header_HalideRuntimeD3D12Compute_h << " \n " ;
330+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeD3D12Compute_h) << " \n " ;
312331 }
313332 if (target.has_feature (Target::WebGPU)) {
314- stream << halide_internal_runtime_header_HalideRuntimeWebGPU_h << " \n " ;
333+ stream << normalize_line_endings ( halide_internal_runtime_header_HalideRuntimeWebGPU_h) << " \n " ;
315334 }
316335 }
317336 stream << " #endif\n " ;
@@ -324,13 +343,7 @@ void CodeGen_C::add_platform_prologue() {
324343
325344void CodeGen_C::add_vector_typedefs (const std::set<Type> &vector_types) {
326345 if (!vector_types.empty ()) {
327- // Voodoo fix: on at least one config (our arm32 buildbot running gcc 5.4),
328- // emitting this long text string was regularly garbled in a predictable pattern;
329- // flushing the stream before or after heals it. Since C++ codegen is rarely
330- // on a compilation critical path, we'll just band-aid it in this way.
331- stream << std::flush;
332- stream << halide_c_template_CodeGen_C_vectors;
333- stream << std::flush;
346+ stream << normalize_line_endings (halide_c_template_CodeGen_C_vectors);
334347
335348 for (const auto &t : vector_types) {
336349 string name = print_type (t, DoNotAppendSpace);
@@ -354,20 +367,20 @@ void CodeGen_C::add_vector_typedefs(const std::set<Type> &vector_types) {
354367
355368void CodeGen_C::set_name_mangling_mode (NameMangling mode) {
356369 if (extern_c_open && mode != NameMangling::C) {
357- stream << R"INLINE_CODE(
370+ stream << normalize_line_endings ( R"INLINE_CODE(
358371#ifdef __cplusplus
359372} // extern "C"
360373#endif
361374
362- )INLINE_CODE" ;
375+ )INLINE_CODE" ) ;
363376 extern_c_open = false ;
364377 } else if (!extern_c_open && mode == NameMangling::C) {
365- stream << R"INLINE_CODE(
378+ stream << normalize_line_endings ( R"INLINE_CODE(
366379#ifdef __cplusplus
367380extern "C" {
368381#endif
369382
370- )INLINE_CODE" ;
383+ )INLINE_CODE" ) ;
371384 extern_c_open = true ;
372385 }
373386}
@@ -2567,10 +2580,10 @@ void CodeGen_C::test() {
25672580 }
25682581
25692582 string correct_source =
2570- string (( const char *) halide_c_template_CodeGen_C_prologue) + ' \n ' +
2571- string (( const char *) halide_internal_runtime_header_HalideRuntime_h) + ' \n ' +
2572- string (( const char *) halide_internal_initmod_inlined_c) + ' \n ' +
2573- ' \n ' + kDefineMustUseResult + R"GOLDEN_CODE(
2583+ normalize_line_endings ( halide_c_template_CodeGen_C_prologue) + ' \n ' +
2584+ normalize_line_endings ( halide_internal_runtime_header_HalideRuntime_h) + ' \n ' +
2585+ normalize_line_endings ( halide_internal_initmod_inlined_c) + ' \n ' +
2586+ ' \n ' + kDefineMustUseResult + normalize_line_endings ( R"GOLDEN_CODE(
25742587#ifndef HALIDE_FUNCTION_ATTRS
25752588#define HALIDE_FUNCTION_ATTRS
25762589#endif
@@ -2638,7 +2651,7 @@ int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta, void
26382651} // extern "C"
26392652#endif
26402653
2641- )GOLDEN_CODE" ;
2654+ )GOLDEN_CODE" ) ;
26422655
26432656 const auto compare_srcs = [](const string &actual, const string &expected) {
26442657 if (actual != expected) {
@@ -2673,7 +2686,7 @@ int test1(struct halide_buffer_t *_buf_buffer, float _alpha, int32_t _beta, void
26732686 cg.compile (m);
26742687 }
26752688
2676- string correct_function_info = R"GOLDEN_CODE( #ifndef HALIDE_FUNCTION_INFO__Function___Info___Test
2689+ string correct_function_info = normalize_line_endings ( R"GOLDEN_CODE( #ifndef HALIDE_FUNCTION_INFO__Function___Info___Test
26772690#define HALIDE_FUNCTION_INFO__Function___Info___Test
26782691
26792692/* MACHINE GENERATED By Halide. */
@@ -2724,7 +2737,7 @@ inline constexpr std::array<::HalideFunctionInfo::ArgumentInfo, 4> test1_argumen
27242737 }};
27252738}
27262739#endif
2727- )GOLDEN_CODE" ;
2740+ )GOLDEN_CODE" ) ;
27282741
27292742 compare_srcs (function_info.str (), correct_function_info);
27302743
0 commit comments