Skip to content

Commit

Permalink
GDV-45: Make use of the modular features of cmake
Browse files Browse the repository at this point in the history
Separate out the public and private target dependencies.

For arrow, export an interface target. This avoids the need to add
include dirs for each dependency on arrow.

Removed dependency on gtest. Instead, build it as an external project.
This is the recommended practice for googletest.

For pre-compiled files, generate the bitcode files for each of them
independently and then, link them to generate a unified bitcode file.
Removed cpplint exceptions since there is no more sourcing of .cc
files.

Separate out the public include files from private includes, and 
add them in the dependency list in cmake.

pass the bytecode filepath from cmake (instead of /tmp)
  • Loading branch information
pravindra authored and praveenbingo committed Sep 10, 2018
1 parent b79d219 commit b3dee6a
Show file tree
Hide file tree
Showing 37 changed files with 290 additions and 176 deletions.
111 changes: 11 additions & 100 deletions cpp/src/gandiva/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,106 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

project(gandiva)
cmake_minimum_required(VERSION 3.10)

set(REQD_LLVM_VERSION 6.0)
find_package(LLVM ${REQD_LLVM_VERSION} REQUIRED CONFIG HINTS
/usr/local/opt/llvm
/usr/share)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
# LLVM/Clang is required by both the subdirs.
find_package(LLVM)

find_library(ARROW_LIB arrow)
message(STATUS "Found arrow : ${ARROW_LIB}")
find_path(ARROW_INCLUDE_DIR arrow/type.h)
message(STATUS "include dir for arrow : ${ARROW_INCLUDE_DIR}")
include_directories(${ARROW_INCLUDE_DIR})
# Set the path where the byte-code files will be installed.
set(GANDIVA_BC_FILE_NAME irhelpers.bc)
set(GANDIVA_BC_INSTALL_DIR
${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/gandiva)
set(GANDIVA_BC_INSTALL_PATH ${GANDIVA_BC_INSTALL_DIR}/${GANDIVA_BC_FILE_NAME})
set(GANDIVA_BC_OUTPUT_PATH ${CMAKE_BINARY_DIR}/irhelpers.bc)

find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})

include_directories(.)
include_directories(codegen)
include_directories(codegen/cex)
include_directories(codegen/dex)
include_directories(codegen/expr)
include_directories(expr)
include_directories(precompiled)
include_directories(${CMAKE_BINARY_DIR})

set(GANDIVA_SRCS
codegen/engine.cc
codegen/function_registry.cc
codegen/llvm_generator.cc
codegen/llvm_types.cc
expr/annotator.cc
expr/evaluator.cc
expr/tree_expr_builder.cc
expr/node.cc)

set(PRECOMPILED_SOURCED_FILES
precompiled/arithmetic_ops.cc
precompiled/bitmap.cc
precompiled/time.cc)

SET(PRECOMPILED_SRC precompiled/irhelpers.cc)
#TODO : fix the location of the .bc file, it shouldn't be /tmp.
SET(PRECOMPILED_OUTPUT /tmp/irhelpers.bc)

find_program(CLANG_EXECUTABLE NAMES clang++-${REQD_LLVM_VERSION} clang)
message(STATUS "Found clang++ ${CLANG_EXECUTABLE}")

get_filename_component(PRECOMPILED_ABSOLUTE_SRC ${PRECOMPILED_SRC} ABSOLUTE)
add_custom_command(
OUTPUT ${PRECOMPILED_OUTPUT}
COMMAND ${CLANG_EXECUTABLE} -flto -O2 -c ${PRECOMPILED_ABSOLUTE_SRC} -o ${PRECOMPILED_OUTPUT}
DEPENDS ${PRECOMPILED_SRC} ${PRECOMPILED_SOURCED_FILES})
add_custom_target(precompile-to-ir ALL DEPENDS ${PRECOMPILED_OUTPUT})

add_library(gandiva SHARED ${GANDIVA_SRCS})

# Find the libraries that correspond to the LLVM components
llvm_map_components_to_libnames(llvm_libs core mcjit native ipo bitreader target linker analysis debuginfodwarf)
# Link against LLVM libraries
target_link_libraries(gandiva ${llvm_libs})
target_link_libraries(gandiva ${ARROW_LIB})

# Test related

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

function(ADD_GANDIVA_TEST LABEL REL_TEST_NAME)
get_filename_component(TEST_NAME ${REL_TEST_NAME} NAME_WE)
set(TEST_PATH "${EXECUTABLE_OUTPUT_PATH}/${TEST_NAME}")

add_executable(${TEST_NAME} ${REL_TEST_NAME} ${ARGN})
if(${LABEL} STREQUAL "integ")
target_link_libraries(${TEST_NAME} gandiva)
endif()
if(${REL_TEST_NAME} MATCHES "llvm")
target_link_libraries(${TEST_NAME} ${llvm_libs})
endif()
target_link_libraries(${TEST_NAME} ${ARROW_LIB})
target_link_libraries(${TEST_NAME} ${GTEST_BOTH_LIBRARIES})
target_link_libraries(${TEST_NAME} Threads::Threads)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
set_property(TEST ${TEST_NAME} PROPERTY LABELS ${LABEL} ${TEST_NAME})
endfunction()

#args: label test-file src-files
ADD_GANDIVA_TEST(unittest codegen/dex_llvm_test.cc)
ADD_GANDIVA_TEST(unittest codegen/engine_llvm_test.cc codegen/engine.cc codegen/llvm_types.cc)
ADD_GANDIVA_TEST(unittest codegen/function_signature_test.cc)
ADD_GANDIVA_TEST(unittest codegen/function_registry_test.cc codegen/function_registry.cc)
ADD_GANDIVA_TEST(unittest codegen/llvm_types_test.cc codegen/llvm_types.cc)
ADD_GANDIVA_TEST(unittest codegen/llvm_generator_test.cc codegen/llvm_generator.cc codegen/engine.cc codegen/llvm_types.cc codegen/function_registry.cc expr/annotator.cc)
ADD_GANDIVA_TEST(unittest expr/annotator_test.cc expr/annotator.cc)
ADD_GANDIVA_TEST(unittest expr/tree_expr_test.cc expr/tree_expr_builder.cc expr/node.cc expr/annotator.cc codegen/function_registry.cc)

ADD_GANDIVA_TEST(integ expr/evaluator_test.cc)
add_subdirectory(codegen)
add_subdirectory(precompiled)
47 changes: 34 additions & 13 deletions cpp/src/gandiva/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,57 @@

## System setup

Gandiva uses CMake as a build configuration system. Currently, it supports
in-source and out-of-source builds with the latter one being preferred.
Gandiva uses CMake as a build configuration system. Currently, it supports
out-of-source builds only.

Build Gandiva requires:

* A C++11-enabled compiler. On Linux, gcc 4.8 and higher should be sufficient.
* CMake
* LLVM
* LLVM
* Arrow
* GTest
* Boost

On OS X, you can use [Homebrew][1]:
On macOS, you can use [Homebrew][1]:

```shell
brew install cmake llvm boost
```

To install arrow, follow the steps in the [arrow Readme][2].
## Building Gandiva

mkdir build
cd build
cmake ..
make

Debug build :

```shell
git clone https://github.com/dremio/gandiva.git
cd gandiva/cpp
mkdir debug
cd debug
cmake ..
make test
```

Release build :

```shell
git clone https://github.com/dremio/gandiva.git
cd gandiva/cpp
mkdir release
cd release
cmake .. -DCMAKE_BUILD_TYPE=Release
make test
```

## Validating code style

We follow the [google cpp code style][2]. To validate compliance,
We follow the [google cpp code style][3]. To validate compliance,

make lint
```shell
cd debug
make lint
```

[1]: https://brew.sh/
[2]: https://google.github.io/styleguide/cppguide.html
[2]: https://github.com/apache/arrow/tree/master/cpp
[3]: https://google.github.io/styleguide/cppguide.html
96 changes: 96 additions & 0 deletions cpp/src/gandiva/codegen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright (C) 2017-2018 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

project(gandiva)

# Find arrow
find_package(ARROW)

find_package(Boost REQUIRED)

set(BC_FILE_PATH_CC "${CMAKE_CURRENT_BINARY_DIR}/bc_file_path.cc")
configure_file(bc_file_path.cc.in ${BC_FILE_PATH_CC})

add_library(gandiva SHARED
annotator.cc
engine.cc
evaluator.cc
function_registry.cc
llvm_generator.cc
llvm_types.cc
node.cc
tree_expr_builder.cc
${BC_FILE_PATH_CC})

# For users of gandiva library (including integ tests), include-dir is :
# /usr/**/include dir after install,
# cpp/include during build
# For building gandiva library itself, include-dir (in addition to above) is :
# cpp/src
target_include_directories(gandiva
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
PRIVATE
${CMAKE_SOURCE_DIR}/src
)

# ARROW is a public dependency i.e users of gandiva also will have a dependency on arrow.
target_link_libraries(gandiva
PUBLIC
ARROW::ARROW
PRIVATE
Boost::boost)

# LLVM is a private dependency i.e users of gandiva will not need to include llvm headers
# or link with llvm libraries.
target_link_llvm(gandiva PRIVATE)

# Set version for the library.
set(GANDIVA_VERSION_MAJOR 0)
set(GANDIVA_VERSION_MINOR 1)
set(GANDIVA_VERSION_PATCH 0)
set(GANDIVA_VERSION ${GANDIVA_VERSION_MAJOR}.${GANDIVA_VERSION_MINOR}.${GANDIVA_VERSION_PATCH})

set_target_properties(gandiva PROPERTIES
VERSION ${GANDIVA_VERSION}
SOVERSION ${GANDIVA_VERSION_MAJOR}
)

# install for gandiva
include(GNUInstallDirs)

# install libgandiva
install(
TARGETS gandiva
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

# install the header files.
install(
DIRECTORY ${CMAKE_SOURCE_DIR}/include/gandiva
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

#args: label test-file src-files
add_gandiva_unit_test(dex_llvm_test.cc)
add_gandiva_unit_test(engine_llvm_test.cc engine.cc llvm_types.cc ${BC_FILE_PATH_CC})
add_gandiva_unit_test(function_signature_test.cc)
add_gandiva_unit_test(function_registry_test.cc function_registry.cc)
add_gandiva_unit_test(llvm_types_test.cc llvm_types.cc)
add_gandiva_unit_test(llvm_generator_test.cc llvm_generator.cc engine.cc llvm_types.cc function_registry.cc annotator.cc ${BC_FILE_PATH_CC})
add_gandiva_unit_test(annotator_test.cc annotator.cc)
add_gandiva_unit_test(tree_expr_test.cc tree_expr_builder.cc node.cc annotator.cc function_registry.cc)

add_gandiva_integ_test(evaluator_test.cc)
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <memory>
#include <string>
#include "expr/annotator.h"
#include "codegen/annotator.h"
#include "codegen/field_descriptor.h"

namespace gandiva {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include <string>
#include <vector>
#include <unordered_map>
#include "common/arrow.h"
#include "common/gandiva_aliases.h"
#include "common/logging.h"
#include "expr/eval_batch.h"
#include "gandiva/arrow.h"
#include "gandiva/gandiva_aliases.h"
#include "gandiva/logging.h"
#include "codegen/eval_batch.h"

namespace gandiva {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <memory>
#include <gtest/gtest.h>
#include <arrow/memory_pool.h>
#include "expr/annotator.h"
#include "codegen/annotator.h"
#include "codegen/field_descriptor.h"

namespace gandiva {
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions cpp/src/gandiva/codegen/bc_file_path.cc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2017-2018 Dremio Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace gandiva {

// Path to the byte-code file.
extern const char kByteCodeFilePath[] = "${GANDIVA_BC_OUTPUT_PATH}";

} // namespace gandiva
2 changes: 1 addition & 1 deletion cpp/src/gandiva/codegen/dex.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include <string>
#include <vector>
#include "common/gandiva_aliases.h"
#include "gandiva/gandiva_aliases.h"
#include "codegen/dex_visitor.h"
#include "codegen/field_descriptor.h"
#include "codegen/func_descriptor.h"
Expand Down
9 changes: 4 additions & 5 deletions cpp/src/gandiva/codegen/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@

namespace gandiva {

const char Engine::kLibPreCompiledIRDir[] = "/tmp/";
bool Engine::init_once_done_ = false;
std::once_flag init_once_flag;

extern const char kByteCodeFilePath[];

/*
* One-time initializations.
*/
Expand Down Expand Up @@ -84,14 +85,12 @@ Engine::Engine()
* Handling for pre-compiled IR libraries.
*/
void Engine::LoadPreCompiledIRFiles() {
std::string fileName = std::string(kLibPreCompiledIRDir) + "irhelpers.bc";

/// Read from file into memory buffer.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer_or_error =
llvm::MemoryBuffer::getFile(fileName);
llvm::MemoryBuffer::getFile(kByteCodeFilePath);
if (!buffer_or_error) {
std::stringstream ss;
ss << "Could not load module from IR " << fileName << ": " <<
ss << "Could not load module from IR " << kByteCodeFilePath << ": " <<
buffer_or_error.getError().message();
throw CodeGenException(ss.str());
}
Expand Down
Loading

0 comments on commit b3dee6a

Please sign in to comment.