amalgamate.py 3.5 KB

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