Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 1 | # -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- |
| 2 | |
| 3 | VERSION='0.1' |
| 4 | APPNAME='ndnjs' |
| 5 | |
| 6 | YUICOMPRESSOR_URL="http://github.com/downloads/yui/yuicompressor/" |
| 7 | YUICOMPRESSOR_NAME="yuicompressor-2.4.7" |
| 8 | |
| 9 | CLOSURE_COMPILER="http://closure-compiler.googlecode.com/files/compiler-20121212.zip" |
| 10 | |
| 11 | from waflib import Task, TaskGen, Utils, Logs |
| 12 | import urllib, subprocess, os, shutil |
| 13 | |
| 14 | def options (opt): |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 15 | js = opt.add_option_group ("ndn.js compilation options") |
| 16 | |
| 17 | js.add_option('--no-js',action='store_false',default=True,dest='js', |
| 18 | help='''Disable ndn.js compilation and installation''') |
Alexander Afanasyev | 7866474 | 2013-02-28 19:40:49 -0800 | [diff] [blame] | 19 | js.add_option('--js-dir',action='store',dest='jsdir', |
| 20 | help='''Directory where .js files will be installed (Default: ${PREFIX}/share/ndn-js)''') |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 21 | js.add_option('--yui',action='store_true',default=False,dest='yui', |
| 22 | help='''Download and use yuicompressor-2.4.7 (http://yui.github.com/yuicompressor/)''') |
| 23 | js.add_option('--compiler',action='store_true',default=False,dest='compiler', |
| 24 | help='''Download and use closure-compiler-r2388 (https://code.google.com/p/closure-compiler/)''') |
| 25 | |
| 26 | ws = opt.add_option_group ("ws-proxy options") |
| 27 | ws.add_option ('--no-ws',action='store_false',default=True,dest='ws', |
| 28 | help='''Disable ws-proxy installation''') |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 29 | |
| 30 | def configure (conf): |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 31 | if conf.options.js: |
| 32 | conf.env.JS = 1 |
Alexander Afanasyev | 7866474 | 2013-02-28 19:40:49 -0800 | [diff] [blame] | 33 | if not conf.env.JSDIR: |
| 34 | if conf.options.jsdir or Utils.is_win32: |
| 35 | conf.env.JSDIR=os.path.abspath (os.path.expanduser (conf.options.jsdir)) |
| 36 | else: |
| 37 | conf.env.JSDIR=Utils.subst_vars ('${PREFIX}/bin',conf.env) |
| 38 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 39 | if conf.options.yui: |
| 40 | conf.start_msg ("Checking for yuicompressor") |
| 41 | if os.path.exists('tools/yuicompressor.jar'): |
| 42 | conf.end_msg('tools/yuicompressor.jar') |
| 43 | conf.env.HAVE_YUI = 1 |
| 44 | else: |
| 45 | conf.end_msg('not found','YELLOW') |
| 46 | url="%s%s.zip" % (YUICOMPRESSOR_URL, YUICOMPRESSOR_NAME) |
| 47 | Logs.info ("Downloading yuicompressor from %s..." % url) |
| 48 | urllib.urlretrieve (url, "build/yuicompressor.zip") |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 50 | filename='%s/build/%s.jar' % (YUICOMPRESSOR_NAME,YUICOMPRESSOR_NAME) |
| 51 | subprocess.check_call (['unzip', '-qq', '-o', '-j', 'build/yuicompressor.zip', |
| 52 | filename, '-d', 'tools/']) |
| 53 | os.rename ("tools/%s.jar" % YUICOMPRESSOR_NAME, "tools/yuicompressor.jar") |
| 54 | conf.env.HAVE_YUI = 1 |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 56 | if conf.options.compiler: |
| 57 | conf.start_msg ("Checking for closure-compiler") |
| 58 | if os.path.exists('tools/compiler.jar'): |
| 59 | conf.end_msg('tools/compiler.jar') |
| 60 | conf.env.HAVE_COMPILER = 1 |
| 61 | else: |
| 62 | conf.end_msg('not found','YELLOW') |
| 63 | Logs.info ("Downloading closure-compiler from %s..." % CLOSURE_COMPILER) |
| 64 | urllib.urlretrieve (CLOSURE_COMPILER, "build/closure-compiler.zip") |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 66 | subprocess.check_call (['unzip', '-qq', '-o', '-j', 'build/closure-compiler.zip', 'compiler.jar', '-d', 'tools/']) |
| 67 | conf.env.HAVE_COMPILER = 1 |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 69 | if conf.options.ws: |
| 70 | conf.env.WS = 1 |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 181a8b9 | 2013-02-28 13:28:53 -0800 | [diff] [blame] | 72 | def build (bld): |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 73 | if bld.env['JS']: |
| 74 | securityLib = ["contrib/securityLib/sha256.js", |
| 75 | "contrib/securityLib/base64.js", |
| 76 | "contrib/securityLib/rsa.js", |
| 77 | "contrib/securityLib/rsa2.js", |
| 78 | "contrib/securityLib/rsapem-1.1.js", |
| 79 | "contrib/securityLib/rsasign-1.2.js", |
| 80 | "contrib/securityLib/asn1hex-1.1.js", |
| 81 | "contrib/securityLib/x509-1.1.js", |
| 82 | "contrib/securityLib/jsbn.js", |
| 83 | "contrib/securityLib/jsbn2.js"] |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 85 | ndnjs = ["js/Closure.js", |
| 86 | "js/WebSocketTransport.js", |
| 87 | "js/util/CCNProtocolDTags.js", |
| 88 | "js/util/CCNTime.js", |
| 89 | "js/util/ExponentialReExpressClosure.js", |
| 90 | "js/Name.js", |
| 91 | "js/ContentObject.js", |
| 92 | "js/encoding/DateFormat.js", |
| 93 | "js/Interest.js", |
| 94 | "js/Key.js", |
| 95 | "js/PublisherID.js", |
| 96 | "js/PublisherPublicKeyDigest.js", |
| 97 | "js/FaceInstance.js", |
| 98 | "js/ForwardingEntry.js", |
| 99 | "js/encoding/DynamicUint8Array.js", |
| 100 | "js/encoding/BinaryXMLEncoder.js", |
| 101 | "js/encoding/BinaryXMLDecoder.js", |
| 102 | "js/encoding/BinaryXMLStructureDecoder.js", |
| 103 | "js/encoding/DataUtils.js", |
| 104 | "js/encoding/EncodingUtils.js", |
| 105 | "js/security/KeyManager.js", |
| 106 | "js/security/Witness.js"] + securityLib + ["js/NDN.js"] |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 108 | ndnjs = bld (features="combine", |
Alexander Afanasyev | ad65962 | 2013-03-02 13:50:58 -0800 | [diff] [blame] | 109 | target="ndn-js", |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 110 | source=ndnjs, |
Alexander Afanasyev | 7866474 | 2013-02-28 19:40:49 -0800 | [diff] [blame] | 111 | install_path="${JSDIR}") |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 113 | if bld.env['HAVE_YUI']: |
| 114 | ndnjs.yui = True |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 116 | if bld.env['HAVE_COMPILER']: |
| 117 | ndnjs.compiler = True |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 119 | if bld.env['WS']: |
Alexander Afanasyev | 7866474 | 2013-02-28 19:40:49 -0800 | [diff] [blame] | 120 | bld.install_as ('${BINDIR}/ccnx-wsproxy-tcp.js', 'wsproxy/wsproxy-tcp.js', chmod=Utils.O755) |
| 121 | bld.install_as ('${BINDIR}/ccnx-wsproxy-udp.js', 'wsproxy/wsproxy-udp.js', chmod=Utils.O755) |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 122 | |
| 123 | @TaskGen.extension('.js') |
| 124 | def js_hook(self, node): |
| 125 | node.sig=Utils.h_file (node.abspath()) |
| 126 | |
| 127 | @TaskGen.feature('combine') |
| 128 | @TaskGen.after_method('process_source') |
| 129 | def apply_combine(self): |
| 130 | out = "%s.js" % self.target |
| 131 | tasks = [] |
| 132 | task = self.create_task ('combine', self.source) |
| 133 | task.set_outputs (task.generator.path.find_or_declare (out)) |
| 134 | tasks.append (task) |
| 135 | |
| 136 | if getattr(self, 'yui', False): |
| 137 | out_yui = "%s.min.yui.js" % self.target |
| 138 | yui = self.create_task ('yuicompressor') |
| 139 | yui.combine = task |
| 140 | yui.set_outputs (yui.generator.path.find_or_declare (out_yui)) |
| 141 | tasks.append (yui) |
| 142 | |
| 143 | if getattr(self, 'compiler', False): |
| 144 | out_min = "%s.min.js" % self.target |
| 145 | compiler = self.create_task ('closure_compiler') |
| 146 | compiler.combine = task |
Alexander Afanasyev | 57dce8b | 2013-02-28 19:20:21 -0800 | [diff] [blame] | 147 | compiler.set_outputs (compiler.generator.path.find_or_declare (out_min)) |
Alexander Afanasyev | f6b7dd4 | 2013-02-28 16:38:59 -0800 | [diff] [blame] | 148 | tasks.append (compiler) |
| 149 | |
| 150 | try: |
| 151 | for task in tasks: |
| 152 | self.bld.install_files (self.install_path, task.outputs[:], env=self.env) |
| 153 | except: |
| 154 | pass |
| 155 | |
| 156 | class combine (Task.Task): |
| 157 | def run(self): |
| 158 | outFile = self.outputs[0] |
| 159 | self.outputs[0].write ("", "w") # make file empty |
| 160 | for inFile in self.inputs: |
| 161 | self.outputs[0].write (inFile.read (), 'a') |
| 162 | |
| 163 | class yuicompressor (Task.Task): |
| 164 | after="combine" |
| 165 | color='PINK' |
| 166 | def __str__(self): |
| 167 | src_str=self.combine.outputs[0].nice_path() |
| 168 | tgt_str=self.outputs[0].nice_path() |
| 169 | return'%s: %s -> %s\n'%(self.__class__.__name__.replace('_task',''),src_str,tgt_str) |
| 170 | |
| 171 | def run(self): |
| 172 | return self.exec_command(['java', |
| 173 | '-jar', '../tools/yuicompressor.jar', |
| 174 | '-o', self.outputs[0].abspath(), |
| 175 | self.combine.outputs[0].abspath()]) |
| 176 | |
| 177 | class closure_compiler (Task.Task): |
| 178 | after="combine" |
| 179 | color='PINK' |
| 180 | def __str__(self): |
| 181 | src_str=self.combine.outputs[0].nice_path() |
| 182 | tgt_str=self.outputs[0].nice_path() |
| 183 | return'%s: %s -> %s\n'%(self.__class__.__name__.replace('_task',''),src_str,tgt_str) |
| 184 | |
| 185 | def run(self): |
| 186 | return self.exec_command(['java', |
| 187 | '-jar', '../tools/compiler.jar', |
| 188 | '--js', self.combine.outputs[0].abspath(), |
| 189 | '--js_output_file', self.outputs[0].abspath()]) |