Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 1 | import random |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 2 | |
| 3 | DEBUG_ARG='JAVA_TOOL_OPTIONS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y"' |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 4 | FORCE_INSTALL=True |
| 5 | NONE='NONE' |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 6 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 7 | SRC = 'src/main/java/org/onosproject/**/' |
| 8 | TEST = 'src/test/java/org/onosproject/**/' |
| 9 | RESOURCES_ROOT = 'src/main/resources/' |
| 10 | |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 11 | ONOS_GROUP_ID = 'org.onosproject' |
| 12 | ONOS_VERSION = '1.6.0-SNAPSHOT' |
| 13 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 14 | def osgi_jar( |
| 15 | name, |
| 16 | srcs, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 17 | test_srcs = None, |
Brian O'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 18 | group_id = ONOS_GROUP_ID, |
| 19 | version = ONOS_VERSION, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 20 | deps = [], |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 21 | test_deps = [ '//lib:TEST' ], |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 22 | visibility = ['PUBLIC'], |
| 23 | license = 'NONE', |
| 24 | description = '', |
| 25 | debug = False, |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 26 | import_packages = '*', |
| 27 | export_packages = '*', |
| 28 | include_resources = NONE, |
| 29 | web_context = NONE, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 30 | resources = None, |
| 31 | resources_root = None, |
| 32 | test_resources = None, |
| 33 | test_resources_root = None, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 34 | **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'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 41 | if resources and not resources_root: |
| 42 | resources_root = RESOURCES_ROOT |
| 43 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 44 | java_library( |
| 45 | name = bare_jar_name, |
| 46 | srcs = srcs, |
| 47 | deps = deps, |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 48 | visibility = [], #intentially, not visible |
| 49 | resources = resources, |
| 50 | resources_root = resources_root, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 51 | **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'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 63 | "'%s'" % import_packages, #packages to import |
| 64 | "'%s'" % export_packages, #packages to export |
| 65 | include_resources, #custom includes to classpath |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 66 | 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'Connor | 1f16598 | 2016-04-06 21:36:09 -0700 | [diff] [blame] | 83 | out = '%s-%s.jar' % (name, version), #FIXME add version to jar file |
Brian O'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 84 | srcs = glob(['src/main/webapp/**']), |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 85 | 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'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 113 | 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'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 119 | 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'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 124 | genrule( |
| 125 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 126 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 127 | out = 'install.log', |
| 128 | visibility = visibility, |
| 129 | ) |
| 130 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 131 | |
| 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 | ) |