#! /bin/sh # Copyright 2016 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # List of environment variables that must be defined as input. # # We use environment variables instead of positional arguments or flags for # clarity in the caller .bzl file and for simplicity of processing. There is no # need to implement a full-blown argument parser for this simple script. global INPUT_VARS := '"JAR OUTPUT PREPROCESSOR PROTO_COMPILER SOURCE'" # Now set defaults for optional input variables. : $(PREPROCESSOR:=cat) # Basename of the script for error reporting purposes. global PROGRAM_NAME := $(0##*/) # A timestamp to mark all generated files with to get deterministic JAR outputs. global TIMESTAMP := '198001010000' # Prints an error and exits. # # Args: # ...: list(str). Parts of the message to print; all of them are joined # with a single space in between. proc err { echo "$(PROGRAM_NAME): $(*)" !1 > !2 exit 1 } # Entry point. proc main { test $(#) -eq 0 || err "No arguments allowed; set the following environment" \ "variables for configuration instead: $(INPUT_VARS)" for var in [$(INPUT_VARS)] { var value = '' eval "value=\"\$$(var)\"" test -n $(value) || err "Input environment variable $(var) is not set" } rm -f $(OUTPUT) var proto_output = ""$(OUTPUT).proto_output"" rm -rf $(proto_output) mkdir -p $(proto_output) # Apply desired preprocessing to the input proto file. For this to work, we # must maintain the name of the original .proto file or else the generated # classes in the JAR file would have an invalid name. var processed_dir = ""$(OUTPUT).preprocessed"" rm -rf $(processed_dir) mkdir -p $(processed_dir) var processed_source = ""$(processed_dir)/$[basename $(SOURCE)]"" $(PREPROCESSOR) <$(SOURCE) >$(processed_source) \ || err "Preprocessor $(PREPROCESSOR) failed" if test -n $(GRPC_JAVA_PLUGIN) { $(PROTO_COMPILER) --plugin=protoc-gen-grpc="$(GRPC_JAVA_PLUGIN)" \ --grpc_out="$(proto_output)" --java_out="$(proto_output)" $(processed_source) \ || err "proto_compiler failed" } else { $(PROTO_COMPILER) --java_out="$(proto_output)" $(processed_source) \ || err "proto_compiler failed" } find $(proto_output) -exec touch -t $(TIMESTAMP) '{}' ';' \ || err "Failed to reset timestamps" $(JAR) cMf "$(OUTPUT).tmp" -C $(proto_output) . \ || err "jar failed" mv "$(OUTPUT).tmp" $(OUTPUT) } main $(@) (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:INPUT_VARS) op: Equal rhs: {(DQ ("JAR OUTPUT PREPROCESSOR PROTO_COMPILER SOURCE"))} spids: [58] ) ] spids: [58] ) (C {(Lit_Other ":")} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonEquals arg_word:{(cat)}) spids: [70 74] ) ) } ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:PROGRAM_NAME) op: Equal rhs: { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_DPound arg_word:{("*") (Lit_Slash /)}) spids: [83 88] ) ) } spids: [81] ) ] spids: [81] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:TIMESTAMP) op:Equal rhs:{(198001010000)} spids:[95])] spids: [95] ) (FuncDef name: err body: (BraceGroup children: [ (SimpleCommand words: [{(echo)} {(DQ (${ VSub_Name PROGRAM_NAME) (": ") (${ VSub_Star "*"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:1 arg_word:{(2)} spids:[133])] ) (C {(exit)} {(1)}) ] spids: [118] ) spids: [114 117] ) (FuncDef name: main body: (BraceGroup children: [ (AndOr children: [ (C {(Lit_Other "[")} {(${ VSub_Pound "#")} {(-eq)} {(0)} {(Lit_Other "]")}) (C {(err)} {(DQ ("No arguments allowed; set the following environment"))} {(DQ ("variables for configuration instead: ") (${ VSub_Name INPUT_VARS))} ) ] op_id: Op_DPipe ) (ForEach iter_name: var iter_words: [{(${ VSub_Name INPUT_VARS)}] do_arg_iter: False body: (DoGroup children: [ (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:value) op:Equal spids:[200])] spids: [198] ) (C {(eval)} { (DQ ("value=") (EscapedLiteralPart token:) (EscapedLiteralPart token:) (${ VSub_Name var) (EscapedLiteralPart token:) ) } ) (AndOr children: [ (C {(Lit_Other "[")} {(-n)} {(DQ (${ VSub_Name value))} {(Lit_Other "]")}) (C {(err)} {(DQ ("Input environment variable ") (${ VSub_Name var) (" is not set"))} ) ] op_id: Op_DPipe ) ] spids: [195 241] ) spids: [189 193] ) (C {(rm)} {(-f)} {(DQ (${ VSub_Name OUTPUT))}) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:proto_output) op: Equal rhs: {(DQ (${ VSub_Name OUTPUT) (.proto_output))} spids: [259] ) ] spids: [257] ) (C {(rm)} {(-rf)} {(DQ (${ VSub_Name proto_output))}) (C {(mkdir)} {(-p)} {(DQ (${ VSub_Name proto_output))}) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:processed_dir) op: Equal rhs: {(DQ (${ VSub_Name OUTPUT) (.preprocessed))} spids: [305] ) ] spids: [303] ) (C {(rm)} {(-rf)} {(DQ (${ VSub_Name processed_dir))}) (C {(mkdir)} {(-p)} {(DQ (${ VSub_Name processed_dir))}) (Assignment keyword: Assign_Local pairs: [ (assign_pair lhs: (LhsName name:processed_source) op: Equal rhs: { (DQ (${ VSub_Name processed_dir) (/) (CommandSubPart command_list: (CommandList children: [(C {(basename)} {(DQ (${ VSub_Name SOURCE))})] ) left_token: spids: [344 352] ) ) } spids: [338] ) ] spids: [336] ) (AndOr children: [ (SimpleCommand words: [{(DQ (${ VSub_Name PREPROCESSOR))}] redirects: [ (Redir op_id: Redir_Less fd: -1 arg_word: {(DQ (${ VSub_Name SOURCE))} spids: [362] ) (Redir op_id: Redir_Great fd: -1 arg_word: {(DQ (${ VSub_Name processed_source))} spids: [369] ) ] ) (C {(err)} {(DQ ("Preprocessor ") (${ VSub_Name PREPROCESSOR) (" failed"))}) ] op_id: Op_DPipe ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(-n)} {(DQ (${ VSub_Name GRPC_JAVA_PLUGIN))} {(Lit_Other "]")} ) terminator: ) ] action: [ (AndOr children: [ (C {(DQ (${ VSub_Name PROTO_COMPILER))} {(--plugin) (Lit_Other "=") (protoc-gen-grpc) (Lit_Other "=") (DQ (${ VSub_Name GRPC_JAVA_PLUGIN)) } {(--grpc_out) (Lit_Other "=") (DQ (${ VSub_Name proto_output))} {(--java_out) (Lit_Other "=") (DQ (${ VSub_Name proto_output))} {(DQ (${ VSub_Name processed_source))} ) (C {(err)} {(DQ ("proto_compiler failed"))}) ] op_id: Op_DPipe ) ] spids: [-1 407] ) ] else_action: [ (AndOr children: [ (C {(DQ (${ VSub_Name PROTO_COMPILER))} {(--java_out) (Lit_Other "=") (DQ (${ VSub_Name proto_output))} {(DQ (${ VSub_Name processed_source))} ) (C {(err)} {(DQ ("proto_compiler failed"))}) ] op_id: Op_DPipe ) ] spids: [461 495] ) (AndOr children: [ (C {(find)} {(DQ (${ VSub_Name proto_output))} {(-exec)} {(touch)} {(-t)} {(DQ (${ VSub_Name TIMESTAMP))} {(SQ <"{}">)} {(EscapedLiteralPart token:)} ) (C {(err)} {(DQ ("Failed to reset timestamps"))}) ] op_id: Op_DPipe ) (AndOr children: [ (C {(DQ (${ VSub_Name JAR))} {(cMf)} {(DQ (${ VSub_Name OUTPUT) (.tmp))} {(-C)} {(DQ (${ VSub_Name proto_output))} {(.)} ) (C {(err)} {(DQ ("jar failed"))}) ] op_id: Op_DPipe ) (C {(mv)} {(DQ (${ VSub_Name OUTPUT) (.tmp))} {(DQ (${ VSub_Name OUTPUT))}) ] spids: [151] ) spids: [147 150] ) (C {(main)} {(DQ (${ VSub_At "@"))}) ] )