Home | History | Annotate | Download | only in precompiler
      1 # -*- mode: python; -*-
      2 # Copyright 2008 Google Inc. All Rights Reserved.
      3 
      4 # Use with
      5 # subinclude('//java/com/google/clearsilver/jsilver/precompiler:compile_cs')
      6 
      7 """compile_cs build target
      8 
      9 compile_cs(name, srcs)
     10 
     11 This rule produces generated Java source code that represents JSilver Template
     12 classes for rendering the given source CS files
     13 It'll output one .java file for each .cs file.
     14 
     15 Arguments
     16 
     17     * name: A unique name for this rule. (Name; required)
     18     * srcs: The list of cs files to pass to the code generator. (List of files,
     19             required)
     20 """
     21 
     22 def compile_cs(name, srcs, mode='none'):
     23   if not srcs:
     24     raise BadRule(None, '%s: srcs is empty' % name)
     25   if mode == 'none':
     26     suffix = '.java'
     27   else:
     28     suffix = '_' + mode + '.java'
     29   gen_java_files = [ file.replace('.cs', suffix) for file in srcs]
     30   input_file = 'gen_cs_' + name + '.in'
     31   output_file = 'gen_cs_' + name + '.out'
     32   map_file = name + '.map' 
     33   genrule(name = 'gen_cs_' + name,
     34           srcs = srcs,
     35           outs = [ map_file ] + gen_java_files,
     36           deps = [
     37                    '//java/com/google/clearsilver/jsilver/precompiler:BatchCompiler',
     38                  ],
     39           cmd = (
     40               'echo "$(SRCS)" > $(@D)/' + input_file + ' && '
     41               'echo "$(OUTS)" > $(@D)/' + output_file + ' && '
     42               '//java/com/google/clearsilver/jsilver/precompiler:BatchCompiler '
     43               '--src_list_file=$(@D)/' + input_file + ' '
     44               '--out_list_file=$(@D)/' + output_file + ' '
     45               '--escape_mode=' + mode
     46             )
     47         )
     48   java_library(name = name,
     49                srcs = gen_java_files,
     50                resources = [ map_file ],
     51                deps = [ '//java/com/google/clearsilver/jsilver/compiler' ]
     52               )
     53 
     54 
     55 """join_compiled_cs build target
     56 
     57 join_compiled_cs(name, deps)
     58 
     59 This rule merges multiple compile_cs output libraries and maps into one Java
     60 library and one map file that will be included as a system resource and can be
     61 read into the binary that wants to load the compiled template classes.
     62 
     63 Arguments
     64 
     65     * name: A unique name for this rule. (Name; required)
     66     * deps: The list of compile_cs BUILD targets to merge (List of labels,
     67             required)
     68 """
     69 
     70 def join_compiled_cs(name, deps):
     71   if not deps:
     72     raise BadRule(None, '%s: deps is empty' % name)
     73   map_files = [ file + '.map' for file in deps]
     74   joined_map_file = name + '.map'
     75   genrule(name = 'gen_' + joined_map_file,
     76           srcs = map_files,
     77           outs = [ joined_map_file ],
     78           cmd = ('cat $(SRCS) > $@')
     79          )
     80   java_library(name = name,
     81                resources = [ joined_map_file ],
     82                deps = deps)
     83