amalgamate.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_ \*/)")
  20. includes = []
  21. print ("Starting amalgamating file "+ args.outfile)
  22. file = io.open(args.outfile, 'w')
  23. file.write(u'''/* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES
  24. * visit http://open62541.org/ for information about this software
  25. * Git-Revision: %s
  26. */
  27. /*
  28. * Copyright (C) 2015 the contributors as stated in the AUTHORS file
  29. *
  30. * This file is part of open62541. open62541 is free software: you can
  31. * redistribute it and/or modify it under the terms of the GNU Lesser General
  32. * Public License, version 3 (as published by the Free Software Foundation) with
  33. * a static linking exception as stated in the LICENSE file provided with
  34. * 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. See the GNU Lesser General Public License for more
  39. * details.
  40. */\n\n''' % args.version)
  41. if not is_c:
  42. file.write(u'''#ifndef %s
  43. #define %s
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif\n\n''' % (outname.upper() + u"_H_", outname.upper() + u"_H_") )
  47. if not is_c:
  48. for inc in includes:
  49. file.write(u"#include " + inc + "\n")
  50. else:
  51. file.write(u'''#ifndef UA_DYNAMIC_LINKING
  52. # define UA_DYNAMIC_LINKING
  53. #endif
  54. #ifndef UA_INTERNAL
  55. #define UA_INTERNAL
  56. #endif
  57. \n''')
  58. file.write(u"#include \"" + outname + ".h\"\n")
  59. for fname in args.inputs:
  60. with io.open(fname, encoding="utf8") as infile:
  61. file.write(u"\n/*********************************** amalgamated original file \"" + fname + u"\" ***********************************/\n\n")
  62. print ("Integrating file '" + fname + "'...", end=""),
  63. for line in infile:
  64. inc_res = include_re.match(line)
  65. guard_res = guard_re.match(line)
  66. if not inc_res and not guard_res:
  67. file.write(line)
  68. print ("done."),
  69. if not is_c:
  70. file.write(u'''
  71. #ifdef __cplusplus
  72. } // extern "C"
  73. #endif
  74. #endif /* %s */''' % (outname.upper() + u"_H_"))
  75. file.close()
  76. print ("The size of "+args.outfile+" is "+ str(os.path.getsize(args.outfile))+" Bytes.")