Skip to content

Commit

Permalink
bazel: build antlr parsers using bazel (#3927)
Browse files Browse the repository at this point in the history
- move antlr parsers to /antlr directory
- make building the parsers hermetic
- check them to "check generated" CI step
  • Loading branch information
sustrik authored Nov 9, 2020
1 parent 4e0284a commit 8b06407
Show file tree
Hide file tree
Showing 32 changed files with 97 additions and 237 deletions.
4 changes: 4 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ steps:
- ./tools/licenses.sh /tmp/test-artifacts/licenses $BUILDKITE_PIPELINE_SLUG
- diff -rNu3 /tmp/test-artifacts/licenses ./licenses/data
- ./tools/gomocks diff
- rm -rf /tmp/test-artifacts/antlr
- cp -R antlr/ /tmp/test-artifacts/antlr
- make antlr
- diff -ur /tmp/test-artifacts/antlr/ antlr/
timeout_in_minutes: 15
key: check_generated
retry:
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.PHONY: all bazel clean gazelle gogen licenses mocks protobuf setcap

.PHONY: all bazel clean gazelle gogen licenses mocks protobuf setcap antlr
.NOTPARALLEL:

GAZELLE_MODE?=fix
Expand All @@ -10,7 +11,7 @@ build: bazel
# is git controlled, and therefore this is only necessary when changing the
# sources for the code generation.
# Note: built in correct order, because .NOTPARALLEL.
all: go_deps.bzl gogen protobuf mocks gazelle licenses build
all: go_deps.bzl gogen protobuf mocks gazelle licenses build antlr

clean:
bazel clean
Expand Down Expand Up @@ -45,3 +46,6 @@ setcap:

licenses:
tools/licenses.sh

antlr:
antlr/generate.sh $(GAZELLE_MODE)
12 changes: 12 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

# Antlr rules
http_archive(
name = "rules_antlr",
sha256 = "26e6a83c665cf6c1093b628b3a749071322f0f70305d12ede30909695ed85591",
strip_prefix = "rules_antlr-0.5.0",
urls = ["https://github.com/marcohu/rules_antlr/archive/0.5.0.tar.gz"],
)

load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_dependencies")

rules_antlr_dependencies("4.7.2")

# Docker rules
http_archive(
name = "io_bazel_rules_docker",
Expand Down
15 changes: 15 additions & 0 deletions antlr/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_antlr//antlr:antlr4.bzl", "antlr")

antlr(
name = "traffic_class",
srcs = ["TrafficClass.g4"],
language = "Go",
package = "traffic_class",
)

antlr(
name = "sequence",
srcs = ["Sequence.g4"],
language = "Go",
package = "sequence",
)
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions antlr/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

GAZELLE_MODE=$1

ROOTDIR=$(dirname "$0")/..

GOSDK=$(bazel info output_base 2>/dev/null)/external/go_sdk/bin

function generate {
PACKAGE=$1

bazel build //antlr:${PACKAGE}
rm -rf ${ROOTDIR}/antlr/${PACKAGE}
mkdir ${ROOTDIR}/antlr/${PACKAGE}
cp -r ${ROOTDIR}/bazel-bin/antlr/${PACKAGE}.go/${PACKAGE}/* ${ROOTDIR}/antlr/${PACKAGE}
chmod 0644 ${ROOTDIR}/antlr/${PACKAGE}/*
${GOSDK}/gofmt -w antlr/${PACKAGE}/*.go
# Make the generated files deterministic.
sed -i '/Code generated from/c\// File generated by ANTLR. DO NOT EDIT.' ${ROOTDIR}/antlr/${PACKAGE}/*
}

generate traffic_class
generate sequence

bazel run //:gazelle -- update -mode=${GAZELLE_MODE} -index=false -external=external ${ROOTDIR}/antlr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
"sequence_listener.go",
"sequence_parser.go",
],
importpath = "github.com/scionproto/scion/go/lib/pathpol/sequence",
importpath = "github.com/scionproto/scion/antlr/sequence",
visibility = ["//visibility:public"],
deps = ["@com_github_antlr_antlr4//runtime/Go/antlr:go_default_library"],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from Sequence.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package sequence // Sequence
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from Sequence.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package sequence

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from Sequence.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package sequence // Sequence
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from Sequence.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package sequence // Sequence
import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
"trafficclass_listener.go",
"trafficclass_parser.go",
],
importpath = "github.com/scionproto/scion/go/lib/pktcls/traffic_class",
importpath = "github.com/scionproto/scion/antlr/traffic_class",
visibility = ["//visibility:public"],
deps = ["@com_github_antlr_antlr4//runtime/Go/antlr:go_default_library"],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from TrafficClass.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package traffic_class // TrafficClass
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from TrafficClass.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package traffic_class

Expand Down Expand Up @@ -138,8 +138,7 @@ func NewTrafficClassLexer(input antlr.CharStream) *TrafficClassLexer {
l := new(TrafficClassLexer)

l.BaseLexer = antlr.NewBaseLexer(input)
l.Interpreter = antlr.NewLexerATNSimulator(l, lexerAtn, lexerDecisionToDFA,
antlr.NewPredictionContextCache())
l.Interpreter = antlr.NewLexerATNSimulator(l, lexerAtn, lexerDecisionToDFA, antlr.NewPredictionContextCache())

l.channelNames = lexerChannelNames
l.modeNames = lexerModeNames
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from TrafficClass.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package traffic_class // TrafficClass
import "github.com/antlr/antlr4/runtime/Go/antlr"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Code generated from TrafficClass.g4 by ANTLR 4.7.1. DO NOT EDIT.
// File generated by ANTLR. DO NOT EDIT.

package traffic_class // TrafficClass
import (
Expand Down Expand Up @@ -85,8 +85,7 @@ func NewTrafficClassParser(input antlr.TokenStream) *TrafficClassParser {

this.BaseParser = antlr.NewBaseParser(input)

this.Interpreter = antlr.NewParserATNSimulator(this, deserializedATN, decisionToDFA,
antlr.NewPredictionContextCache())
this.Interpreter = antlr.NewParserATNSimulator(this, deserializedATN, decisionToDFA, antlr.NewPredictionContextCache())
this.RuleNames = ruleNames
this.LiteralNames = literalNames
this.SymbolicNames = symbolicNames
Expand Down Expand Up @@ -161,9 +160,7 @@ func NewEmptyMatchSrcContext() *MatchSrcContext {

func (*MatchSrcContext) IsMatchSrcContext() {}

func NewMatchSrcContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *MatchSrcContext {

func NewMatchSrcContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *MatchSrcContext {
var p = new(MatchSrcContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -266,9 +263,7 @@ func NewEmptyMatchDstContext() *MatchDstContext {

func (*MatchDstContext) IsMatchDstContext() {}

func NewMatchDstContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *MatchDstContext {

func NewMatchDstContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *MatchDstContext {
var p = new(MatchDstContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -371,9 +366,7 @@ func NewEmptyMatchDSCPContext() *MatchDSCPContext {

func (*MatchDSCPContext) IsMatchDSCPContext() {}

func NewMatchDSCPContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *MatchDSCPContext {

func NewMatchDSCPContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *MatchDSCPContext {
var p = new(MatchDSCPContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -488,9 +481,7 @@ func NewEmptyMatchTOSContext() *MatchTOSContext {

func (*MatchTOSContext) IsMatchTOSContext() {}

func NewMatchTOSContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *MatchTOSContext {

func NewMatchTOSContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *MatchTOSContext {
var p = new(MatchTOSContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -605,9 +596,7 @@ func NewEmptyCondClsContext() *CondClsContext {

func (*CondClsContext) IsCondClsContext() {}

func NewCondClsContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondClsContext {

func NewCondClsContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondClsContext {
var p = new(CondClsContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -702,9 +691,7 @@ func NewEmptyCondAnyContext() *CondAnyContext {

func (*CondAnyContext) IsCondAnyContext() {}

func NewCondAnyContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondAnyContext {

func NewCondAnyContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondAnyContext {
var p = new(CondAnyContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -849,9 +836,7 @@ func NewEmptyCondAllContext() *CondAllContext {

func (*CondAllContext) IsCondAllContext() {}

func NewCondAllContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondAllContext {

func NewCondAllContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondAllContext {
var p = new(CondAllContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -996,9 +981,7 @@ func NewEmptyCondNotContext() *CondNotContext {

func (*CondNotContext) IsCondNotContext() {}

func NewCondNotContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondNotContext {

func NewCondNotContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondNotContext {
var p = new(CondNotContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -1111,9 +1094,7 @@ func NewEmptyCondBoolContext() *CondBoolContext {

func (*CondBoolContext) IsCondBoolContext() {}

func NewCondBoolContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondBoolContext {

func NewCondBoolContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondBoolContext {
var p = new(CondBoolContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -1220,9 +1201,7 @@ func NewEmptyCondIPv4Context() *CondIPv4Context {

func (*CondIPv4Context) IsCondIPv4Context() {}

func NewCondIPv4Context(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondIPv4Context {

func NewCondIPv4Context(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondIPv4Context {
var p = new(CondIPv4Context)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -1379,9 +1358,7 @@ func NewEmptyCondContext() *CondContext {

func (*CondContext) IsCondContext() {}

func NewCondContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *CondContext {

func NewCondContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *CondContext {
var p = new(CondContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down Expand Up @@ -1519,9 +1496,7 @@ func (p *TrafficClassParser) Cond() (localctx ICondContext) {
p.CondNot()
}

case TrafficClassParserSRC, TrafficClassParserDST, TrafficClassParserDSCP,
TrafficClassParserTOS:

case TrafficClassParserSRC, TrafficClassParserDST, TrafficClassParserDSCP, TrafficClassParserTOS:
p.EnterOuterAlt(localctx, 4)
{
p.SetState(85)
Expand Down Expand Up @@ -1574,9 +1549,7 @@ func NewEmptyTrafficClassContext() *TrafficClassContext {

func (*TrafficClassContext) IsTrafficClassContext() {}

func NewTrafficClassContext(parser antlr.Parser, parent antlr.ParserRuleContext,
invokingState int) *TrafficClassContext {

func NewTrafficClassContext(parser antlr.Parser, parent antlr.ParserRuleContext, invokingState int) *TrafficClassContext {
var p = new(TrafficClassContext)

p.BaseParserRuleContext = antlr.NewBaseParserRuleContext(parent, invokingState)
Expand Down
2 changes: 1 addition & 1 deletion go/lib/pathpol/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ go_library(
importpath = "github.com/scionproto/scion/go/lib/pathpol",
visibility = ["//visibility:public"],
deps = [
"//antlr/sequence:go_default_library",
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/pathpol/sequence:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/lib/snet:go_default_library",
"@com_github_antlr_antlr4//runtime/Go/antlr:go_default_library",
Expand Down
4 changes: 0 additions & 4 deletions go/lib/pathpol/Makefile

This file was deleted.

8 changes: 0 additions & 8 deletions go/lib/pathpol/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion go/lib/pathpol/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

"github.com/antlr/antlr4/runtime/Go/antlr"

"github.com/scionproto/scion/antlr/sequence"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/pathpol/sequence"
"github.com/scionproto/scion/go/lib/snet"
)

Expand Down
Loading

0 comments on commit 8b06407

Please sign in to comment.