From 8bbeb2ae453a113f6557afe5048d428a69af8bbb Mon Sep 17 00:00:00 2001 From: Yinghai Lu Date: Fri, 2 Mar 2018 16:44:55 -0800 Subject: [PATCH] Improve CMakefile of ONNX (#563) --- CMakeLists.txt | 44 ++++++++++++++++++++------------ cmake/Modules/Findpybind11.cmake | 18 +++++++++++++ 2 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 cmake/Modules/Findpybind11.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5284b2cc305..25e40e1396c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -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 @@ -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}" @@ -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 ) diff --git a/cmake/Modules/Findpybind11.cmake b/cmake/Modules/Findpybind11.cmake new file mode 100644 index 00000000000..056ed1eab04 --- /dev/null +++ b/cmake/Modules/Findpybind11.cmake @@ -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)