scala.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Thomas Nagy, 2010 (ita)
  4. """
  5. Scala support
  6. scalac outputs files a bit where it wants to
  7. """
  8. import os
  9. from waflib import Task, Utils, Node
  10. from waflib.TaskGen import feature, before_method, after_method
  11. from waflib.Tools import ccroot
  12. ccroot.USELIB_VARS['scalac'] = set(['CLASSPATH', 'SCALACFLAGS'])
  13. from waflib.Tools import javaw
  14. @feature('scalac')
  15. @before_method('process_source')
  16. def apply_scalac(self):
  17. Utils.def_attrs(self, jarname='', classpath='',
  18. sourcepath='.', srcdir='.',
  19. jar_mf_attributes={}, jar_mf_classpath=[])
  20. outdir = getattr(self, 'outdir', None)
  21. if outdir:
  22. if not isinstance(outdir, Node.Node):
  23. outdir = self.path.get_bld().make_node(self.outdir)
  24. else:
  25. outdir = self.path.get_bld()
  26. outdir.mkdir()
  27. self.env['OUTDIR'] = outdir.abspath()
  28. self.scalac_task = tsk = self.create_task('scalac')
  29. tmp = []
  30. srcdir = getattr(self, 'srcdir', '')
  31. if isinstance(srcdir, Node.Node):
  32. srcdir = [srcdir]
  33. for x in Utils.to_list(srcdir):
  34. if isinstance(x, Node.Node):
  35. y = x
  36. else:
  37. y = self.path.find_dir(x)
  38. if not y:
  39. self.bld.fatal('Could not find the folder %s from %s' % (x, self.path))
  40. tmp.append(y)
  41. tsk.srcdir = tmp
  42. # reuse some code
  43. feature('scalac')(javaw.use_javac_files)
  44. after_method('apply_scalac')(javaw.use_javac_files)
  45. feature('scalac')(javaw.set_classpath)
  46. after_method('apply_scalac', 'use_scalac_files')(javaw.set_classpath)
  47. SOURCE_RE = '**/*.scala'
  48. class scalac(javaw.javac):
  49. color = 'GREEN'
  50. vars = ['CLASSPATH', 'SCALACFLAGS', 'SCALAC', 'OUTDIR']
  51. def runnable_status(self):
  52. """
  53. Wait for dependent tasks to be complete, then read the file system to find the input nodes.
  54. """
  55. for t in self.run_after:
  56. if not t.hasrun:
  57. return Task.ASK_LATER
  58. if not self.inputs:
  59. global SOURCE_RE
  60. self.inputs = []
  61. for x in self.srcdir:
  62. self.inputs.extend(x.ant_glob(SOURCE_RE, remove=False))
  63. return super(javaw.javac, self).runnable_status()
  64. def run(self):
  65. """
  66. Execute the scalac compiler
  67. """
  68. env = self.env
  69. gen = self.generator
  70. bld = gen.bld
  71. wd = bld.bldnode.abspath()
  72. def to_list(xx):
  73. if isinstance(xx, str):
  74. return [xx]
  75. return xx
  76. self.last_cmd = lst = []
  77. lst.extend(to_list(env['SCALAC']))
  78. lst.extend(['-classpath'])
  79. lst.extend(to_list(env['CLASSPATH']))
  80. lst.extend(['-d'])
  81. lst.extend(to_list(env['OUTDIR']))
  82. lst.extend(to_list(env['SCALACFLAGS']))
  83. lst.extend([a.abspath() for a in self.inputs])
  84. lst = [x for x in lst if x]
  85. try:
  86. self.out = self.generator.bld.cmd_and_log(lst, cwd=wd, env=env.env or None, output=0, quiet=0)[1]
  87. except:
  88. self.generator.bld.cmd_and_log(lst, cwd=wd, env=env.env or None)
  89. def configure(self):
  90. """
  91. Detect the scalac program
  92. """
  93. # If SCALA_HOME is set, we prepend it to the path list
  94. java_path = self.environ['PATH'].split(os.pathsep)
  95. v = self.env
  96. if 'SCALA_HOME' in self.environ:
  97. java_path = [os.path.join(self.environ['SCALA_HOME'], 'bin')] + java_path
  98. self.env['SCALA_HOME'] = [self.environ['SCALA_HOME']]
  99. for x in 'scalac scala'.split():
  100. self.find_program(x, var=x.upper(), path_list=java_path)
  101. if 'CLASSPATH' in self.environ:
  102. v['CLASSPATH'] = self.environ['CLASSPATH']
  103. v.SCALACFLAGS = ['-verbose']
  104. if not v['SCALAC']:
  105. self.fatal('scalac is required for compiling scala classes')