]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/cmake/ExternalAntlr4Cpp.cmake
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / cmake / ExternalAntlr4Cpp.cmake
1 cmake_minimum_required(VERSION 3.7)
2
3 include(ExternalProject)
4
5 set(ANTLR4_ROOT ${CMAKE_CURRENT_BINARY_DIR}/antlr4_runtime/src/antlr4_runtime)
6 set(ANTLR4_INCLUDE_DIRS ${ANTLR4_ROOT}/runtime/Cpp/runtime/src)
7 set(ANTLR4_GIT_REPOSITORY https://github.com/antlr/antlr4.git)
8 if(NOT DEFINED ANTLR4_TAG)
9   # Set to branch name to keep library updated at the cost of needing to rebuild after 'clean'
10   # Set to commit hash to keep the build stable and does not need to rebuild after 'clean'
11   set(ANTLR4_TAG master)
12 endif()
13
14 if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
15   set(ANTLR4_OUTPUT_DIR ${ANTLR4_ROOT}/runtime/Cpp/dist/$(Configuration))
16 elseif(${CMAKE_GENERATOR} MATCHES "Xcode.*")
17   set(ANTLR4_OUTPUT_DIR ${ANTLR4_ROOT}/runtime/Cpp/dist/$(CONFIGURATION))
18 else()
19   set(ANTLR4_OUTPUT_DIR ${ANTLR4_ROOT}/runtime/Cpp/dist)
20 endif()
21
22 if(MSVC)
23   set(ANTLR4_STATIC_LIBRARIES
24       ${ANTLR4_OUTPUT_DIR}/antlr4-runtime-static.lib)
25   set(ANTLR4_SHARED_LIBRARIES
26       ${ANTLR4_OUTPUT_DIR}/antlr4-runtime.lib)
27   set(ANTLR4_RUNTIME_LIBRARIES
28       ${ANTLR4_OUTPUT_DIR}/antlr4-runtime.dll)
29 else()
30   set(ANTLR4_STATIC_LIBRARIES
31       ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.a)
32   if(MINGW)
33     set(ANTLR4_SHARED_LIBRARIES
34         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.dll.a)
35     set(ANTLR4_RUNTIME_LIBRARIES
36         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.dll)
37   elseif(CYGWIN)
38     set(ANTLR4_SHARED_LIBRARIES
39         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.dll.a)
40     set(ANTLR4_RUNTIME_LIBRARIES
41         ${ANTLR4_OUTPUT_DIR}/cygantlr4-runtime-4.9.2.dll)
42   elseif(APPLE)
43     set(ANTLR4_RUNTIME_LIBRARIES
44         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.dylib)
45   else()
46     set(ANTLR4_RUNTIME_LIBRARIES
47         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.so)
48   endif()
49 endif()
50
51 if(${CMAKE_GENERATOR} MATCHES ".* Makefiles")
52   # This avoids
53   # 'warning: jobserver unavailable: using -j1. Add '+' to parent make rule.'
54   set(ANTLR4_BUILD_COMMAND $(MAKE))
55 elseif(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
56   set(ANTLR4_BUILD_COMMAND
57       ${CMAKE_COMMAND}
58           --build .
59           --config $(Configuration)
60           --target)
61 elseif(${CMAKE_GENERATOR} MATCHES "Xcode.*")
62   set(ANTLR4_BUILD_COMMAND
63       ${CMAKE_COMMAND}
64           --build .
65           --config $(CONFIGURATION)
66           --target)
67 else()
68   set(ANTLR4_BUILD_COMMAND
69       ${CMAKE_COMMAND}
70           --build .
71           --target)
72 endif()
73
74 if(NOT DEFINED ANTLR4_WITH_STATIC_CRT)
75   set(ANTLR4_WITH_STATIC_CRT ON)
76 endif()
77
78 if(ANTLR4_ZIP_REPOSITORY)
79   ExternalProject_Add(
80       antlr4_runtime
81       PREFIX antlr4_runtime
82       URL ${ANTLR4_ZIP_REPOSITORY}
83       DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}
84       BUILD_COMMAND ""
85       BUILD_IN_SOURCE 1
86       SOURCE_DIR ${ANTLR4_ROOT}
87       SOURCE_SUBDIR runtime/Cpp
88       CMAKE_CACHE_ARGS
89           -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
90           -DWITH_STATIC_CRT:BOOL=${ANTLR4_WITH_STATIC_CRT}
91           # -DCMAKE_CXX_STANDARD:STRING=17 # if desired, compile the runtime with a different C++ standard
92           # -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD} # alternatively, compile the runtime with the same C++ standard as the outer project
93       INSTALL_COMMAND ""
94       EXCLUDE_FROM_ALL 1)
95 else()
96   ExternalProject_Add(
97       antlr4_runtime
98       PREFIX antlr4_runtime
99       GIT_REPOSITORY ${ANTLR4_GIT_REPOSITORY}
100       GIT_TAG ${ANTLR4_TAG}
101       DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}
102       BUILD_COMMAND ""
103       BUILD_IN_SOURCE 1
104       SOURCE_DIR ${ANTLR4_ROOT}
105       SOURCE_SUBDIR runtime/Cpp
106       CMAKE_CACHE_ARGS
107           -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
108           -DWITH_STATIC_CRT:BOOL=${ANTLR4_WITH_STATIC_CRT}
109           # -DCMAKE_CXX_STANDARD:STRING=17 # if desired, compile the runtime with a different C++ standard
110           # -DCMAKE_CXX_STANDARD:STRING=${CMAKE_CXX_STANDARD} # alternatively, compile the runtime with the same C++ standard as the outer project
111       INSTALL_COMMAND ""
112       EXCLUDE_FROM_ALL 1)
113 endif()
114
115 # Seperate build step as rarely people want both
116 set(ANTLR4_BUILD_DIR ${ANTLR4_ROOT})
117 if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14.0")
118   # CMake 3.14 builds in above's SOURCE_SUBDIR when BUILD_IN_SOURCE is true
119   set(ANTLR4_BUILD_DIR ${ANTLR4_ROOT}/runtime/Cpp)
120 endif()
121
122 ExternalProject_Add_Step(
123     antlr4_runtime
124     build_static
125     COMMAND ${ANTLR4_BUILD_COMMAND} antlr4_static
126     # Depend on target instead of step (a custom command)
127     # to avoid running dependent steps concurrently
128     DEPENDS antlr4_runtime
129     BYPRODUCTS ${ANTLR4_STATIC_LIBRARIES}
130     EXCLUDE_FROM_MAIN 1
131     WORKING_DIRECTORY ${ANTLR4_BUILD_DIR})
132 ExternalProject_Add_StepTargets(antlr4_runtime build_static)
133
134 add_library(antlr4_static STATIC IMPORTED)
135 add_dependencies(antlr4_static antlr4_runtime-build_static)
136 set_target_properties(antlr4_static PROPERTIES
137                       IMPORTED_LOCATION ${ANTLR4_STATIC_LIBRARIES})
138
139 ExternalProject_Add_Step(
140     antlr4_runtime
141     build_shared
142     COMMAND ${ANTLR4_BUILD_COMMAND} antlr4_shared
143     # Depend on target instead of step (a custom command)
144     # to avoid running dependent steps concurrently
145     DEPENDS antlr4_runtime
146     BYPRODUCTS ${ANTLR4_SHARED_LIBRARIES} ${ANTLR4_RUNTIME_LIBRARIES}
147     EXCLUDE_FROM_MAIN 1
148     WORKING_DIRECTORY ${ANTLR4_BUILD_DIR})
149 ExternalProject_Add_StepTargets(antlr4_runtime build_shared)
150
151 add_library(antlr4_shared SHARED IMPORTED)
152 add_dependencies(antlr4_shared antlr4_runtime-build_shared)
153 set_target_properties(antlr4_shared PROPERTIES
154                       IMPORTED_LOCATION ${ANTLR4_RUNTIME_LIBRARIES})
155 if(ANTLR4_SHARED_LIBRARIES)
156   set_target_properties(antlr4_shared PROPERTIES
157                         IMPORTED_IMPLIB ${ANTLR4_SHARED_LIBRARIES})
158 endif()