glib/gio/data-to-c.py
Aleksandr Mezin 19106af47f data-to-c.py: autodetect line endings
When GLib code is checked out with Windows line endings (happens on Windows),
data-to-c.py embedded that line endings into generated string literal. And
then they translated to double newlines in glib-compile-resources output.

clang-cl failed to compile such files because of empty lines in the middle of
multiline macros:

    #define G_MSVC_CTOR(_func,_sym_prefix) \

      static void _func(void); \

To fix the issue, enable 'universal newlines' mode when reading the input in
data-to-c.py - translate both '\n' and '\r\n' to '\n'.

Fixes https://gitlab.gnome.org/GNOME/glib/-/issues/2340
2021-03-01 23:59:34 +06:00

18 lines
409 B
Python
Executable File

#!/usr/bin/env python3
import sys
if len(sys.argv) < 4:
print("Usage: {0} <filename> <variable> <output>")
with open(sys.argv[1], "r", encoding="utf-8", errors="backslashreplace") as f:
in_data = f.read()
b = [r"\x{:02x}".format(ord(c)) for c in in_data]
out_data = 'const char {0}[] = "'.format(sys.argv[2])
out_data += "".join(b) + '";'
with open(sys.argv[3], "w") as f:
f.write(out_data)