amalgamate.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. # coding: UTF-8
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. from __future__ import print_function
  7. import argparse
  8. import os.path
  9. import re
  10. import io
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('version', help='file version')
  13. parser.add_argument('outfile', help='outfile with extension .c/.h')
  14. parser.add_argument('inputs', nargs='*', action='store', help='input filenames')
  15. args = parser.parse_args()
  16. outname = args.outfile.split("/")[-1]
  17. is_c = False
  18. if outname[-2:] == ".c":
  19. is_c = True
  20. pos = outname.find(".")
  21. if pos > 0:
  22. outname = outname[:pos]
  23. include_re = re.compile("^#[\s]*include (\".*\").*$")
  24. guard_re = re.compile("^#(?:(?:ifndef|define)\s*[A-Z_]+_H_|endif /\* [A-Z_]+_H_ \*/|endif // [A-Z_]+_H_|endif\s*/\*\s*!?[A-Z_]+_H[_]+\s*\*/)")
  25. print ("Starting amalgamating file "+ args.outfile)
  26. file = io.open(args.outfile, 'wt', encoding='utf8', errors='replace')
  27. file.write(u"""/* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES
  28. * visit http://open62541.org/ for information about this software
  29. * Git-Revision: %s
  30. */
  31. /*
  32. * Copyright (C) 2014-2018 the contributors as stated in the AUTHORS file
  33. *
  34. * This file is part of open62541. open62541 is free software: you can
  35. * redistribute it and/or modify it under the terms of the Mozilla Public
  36. * License v2.0 as stated in the LICENSE file provided with open62541.
  37. *
  38. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  39. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  40. * A PARTICULAR PURPOSE.
  41. */\n\n""" % args.version)
  42. if is_c:
  43. file.write(u'''#ifndef UA_DYNAMIC_LINKING_EXPORT
  44. # define UA_DYNAMIC_LINKING_EXPORT
  45. # define MDNSD_DYNAMIC_LINKING
  46. #endif
  47. /* Enable POSIX features */
  48. #if !defined(_XOPEN_SOURCE) && !defined(_WRS_KERNEL)
  49. # define _XOPEN_SOURCE 600
  50. #endif
  51. #ifndef _DEFAULT_SOURCE
  52. # define _DEFAULT_SOURCE
  53. #endif
  54. /* On older systems we need to define _BSD_SOURCE.
  55. * _DEFAULT_SOURCE is an alias for that. */
  56. #ifndef _BSD_SOURCE
  57. # define _BSD_SOURCE
  58. #endif
  59. /* Disable security warnings for BSD sockets on MSVC */
  60. #ifdef _MSC_VER
  61. # define _CRT_SECURE_NO_WARNINGS
  62. #endif
  63. #include "%s.h"
  64. ''' % outname)
  65. else:
  66. file.write(u'''#ifndef %s
  67. #define %s
  68. ''' % (outname.upper() + u"_H_", outname.upper() + u"_H_"))
  69. for fname in args.inputs:
  70. with io.open(fname, encoding='utf8', errors='replace') as infile:
  71. file.write(u"\n/*********************************** amalgamated original file \"" + fname + u"\" ***********************************/\n\n")
  72. print ("Integrating file '" + fname + "'...", end=""),
  73. for line in infile:
  74. inc_res = include_re.match(line)
  75. guard_res = guard_re.match(line)
  76. if not inc_res and not guard_res:
  77. file.write(line)
  78. # Ensure file is written to disk.
  79. file.flush()
  80. os.fsync(file.fileno())
  81. print ("done."),
  82. if not is_c:
  83. file.write(u"#endif /* %s */\n" % (outname.upper() + u"_H_"))
  84. # Ensure file is written to disk.
  85. # See https://stackoverflow.com/questions/13761961/large-file-not-flushed-to-disk-immediately-after-calling-close
  86. file.flush()
  87. os.fsync(file.fileno())
  88. file.close()
  89. print ("The size of "+args.outfile+" is "+ str(os.path.getsize(args.outfile))+" Bytes.")