amalgamate.py 2.6 KB

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