amalgamate.py 2.8 KB

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