amalgamate.py 2.7 KB

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