amalgamate.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. for fname in args.inputs:
  19. if("util.h" in fname):
  20. is_c = True
  21. continue
  22. with open(fname) as infile:
  23. for line in infile:
  24. res = include_re.match(line)
  25. if res:
  26. inc = res.group(1)
  27. if not inc in includes and not inc[0] == '"':
  28. includes.append(inc)
  29. file = open(args.outfile, 'w')
  30. file.write('''/* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES
  31. * visit http://open62541.org/ for information about this software
  32. * Git-Revision: %s
  33. */
  34. /*
  35. * Copyright (C) 2015 the contributors as stated in the AUTHORS file
  36. *
  37. * This file is part of open62541. open62541 is free software: you can
  38. * redistribute it and/or modify it under the terms of the GNU Lesser General
  39. * Public License, version 3 (as published by the Free Software Foundation) with
  40. * a static linking exception as stated in the LICENSE file provided with
  41. * open62541.
  42. *
  43. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  44. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  45. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  46. * details.
  47. */\n\n''' % args.version)
  48. if not is_c:
  49. file.write('''#ifndef %s
  50. #define %s
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif\n\n''' % (outname.upper() + "_H_", outname.upper() + "_H_") )
  54. if not is_c:
  55. for inc in includes:
  56. file.write("#include " + inc + "\n")
  57. else:
  58. file.write("#define UA_AMALGAMATE\n")
  59. file.write('''#ifndef UA_DYNAMIC_LINKING
  60. # define UA_DYNAMIC_LINKING
  61. #endif\n\n''')
  62. for fname in args.inputs:
  63. if "ua_config.h" in fname or "ua_util.h" in fname:
  64. with open(fname) as infile:
  65. for line in infile:
  66. file.write(line)
  67. file.write("#include \"" + outname + ".h\"\n")
  68. for fname in args.inputs:
  69. if not "util.h" in fname:
  70. with open(fname) as infile:
  71. file.write("/*********************************** amalgamated original file \"" + fname + "\" ***********************************/\n")
  72. for line in infile:
  73. inc_res = include_re.match(line)
  74. guard_res = guard_re.match(line)
  75. if not inc_res and not guard_res:
  76. file.write(line)
  77. if not is_c:
  78. file.write('''
  79. #ifdef __cplusplus
  80. } // extern "C"
  81. #endif
  82. #endif /* %s */''' % (outname.upper() + "_H_"))
  83. file.close()
  84. print ("The size of "+args.outfile+" is "+ str(os.path.getsize(args.outfile)))