forked from jasder/antlr
(Visual C++) Added CMake option to link CRT
Previously Visual C++ users were forced to link CRT statically, i.e. use /MT flag whenever they want to use the static library. Linker have an error if user tries to link a /MT static library to a /MD executable. This commit defaults the build to statically link with CRT, but may be turned off if needed.
This commit is contained in:
parent
f296b75473
commit
526e6cdb9d
|
@ -20,6 +20,8 @@ if(NOT WITH_DEMO)
|
||||||
endif(NOT WITH_DEMO)
|
endif(NOT WITH_DEMO)
|
||||||
|
|
||||||
option(WITH_LIBCXX "Building with clang++ and libc++(in Linux). To enable with: -DWITH_LIBCXX=On" On)
|
option(WITH_LIBCXX "Building with clang++ and libc++(in Linux). To enable with: -DWITH_LIBCXX=On" On)
|
||||||
|
option(WITH_STATIC_CRT "(Visual C++) Enable to statically link CRT, which avoids requiring users to install the redistribution package.
|
||||||
|
To disable with: -DWITH_STATIC_CRT=Off" On)
|
||||||
|
|
||||||
project(LIBANTLR4)
|
project(LIBANTLR4)
|
||||||
|
|
||||||
|
|
|
@ -60,6 +60,10 @@ else()
|
||||||
--target)
|
--target)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(NOT DEFINED ANTLR4_WITH_STATIC_CRT)
|
||||||
|
set(ANTLR4_WITH_STATIC_CRT ON)
|
||||||
|
endif()
|
||||||
|
|
||||||
ExternalProject_Add(
|
ExternalProject_Add(
|
||||||
antlr4_runtime
|
antlr4_runtime
|
||||||
PREFIX antlr4_runtime
|
PREFIX antlr4_runtime
|
||||||
|
@ -72,6 +76,7 @@ ExternalProject_Add(
|
||||||
SOURCE_SUBDIR runtime/Cpp
|
SOURCE_SUBDIR runtime/Cpp
|
||||||
CMAKE_CACHE_ARGS
|
CMAKE_CACHE_ARGS
|
||||||
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
|
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
|
||||||
|
-DWITH_STATIC_CRT:BOOL=${ANTLR4_WITH_STATIC_CRT}
|
||||||
INSTALL_COMMAND ""
|
INSTALL_COMMAND ""
|
||||||
EXCLUDE_FROM_ALL 1)
|
EXCLUDE_FROM_ALL 1)
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,8 @@ set(CMAKE_CXX_STANDARD 11)
|
||||||
# required if linking to static library
|
# required if linking to static library
|
||||||
add_definitions(-DANTLR4CPP_STATIC)
|
add_definitions(-DANTLR4CPP_STATIC)
|
||||||
|
|
||||||
|
# using /MD flag for antlr4_runtime (for Visual C++ compilers only)
|
||||||
|
set(ANTLR4_WITH_STATIC_CRT OFF)
|
||||||
# add external build for antlrcpp
|
# add external build for antlrcpp
|
||||||
include(ExternalAntlr4Cpp)
|
include(ExternalAntlr4Cpp)
|
||||||
# add antrl4cpp artifacts to project environment
|
# add antrl4cpp artifacts to project environment
|
||||||
|
@ -114,6 +116,8 @@ ANTLR4_TAG - branch/tag used for building antlr4 library
|
||||||
```
|
```
|
||||||
`ANTLR4_TAG` is set to master branch by default to keep antlr4 updated. However, it will be required to rebuild after every `clean` is called. Set `ANTLR4_TAG` to a desired commit hash value to avoid rebuilding after every `clean` and keep the build stable, at the cost of not automatically update to latest commit.
|
`ANTLR4_TAG` is set to master branch by default to keep antlr4 updated. However, it will be required to rebuild after every `clean` is called. Set `ANTLR4_TAG` to a desired commit hash value to avoid rebuilding after every `clean` and keep the build stable, at the cost of not automatically update to latest commit.
|
||||||
|
|
||||||
|
Visual C++ compiler users may want to additionally define `ANTLR4_WITH_STATIC_CRT` before including the file. Set `ANTLR4_WITH_STATIC_CRT` to true if ANTLR4 C++ runtime library should be compiled with `/MT` flag, otherwise will be compiled with `/MD` flag. This variable has a default value of `OFF`. Changing `ANTLR4_WITH_STATIC_CRT` after building the library may require reinitialization of CMake or `clean` for the library to get rebuilt.
|
||||||
|
|
||||||
#### Examples
|
#### Examples
|
||||||
To build and link ANTLR4 static library to a target one may call:
|
To build and link ANTLR4 static library to a target one may call:
|
||||||
```cmake
|
```cmake
|
||||||
|
|
|
@ -64,8 +64,13 @@ if (WIN32)
|
||||||
set(extra_static_compile_flags "-DANTLR4CPP_STATIC")
|
set(extra_static_compile_flags "-DANTLR4CPP_STATIC")
|
||||||
endif(WIN32)
|
endif(WIN32)
|
||||||
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
|
||||||
target_compile_options(antlr4_shared PRIVATE "/MD$<$<CONFIG:Debug>:d>")
|
if(WITH_STATIC_CRT)
|
||||||
target_compile_options(antlr4_static PRIVATE "/MT$<$<CONFIG:Debug>:d>")
|
target_compile_options(antlr4_shared PRIVATE "/MT$<$<CONFIG:Debug>:d>")
|
||||||
|
target_compile_options(antlr4_static PRIVATE "/MT$<$<CONFIG:Debug>:d>")
|
||||||
|
else()
|
||||||
|
target_compile_options(antlr4_shared PRIVATE "/MD$<$<CONFIG:Debug>:d>")
|
||||||
|
target_compile_options(antlr4_static PRIVATE "/MD$<$<CONFIG:Debug>:d>")
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(static_lib_suffix "")
|
set(static_lib_suffix "")
|
||||||
|
|
Loading…
Reference in New Issue