Skip to content

Commit

Permalink
Improve ExternalProject cmake file (#3697)
Browse files Browse the repository at this point in the history
* Set cmake CMP0144, if available

Signed-off-by: Robert Adam <dev@robert-adam.de>

* Add include dirs directly to defined targets

By directly associating the defined antlr_* targets with the respective
include directories, we make sure that any user that links against one
of these targets, automatically has the necessary directories in their
include path as well (without the need to explicitly specify them) as
cmake will transitively propagate the interface include dirs to the
linked-to target.

Signed-off-by: Robert Adam <dev@robert-adam.de>
  • Loading branch information
Krzmbrzl authored May 7, 2022
1 parent a9e0ccf commit 7e8a8b5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
cmake_minimum_required(VERSION 3.7)

if(POLICY CMP0114)
cmake_policy(SET CMP0114 NEW)
endif()

include(ExternalProject)

set(ANTLR4_ROOT ${CMAKE_CURRENT_BINARY_DIR}/antlr4_runtime/src/antlr4_runtime)
Expand All @@ -11,6 +15,10 @@ if(NOT DEFINED ANTLR4_TAG)
set(ANTLR4_TAG master)
endif()

# Ensure that the include dir already exists at configure time (to avoid cmake erroring
# on non-existent include dirs)
file(MAKE_DIRECTORY "${ANTLR4_INCLUDE_DIRS}")

if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*")
set(ANTLR4_OUTPUT_DIR ${ANTLR4_ROOT}/runtime/Cpp/dist/$(Configuration))
elseif(${CMAKE_GENERATOR} MATCHES "Xcode.*")
Expand Down Expand Up @@ -135,6 +143,10 @@ add_library(antlr4_static STATIC IMPORTED)
add_dependencies(antlr4_static antlr4_runtime-build_static)
set_target_properties(antlr4_static PROPERTIES
IMPORTED_LOCATION ${ANTLR4_STATIC_LIBRARIES})
target_include_directories(antlr4_static
INTERFACE
${ANTLR4_INCLUDE_DIRS}
)

ExternalProject_Add_Step(
antlr4_runtime
Expand All @@ -152,6 +164,11 @@ add_library(antlr4_shared SHARED IMPORTED)
add_dependencies(antlr4_shared antlr4_runtime-build_shared)
set_target_properties(antlr4_shared PROPERTIES
IMPORTED_LOCATION ${ANTLR4_RUNTIME_LIBRARIES})
target_include_directories(antlr4_shared
INTERFACE
${ANTLR4_INCLUDE_DIRS}
)

if(ANTLR4_SHARED_LIBRARIES)
set_target_properties(antlr4_shared PROPERTIES
IMPORTED_IMPLIB ${ANTLR4_SHARED_LIBRARIES})
Expand Down

0 comments on commit 7e8a8b5

Please sign in to comment.