amalgamate.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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('''/*
  29. * Copyright (C) 2014 the contributors as stated in the AUTHORS file
  30. *
  31. * This file is part of open62541. open62541 is free software: you can
  32. * redistribute it and/or modify it under the terms of the GNU Lesser General
  33. * Public License, version 3 (as published by the Free Software Foundation) with
  34. * a static linking exception as stated in the LICENSE file provided with
  35. * open62541.
  36. *
  37. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  38. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  39. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  40. * details.
  41. */
  42. /* THIS IS A SINGLE-FILE DISTRIBUTION CONCATENATED FROM THE OPEN62541 SOURCES */\n\n''')
  43. if not is_c:
  44. file.write('''#ifndef %s
  45. #define %s
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif\n\n''' % (outname.upper() + "_H_", outname.upper() + "_H_") )
  49. if not is_c:
  50. for inc in includes:
  51. file.write("#include " + inc + "\n")
  52. else:
  53. file.write("#define UA_AMALGAMATE\n")
  54. file.write('''#ifndef UA_DYNAMIC_LINKING
  55. # define UA_DYNAMIC_LINKING
  56. #endif\n\n''')
  57. for fname in args.inputs:
  58. if "ua_config.h" in fname or "ua_util.h" in fname:
  59. with open(fname) as infile:
  60. for line in infile:
  61. file.write(line)
  62. file.write("#include \"" + outname + ".h\"\n")
  63. for fname in args.inputs:
  64. if not "util.h" in fname:
  65. with open(fname) as infile:
  66. for line in infile:
  67. inc_res = include_re.match(line)
  68. guard_res = guard_re.match(line)
  69. if not inc_res and not guard_res:
  70. file.write(line)
  71. if not is_c:
  72. file.write('''
  73. #ifdef __cplusplus
  74. } // extern "C"
  75. #endif
  76. #endif /* %s */''' % (outname.upper() + "_H_"))
  77. file.close()