Skip to content

Commit

Permalink
Improve CMakefile of ONNX (onnx#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yinghai Lu authored Mar 3, 2018
1 parent 679d70e commit 8bbeb2a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
44 changes: 28 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ project(onnx C CXX)
set(CMAKE_CXX_STANDARD 11)

set(ONNX_ROOT ${PROJECT_SOURCE_DIR})
if (WIN32)
# parallel build
add_compile_options(/MP)
add_compile_options(/WX)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /WX")
set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /WX")
endif()

# ---[ pybind11
if (NOT TARGET pybind11)
add_subdirectory(third_party/pybind11)
endif()
set(CMAKE_MODULE_PATH "")
list(APPEND CMAKE_MODULE_PATH ${ONNX_ROOT}/cmake/Modules)

# Customized version of find Protobuf. We need to avoid situations mentioned
# in https://github.com/caffe2/caffe2/blob/b7d983f255ef5496474f1ea188edb5e0ac442761/cmake/ProtoBuf.cmake#L82-L92
Expand Down Expand Up @@ -70,7 +61,12 @@ else()
endif()
add_definitions(${MY_ONNX_NAMESPACE})

find_package(PythonInterp REQUIRED)
# Python command
if ("${PYTHON_EXECUTABLE}" STREQUAL "")
set(PYTHON_EXE "python")
else()
set(PYTHON_EXE "${PYTHON_EXECUTABLE}")
endif()

# function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR)
# from https://github.com/tensorflow/tensorflow/blob/d2c3b873c6f8ff999a2e4ee707a84ff00d9c15a5/tensorflow/contrib/cmake/tf_core_framework.cmake
Expand Down Expand Up @@ -117,7 +113,7 @@ function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR)
OUTPUT "${GENERATED_PROTO}"
COMMAND mkdir
ARGS -p ${OUTPUT_PROTO_DIR}
COMMAND ${PYTHON_EXECUTABLE} ${GEN_PROTO_PY}
COMMAND ${PYTHON_EXE} ${GEN_PROTO_PY}
ARGS -p ${ONNX_NAMESPACE} -o ${OUTPUT_PROTO_DIR} ${FILE_WE}
DEPENDS ${INFILE}
COMMENT "Running gen_proto.py on ${INFILE}"
Expand Down Expand Up @@ -175,20 +171,36 @@ add_library(onnx ${onnx_src})
target_include_directories(onnx PUBLIC ${ONNX_ROOT} "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(onnx PUBLIC onnx_proto)

add_library(onnx_pybind "${ONNX_ROOT}/onnx/cpp2py_export.cc")
target_include_directories(onnx_pybind PUBLIC "${CMAKE_CURRENT_BINARY_DIR}" "${PROTOBUF_INCLUDE_DIRS}" "${PYBIND11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}")
target_link_libraries(onnx_pybind PUBLIC onnx_proto onnx)
add_library(onnx_pybind MODULE "${ONNX_ROOT}/onnx/cpp2py_export.cc")
target_include_directories(onnx_pybind PRIVATE "${CMAKE_CURRENT_BINARY_DIR}" "${PROTOBUF_INCLUDE_DIRS}" "${PYTHON_INCLUDE_DIR}")

# pybind11 is a header only lib
find_package(pybind11)
if(pybind11_FOUND)
target_include_directories(onnx_pybind PRIVATE ${pybind11_INCLUDE_DIRS})
else()
target_include_directories(onnx_pybind PRIVATE ${ONNX_ROOT}/third_party/pybind11/include)
endif()
target_link_libraries(onnx_pybind PRIVATE onnx_proto onnx)

# Export include directories
set(ONNX_INCLUDE_DIRS "${ONNX_ROOT}" "${CMAKE_CURRENT_BINARY_DIR}")
set(ONNX_INCLUDE_DIRS ${ONNX_INCLUDE_DIRS} PARENT_SCOPE)

if (WIN32)
target_compile_options(onnx_proto PRIVATE
/MP
/MX
)
target_compile_options(onnx PRIVATE
/MP
/MX
/wd4800 # disable warning type' : forcing value to bool 'true' or 'false' (performance warning)
/wd4503 # identifier' : decorated name length exceeded, name was truncated
)
target_compile_options(onnx_pybind PRIVATE
/MP
/MX
/wd4800 # disable warning type' : forcing value to bool 'true' or 'false' (performance warning)
/wd4503 # identifier' : decorated name length exceeded, name was truncated
)
Expand Down
18 changes: 18 additions & 0 deletions cmake/Modules/Findpybind11.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Try to find the pybind11 library and headers.
# pybind11_FOUND - system has pybind11
# pybind11_INCLUDE_DIRS - the pybind11 include directory

find_path(pybind11_INCLUDE_DIR
NAMES pybind11/pybind11.h
DOC "The directory where pybind11 includes reside"
)

set(pybind11_INCLUDE_DIRS ${pybind11_INCLUDE_DIR})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(pybind11
FOUND_VAR pybind11_FOUND
REQUIRED_VARS pybind11_INCLUDE_DIR
)

mark_as_advanced(pybind11_FOUND)

0 comments on commit 8bbeb2a

Please sign in to comment.