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 | |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 96 | ### 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'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 111 | 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'Connor | e5817c9 | 2016-04-06 15:41:48 -0700 | [diff] [blame] | 117 | 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'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 122 | genrule( |
| 123 | name = name + '-install', |
Brian O'Connor | b86c920 | 2016-04-05 20:15:04 -0700 | [diff] [blame] | 124 | bash = cmd, |
Brian O'Connor | 42c38cf | 2016-04-05 17:05:57 -0700 | [diff] [blame] | 125 | out = 'install.log', |
| 126 | visibility = visibility, |
| 127 | ) |
| 128 | |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 129 | |
| 130 | if test_resources and not test_resources_root: |
| 131 | test_resources_root = RESOURCES_ROOT |
| 132 | |
Brian O'Connor | e124f3d | 2016-04-06 23:54:26 -0700 | [diff] [blame^] | 133 | if test_srcs is not None: |
Brian O'Connor | 0f6677d | 2016-04-06 23:26:51 -0700 | [diff] [blame] | 134 | 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 | ) |