| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | #!/usr/bin/env python3 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright © 2022 Collabora Inc. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # SPDX-License-Identifier: LGPL-2.1-or-later | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Original author: Xavier Claessens <xclaesse@gmail.com> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import argparse | 
					
						
							|  |  |  | import textwrap | 
					
						
							|  |  |  | from pathlib import Path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:14:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Disable line length warnings as wrapping the C code templates would be hard | 
					
						
							|  |  |  | # flake8: noqa: E501 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | def gen_versions_macros(args, current_minor_version): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     with args.out_path.open("w", encoding="utf-8") as ofile, args.in_path.open( | 
					
						
							|  |  |  |         "r", encoding="utf-8" | 
					
						
							|  |  |  |     ) as ifile: | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |         for line in ifile.readlines(): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |             if "@GLIB_VERSIONS@" in line: | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |                 for minor in range(2, current_minor_version + 2, 2): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |                     ofile.write( | 
					
						
							|  |  |  |                         textwrap.dedent( | 
					
						
							|  |  |  |                             f"""\
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |                         /** | 
					
						
							|  |  |  |                         * GLIB_VERSION_2_{minor}: | 
					
						
							|  |  |  |                         * | 
					
						
							|  |  |  |                         * A macro that evaluates to the 2.{minor} version of GLib, in a format | 
					
						
							|  |  |  |                         * that can be used by the C pre-processor. | 
					
						
							|  |  |  |                         * | 
					
						
							|  |  |  |                         * Since: 2.{max(minor, 32)} | 
					
						
							|  |  |  |                         */ | 
					
						
							|  |  |  |                         #define GLIB_VERSION_2_{minor}       (G_ENCODE_VERSION (2, {minor})) | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |                         """
 | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 ofile.write(line) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | def gen_doc_sections(args, current_minor_version): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     with args.out_path.open("w", encoding="utf-8") as ofile, args.in_path.open( | 
					
						
							|  |  |  |         "r", encoding="utf-8" | 
					
						
							|  |  |  |     ) as ifile: | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |         for line in ifile.readlines(): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |             if "@GLIB_VERSIONS@" in line: | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |                 for minor in range(2, current_minor_version + 2, 2): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |                     ofile.write( | 
					
						
							|  |  |  |                         textwrap.dedent( | 
					
						
							|  |  |  |                             f"""\
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |                         GLIB_VERSION_2_{minor} | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |                         """
 | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     ) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 ofile.write(line) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | def gen_visibility_macros(args, current_minor_version): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     Generates a set of macros for each minor stable version of GLib | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - GLIB_VAR | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - GLIB_DEPRECATED | 
					
						
							|  |  |  |     - GLIB_DEPRECATED_IN_… | 
					
						
							|  |  |  |     - GLIB_DEPRECATED_MACRO_IN_… | 
					
						
							|  |  |  |     - GLIB_DEPRECATED_ENUMERATOR_IN_… | 
					
						
							|  |  |  |     - GLIB_DEPRECATED_TYPE_IN_… | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_IN_ALL | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_IN_… | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_STATIC_INLINE_IN_… | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_MACRO_IN_… | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_ENUMERATOR_IN_… | 
					
						
							|  |  |  |     - GLIB_AVAILABLE_TYPE_IN_… | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     - GLIB_UNAVAILABLE(maj,min) | 
					
						
							|  |  |  |     - GLIB_UNAVAILABLE_STATIC_INLINE(maj,min) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-21 12:38:40 +01:00
										 |  |  |     The GLIB namespace can be replaced with one of GOBJECT, GIO, GMODULE, GI. | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ns = args.namespace | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     with args.out_path.open("w", encoding="utf-8") as f: | 
					
						
							|  |  |  |         f.write( | 
					
						
							|  |  |  |             textwrap.dedent( | 
					
						
							|  |  |  |                 f"""\
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |             #pragma once | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined({ns}_STATIC_COMPILATION) | 
					
						
							|  |  |  |             #  define _{ns}_EXPORT __declspec(dllexport) | 
					
						
							|  |  |  |             #  define _{ns}_IMPORT __declspec(dllimport) | 
					
						
							|  |  |  |             #elif __GNUC__ >= 4 | 
					
						
							|  |  |  |             #  define _{ns}_EXPORT __attribute__((visibility("default"))) | 
					
						
							|  |  |  |             #  define _{ns}_IMPORT | 
					
						
							|  |  |  |             #else | 
					
						
							|  |  |  |             #  define _{ns}_EXPORT | 
					
						
							|  |  |  |             #  define _{ns}_IMPORT | 
					
						
							|  |  |  |             #endif | 
					
						
							|  |  |  |             #ifdef {ns}_COMPILATION | 
					
						
							|  |  |  |             #  define _{ns}_API _{ns}_EXPORT | 
					
						
							|  |  |  |             #else | 
					
						
							|  |  |  |             #  define _{ns}_API _{ns}_IMPORT | 
					
						
							|  |  |  |             #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #define _{ns}_EXTERN _{ns}_API extern | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #define {ns}_VAR _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_AVAILABLE_IN_ALL _{ns}_EXTERN | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #ifdef GLIB_DISABLE_DEPRECATION_WARNINGS | 
					
						
							|  |  |  |             #define {ns}_DEPRECATED _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_DEPRECATED_FOR(f) _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_UNAVAILABLE(maj,min) _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_UNAVAILABLE_STATIC_INLINE(maj,min) | 
					
						
							|  |  |  |             #else | 
					
						
							|  |  |  |             #define {ns}_DEPRECATED G_DEPRECATED _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_UNAVAILABLE(maj,min) G_UNAVAILABLE(maj,min) _{ns}_EXTERN | 
					
						
							|  |  |  |             #define {ns}_UNAVAILABLE_STATIC_INLINE(maj,min) G_UNAVAILABLE(maj,min) | 
					
						
							|  |  |  |             #endif | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |             """
 | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |         for minor in range(26, current_minor_version + 2, 2): | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |             f.write( | 
					
						
							|  |  |  |                 textwrap.dedent( | 
					
						
							|  |  |  |                     f"""
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |                 #if GLIB_VERSION_MIN_REQUIRED >= GLIB_VERSION_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_IN_2_{minor} {ns}_DEPRECATED | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_IN_2_{minor}_FOR(f) {ns}_DEPRECATED_FOR (f) | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_MACRO_IN_2_{minor} GLIB_DEPRECATED_MACRO | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_MACRO_IN_2_{minor}_FOR(f) GLIB_DEPRECATED_MACRO_FOR (f) | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_ENUMERATOR_IN_2_{minor} GLIB_DEPRECATED_ENUMERATOR | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_ENUMERATOR_IN_2_{minor}_FOR(f) GLIB_DEPRECATED_ENUMERATOR_FOR (f) | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_TYPE_IN_2_{minor} GLIB_DEPRECATED_TYPE | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_TYPE_IN_2_{minor}_FOR(f) GLIB_DEPRECATED_TYPE_FOR (f) | 
					
						
							|  |  |  |                 #else | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_IN_2_{minor} _{ns}_EXTERN | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_IN_2_{minor}_FOR(f) _{ns}_EXTERN | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_MACRO_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_MACRO_IN_2_{minor}_FOR(f) | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_ENUMERATOR_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_ENUMERATOR_IN_2_{minor}_FOR(f) | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_TYPE_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_DEPRECATED_TYPE_IN_2_{minor}_FOR(f) | 
					
						
							|  |  |  |                 #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 #if GLIB_VERSION_MAX_ALLOWED < GLIB_VERSION_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_IN_2_{minor} {ns}_UNAVAILABLE (2, {minor}) | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_STATIC_INLINE_IN_2_{minor} GLIB_UNAVAILABLE_STATIC_INLINE (2, {minor}) | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_MACRO_IN_2_{minor} GLIB_UNAVAILABLE_MACRO (2, {minor}) | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_ENUMERATOR_IN_2_{minor} GLIB_UNAVAILABLE_ENUMERATOR (2, {minor}) | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_TYPE_IN_2_{minor} GLIB_UNAVAILABLE_TYPE (2, {minor}) | 
					
						
							|  |  |  |                 #else | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_IN_2_{minor} _{ns}_EXTERN | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_STATIC_INLINE_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_MACRO_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_ENUMERATOR_IN_2_{minor} | 
					
						
							|  |  |  |                 #define {ns}_AVAILABLE_TYPE_IN_2_{minor} | 
					
						
							|  |  |  |                 #endif | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |                 """
 | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     parser = argparse.ArgumentParser() | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     parser.add_argument("glib_version", help="Current GLib version") | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     subparsers = parser.add_subparsers() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     versions_parser = subparsers.add_parser( | 
					
						
							|  |  |  |         "versions-macros", help="Generate versions macros" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     versions_parser.add_argument("in_path", help="input file", type=Path) | 
					
						
							|  |  |  |     versions_parser.add_argument("out_path", help="output file", type=Path) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     versions_parser.set_defaults(func=gen_versions_macros) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     doc_parser = subparsers.add_parser( | 
					
						
							|  |  |  |         "doc-sections", help="Generate glib-sections.txt" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     doc_parser.add_argument("in_path", help="input file", type=Path) | 
					
						
							|  |  |  |     doc_parser.add_argument("out_path", help="output file", type=Path) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     doc_parser.set_defaults(func=gen_doc_sections) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     visibility_parser = subparsers.add_parser( | 
					
						
							|  |  |  |         "visibility-macros", help="Generate visibility macros" | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     visibility_parser.add_argument("namespace", help="Macro namespace") | 
					
						
							|  |  |  |     visibility_parser.add_argument("out_path", help="output file", type=Path) | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     visibility_parser.set_defaults(func=gen_visibility_macros) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  |     version = [int(i) for i in args.glib_version.split(".")] | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     assert version[0] == 2 | 
					
						
							|  |  |  |     args.func(args, version[1]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-14 18:11:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2022-10-03 10:36:04 -04:00
										 |  |  |     main() |