amalgamate.py 3.0 KB

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