blob: d64ba162795ae03f6b088d409ff757d683816bfa [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
Brian O'Connor42c38cf2016-04-05 17:05:57 -070096 ### Checkstyle
97 chk_cmd = ' '.join(( 'java -jar $(location //lib:checkstyle)',
98 '-o $OUT',
99 '-c $(location //tools/build/conf:checkstyle-xml)',
100 ' '.join(srcs) ))
101 error_cmd = '(touch $OUT; cat $OUT | grep "^\[ERROR\]"; exit 1)'
102 cmd = ' || '.join((chk_cmd, error_cmd))
103 genrule(
104 name = name + '-checkstyle',
105 bash = cmd,
106 srcs = srcs,
107 out = 'checkstyle.log',
108 )
109
110 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700111 mvn_cmd = ' '.join(( 'mvn install:install-file',
112 '-Dfile=$(location :%s)' % name,
113 '-DgroupId=%s' % group_id,
114 '-DartifactId=%s' % name,
115 '-Dversion=%s' % version,
116 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700117 cmd = mvn_cmd + ' > $OUT'
118 if FORCE_INSTALL:
119 # Add a random number to the command to force this rule to run.
120 # TODO We should make this configurable from CLI, perhaps with a flag.
121 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700122 genrule(
123 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700124 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700125 out = 'install.log',
126 visibility = visibility,
127 )
128
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700129
130 if test_resources and not test_resources_root:
131 test_resources_root = RESOURCES_ROOT
132
Brian O'Connore124f3d2016-04-06 23:54:26 -0700133 if test_srcs is not None:
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700134 java_test(
135 name = 'tests',
136 srcs = test_srcs,
137 deps = deps +
138 test_deps +
139 [':' + bare_jar_name],
140 source_under_test = [':' + bare_jar_name],
141 resources = test_resources,
142 resources_root = test_resources_root
143 )