amalgamate.py 3.0 KB

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