diff --git a/CMakeLists.txt b/CMakeLists.txt index aea7ce6d3981..a5db5d9e923e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,14 +125,15 @@ file(GLOB SOURCES ) if (USE_PROTO) + if(MSVC) + message(FATAL_ERROR "Cannot use proto with MSVC.") + endif(MSVC) find_package(Protobuf REQUIRED) PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS proto/model.proto) include_directories(${PROTOBUF_INCLUDE_DIRS}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) + ADD_DEFINITIONS(-DUSE_PROTO) SET(PROTO_FILES src/proto/gbdt_model_proto.cpp ${PROTO_HDRS} ${PROTO_SRCS}) -else() - include_directories(src/proto/not_implemented) - SET(PROTO_FILES src/proto/not_implemented/gbdt_model_proto.cpp) endif(USE_PROTO) add_executable(lightgbm src/main.cpp ${SOURCES} ${PROTO_FILES}) diff --git a/include/LightGBM/boosting.h b/include/LightGBM/boosting.h index 04a73504cef0..fca7f17b497d 100644 --- a/include/LightGBM/boosting.h +++ b/include/LightGBM/boosting.h @@ -3,7 +3,10 @@ #include #include + +#ifdef USE_PROTO #include "model.pb.h" +#endif // USE_PROTO #include #include @@ -188,6 +191,7 @@ class LIGHTGBM_EXPORT Boosting { */ virtual bool LoadModelFromString(const std::string& model_str) = 0; + #ifdef USE_PROTO /*! * \brief Save model with protobuf * \param num_iterations Number of model that want to save, -1 means save all @@ -201,6 +205,7 @@ class LIGHTGBM_EXPORT Boosting { * \return true if succeeded */ virtual bool LoadModelFromProto(const char* filename) = 0; + #endif // USE_PROTO /*! * \brief Calculate feature importances diff --git a/include/LightGBM/tree.h b/include/LightGBM/tree.h index 8b3f9f5c79cd..471b8370b428 100644 --- a/include/LightGBM/tree.h +++ b/include/LightGBM/tree.h @@ -3,7 +3,9 @@ #include #include +#ifdef USE_PROTO #include "model.pb.h" +#endif // USE_PROTO #include #include @@ -31,12 +33,13 @@ class Tree { * \param str Model string */ explicit Tree(const std::string& str); - + #ifdef USE_PROTO /*! * \brief Construtor, from a protobuf object * \param model_tree Model protobuf object */ - explicit Tree(const LightGBM::Model_Tree& model_tree); + explicit Tree(const Model_Tree& model_tree); + #endif // USE_PROTO ~Tree(); @@ -172,8 +175,10 @@ class Tree { /*! \brief Serialize this object to if-else statement*/ std::string ToIfElse(int index, bool is_predict_leaf_index) const; + #ifdef USE_PROTO /*! \brief Serialize this object to protobuf object*/ void ToProto(Model_Tree& model_tree) const; + #endif // USE_PROTO inline static bool IsZero(double fval) { if (fval > -kZeroAsMissingValueRange && fval <= kZeroAsMissingValueRange) { diff --git a/src/application/application.cpp b/src/application/application.cpp index 3924132ef682..c0637dea9b13 100644 --- a/src/application/application.cpp +++ b/src/application/application.cpp @@ -215,7 +215,11 @@ void Application::Train() { if (model_format == std::string("text")) { boosting_->SaveModelToFile(-1, save_file_name.c_str()); } else if (model_format == std::string("proto")) { + #ifdef USE_PROTO boosting_->SaveModelToProto(-1, save_file_name.c_str()); + #else + Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); + #endif // USE_PROTO } else { Log::Fatal("Unknown model format during saving: %s", model_format.c_str()); } diff --git a/src/boosting/boosting.cpp b/src/boosting/boosting.cpp index 01daed4af3b1..a21e8b28944a 100644 --- a/src/boosting/boosting.cpp +++ b/src/boosting/boosting.cpp @@ -25,9 +25,13 @@ bool Boosting::LoadFileToBoosting(Boosting* boosting, const std::string& format, return false; } } else if (format == std::string("proto")) { + #ifdef USE_PROTO if (!boosting->LoadModelFromProto(filename)) { return false; } + #else + Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); + #endif // USE_PROTO } else { Log::Fatal("Unknown model format during loading: %s", format.c_str()); } diff --git a/src/boosting/gbdt.h b/src/boosting/gbdt.h index 3bdb640a8e3c..5b4893e594a5 100644 --- a/src/boosting/gbdt.h +++ b/src/boosting/gbdt.h @@ -236,6 +236,7 @@ class GBDT: public GBDTBase { */ bool LoadModelFromString(const std::string& model_str) override; + #ifdef USE_PROTO /*! * \brief Save model with protobuf * \param num_iterations Number of model that want to save, -1 means save all @@ -249,6 +250,7 @@ class GBDT: public GBDTBase { * \return true if succeeded */ bool LoadModelFromProto(const char* filename) override; + #endif // USE_PROTO /*! * \brief Calculate feature importances diff --git a/src/proto/not_implemented/gbdt_model_proto.cpp b/src/proto/not_implemented/gbdt_model_proto.cpp deleted file mode 100644 index 3e5d45901f8c..000000000000 --- a/src/proto/not_implemented/gbdt_model_proto.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "../../boosting/gbdt.h" - -namespace LightGBM { - -void GBDT::SaveModelToProto(int, const char*) const { - Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); -} - -bool GBDT::LoadModelFromProto(const char*) { - Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); - return false; -} - -void Tree::ToProto(LightGBM::Model_Tree&) const { - Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); -} - -Tree::Tree(const LightGBM::Model_Tree&) { - Log::Fatal("Please cmake with -DUSE_PROTO=ON to use protobuf."); -} - -} // namespace LightGBM diff --git a/src/proto/not_implemented/model.pb.h b/src/proto/not_implemented/model.pb.h deleted file mode 100644 index be7465515add..000000000000 --- a/src/proto/not_implemented/model.pb.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef PROTOBUF_model_2eproto__INCLUDED -#define PROTOBUF_model_2eproto__INCLUDED - -namespace LightGBM { - class Model; - class Model_Tree; -} // namespace LightGBM - -#endif // PROTOBUF_model_2eproto__INCLUDED diff --git a/windows/LightGBM.vcxproj b/windows/LightGBM.vcxproj index 7affdd55c496..5731a92a0ffb 100644 --- a/windows/LightGBM.vcxproj +++ b/windows/LightGBM.vcxproj @@ -242,15 +242,12 @@ - - - diff --git a/windows/LightGBM.vcxproj.filters b/windows/LightGBM.vcxproj.filters index 953b985558f4..558dc03c3ce6 100644 --- a/windows/LightGBM.vcxproj.filters +++ b/windows/LightGBM.vcxproj.filters @@ -195,9 +195,6 @@ src\boosting - - src\proto\not_implemented - @@ -284,11 +281,5 @@ src\boosting - - src\proto - - - src\proto\not_implemented - \ No newline at end of file