Skip to content

Commit

Permalink
feat: Make main target installable. (#103)
Browse files Browse the repository at this point in the history
This allows to run `cmake --install .` to install the project to a
system such that it can be consumed with `find_package()`. This required
the following changes:

* Add `install(TARGET ...)` commands for all library and executable
targets.
* Add all public header files to the `HEADER` `FILE_SET` of their
respective target.
* Export all these targets to an export set and `install(EXPORT ...)`
that export set.
* Provide install-time paths for several `target_include_directories()`,
  some of which are empty because the headers are not installed.

Since some of the library targets depend on external projects
(`abseil-cpp`
and `yaml-cpp`), the install logic of these projects also needs to be
enabled.

This change is not only necessary to actually install the project, but
also to consume it via `add_subdirectory` from any project that,
itself, is installable. (All dependencies of installable targets must be
installable.)
  • Loading branch information
ingomueller-net committed Mar 18, 2024
1 parent 579d884 commit cc8d08a
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ if(${SUBSTRAIT_CPP_BUILD_TESTING})
enable_testing()
endif()

install(EXPORT SubstraitTargets DESTINATION lib/cmake/Substrait)

add_subdirectory(src/substrait)
add_subdirectory(export)
7 changes: 2 additions & 5 deletions export/planloader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ add_library(planloader SHARED planloader.cpp)
add_dependencies(planloader substrait_io)
target_link_libraries(planloader substrait_io)

install(
TARGETS planloader
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PRIVATE_HEADER DESTINATION ${CMAKE_INSTALL_INCDIR})

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(TARGETS planloader EXPORT SubstraitTargets)
15 changes: 11 additions & 4 deletions src/substrait/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# SPDX-License-Identifier: Apache-2.0

add_library(substrait_common Exceptions.cpp)
target_sources(
substrait_common PUBLIC FILE_SET HEADERS BASE_DIRS ../../../include/ FILES
../../../include/substrait/common/Exceptions.h)
target_link_libraries(substrait_common fmt::fmt-header-only)

add_library(substrait_io STATIC Io.cpp)
target_sources(substrait_io PUBLIC FILE_SET HEADERS BASE_DIRS ../../../include/
FILES ../../../include/substrait/common/Io.h)

add_dependencies(
substrait_io
substrait_proto
Expand All @@ -24,10 +30,11 @@ if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(TARGETS substrait_io LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ../../../include/substrait/common/Io.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/substrait/common")

add_executable(plantransformer PlanTransformerTool.cpp)

target_link_libraries(plantransformer substrait_io)

install(
TARGETS substrait_common substrait_io plantransformer
EXPORT SubstraitTargets
LIBRARY FILE_SET HEADERS)
9 changes: 9 additions & 0 deletions src/substrait/expression/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# SPDX-License-Identifier: Apache-2.0

add_library(substrait_expression DecimalLiteral.cpp)
target_sources(
substrait_expression
PUBLIC FILE_SET HEADERS BASE_DIRS ../../../include/ FILES
../../../include/substrait/expression/DecimalLiteral.h)

target_link_libraries(substrait_expression substrait_proto absl::numeric
absl::strings)

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(
TARGETS substrait_expression
EXPORT SubstraitTargets
FILE_SET HEADERS)
16 changes: 16 additions & 0 deletions src/substrait/function/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@
set(FUNCTION_SRCS Function.cpp Extension.cpp FunctionLookup.cpp)

add_library(substrait_function ${FUNCTION_SRCS})
target_sources(
substrait_function
PUBLIC FILE_SET
HEADERS
BASE_DIRS
../../../include/
FILES
../../../include/substrait/function/Extension.h
../../../include/substrait/function/Function.h
../../../include/substrait/function/FunctionLookup.h
../../../include/substrait/function/FunctionSignature.h)

target_link_libraries(substrait_function substrait_type yaml-cpp)

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(
TARGETS substrait_function
EXPORT SubstraitTargets
FILE_SET HEADERS)
24 changes: 20 additions & 4 deletions src/substrait/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,30 @@ foreach(PROTO_FILE IN LISTS PROTOBUF_FILELIST)
endforeach()

# Add the generated protobuf C++ files to our exported library.
add_library(substrait_proto ${PROTO_SRCS} ${PROTO_HDRS} ProtoUtils.cpp
ProtoUtils.h)
add_library(substrait_proto ${PROTO_SRCS} ${PROTO_HDRS} ProtoUtils.cpp)
target_sources(
substrait_proto
PUBLIC FILE_SET
HEADERS
BASE_DIRS
${PROTO_OUTPUT_TOPLEVEL_DIR}/src
../..
FILES
${PROTO_HDRS}
ProtoUtils.h)

# Include the protobuf library as a dependency to use this class.
target_link_libraries(substrait_proto protobuf::libprotobuf)

# Make sure we can see our own generated include files.
target_include_directories(
substrait_proto
PUBLIC "${PROTO_OUTPUT_TOPLEVEL_DIR}/src"
PUBLIC "${protobuf_SOURCE_DIR}/src")
PUBLIC $<BUILD_INTERFACE:${PROTO_OUTPUT_TOPLEVEL_DIR}/src>
$<INSTALL_INTERFACE:include>
PUBLIC $<BUILD_INTERFACE:${protobuf_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include>)

install(
TARGETS substrait_proto
EXPORT SubstraitTargets
LIBRARY FILE_SET HEADERS)
7 changes: 5 additions & 2 deletions src/substrait/textplan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ target_link_libraries(
date::date)

# Provide access to the generated protobuffer headers hierarchy.
target_include_directories(symbol_table
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/../..")
target_include_directories(
symbol_table PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../..>)

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(TARGETS error_listener parse_result symbol_table
EXPORT SubstraitTargets)
4 changes: 4 additions & 0 deletions src/substrait/textplan/converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ add_library(substrait_textplan_normalizer ${NORMALIZER_SRCS})

target_link_libraries(substrait_textplan_normalizer
substrait_textplan_converter)

install(TARGETS planconverter substrait_textplan_converter
substrait_base_proto_visitor substrait_textplan_normalizer
EXPORT SubstraitTargets)
2 changes: 2 additions & 0 deletions src/substrait/textplan/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ target_link_libraries(planparser substrait_textplan_loader error_listener)
if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(TARGETS planparser substrait_textplan_loader EXPORT SubstraitTargets)
7 changes: 5 additions & 2 deletions src/substrait/textplan/parser/grammar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ add_library(textplan_grammar ${ANTLR_SubstraitPlanLexer_CXX_OUTPUTS}

message(STATUS "generated dir: ${GRAMMAR_DIR}/antlr4cpp_generated_src")

target_include_directories(textplan_grammar
PUBLIC "${GRAMMAR_DIR}/antlr4cpp_generated_src")
target_include_directories(
textplan_grammar
PUBLIC $<BUILD_INTERFACE:${GRAMMAR_DIR}/antlr4cpp_generated_src>)

target_link_libraries(textplan_grammar antlr4_static)

install(TARGETS textplan_grammar EXPORT SubstraitTargets)
8 changes: 8 additions & 0 deletions src/substrait/type/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
set(TYPE_SRCS Type.cpp)

add_library(substrait_type ${TYPE_SRCS})
target_sources(
substrait_type PUBLIC FILE_SET HEADERS BASE_DIRS ../../../include/ FILES
../../../include/substrait/type/Type.h)

target_link_libraries(substrait_type substrait_common)

if(${SUBSTRAIT_CPP_BUILD_TESTING})
add_subdirectory(tests)
endif()

install(
TARGETS substrait_type
EXPORT SubstraitTargets
FILE_SET HEADERS)
5 changes: 5 additions & 0 deletions third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

# Ensure `option()` in subdirectories honors normal variables set here.
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

set(ABSL_ENABLE_INSTALL ON)
if(NOT ${ABSL_INCLUDED_WITH_PROTOBUF})
set(ABSL_PROPAGATE_CXX_STD ON)
add_subdirectory(abseil-cpp)
Expand All @@ -26,6 +30,7 @@ set(BUILD_TESTING OFF)
add_subdirectory(protobuf-matchers)
set(BUILD_TESTING ${PREVIOUS_BUILD_TESTING})

set(YAML_CPP_INSTALL ON)
set(YAML_CPP_BUILD_TESTS
OFF
CACHE BOOL "Enable testing")
Expand Down

0 comments on commit cc8d08a

Please sign in to comment.