Skip to content

Commit 2460f90

Browse files
soiferjjroesch
authored andcommitted
[cmake][ANTLR] Support setting path to ANTLR jar (#4176)
* Support setting path to ANTLR jar * Update comment
1 parent f9b5b30 commit 2460f90

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include(cmake/util/FindCUDA.cmake)
77
include(cmake/util/FindVulkan.cmake)
88
include(cmake/util/FindLLVM.cmake)
99
include(cmake/util/FindROCM.cmake)
10+
include(cmake/util/FindANTLR.cmake)
1011

1112
if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/config.cmake)
1213
include(${CMAKE_CURRENT_BINARY_DIR}/config.cmake)

cmake/config.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ set(USE_ROCBLAS OFF)
144144
set(USE_SORT ON)
145145

146146
# Build ANTLR parser for Relay text format
147+
# Possible values:
148+
# - ON: enable ANTLR by searching default locations (cmake find_program for antlr4 and /usr/local for jar)
149+
# - OFF: disable ANTLR
150+
# - /path/to/antlr-*-complete.jar: path to specific ANTLR jar file
147151
set(USE_ANTLR OFF)
148152

149153
# Whether use Relay debug mode

cmake/modules/ANTLR.cmake

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
if(USE_ANTLR)
18-
find_program(ANTLR4 antlr4)
19-
20-
if (NOT ANTLR4)
21-
file(GLOB_RECURSE ANTLR4JAR
22-
/usr/local/lib/antlr-*-complete.jar
23-
/usr/local/Cellar/*antlr-*-complete.jar)
24-
25-
# Get the first element of the list of antlr jars.
26-
# Sort and reverse the list so the item selected is the highest
27-
# version in lib or else in Cellar if no lib installation exists.
28-
list(SORT ANTLR4JAR)
29-
list(REVERSE ANTLR4JAR)
30-
list(GET ANTLR4JAR 0 ANTLR4JAR)
31-
32-
set(JAVA_HOME $ENV{JAVA_HOME})
33-
if (NOT DEFINED JAVA_HOME)
34-
# Hack to get system to search for Java itself.
35-
set(JAVA_HOME "/usr")
36-
endif()
37-
38-
set(ANTLR4 ${JAVA_HOME}/bin/java -jar ${ANTLR4JAR})
39-
endif()
40-
18+
find_antlr(${USE_ANTLR})
4119
if(ANTLR4)
4220

4321
set(RELAY_PARSER_DIR

cmake/util/FindANTLR.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
#######################################################
19+
# Enhanced version of find ANTLR.
20+
#
21+
# Usage:
22+
# find_antlr(${USE_ANTLR})
23+
#
24+
# - When USE_ANTLR=ON, use auto search by first trying to find antlr4 program,
25+
# then trying to find antlr-*-complete.jar
26+
# - When USE_ANTLR=/path/to/antlr-*-complete.jar, use provided jar
27+
#
28+
# Provide variables:
29+
# - ANTLR4
30+
#
31+
macro(find_antlr use_antlr)
32+
set(JAVA_HOME $ENV{JAVA_HOME})
33+
if (NOT DEFINED JAVA_HOME)
34+
# Hack to get system to search for Java itself.
35+
message(STATUS "JAVA_HOME is not defined. Set it to ensure proper use")
36+
set(JAVA_HOME "/usr")
37+
endif()
38+
if(MSVC)
39+
set(JAVA_PROGRAM ${JAVA_HOME}/java.exe)
40+
else()
41+
set(JAVA_PROGRAM ${JAVA_HOME}/bin/java)
42+
endif()
43+
message(STATUS "Using Java at " ${JAVA_PROGRAM})
44+
45+
if (${use_antlr} STREQUAL "ON")
46+
find_program(ANTLR4 antlr4)
47+
if (NOT ANTLR4)
48+
file(GLOB_RECURSE ANTLR4JAR
49+
/usr/local/lib/antlr-*-complete.jar
50+
/usr/local/Cellar/*antlr-*-complete.jar)
51+
52+
# Get the first element of the list of antlr jars.
53+
# Sort and reverse the list so the item selected is the highest
54+
# version in lib or else in Cellar if no lib installation exists.
55+
list(SORT ANTLR4JAR)
56+
list(REVERSE ANTLR4JAR)
57+
list(GET ANTLR4JAR 0 ANTLR4JAR)
58+
59+
set(ANTLR4 ${JAVA_PROGRAM} -jar ${ANTLR4JAR})
60+
endif()
61+
elseif(NOT ${use_antlr} STREQUAL "OFF")
62+
set(ANTLR4 ${JAVA_PROGRAM} -jar ${use_antlr})
63+
endif()
64+
message(STATUS "ANTLR4="${ANTLR4})
65+
endmacro(find_antlr)

0 commit comments

Comments
 (0)