Skip to content

Commit

Permalink
enable protobuf;
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoqing committed Dec 10, 2024
1 parent 188e077 commit 6d0d595
Show file tree
Hide file tree
Showing 31 changed files with 37 additions and 13 deletions.
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ CMAKE_OPTIONS :=
USE_LIBTENSORFLOW_CC := 0
ifeq ($(USE_LIBTENSORFLOW_CC),1)
ifeq ($(CMAKE_OPTIONS),)
CMAKE_OPTIONS += -DCMAKE_PREFIX_PATH="$(REPO_DIR)/third_party/protobuf;$(REPO_DIR)/third_party/libtorch;$(REPO_DIR)/third_party/libtensorflow_cc"
CMAKE_OPTIONS += -DCMAKE_PREFIX_PATH="$(REPO_DIR)/third_party/libtorch;$(REPO_DIR)/third_party/libtensorflow_cc"
endif
else
ifeq ($(CMAKE_OPTIONS),)
CMAKE_OPTIONS += -DCMAKE_PREFIX_PATH="$(REPO_DIR)/third_party/protobuf"
endif
endif

Expand Down Expand Up @@ -114,12 +118,9 @@ source-all: FORCE
$(call message, Source)
$(CMAKE) -B build $(CMAKE_OPTIONS)

PROTO_FILES := $(shell env -C $(REPO_DIR)/src/TensorflowCpy/source/ find ./ -type f -name '*.proto')
source-sample: FORCE
$(call message, Source)
$(CMAKE) -S sample -B build/sample $(CMAKE_OPTIONS)
$(call message, Generating Protobuf)
env -C $(REPO_DIR)/src/TensorflowCpy/source/ $(THIRD_PARTY_DIR)/protobuf/bin/protoc --proto_path ./ --cpp_out ../include/tensorflow_cpy/ $(PROTO_FILES)

build-sample: source-sample
$(call message, Build)
Expand All @@ -132,6 +133,14 @@ doc: FORCE
$(CMAKE) --build build/doc --target GenerateDocs


#* Protobuf
.PHONY: protobuf-install
protobuf-install:
mkdir -p $(THIRD_PARTY_DIR)
-env -C $(THIRD_PARTY_DIR) git clone -b v3.9.2 https://github.com/protocolbuffers/protobuf protobuf.src
env -C $(THIRD_PARTY_DIR)/protobuf.src cmake -S cmake -B build -DCMAKE_INSTALL_PREFIX=$(THIRD_PARTY_DIR)/protobuf -Dprotobuf_BUILD_TESTS=OFF
env -C $(THIRD_PARTY_DIR)/protobuf.src cmake --build build --target install

#* Poetry
.PHONY: poetry-download
poetry-download:
Expand Down Expand Up @@ -258,7 +267,6 @@ build-dist:
.PHONY: build-remove
build-remove:
rm -rf $(REPO_DIR)/dist/
find $(REPO_DIR)/src/TensorflowCpy/include/ -name '*.pb.cc' -or -name '*.pb.h' -exec rm {} \;
rm -rf $(REPO_DIR)/build/

.PHONY: cleanup
Expand Down
13 changes: 5 additions & 8 deletions src/TensorflowCpy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.14...3.22)

# ---- Project ----

# Note: update this to your new project's name and version
project(TensorflowCpy LANGUAGES CXX)

set(PROJECT_SNAKE_NAME tensorflow_cpy)
Expand All @@ -22,7 +21,7 @@ else()
if(USE_LIBTENSORFLOW_CC)
find_package(TensorFlow REQUIRED)
else()
#find_package(Protobuf REQUIRED)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/proto")
endif()
endif()

Expand All @@ -31,7 +30,9 @@ endif()
# Note: globbing sources is considered bad practice as CMake's generators may not detect new files
# automatically. Keep that in mind when changing files, or explicitly mention them here.
file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/**/*.h")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
file(GLOB_RECURSE sources CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp"
)

# ---- Create library ----
# Note: for header-only libraries change all PUBLIC flags to INTERFACE and create an interface
Expand All @@ -53,10 +54,6 @@ else()
target_include_directories(
${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/tensorflow_cpy>
)
target_include_directories(
${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/../../third_party/protobuf/include>
)
#target_include_directories(${PROJECT_NAME} PRIVATE ${Protobuf_INCLUDE_DIRS})
endif()
endif()

Expand All @@ -67,6 +64,6 @@ else()
if(USE_LIBTENSORFLOW_CC)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TensorFlow_LIBRARIES})
else()
#target_link_libraries(${PROJECT_NAME} PRIVATE ${Protobuf_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PRIVATE proto-objects)
endif()
endif()
19 changes: 19 additions & 0 deletions src/TensorflowCpy/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
find_package(Protobuf CONFIG REQUIRED)

file(GLOB_RECURSE protos CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/tensorflow/**/*.proto"
)

add_library(proto-objects OBJECT ${protos})
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/protobuf-generated")
make_directory(${PROTO_BINARY_DIR})

protobuf_generate(
TARGET proto-objects
LANGUAGE cpp
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
IMPORT_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/tensorflow"
)

target_include_directories(proto-objects PUBLIC "$<BUILD_INTERFACE:${PROTO_BINARY_DIR}>")
target_link_libraries(proto-objects PUBLIC protobuf::libprotobuf)

0 comments on commit 6d0d595

Please sign in to comment.