blob: 37a086490e0dcd9199d13ed4638ff8601ef242b3 [file] [log] [blame]
Brian O'Connorb86c9202016-04-05 20:15:04 -07001import random
Brian O'Connor42c38cf2016-04-05 17:05:57 -07002
3DEBUG_ARG='JAVA_TOOL_OPTIONS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y"'
Brian O'Connore5817c92016-04-06 15:41:48 -07004FORCE_INSTALL=True
5NONE='NONE'
Brian O'Connor42c38cf2016-04-05 17:05:57 -07006
Brian O'Connor0f6677d2016-04-06 23:26:51 -07007SRC = 'src/main/java/org/onosproject/**/'
8TEST = 'src/test/java/org/onosproject/**/'
9RESOURCES_ROOT = 'src/main/resources/'
10
Brian O'Connor1f165982016-04-06 21:36:09 -070011ONOS_GROUP_ID = 'org.onosproject'
12ONOS_VERSION = '1.6.0-SNAPSHOT'
13
Brian O'Connor42c38cf2016-04-05 17:05:57 -070014def osgi_jar(
15 name,
16 srcs,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070017 test_srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070018 group_id = ONOS_GROUP_ID,
19 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070020 deps = [],
Brian O'Connor0f6677d2016-04-06 23:26:51 -070021 test_deps = [ '//lib:TEST' ],
Brian O'Connor42c38cf2016-04-05 17:05:57 -070022 visibility = ['PUBLIC'],
23 license = 'NONE',
24 description = '',
25 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -070026 import_packages = '*',
27 export_packages = '*',
28 include_resources = NONE,
29 web_context = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070030 resources = None,
31 resources_root = None,
32 test_resources = None,
33 test_resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070034 **kwargs
35 ):
36
37 bare_jar_name = name + '-jar'
38 osgi_jar_name = name + '-osgi'
39 mvn_coords = group_id + ':' + name + ':' + version
40
Brian O'Connor0f6677d2016-04-06 23:26:51 -070041 if resources and not resources_root:
42 resources_root = RESOURCES_ROOT
43
Brian O'Connor42c38cf2016-04-05 17:05:57 -070044 java_library(
45 name = bare_jar_name,
46 srcs = srcs,
47 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -070048 visibility = [], #intentially, not visible
49 resources = resources,
50 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -070051 **kwargs
52 )
53
54 cp = ':'.join(['$(classpath %s)' % c for c in deps])
55
56 args = ( '$(location :%s)' % bare_jar_name, #input jar
57 '$OUT', #output jar
58 cp, #classpath
59 name, #bundle name
60 group_id, #group id
61 version, #version
62 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -070063 "'%s'" % import_packages, #packages to import
64 "'%s'" % export_packages, #packages to export
65 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -070066 web_context, #web context (REST API only)
67 description, #description
68 )
69
70 #TODO stage_jar is a horrendous hack
71 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
72 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
73 bash = stage_jar + wrap_jar
74 if debug:
75 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
76 print bash
77 # TODO this is a hack to add checkstyle as dependency before generating jar
78 bash = 'ls $(location :' + name + '-checkstyle) > /dev/null; ' + bash
79
80 genrule(
81 name = osgi_jar_name,
82 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -070083 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -070084 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -070085 visibility = [], #intentially, not visible
86 )
87
88 # TODO we really should shade the jar with maven flavor
89 prebuilt_jar(
90 name = name,
91 maven_coords = mvn_coords,
92 binary_jar = ':' + osgi_jar_name,
93 visibility = visibility,
94 )
95
96
97
98 ### Checkstyle
99 chk_cmd = ' '.join(( 'java -jar $(location //lib:checkstyle)',
100 '-o $OUT',
101 '-c $(location //tools/build/conf:checkstyle-xml)',
102 ' '.join(srcs) ))
103 error_cmd = '(touch $OUT; cat $OUT | grep "^\[ERROR\]"; exit 1)'
104 cmd = ' || '.join((chk_cmd, error_cmd))
105 genrule(
106 name = name + '-checkstyle',
107 bash = cmd,
108 srcs = srcs,
109 out = 'checkstyle.log',
110 )
111
112 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700113 mvn_cmd = ' '.join(( 'mvn install:install-file',
114 '-Dfile=$(location :%s)' % name,
115 '-DgroupId=%s' % group_id,
116 '-DartifactId=%s' % name,
117 '-Dversion=%s' % version,
118 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700119 cmd = mvn_cmd + ' > $OUT'
120 if FORCE_INSTALL:
121 # Add a random number to the command to force this rule to run.
122 # TODO We should make this configurable from CLI, perhaps with a flag.
123 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700124 genrule(
125 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700126 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700127 out = 'install.log',
128 visibility = visibility,
129 )
130
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700131
132 if test_resources and not test_resources_root:
133 test_resources_root = RESOURCES_ROOT
134
135 if test_srcs:
136 java_test(
137 name = 'tests',
138 srcs = test_srcs,
139 deps = deps +
140 test_deps +
141 [':' + bare_jar_name],
142 source_under_test = [':' + bare_jar_name],
143 resources = test_resources,
144 resources_root = test_resources_root
145 )