diff --git a/CMakeLists.txt b/CMakeLists.txt index 0db49c77f73..0de20e5ed8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,11 +103,6 @@ else() set(ONNX_NAMESPACE "onnx") set(MY_ONNX_NAMESPACE "-DONNX_NAMESPACE=onnx") endif() -add_definitions(${MY_ONNX_NAMESPACE}) - -if(ONNX_ML) - add_definitions("-DONNX_ML=1") -endif() # function(RELATIVE_PROTOBUF_GENERATE_CPP SRCS HDRS ROOT_DIR) from https://githu # b.com/tensorflow/tensorflow/blob/d2c3b873c6f8ff999a2e4ee707a84ff00d9c15a5/tens @@ -276,11 +271,19 @@ if(TARGET protobuf::libprotobuf) else() target_link_libraries(onnx_proto PUBLIC ${PROTOBUF_LIBRARIES}) endif() +target_compile_definitions(onnx_proto PUBLIC ${MY_ONNX_NAMESPACE}) add_library(onnx ${onnx_src}) target_include_directories(onnx PUBLIC ${ONNX_ROOT} "${CMAKE_CURRENT_BINARY_DIR}") target_link_libraries(onnx PUBLIC onnx_proto) +# interface compile options of onnx_proto will also take effect here, but I think setting it explicitly is better. +target_compile_definitions(onnx PUBLIC ${MY_ONNX_NAMESPACE}) + +if(ONNX_ML) + target_compile_definitions(onnx_proto PUBLIC ONNX_ML=1) + target_compile_definitions(onnx PUBLIC ONNX_ML=1) +endif() if(BUILD_ONNX_PYTHON) if("${PY_EXT_SUFFIX}" STREQUAL "") @@ -298,6 +301,8 @@ if(BUILD_ONNX_PYTHON) set_target_properties(onnx_cpp2py_export PROPERTIES SUFFIX ${PY_EXT_SUFFIX}) set_target_properties(onnx_cpp2py_export PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) + # Setting compile flags about namespace explicitly + target_compile_definitions(onnx_cpp2py_export PUBLIC ${MY_ONNX_NAMESPACE}) target_include_directories(onnx_cpp2py_export PRIVATE "${ONNX_ROOT}" "${CMAKE_CURRENT_BINARY_DIR}" @@ -372,6 +377,9 @@ if(BUILD_ONNX_PYTHON) ${EXTRA_FLAGS}) target_compile_options(onnx_cpp2py_export PRIVATE /MT) endif() + if (ONNX_ML) + target_compile_definitions(onnx_cpp2py_export PUBLIC ONNX_ML=1) + endif() endif() if(ONNX_BUILD_BENCHMARKS)