Skip to content

Commit

Permalink
Add Protobuf as submodule
Browse files Browse the repository at this point in the history
This ensures that the user has a working Protobuf installation
  • Loading branch information
hcho3 committed Aug 23, 2018
1 parent 0c374ad commit b3887a9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "3rdparty/fmt"]
path = 3rdparty/fmt
url = https://github.com/fmtlib/fmt.git
[submodule "3rdparty/protobuf"]
path = 3rdparty/protobuf
url = https://github.com/google/protobuf.git
1 change: 1 addition & 0 deletions 3rdparty/protobuf
Submodule protobuf added at 106ffc
32 changes: 14 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required (VERSION 3.1)
project(treelite)
include(cmake/Utils.cmake)
find_package(OpenMP)
find_package(Protobuf)

# Use RPATH on Mac OS X as flexible mechanism for locating dependencies
# See https://blog.kitware.com/upcoming-in-cmake-2-8-12-osx-rpath-support/
Expand All @@ -11,11 +10,18 @@ set(CMAKE_MACOSX_RPATH TRUE)
# Set BUILD_SHARED_LIBS as option. By default, build shared libraries;
# User can set this to OFF to build static libraries instead.
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(DISABLE_PROTOBUF "Disable Protobuf" OFF)

# enable custom logging facility in dmlc-core
add_definitions(-DDMLC_LOG_CUSTOMIZE)

# Protobuf
set(protobuf_BUILD_TESTS OFF CACHE BOOL "enable tests for protobuf" FORCE)
set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "enable shared libs for protobuf" FORCE)
add_subdirectory(3rdparty/protobuf/cmake)
set(PROTOBUF_PROTOC_EXECUTABLE protoc)
set(PROTOBUF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/3rdparty/protobuf/src")
set(PROTOBUF_LIBRARIES libprotobuf)

# check MSVC version
if(MSVC)
if(MSVC_VERSION LESS 1900)
Expand All @@ -28,10 +34,8 @@ msvc_use_static_runtime()

# OpenMP check
if(OPENMP_FOUND)
message("Got OpenMP")
add_definitions(-DTREELITE_OPENMP_SUPPORT)
else(OPENMP_FOUND)
message("Don't have OpenMP")
include(dmlc-core/cmake/Utils.cmake)
dmlccore_option(USE_OPENMP "Build with OpenMP" OFF)
endif(OPENMP_FOUND)
Expand Down Expand Up @@ -96,15 +100,11 @@ endforeach(g)
list(REMOVE_ITEM SOURCES ${PROJECT_SOURCE_DIR}/src/cli_main.cc ${COMMON_SOURCES_WITH_PREFIX} ${RUNTIME_SOURCES_WITH_PREFIX})

# Protobuf library
if (NOT DISABLE_PROTOBUF)
if (Protobuf_FOUND OR PROTOBUF_FOUND)
set_source_files_properties(src/frontend/protobuf.cc PROPERTIES COMPILE_DEFINITIONS PROTOBUF_SUPPORT)
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS src/tree.proto)
list(INSERT SOURCES 0 ${PROTO_SRCS} ${PROTO_HDRS})
endif()
endif()
set_source_files_properties(src/frontend/protobuf.cc PROPERTIES COMPILE_DEFINITIONS PROTOBUF_SUPPORT)
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS src/tree.proto)
list(INSERT SOURCES 0 ${PROTO_SRCS} ${PROTO_HDRS})

# dmlc-core
set(BUILD_SHARED_LIBS_SAVED "${BUILD_SHARED_LIBS}") # Save BUILD_SHARED_LIBS
Expand All @@ -125,11 +125,7 @@ if (UNIX)
endif (UNIX)

# Protobuf library
if (NOT DISABLE_PROTOBUF)
if (Protobuf_FOUND OR PROTOBUF_FOUND)
list(APPEND LINK_LIBRARIES ${PROTOBUF_LIBRARIES})
endif()
endif()
list(APPEND LINK_LIBRARIES ${PROTOBUF_LIBRARIES})

add_library(objtreelite OBJECT ${SOURCES})
add_library(objtreelite_runtime OBJECT ${RUNTIME_SOURCES})
Expand Down
67 changes: 67 additions & 0 deletions cmake/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,70 @@ function(format_gencode_flags flags out)
endforeach()
set(${out} "${${out}}" PARENT_SCOPE)
endfunction(format_gencode_flags flags)

#=============================================================================
# Copyright 2009 Kitware, Inc.
# Copyright 2009-2011 Philip Lowman <[email protected]>
# Copyright 2008 Esben Mose Hansen, Ange Optimization ApS
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
# License text for the above reference.)
function(PROTOBUF_GENERATE_CPP SRCS HDRS)
if(NOT ARGN)
message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
return()
endif()

# Create an include path for each file specified
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endif()
endforeach()

if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(DIR ${PROTOBUF_IMPORT_DIRS})
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endif()
endforeach()
endif()

set(PROTOC_DEPENDENCY ${PROTOBUF_PROTOC_EXECUTABLE})

set(${SRCS})
set(${HDRS})
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)

list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")

add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
"${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
DEPENDS ${ABS_FIL} ${PROTOC_DEPENDENCY}
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
VERBATIM
)
endforeach()

set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()
25 changes: 0 additions & 25 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,28 +164,3 @@ to C++ code will be immediately visible to Python side without re-running
export PYTHONPATH=path/to/treelite/python
python # enter interactive session
.. note:: Compiling with Protocol Buffers support

If your system has Protocol Buffers
(`google/protobuf <https://github.com/google/protobuf>`_) library installed,
treelite will be compiled with Protocol Buffers support. That is, you will
able to read tree ensemble models that had been serialized using Protocol
Buffers. Protocol Buffers support is strictly optional; treelite can be
compiled without it. Should you decide to use Protocol Buffers, you should
specify your ensemble model according to the specification `src/tree.proto
<https://github.com/dmlc/treelite/blob/master/src/tree.proto>`_.

**Binary releases hosted on PyPI have been compiled with Protocol Buffers support.**

**On Windows,** you should specify the root directory containing Protobuf
compilers and libraries by setting the environment variable
``CMAKE_PREFIX_PATH`` as follows:

.. code-block:: dosbatch
mkdir build
cd build
:: Specify location of protobuf (Protocol Buffers)
set CMAKE_PREFIX_PATH=C:\path\to\protobuf
cmake .. -G"Visual Studio 15 2017 Win64"

0 comments on commit b3887a9

Please sign in to comment.