Skip to content

Commit 2ae39d0

Browse files
author
Shivendra Devadhe
committed
feat(cpp): improve CMake configuration for portable distribution and installation
1 parent 60923ec commit 2ae39d0

2 files changed

Lines changed: 117 additions & 108 deletions

File tree

cpp/CMakeLists.txt

Lines changed: 79 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
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
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
88
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
9+
#http: // www.apache.org/licenses/LICENSE-2.0
1010
#
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.
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.
1717

1818
cmake_minimum_required(VERSION 3.5)
1919

20-
# Avoid mixing plain and keyword signature of target_link_libraries
20+
#Avoid mixing plain and keyword signature of target_link_libraries
2121
if (POLICY CMP0023)
2222
cmake_policy(SET CMP0023 NEW)
2323
endif()
@@ -26,7 +26,7 @@ if(POLICY CMP0048)
2626
cmake_policy(SET CMP0048 NEW)
2727
endif()
2828

29-
# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
29+
#Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
3030
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
3131
cmake_policy(SET CMP0135 NEW)
3232
endif()
@@ -37,9 +37,9 @@ set(GRAPHAR_PATCH_VERSION 0)
3737
set(GRAPHAR_VERSION ${GRAPHAR_MAJOR_VERSION}.${GRAPHAR_MINOR_VERSION}.${GRAPHAR_PATCH_VERSION})
3838
project(graphar-cpp LANGUAGES C CXX VERSION ${GRAPHAR_VERSION})
3939

40-
# ------------------------------------------------------------------------------
41-
# cmake options
42-
# ------------------------------------------------------------------------------
40+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
41+
#cmake options
42+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
4343

4444
option(BUILD_TESTS "Build unit tests" OFF)
4545
option(BUILD_EXAMPLES "Build examples" OFF)
@@ -63,8 +63,9 @@ if (ENABLE_DOCS OR BUILD_DOCS_ONLY)
6363
find_package(Doxygen REQUIRED)
6464
set(DOXYGEN_IN ${PROJECT_SOURCE_DIR}/Doxyfile)
6565
set(DOXYGEN_OUT ${CMAKE_BINARY_DIR}/Doxyfile.out)
66-
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
67-
# Copy disclaimer_footer.html to the build directory
66+
configure_file(${DOXYGEN_IN} ${
67+
DOXYGEN_OUT} @ONLY)
68+
#Copy disclaimer_footer.html to the build directory
6869
configure_file(${PROJECT_SOURCE_DIR}/disclaimer_footer.html ${CMAKE_BINARY_DIR}/disclaimer_footer.html COPYONLY)
6970
add_custom_target(docs
7071
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
@@ -77,9 +78,9 @@ if (ENABLE_DOCS OR BUILD_DOCS_ONLY)
7778
endif()
7879
endif()
7980

80-
# ------------------------------------------------------------------------------
81-
# setting default cmake type to Release
82-
# ------------------------------------------------------------------------------
81+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
82+
#setting default cmake type to Release
83+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
8384
set(DEFAULT_BUILD_TYPE "Release")
8485
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
8586
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
@@ -95,7 +96,8 @@ if(NOT (CMAKE_CXX_COMPILER_LAUNCHER MATCHES "ccache") AND NOT (CMAKE_C_COMPILER_
9596
set(CMAKE_C_COMPILER_LAUNCHER ${ccache_EXECUTABLE})
9697
set(CMAKE_CXX_COMPILER_LAUNCHER ${ccache_EXECUTABLE})
9798
add_custom_target(graphar-ccache-stats
98-
COMMAND ${ccache_EXECUTABLE} --show-stats
99+
COMMAND ${
100+
ccache_EXECUTABLE} --show-stats
99101
)
100102
else()
101103
add_custom_target(graphar-ccache-stats
@@ -107,22 +109,23 @@ endif()
107109
set(GAR_CXX_STANDARD 17)
108110

109111
if(MSVC)
110-
# Avoid GCC/Clang-specific flags on MSVC.
111-
# C++17 is already enforced via CMAKE_CXX_STANDARD/target features.
112+
#Avoid GCC / Clang - specific flags on MSVC.
113+
#C++ 17 is already enforced via CMAKE_CXX_STANDARD / target features.
112114
else()
113115
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-empty-body")
114116
endif()
115117

116118
if (APPLE)
117119
set(CMAKE_MACOSX_RPATH ON)
120+
set(CMAKE_INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR}")
118121
elseif(UNIX)
119-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,$ORIGIN")
122+
set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
120123
endif ()
121124

122125
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -fno-omit-frame-pointer")
123126
if (GRAPHAR_ENABLE_SANITIZER)
124127
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address")
125-
# Linker flags are required to pull in the ASan runtime.
128+
#Linker flags are required to pull in the ASan runtime.
126129
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fsanitize=address")
127130
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fsanitize=address")
128131
endif()
@@ -140,9 +143,9 @@ if (CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG")
140143
message(STATUS "[graphar] GRAPHAR_ENABLE_COVERAGE: ${GRAPHAR_ENABLE_COVERAGE}")
141144
endif()
142145

143-
# ------------------------------------------------------------------------------
144-
# cmake configs
145-
# ------------------------------------------------------------------------------
146+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
147+
#cmake configs
148+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
146149
include(CheckLibraryExists)
147150
include(GNUInstallDirs)
148151
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
@@ -156,10 +159,9 @@ target_include_directories(
156159
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
157160
)
158161

159-
160-
# ------------------------------------------------------------------------------
161-
# macro functions
162-
# ------------------------------------------------------------------------------
162+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
163+
#macro functions
164+
#-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
163165
macro(add_subdirectory_shared directory)
164166
set(BUILD_SHARED_LIBS_SAVED "${BUILD_SHARED_LIBS}")
165167
set(BUILD_SHARED_LIBS ON)
@@ -190,17 +192,16 @@ macro(find_yaml_cpp)
190192
endmacro()
191193

192194
macro(install_graphar_target target)
193-
# install
194195
install(TARGETS ${target} graphar_thirdparty
195196
EXPORT graphar-targets
196-
ARCHIVE DESTINATION lib
197-
LIBRARY DESTINATION lib
198-
RUNTIME DESTINATION bin
199-
INCLUDES DESTINATION include
197+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
198+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
199+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL
200+
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
200201
)
201202
endmacro()
202203

203-
# Implementations of lisp "car" and "cdr" functions
204+
#Implementations of lisp "car" and "cdr" functions
204205
macro(GRAPHAR_CAR var)
205206
set(${var} ${ARGV1})
206207
endmacro()
@@ -209,8 +210,8 @@ macro(GRAPHAR_CDR var rest)
209210
set(${var} ${ARGN})
210211
endmacro()
211212

212-
# Based on MIT-licensed
213-
# https://gist.github.com/cristianadam/ef920342939a89fae3e8a85ca9459b49
213+
#Based on MIT - licensed
214+
#https: // gist.github.com/cristianadam/ef920342939a89fae3e8a85ca9459b49
214215
function(graphar_create_merged_static_lib output_target)
215216
set(options)
216217
set(one_value_args NAME ROOT)
@@ -301,62 +302,56 @@ endmacro()
301302
macro(build_graphar)
302303
build_graphar_thirdparty()
303304
file(GLOB_RECURSE CORE_SRC_FILES "src/graphar/*.cc")
305+
304306
if(GRAPHAR_BUILD_STATIC)
305307
add_library(graphar STATIC ${CORE_SRC_FILES})
306308
else()
307309
add_library(graphar SHARED ${CORE_SRC_FILES})
308310
endif()
311+
309312
install_graphar_target(graphar)
313+
310314
target_compile_features(graphar PRIVATE cxx_std_${GAR_CXX_STANDARD})
311-
target_include_directories(graphar PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
315+
316+
target_include_directories(graphar
317+
PUBLIC
318+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
319+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
320+
PRIVATE
321+
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty
322+
)
323+
312324
if(GRAPHAR_BUILD_STATIC)
313325
target_link_libraries(graphar PUBLIC graphar_thirdparty ${CMAKE_DL_LIBS})
314326
else()
315327
target_link_libraries(graphar PRIVATE graphar_thirdparty ${CMAKE_DL_LIBS})
316328
endif()
317329

330+
set(ARROW_LIB_SUFFIX "shared")
331+
if(USE_STATIC_ARROW)
332+
set(ARROW_LIB_SUFFIX "static")
333+
endif()
334+
335+
set(ARROW_DEPS
336+
Arrow::arrow_${ARROW_LIB_SUFFIX}
337+
Parquet::parquet_${ARROW_LIB_SUFFIX}
338+
ArrowDataset::arrow_dataset_${ARROW_LIB_SUFFIX}
339+
)
340+
if(ArrowAcero_FOUND)
341+
list(APPEND ARROW_DEPS ArrowAcero::arrow_acero_${ARROW_LIB_SUFFIX})
342+
endif()
343+
318344
if(APPLE)
319-
if(USE_STATIC_ARROW)
320-
target_link_libraries(graphar PRIVATE -Wl,-force_load
321-
Arrow::arrow_static
322-
Parquet::parquet_static
323-
ArrowDataset::arrow_dataset_static
324-
ArrowAcero::arrow_acero_static)
325-
else()
326-
target_link_libraries(graphar PRIVATE -Wl,-force_load Arrow::arrow_shared
327-
Parquet::parquet_shared
328-
ArrowDataset::arrow_dataset_shared
329-
ArrowAcero::arrow_acero_shared)
330-
endif()
345+
target_link_libraries(graphar PUBLIC -Wl,-force_load ${ARROW_DEPS})
331346
elseif(MSVC)
332-
# MSVC doesn't support GNU ld-style -Wl flags. For shared libraries we
333-
# don't need whole-archive semantics.
334-
if(USE_STATIC_ARROW)
335-
target_link_libraries(graphar PRIVATE
336-
Arrow::arrow_static
337-
Parquet::parquet_static
338-
ArrowDataset::arrow_dataset_static
339-
ArrowAcero::arrow_acero_static)
340-
else()
341-
target_link_libraries(graphar PRIVATE
342-
Arrow::arrow_shared
343-
Parquet::parquet_shared
344-
ArrowDataset::arrow_dataset_shared
345-
ArrowAcero::arrow_acero_shared)
346-
endif()
347+
target_link_libraries(graphar PUBLIC ${ARROW_DEPS})
347348
else()
348-
if(USE_STATIC_ARROW)
349-
target_link_libraries(graphar PRIVATE -Wl,--exclude-libs,ALL -Wl,--whole-archive
350-
Arrow::arrow_static
351-
Parquet::parquet_static
352-
ArrowDataset::arrow_dataset_static
353-
ArrowAcero::arrow_acero_static -Wl,--no-whole-archive)
354-
else()
355-
target_link_libraries(graphar PRIVATE -Wl,--exclude-libs,ALL -Wl,--whole-archive Arrow::arrow_shared
356-
Parquet::parquet_shared
357-
ArrowDataset::arrow_dataset_shared
358-
ArrowAcero::arrow_acero_shared -Wl,--no-whole-archive)
359-
endif()
349+
target_link_libraries(graphar PUBLIC
350+
-Wl,--exclude-libs,ALL
351+
-Wl,--whole-archive
352+
${ARROW_DEPS}
353+
-Wl,--no-whole-archive
354+
)
360355
endif()
361356
endmacro()
362357

cpp/graphar-config.in.cmake

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,45 @@
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
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
88
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
9+
#http: // www.apache.org/licenses/LICENSE-2.0
1010
#
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.
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.
1717

18-
# - Config file for the graphar package
18+
#- Config file for the graphar package
1919
#
20-
# It defines the following variables
20+
#It defines the following variables
2121
#
22-
# GRAPHAR_INCLUDE_DIR - include directory for graphar
23-
# GRAPHAR_INCLUDE_DIRS - include directories for graphar
24-
# GRAPHAR_LIBRARIES - libraries to link against
22+
#GRAPHAR_INCLUDE_DIR - include directory for graphar
23+
#GRAPHAR_INCLUDE_DIRS - include directories for graphar
24+
#GRAPHAR_LIBRARIES - libraries to link against
2525

26-
set(GRAPHAR_HOME "${CMAKE_CURRENT_LIST_DIR}/../../..")
27-
include("${CMAKE_CURRENT_LIST_DIR}/graphar-targets.cmake")
26+
@PACKAGE_INIT @
2827

29-
set(GRAPHAR_LIBRARIES graphar)
30-
set(GRAPHAR_INCLUDE_DIR "${GRAPHAR_HOME}/include")
31-
set(GRAPHAR_INCLUDE_DIRS "${GRAPHAR_INCLUDE_DIR}")
28+
include(CMakeFindDependencyMacro)
29+
30+
#Only find_dependency if we didn't bundle everything
31+
if (NOT @BUILD_ARROW_FROM_SOURCE
32+
@) find_dependency(Arrow REQUIRED) find_dependency(Parquet REQUIRED)
33+
find_dependency(ArrowDataset REQUIRED) if (@Arrow_VERSION
34+
@VERSION_GREATER_EQUAL
35+
"12.0.0")
36+
find_dependency(ArrowAcero) endif() endif()
37+
38+
include("${CMAKE_CURRENT_LIST_DIR}/graphar-targets.cmake")
39+
40+
#Maintain backward compatibility for users using variables
41+
set(GRAPHAR_LIBRARIES graphar::graphar)
42+
get_target_property(
43+
GRAPHAR_INCLUDE_DIRS graphar::graphar
44+
INTERFACE_INCLUDE_DIRECTORIES)
45+
set(GRAPHAR_INCLUDE_DIR ${GRAPHAR_INCLUDE_DIRS})

0 commit comments

Comments
 (0)