summaryrefslogtreecommitdiff
blob: 3d4f0e5b9450b5c2de258f182517a3c285ae077e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# Artifact ID for antlr-tool is 'antlr4'
# https://github.com/antlr/antlr4/blob/4.9.3/tool/pom.xml#L14
MAVEN_ID="org.antlr:antlr4:4.9.3"

JAVA_PKG_IUSE="doc source test"
JAVA_TESTING_FRAMEWORKS="junit-4"

inherit java-pkg-2 java-pkg-simple

MY_PN="${PN%-tool}"
MY_P="${MY_PN}-${PV}"

DESCRIPTION="The ANTLR 4 grammar compiler"
HOMEPAGE="https://www.antlr.org/"
# Maven Central sources JAR for tool/src/org/antlr/v4/unicode/UnicodeData.java,
# which requires string-template-maven-plugin to generate; tarball for the rest
SRC_URI="
	https://github.com/antlr/antlr4/archive/${PV}.tar.gz -> ${MY_P}.tar.gz
	https://repo1.maven.org/maven2/org/antlr/antlr4/${PV}/antlr4-${PV}-sources.jar -> ${P}-sources.jar
"

LICENSE="BSD"
SLOT="4"
KEYWORDS="amd64 ~arm arm64 ppc64 x86 ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~sparc-solaris ~sparc64-solaris ~x64-solaris ~x86-solaris"

CP_DEPEND="
	~dev-java/antlr-runtime-${PV}:${SLOT}
	dev-java/antlr-runtime:3.5
	dev-java/jakarta-json-api:1
	dev-java/icu4j:70
	dev-java/stringtemplate:4
	dev-java/treelayout:0
"

DEPEND="
	>=virtual/jdk-1.8:*
	${CP_DEPEND}
	dev-java/antlr-tool:3.5
	test? (
		dev-java/jol-core:0
	)
"

RDEPEND="
	>=virtual/jre-1.8:*
	${CP_DEPEND}
"

S="${WORKDIR}/${MY_PN}4-${PV}"

JAVA_SRC_DIR=(
	tool/src
	../src/org # For UnicodeData.java
)
JAVA_RESOURCE_DIRS=( tool/resources )

JAVA_TEST_GENTOO_CLASSPATH="junit-4,jol-core"
JAVA_TEST_SRC_DIR=(
	tool-testsuite/test
	# The runtime test suite is used as a library for tool test suite
	runtime-testsuite/{annotations,test}
)
JAVA_TEST_RESOURCE_DIRS=(
	# Some resources are mixed with test source files
	"${JAVA_TEST_SRC_DIR[0]}"
)

pkg_setup() {
	java-pkg-2_pkg_setup
	MY_JAVA="$(java-config -J)"
}

src_prepare() {
	java-pkg_clean
	eapply "${FILESDIR}/${PV}-test-fixes.patch"
	java-pkg-2_src_prepare
}

src_compile() {
	einfo "Generating ANTLR 3 parsers"
	"${MY_JAVA}" -cp "$(java-pkg_getjars \
		--build-only --with-dependencies antlr-tool-3.5)" \
		org.antlr.Tool $(find "${JAVA_SRC_DIR[@]}" -name "*.g") ||
		die "Failed to generate ANTLR 3 parsers"
	java-pkg-simple_src_compile
}

src_test() {
	# Build classpath for tests
	local test_deps=(
		"${JAVA_TEST_GENTOO_CLASSPATH}"
		antlr-runtime-${SLOT}
		antlr-runtime-3.5
		jakarta-json-api-1
		icu4j-70
		stringtemplate-4
		treelayout
	)
	local CP="${S}/${JAVA_JAR_FILENAME}"
	for test_dep in "${test_deps[@]}"; do
		CP+=":$(java-pkg_getjars --with-dependencies "${test_dep}")"
	done
	for res_dir in "${JAVA_TEST_RESOURCE_DIRS[@]}"; do
		CP+=":${res_dir}"
	done

	einfo "Generating ANTLR 4 parsers for tests"
	pushd runtime-testsuite/test > /dev/null ||
		die "Failed to enter runtime test source directory"
	g4_files=( $(find * -name "*.g4") )
	for file in "${g4_files[@]}"; do
		java_pkg="${file%/*.g4}"
		java_pkg="${java_pkg//\//.}"
		"${MY_JAVA}" -cp "${CP}" org.antlr.v4.Tool \
			-visitor -package "${java_pkg}" "${file}" ||
			die "Failed to generate ANTLR 4 parser from ${file}"
	done
	popd > /dev/null || die "Failed to leave runtime test source directory"

	# Compile Java test sources
	local classes="target/classes"
	ejavac -d "${classes}" -cp "${CP}" \
		$(find "${JAVA_TEST_SRC_DIR[@]}" -name "*.java")

	# Create a list of tests to run
	pushd "${JAVA_TEST_SRC_DIR[0]}" > /dev/null ||
		die "Failed to enter test source directory for ${PN}"
	local TESTS=$(find * -type f \
		\( -name "Test*.java" -o -name "*Test.java" \) \
		-not -name "BaseJavaToolTest.java" # No runnable methods
	)
	TESTS="${TESTS//.java}"
	TESTS="${TESTS//\//.}"
	popd > /dev/null || die "Failed to leave test source directory for ${PN}"

	ejunit4 -classpath "${classes}:${CP}" ${TESTS}
}