Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 1 | include_defs('//onos.defs') |
| 2 | include_defs('//bucklets/onos.bucklet') |
| 3 | |
| 4 | |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 5 | DEFAULT_PROTOC_VERSION = '3.2.0' |
| 6 | DEFAULT_GRPC_PLUGIN_VERSION = '1.3.0' |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 7 | |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 8 | PROTOC_RELEASE_BASE_URL = "https://github.com/google/protobuf/releases/download" |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 9 | GRPC_PLUGIN_BASE_URL = "https://repo1.maven.org/maven2/io/grpc/protoc-gen-grpc-java" |
| 10 | |
| 11 | PROTOC_SHA1S = { |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 12 | "protoc-3.2.0-linux-x86_64.zip":"f418d246d183a534d9bd749e614f639a55f6829b", |
| 13 | "protoc-3.2.0-osx-x86_64.zip":"81f4fac3200ba2cb12a98df0a9ee4d1c584e9210" |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | GRPC_JAVA_SHA1S = { |
| 17 | "protoc-gen-grpc-java-1.3.0-linux-x86_64.exe":"44a0fa3e6074852ea84f93d258233b3f4f6d9e53", |
| 18 | "protoc-gen-grpc-java-1.3.0-osx-x86_64.exe":"61a1b81b9f0af7d0900c314a4201972b52fb5f12" |
| 19 | } |
| 20 | |
| 21 | #Returns the string for the OS and architecture of the system of the form 'OS-ARCH' |
| 22 | def get_system_arch(): |
| 23 | import platform |
| 24 | os = platform.system().lower() |
| 25 | arch = platform.machine() |
| 26 | if os == "darwin": |
| 27 | os = "osx" |
| 28 | return "%s-%s" % ( os, arch) |
| 29 | |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 30 | def fetch_protoc( |
| 31 | version |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 32 | ): |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 33 | file_name = "protoc-%s-%s.zip" % (version, get_system_arch()) |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 34 | if file_name not in PROTOC_SHA1S: |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 35 | raise Exception('Cannot download %s, architecture or version not supported' % file_name) |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 36 | remote_file( |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 37 | name = 'protoc-release-' + version, |
| 38 | url = PROTOC_RELEASE_BASE_URL + '/v' + version + '/' + file_name, |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 39 | sha1 = PROTOC_SHA1S[file_name], |
| 40 | ) |
| 41 | genrule( |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 42 | name = 'protoc-exe-' + version, |
| 43 | bash = 'jar xf $(location :protoc-release-' + version + ') bin/protoc && ' + |
| 44 | 'mv bin/protoc $OUT && ' + |
| 45 | 'chmod +x $OUT', |
| 46 | out = 'protoc.exe', |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 47 | executable = True, |
| 48 | visibility = [ "PUBLIC" ], |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 49 | ) |
| 50 | genrule( |
| 51 | name = 'protoc-lib-' + version, |
| 52 | bash = 'jar xf $(location :protoc-release-' + version + ') include && mv include $OUT', |
| 53 | out = 'include', |
| 54 | visibility = [ "PUBLIC" ], |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 55 | ) |
| 56 | |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 57 | def fetch_grpc_plugin( |
| 58 | version |
| 59 | ): |
| 60 | file_name = "protoc-gen-grpc-java-%s-%s.exe" % (version, get_system_arch()) |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 61 | if file_name not in GRPC_JAVA_SHA1S: |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 62 | raise Exception('Cannot download %s, architecture or version not supported' % file_name) |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 63 | remote_file( |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 64 | name = 'grpc-plugin-binary-' + version, |
| 65 | url = GRPC_PLUGIN_BASE_URL + '/' + version + '/' + file_name, |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 66 | sha1 = GRPC_JAVA_SHA1S[file_name], |
| 67 | ) |
| 68 | genrule( |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 69 | name = 'grpc-plugin-exe-' + version, |
| 70 | bash = 'cp $(location :grpc-plugin-binary-' + version + ') $OUT && chmod +x $OUT', |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 71 | executable = True, |
| 72 | visibility = [ "PUBLIC" ], |
| 73 | out = 'grpc-plugin.exe', |
| 74 | ) |
| 75 | |
| 76 | def _get_name(): |
| 77 | base_path = get_base_path() |
| 78 | return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator |
| 79 | |
| 80 | def grpc_jar( |
| 81 | name = None, |
| 82 | deps = [], |
| 83 | #NOTE: if targeting a directory also built with maven this path MUST end in |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 84 | # /proto because maven plugin interprets imports relative to the proto |
| 85 | # directory and BUCK interprets imports relative to the last directory |
| 86 | # listed in the first listed proto_path which contains the specified |
| 87 | # file |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 88 | proto_paths = [], |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 89 | srcs = [], |
| 90 | src_string = '', # Useful to build proto files external to the ONOS sources, using BUCK's environment variables. |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 91 | proto_match_patterns = [ "**/proto/**/*.proto" ], |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 92 | protoc_version = DEFAULT_PROTOC_VERSION, |
| 93 | plugin_version = DEFAULT_GRPC_PLUGIN_VERSION, |
| 94 | include_std_lib = False, |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 95 | **kwargs |
| 96 | ): |
| 97 | |
| 98 | #Get the correct name for the protoc compilation call |
| 99 | if name is None: |
| 100 | name = _get_name() |
| 101 | |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 102 | # Create the string for the proto_path arguments (order matters, similar to classpath) |
| 103 | |
| 104 | if include_std_lib: |
| 105 | # Add protoc standard lib to the includes |
| 106 | proto_paths = ['$(location //incubator/protobuf-dependencies:protoc-lib-' + protoc_version + ')'] + proto_paths |
| 107 | |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 108 | if len(proto_paths) != 0: |
| 109 | proto_paths_string = "-I=" + reduce(lambda a,b: a +" -I=" + b, proto_paths) |
| 110 | else: |
| 111 | proto_paths_string = "" |
| 112 | protoc = name + '-protoc' |
| 113 | |
| 114 | genrule( |
| 115 | name = protoc, |
| 116 | srcs = glob(proto_match_patterns), |
| 117 | out = 'grpc.src.zip', |
| 118 | cmd = '$(location //buck-tools:grpc) $OUT ' |
| 119 | + '\"' + proto_paths_string + '\" ' |
Carmelo Cascone | 65f98ff | 2017-06-21 02:04:55 -0400 | [diff] [blame] | 120 | + '$(location //incubator/protobuf-dependencies:protoc-exe-'+ protoc_version + ') ' |
| 121 | + '$(location //incubator/grpc-dependencies:grpc-plugin-exe-' + plugin_version + ') ' |
| 122 | + '$SRCS' + src_string, |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 123 | ) |
| 124 | |
Aaron Kruglikov | 9f95f99 | 2017-06-23 14:15:25 +0900 | [diff] [blame] | 125 | osgi_jar( |
Aaron Kruglikov | e630fb1 | 2017-04-24 13:05:26 -0700 | [diff] [blame] | 126 | name = name, |
| 127 | srcs = [ ':' + protoc ], |
| 128 | deps = deps + [ ':' + protoc ], |
| 129 | do_javadocs = False, |
| 130 | do_checkstyle = False, |
| 131 | **kwargs |
| 132 | ) |