Skip to content

Commit 253bacd

Browse files
authored
Merge pull request #1 from chaojun-zhang/main
Init commit of substrait-cpp
2 parents 548d653 + b5a88fe commit 253bacd

25 files changed

+2262
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@
3030
*.exe
3131
*.out
3232
*.app
33+
34+
src/proto/substrait

.gitmodules

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[submodule "third_party/yaml-cpp"]
2+
path = third_party/yaml-cpp
3+
url = https://github.com/jbeder/yaml-cpp.git
4+
[submodule "third_party/googletest"]
5+
path = third_party/googletest
6+
url = https://github.com/google/googletest.git
7+
[submodule "third_party/substrait"]
8+
path = third_party/substrait
9+
url = https://github.com/substrait-io/substrait.git

CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
cmake_minimum_required(VERSION 3.10)
13+
14+
# set the project name
15+
project(substrait-cpp)
16+
17+
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
18+
19+
set(CMAKE_CXX_STANDARD 17)
20+
set(CMAKE_CXX_STANDARD_REQUIRED True)
21+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
22+
23+
find_package(Protobuf REQUIRED)
24+
include_directories(${PROTOBUF_INCLUDE_DIRS})
25+
26+
# GCC needs to link a library to enable std::filesystem.
27+
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
28+
set(FILESYSTEM "stdc++fs")
29+
30+
# Ensure we have gcc at least 8+.
31+
if(CMAKE_CXX_COMPILER_VERSION LESS 8.0)
32+
message(
33+
FATAL_ERROR "Substrait-CPP requires gcc > 8. Found ${CMAKE_CXX_COMPILER_VERSION}")
34+
endif()
35+
else()
36+
set(FILESYSTEM "")
37+
endif()
38+
39+
add_subdirectory(third_party)
40+
add_subdirectory(src)

Makefile

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
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.
17+
18+
.PHONY: all clean build debug release
19+
20+
BUILD_TYPE := Release
21+
22+
all: release
23+
24+
clean:
25+
@rm -rf build-*
26+
27+
build-common:
28+
29+
@mkdir -p build-${BUILD_TYPE}
30+
@cd build-${BUILD_TYPE} && \
31+
cmake -Wno-dev \
32+
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
33+
-DPREFER_STATIC_LIBS=OFF \
34+
$(FORCE_COLOR) \
35+
..
36+
37+
build:
38+
VERBOSE=1 cmake --build build-${BUILD_TYPE} -j $${CPU_COUNT:-`nproc`} || \
39+
cmake --build build-${BUILD_TYPE}
40+
41+
debug:
42+
@$(MAKE) build-common BUILD_TYPE=Debug
43+
@$(MAKE) build BUILD_TYPE=Debug
44+
45+
release:
46+
@$(MAKE) build-common BUILD_TYPE=Release
47+
@$(MAKE) build BUILD_TYPE=Release

scripts/setup-helper-functions.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/bin/bash
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# github_checkout $REPO $VERSION $GIT_CLONE_PARAMS clones or re-uses an existing clone of the
15+
# specified repo, checking out the requested version.
16+
function github_checkout {
17+
local REPO=$1
18+
shift
19+
local VERSION=$1
20+
shift
21+
local GIT_CLONE_PARAMS=$@
22+
local DIRNAME=$(basename $REPO)
23+
cd "${DEPENDENCY_DIR}"
24+
if [ -z "${DIRNAME}" ]; then
25+
echo "Failed to get repo name from ${REPO}"
26+
exit 1
27+
fi
28+
if [ -d "${DIRNAME}" ] && prompt "${DIRNAME} already exists. Delete?"; then
29+
rm -rf "${DIRNAME}"
30+
fi
31+
if [ ! -d "${DIRNAME}" ]; then
32+
git clone -q -b $VERSION $GIT_CLONE_PARAMS "https://github.com/${REPO}.git"
33+
fi
34+
cd "${DIRNAME}"
35+
}
36+
37+
38+
# get_cxx_flags [$CPU_ARCH]
39+
# Sets and exports the variable VELOX_CXX_FLAGS with appropriate compiler flags.
40+
# If $CPU_ARCH is set then we use that else we determine best possible set of flags
41+
# to use based on current cpu architecture.
42+
# The goal of this function is to consolidate all architecture specific flags to one
43+
# location.
44+
# The values that CPU_ARCH can take are as follows:
45+
# arm64 : Target Apple silicon.
46+
# aarch64: Target general 64 bit arm cpus.
47+
# avx: Target Intel CPUs with AVX.
48+
# sse: Target Intel CPUs with sse.
49+
# Echo's the appropriate compiler flags which can be captured as so
50+
# CXX_FLAGS=$(get_cxx_flags) or
51+
# CXX_FLAGS=$(get_cxx_flags "avx")
52+
53+
function get_cxx_flags {
54+
local CPU_ARCH=$1
55+
56+
local OS
57+
OS=$(uname)
58+
local MACHINE
59+
MACHINE=$(uname -m)
60+
61+
if [ -z "$CPU_ARCH" ]; then
62+
63+
if [ "$OS" = "Darwin" ]; then
64+
65+
if [ "$MACHINE" = "x86_64" ]; then
66+
local CPU_CAPABILITIES
67+
CPU_CAPABILITIES=$(sysctl -a | grep machdep.cpu.features | awk '{print tolower($0)}')
68+
69+
if [[ $CPU_CAPABILITIES =~ "avx" ]]; then
70+
CPU_ARCH="avx"
71+
else
72+
CPU_ARCH="sse"
73+
fi
74+
75+
elif [[ $(sysctl -a | grep machdep.cpu.brand_string) =~ "Apple" ]]; then
76+
# Apple silicon.
77+
CPU_ARCH="arm64"
78+
fi
79+
else [ "$OS" = "Linux" ];
80+
81+
local CPU_CAPABILITIES
82+
CPU_CAPABILITIES=$(cat /proc/cpuinfo | grep flags | head -n 1| awk '{print tolower($0)}')
83+
84+
if [[ "$CPU_CAPABILITIES" =~ "avx" ]]; then
85+
CPU_ARCH="avx"
86+
elif [[ "$CPU_CAPABILITIES" =~ "sse" ]]; then
87+
CPU_ARCH="sse"
88+
elif [ "$MACHINE" = "aarch64" ]; then
89+
CPU_ARCH="aarch64"
90+
fi
91+
fi
92+
fi
93+
94+
case $CPU_ARCH in
95+
96+
"arm64")
97+
echo -n "-mcpu=apple-m1+crc -std=c++17"
98+
;;
99+
100+
"avx")
101+
echo -n "-mavx2 -mfma -mavx -mf16c -mlzcnt -std=c++17"
102+
;;
103+
104+
"sse")
105+
echo -n "-msse4.2 -std=c++17"
106+
;;
107+
108+
"aarch64")
109+
echo -n "-mcpu=neoverse-n1 -std=c++17"
110+
;;
111+
*)
112+
echo -n "Architecture not supported!"
113+
esac
114+
115+
}
116+
117+
function cmake_install {
118+
local NAME=$(basename "$(pwd)")
119+
local BINARY_DIR=_build
120+
if [ -d "${BINARY_DIR}" ] && prompt "Do you want to rebuild ${NAME}?"; then
121+
rm -rf "${BINARY_DIR}"
122+
fi
123+
mkdir -p "${BINARY_DIR}"
124+
CPU_TARGET="${CPU_TARGET:-avx}"
125+
COMPILER_FLAGS=$(get_cxx_flags $CPU_TARGET)
126+
127+
# CMAKE_POSITION_INDEPENDENT_CODE is required so that Velox can be built into dynamic libraries \
128+
cmake -Wno-dev -B"${BINARY_DIR}" \
129+
-GNinja \
130+
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
131+
-DCMAKE_CXX_STANDARD=17 \
132+
"${INSTALL_PREFIX+-DCMAKE_PREFIX_PATH=}${INSTALL_PREFIX-}" \
133+
"${INSTALL_PREFIX+-DCMAKE_INSTALL_PREFIX=}${INSTALL_PREFIX-}" \
134+
-DCMAKE_CXX_FLAGS="$COMPILER_FLAGS" \
135+
-DBUILD_TESTING=OFF \
136+
"$@"
137+
ninja -C "${BINARY_DIR}" install
138+
}
139+

scripts/setup-macos.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
set -e # Exit on error.
15+
set -x # Print commands that are executed.
16+
17+
SCRIPTDIR=$(dirname "${BASH_SOURCE[0]}")
18+
source $SCRIPTDIR/setup-helper-functions.sh
19+
20+
CPU_TARGET="${CPU_TARGET:-avx}"
21+
NPROC=$(getconf _NPROCESSORS_ONLN)
22+
COMPILER_FLAGS=$(get_cxx_flags $CPU_TARGET)
23+
24+
DEPENDENCY_DIR=${DEPENDENCY_DIR:-$(pwd)}
25+
MACOS_DEPS="ninja bison cmake ccache gflags protobuf"
26+
27+
function run_and_time {
28+
time "$@"
29+
{ echo "+ Finished running $*"; } 2> /dev/null
30+
}
31+
32+
function prompt {
33+
(
34+
while true; do
35+
local input="${PROMPT_ALWAYS_RESPOND:-}"
36+
echo -n "$(tput bold)$* [Y, n]$(tput sgr0) "
37+
[[ -z "${input}" ]] && read input
38+
if [[ "${input}" == "Y" || "${input}" == "y" || "${input}" == "" ]]; then
39+
return 0
40+
elif [[ "${input}" == "N" || "${input}" == "n" ]]; then
41+
return 1
42+
fi
43+
done
44+
) 2> /dev/null
45+
}
46+
47+
function update_brew {
48+
/usr/local/bin/brew update --force --quiet
49+
}
50+
51+
function install_build_prerequisites {
52+
for pkg in ${MACOS_DEPS}
53+
do
54+
if [[ "${pkg}" =~ ^([0-9a-z-]*):([0-9](\.[0-9\])*)$ ]];
55+
then
56+
pkg=${BASH_REMATCH[1]}
57+
ver=${BASH_REMATCH[2]}
58+
echo "Installing '${pkg}' at '${ver}'"
59+
tap="local-${pkg}"
60+
brew tap-new "${tap}"
61+
brew extract "--version=${ver}" "${pkg}" "${tap}"
62+
brew install "${tap}/${pkg}@${ver}"
63+
else
64+
brew install --formula "${pkg}" && echo "Installation of ${pkg} is successful" || brew upgrade --formula "$pkg"
65+
fi
66+
done
67+
68+
pip3 install --user cmake-format regex
69+
}
70+
71+
function install_deps {
72+
if [ "${INSTALL_PREREQUISITES:-Y}" == "Y" ]; then
73+
run_and_time install_build_prerequisites
74+
fi
75+
}
76+
77+
(return 2> /dev/null) && return # If script was sourced, don't run commands.
78+
79+
(
80+
if [[ $# -ne 0 ]]; then
81+
for cmd in "$@"; do
82+
run_and_time "${cmd}"
83+
done
84+
else
85+
install_deps
86+
fi
87+
)
88+
89+
echo "All deps installed! Now try \"make\""

0 commit comments

Comments
 (0)