Skip to content

Commit

Permalink
Compile go proto types (cadence-workflow#94)
Browse files Browse the repository at this point in the history
* Compile go proto types

* Fix build on M1 macs

* License header for generated files
  • Loading branch information
vytautas-karpavicius authored Nov 11, 2021
1 parent 2d25c6d commit 64d16b2
Show file tree
Hide file tree
Showing 32 changed files with 79,441 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Uber Technologies, Inc.
Copyright (c) 2021 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
62 changes: 59 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
.DEFAULT_GOAL := proto-lint
.DEFAULT_GOAL := all

# M1 macs may need to switch back to x86, until arm releases are available
EMULATE_X86 =
ifeq ($(shell uname -sm),Darwin arm64)
EMULATE_X86 = arch -x86_64
endif

OS = $(shell uname -s)
ARCH = $(shell $(EMULATE_X86) uname -m)

BIN := .bin
$(BIN):
Expand All @@ -7,8 +16,6 @@ $(BIN):
# https://docs.buf.build/
# changing BUF_VERSION will automatically download and use the specified version.
BUF_VERSION = 0.36.0
OS = $(shell uname -s)
ARCH = $(shell uname -m)
BUF_URL = https://github.com/bufbuild/buf/releases/download/v$(BUF_VERSION)/buf-$(OS)-$(ARCH)
# use BUF_VERSION_BIN as a bin prerequisite, not "buf", so the correct version will be used.
BUF_VERSION_BIN = buf-$(BUF_VERSION)
Expand All @@ -21,3 +28,52 @@ PROTO_ROOT := proto
PROTO_FILES = $(shell find ./$(PROTO_ROOT) -name "*.proto")
proto-lint: $(PROTO_FILES) $(BIN)/$(BUF_VERSION_BIN)
@$(BIN)/$(BUF_VERSION_BIN) lint

# https://www.grpc.io/docs/languages/go/quickstart/
# protoc-gen-gogofast (yarpc) are versioned via tools.go + go.mod (built above) and will be rebuilt as needed.
# changing PROTOC_VERSION will automatically download and use the specified version
PROTOC_VERSION = 3.14.0
PROTOC_URL = https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-$(subst Darwin,osx,$(OS))-$(ARCH).zip
# the zip contains an /include folder that we need to use to learn the well-known types
PROTOC_UNZIP_DIR = $(BIN)/protoc-$(PROTOC_VERSION)-zip
# use PROTOC_VERSION_BIN as a bin prerequisite, not "protoc", so the correct version will be used.
# otherwise this must be a .PHONY rule, or the buf bin / symlink could become out of date.
PROTOC_VERSION_BIN = protoc-$(PROTOC_VERSION)
$(BIN)/$(PROTOC_VERSION_BIN): | $(BIN)
@echo "downloading protoc $(PROTOC_VERSION)"
@# recover from partial success
@rm -rf $(BIN)/protoc.zip $(PROTOC_UNZIP_DIR)
@# download, unzip, copy to a normal location
@curl -sSL $(PROTOC_URL) -o $(BIN)/protoc.zip
@unzip -q $(BIN)/protoc.zip -d $(PROTOC_UNZIP_DIR)
@cp $(PROTOC_UNZIP_DIR)/bin/protoc $@

$(BIN)/protoc-gen-gogofast: go.mod | $(BIN)
go build -o $(BIN)/protoc-gen-gogofast github.com/gogo/protobuf/protoc-gen-gogofast

$(BIN)/protoc-gen-yarpc-go: go.mod | $(BIN)
go build -o $(BIN)/protoc-gen-yarpc-go go.uber.org/yarpc/encoding/protobuf/protoc-gen-yarpc-go

PROTO_GO_OUT := go/proto
LICENSE_GO := LICENSE.go
proto-go: $(PROTO_FILES) $(BIN)/$(PROTOC_VERSION_BIN) $(BIN)/protoc-gen-gogofast $(BIN)/protoc-gen-yarpc-go
@mkdir -p $(PROTO_GO_OUT)
@echo "protoc..."
@$(EMULATE_X86) $(BIN)/$(PROTOC_VERSION_BIN) \
--plugin $(BIN)/protoc-gen-gogofast \
--plugin $(BIN)/protoc-gen-yarpc-go \
-I=$(PROTO_ROOT) \
-I=$(PROTOC_UNZIP_DIR)/include \
--gogofast_out=Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/field_mask.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types,paths=source_relative:$(PROTO_GO_OUT) \
--yarpc-go_out=$(PROTO_GO_OUT) \
$(PROTO_FILES);
@rm -r $(PROTO_GO_OUT)/api
@mv $(PROTO_GO_OUT)/uber/cadence/api $(PROTO_GO_OUT)
@rm -r $(PROTO_GO_OUT)/uber

@sed 's/^/\/\/ /' LICENSE > $(LICENSE_GO)
@echo >> $(LICENSE_GO)
find $(PROTO_GO_OUT) -type f -exec sh -c 'cat $(LICENSE_GO) $$1 > $$1.tmp; mv $$1.tmp $$1' sh {} \;
@rm $(LICENSE_GO)

all: proto-lint proto-go
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/uber/cadence-idl

go 1.16

require (
github.com/gogo/protobuf v1.3.2
go.uber.org/fx v1.10.0
go.uber.org/yarpc v1.55.0
)
295 changes: 295 additions & 0 deletions go.sum

Large diffs are not rendered by default.

Loading

0 comments on commit 64d16b2

Please sign in to comment.