Skip to content

Commit

Permalink
[fix] put generated pb files into a lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ouonline committed Apr 16, 2024
1 parent ed3c410 commit 73dbd8d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
29 changes: 15 additions & 14 deletions cmake/onnx_model.cmake
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
file(GLOB_RECURSE __PPLNN_MODEL_ONNX_SRC__ src/ppl/nn/models/onnx/*.cc)
list(REMOVE_ITEM __PPLNN_MODEL_ONNX_SRC__ ${PROJECT_SOURCE_DIR}/src/ppl/nn/models/onnx/generated/onnx.pb.cc)

if(PPLNN_PROTOBUF_VERSION AND NOT PPLNN_ONNX_GENERATED_LIBS)
message(FATAL_ERROR "`PPLNN_PROTOBUF_VERSION` is set to be [${PPLNN_PROTOBUF_VERSION}], but `PPLNN_ONNX_GENERATED_LIBS` is not set.")
endif()

# replace default *.pb.* files
if(PPLNN_ONNX_GENERATED_LIBS)
list(REMOVE_ITEM __PPLNN_MODEL_ONNX_SRC__ ${PROJECT_SOURCE_DIR}/src/ppl/nn/models/onnx/generated/onnx.pb.cc)
if(NOT TARGET libprotobuf)
include(cmake/protobuf.cmake)
endif()

# use default *.pb.* files
if(NOT PPLNN_ONNX_GENERATED_LIBS)
add_library(pplnn_onnx_pb_generated_static ${PROJECT_SOURCE_DIR}/src/ppl/nn/models/onnx/generated/onnx.pb.cc)
target_link_libraries(pplnn_onnx_pb_generated_static PUBLIC libprotobuf)
target_include_directories(pplnn_onnx_pb_generated_static PUBLIC
${protobuf_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/ppl/nn/models/onnx/generated)
set(PPLNN_ONNX_GENERATED_LIBS pplnn_onnx_pb_generated_static)
endif()

# if external sources are set, remove `default_register_resources.cc`
Expand All @@ -18,22 +28,13 @@ add_library(pplnn_onnx_static STATIC ${__PPLNN_MODEL_ONNX_SRC__} ${PPLNN_SOURCE_

unset(__PPLNN_MODEL_ONNX_SRC__)

if(PPLNN_ONNX_GENERATED_LIBS)
target_link_libraries(pplnn_onnx_static PRIVATE ${PPLNN_ONNX_GENERATED_LIBS})
else()
target_include_directories(pplnn_onnx_static PRIVATE ${PROJECT_SOURCE_DIR}/src/ppl/nn/models/onnx/generated)
endif()

target_compile_definitions(pplnn_onnx_static PUBLIC PPLNN_ENABLE_ONNX_MODEL)
target_link_libraries(pplnn_onnx_static PUBLIC pplnn_basic_static)

include(cmake/protobuf.cmake)
target_link_libraries(pplnn_onnx_static PUBLIC libprotobuf)
target_include_directories(pplnn_onnx_static PRIVATE ${protobuf_SOURCE_DIR}/src)
target_link_libraries(pplnn_onnx_static PRIVATE ${PPLNN_ONNX_GENERATED_LIBS})

target_link_libraries(pplnn_static INTERFACE pplnn_onnx_static)

if(PPLNN_INSTALL)
install(DIRECTORY include/ppl/nn/models/onnx DESTINATION include/ppl/nn/models)
install(TARGETS pplnn_onnx_static DESTINATION lib)
install(TARGETS pplnn_onnx_static ${PPLNN_ONNX_GENERATED_LIBS} DESTINATION lib)
endif()
20 changes: 14 additions & 6 deletions cmake/pplnn-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,14 @@ if(PPLNN_USE_LLM_CUDA)
list(APPEND __LINK_LIBS__ ${NCCL_LIBRARIES})
endif()

get_filename_component(__LIB_PATH__ "${__PPLNN_PACKAGE_ROOTDIR__}/lib/@HPCC_STATIC_LIB_PREFIX@ppl_llm_cuda_static@HPCC_STATIC_LIB_SUFFIX@" ABSOLUTE)
get_filename_component(__PPLNN_LIB_PATH__ "${__PPLNN_PACKAGE_ROOTDIR__}/lib/@HPCC_STATIC_LIB_PREFIX@ppl_llm_cuda_static@HPCC_STATIC_LIB_SUFFIX@" ABSOLUTE)
add_library(ppl_llm_cuda_static STATIC IMPORTED)
set_target_properties(ppl_llm_cuda_static PROPERTIES
INTERFACE_LINK_LIBRARIES "${__LINK_LIBS__}"
IMPORTED_LOCATION "${__LIB_PATH__}"
IMPORTED_LOCATION_DEBUG "${__LIB_PATH__}"
IMPORTED_LOCATION_RELEASE "${__LIB_PATH__}")
unset(__LIB_PATH__)
IMPORTED_LOCATION "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_DEBUG "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_RELEASE "${__PPLNN_LIB_PATH__}")
unset(__PPLNN_LIB_PATH__)

unset(__LINK_LIBS__)

Expand All @@ -147,10 +147,18 @@ if(PPLNN_ENABLE_ONNX_MODEL)
include(${__PPLNN_PACKAGE_ROOTDIR__}/lib/cmake/protobuf/protobuf-config.cmake)
endif()

get_filename_component(__PPLNN_LIB_PATH__ "${__PPLNN_PACKAGE_ROOTDIR__}/lib/@HPCC_STATIC_LIB_PREFIX@@PPLNN_ONNX_GENERATED_LIBS@@HPCC_STATIC_LIB_SUFFIX@" ABSOLUTE)
add_library(pplnn_onnx_generated_static STATIC IMPORTED)
set_target_properties(pplnn_onnx_generated_static PROPERTIES
INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf"
IMPORTED_LOCATION "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_DEBUG "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_RELEASE "${__PPLNN_LIB_PATH__}")

get_filename_component(__PPLNN_LIB_PATH__ "${__PPLNN_PACKAGE_ROOTDIR__}/lib/@HPCC_STATIC_LIB_PREFIX@pplnn_onnx_static@HPCC_STATIC_LIB_SUFFIX@" ABSOLUTE)
add_library(pplnn_onnx_static STATIC IMPORTED)
set_target_properties(pplnn_onnx_static PROPERTIES
INTERFACE_LINK_LIBRARIES "pplnn_basic_static;protobuf::libprotobuf"
INTERFACE_LINK_LIBRARIES "pplnn_basic_static;pplnn_onnx_generated_static"
IMPORTED_LOCATION "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_DEBUG "${__PPLNN_LIB_PATH__}"
IMPORTED_LOCATION_RELEASE "${__PPLNN_LIB_PATH__}")
Expand Down

0 comments on commit 73dbd8d

Please sign in to comment.