Skip to content

Commit

Permalink
cmake (onnx#1233)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrphercule authored and Yinghai Lu committed Jul 26, 2018
1 parent 8594b92 commit c761845
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 147 deletions.
49 changes: 5 additions & 44 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Minimum CMake required
cmake_minimum_required(VERSION 3.1)

include(cmake/Utils.cmake)
# Set default build type
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "Build type not set - defaulting to Release")
Expand Down Expand Up @@ -420,23 +420,8 @@ if(MSVC)
/wd4146 # unary minus operator applied to
# unsigned type, result still unsigned
${EXTRA_FLAGS})
if(${ONNX_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnx_proto PRIVATE /MTd)
target_compile_options(onnx PRIVATE /MTd)
else()
target_compile_options(onnx_proto PRIVATE /MT)
target_compile_options(onnx PRIVATE /MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnx_proto PRIVATE /MDd)
target_compile_options(onnx PRIVATE /MDd)
else()
target_compile_options(onnx_proto PRIVATE /MD)
target_compile_options(onnx PRIVATE /MD)
endif()
endif()
add_msvc_runtime_flag(onnx_proto)
add_msvc_runtime_flag(onnx)
set(onnx_static_library_flags
-IGNORE:4221 # LNK4221: This object file does not define any previously
# undefined public symbols, so it will not be used by any
Expand Down Expand Up @@ -472,19 +457,7 @@ set_target_properties(onnxifi_loader
NO)
target_link_libraries(onnxifi_loader PUBLIC onnxifi ${CMAKE_DL_LIBS})
if(MSVC)
if(${ONNX_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnxifi_loader PRIVATE /MTd)
else()
target_compile_options(onnxifi_loader PRIVATE /MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnxifi_loader PRIVATE /MDd)
else()
target_compile_options(onnxifi_loader PRIVATE /MD)
endif()
endif()
add_msvc_runtime_flag(onnxifi_loader)
endif()
target_include_directories(onnxifi_loader PUBLIC onnx)
target_include_directories(onnxifi_loader PUBLIC ${ONNX_ROOT})
Expand All @@ -496,19 +469,7 @@ target_include_directories(onnxifi_dummy PUBLIC onnx)
target_include_directories(onnxifi_dummy PUBLIC ${ONNX_ROOT})
target_compile_definitions(onnxifi_dummy PRIVATE ONNXIFI_BUILD_LIBRARY=TRUE)
if(MSVC)
if(${ONNX_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnxifi_dummy PRIVATE /MTd)
else()
target_compile_options(onnxifi_dummy PRIVATE /MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(onnxifi_dummy PRIVATE /MDd)
else()
target_compile_options(onnxifi_dummy PRIVATE /MD)
endif()
endif()
add_msvc_runtime_flag(onnxifi_dummy)
endif()
install(TARGETS onnx onnx_proto DESTINATION lib)
install(DIRECTORY ${ONNX_ROOT}/onnx
Expand Down
17 changes: 17 additions & 0 deletions cmake/Utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Add MSVC RunTime Flag
function(add_msvc_runtime_flag lib)
if(${ONNX_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(${lib} PRIVATE /MTd)
else()
target_compile_options(${lib} PRIVATE /MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(${lib} PRIVATE /MDd)
else()
target_compile_options(${lib} PRIVATE /MD)
endif()
endif()
endfunction()
199 changes: 96 additions & 103 deletions cmake/unittest.cmake
Original file line number Diff line number Diff line change
@@ -1,103 +1,96 @@
set(UT_NAME ${PROJECT_NAME}_gtests)
set(ONNX_ROOT ${PROJECT_SOURCE_DIR})

find_package(Threads)

function(add_whole_archive_flag lib output_var)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(${output_var} -Wl,-force_load,$<TARGET_FILE:${lib}> PARENT_SCOPE)
elseif(MSVC)
# In MSVC, we will add whole archive in default.
set(${output_var} -WHOLEARCHIVE:$<SHELL_PATH:$<TARGET_FILE:${lib}>> PARENT_SCOPE)
else()
# Assume everything else is like gcc
set(${output_var} "-Wl,--whole-archive $<TARGET_FILE:${lib}> -Wl,--no-whole-archive" PARENT_SCOPE)
endif()
endfunction()

set(${UT_NAME}_libs
${googletest_STATIC_LIBRARIES}
)

add_whole_archive_flag(onnx tmp)
list(APPEND ${UT_NAME}_libs ${tmp})
list(APPEND ${UT_NAME}_libs onnx_proto)
list(APPEND ${UT_NAME}_libs onnxifi_loader)
list(APPEND ${UT_NAME}_libs ${PROTOBUF_LIBRARIES})

file(GLOB_RECURSE ${UT_NAME}_src
"${ONNX_ROOT}/onnx/test/cpp/*.cc"
)

function(AddTest)
cmake_parse_arguments(_UT "" "TARGET" "LIBS;SOURCES" ${ARGN})

list(REMOVE_DUPLICATES _UT_LIBS)
list(REMOVE_DUPLICATES _UT_SOURCES)

add_executable(${_UT_TARGET} ${_UT_SOURCES})
add_dependencies(${_UT_TARGET} onnx onnx_proto googletest)

target_include_directories(${_UT_TARGET} PUBLIC ${googletest_INCLUDE_DIRS} ${ONNX_INCLUDE_DIRS} ${PROTOBUF_INCLUDE_DIRS} ${ONNX_ROOT} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${_UT_TARGET} ${_UT_LIBS} ${CMAKE_THREAD_LIBS_INIT})
if(TARGET protobuf::libprotobuf)
target_link_libraries(${_UT_TARGET} protobuf::libprotobuf)
else()
target_link_libraries(${_UT_TARGET} ${PROTOBUF_LIBRARIES})
endif()

if (WIN32)
target_compile_options(${_UT_TARGET} PRIVATE
/EHsc # exception handling - C++ may throw, extern "C" will not
)
if(${ONNX_USE_MSVC_STATIC_RUNTIME})
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(${_UT_TARGET} PRIVATE /MTd)
else()
target_compile_options(${_UT_TARGET} PRIVATE /MT)
endif()
else()
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
target_compile_options(${_UT_TARGET} PRIVATE /MDd)
else()
target_compile_options(${_UT_TARGET} PRIVATE /MD)
endif()
endif()

endif()

if(MSVC)
target_compile_options(${_UT_TARGET} PRIVATE
/wd4146 # unary minus operator applied to unsigned type, result still unsigned from include\google\protobuf\wire_format_lite.h
)
endif()

set(TEST_ARGS)
if (ONNX_GENERATE_TEST_REPORTS)
# generate a report file next to the test program
list(APPEND TEST_ARGS
"--gtest_output=xml:$<SHELL_PATH:$<TARGET_FILE:${_UT_TARGET}>.$<CONFIG>.results.xml>")
endif()

add_test(NAME ${_UT_TARGET}
COMMAND ${_UT_TARGET} ${TEST_ARGS}
WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>
)

endfunction(AddTest)

AddTest(
TARGET ${UT_NAME}
SOURCES ${${UT_NAME}_src}
LIBS ${${UT_NAME}_libs}
)

set(TEST_DATA_SRC ${ONNX_ROOT}/onnx/test/cpp/testdata)
set(TEST_DATA_DES $<TARGET_FILE_DIR:${UT_NAME}>/testdata)

# Copy test data from source to destination.
add_custom_command(
TARGET ${UT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${TEST_DATA_SRC}
${TEST_DATA_DES})
set(UT_NAME ${PROJECT_NAME}_gtests)
set(ONNX_ROOT ${PROJECT_SOURCE_DIR})

include(${ONNX_ROOT}/cmake/Utils.cmake)

find_package(Threads)

function(add_whole_archive_flag lib output_var)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(${output_var} -Wl,-force_load,$<TARGET_FILE:${lib}> PARENT_SCOPE)
elseif(MSVC)
# In MSVC, we will add whole archive in default.
set(${output_var} -WHOLEARCHIVE:$<SHELL_PATH:$<TARGET_FILE:${lib}>>
PARENT_SCOPE)
else()
# Assume everything else is like gcc
set(${output_var}
"-Wl,--whole-archive $<TARGET_FILE:${lib}> -Wl,--no-whole-archive"
PARENT_SCOPE)
endif()
endfunction()

set(${UT_NAME}_libs ${googletest_STATIC_LIBRARIES})

add_whole_archive_flag(onnx tmp)
list(APPEND ${UT_NAME}_libs ${tmp})
list(APPEND ${UT_NAME}_libs onnx_proto)
list(APPEND ${UT_NAME}_libs onnxifi_loader)
list(APPEND ${UT_NAME}_libs ${PROTOBUF_LIBRARIES})

file(GLOB_RECURSE ${UT_NAME}_src "${ONNX_ROOT}/onnx/test/cpp/*.cc")

function(AddTest)
cmake_parse_arguments(_UT "" "TARGET" "LIBS;SOURCES" ${ARGN})

list(REMOVE_DUPLICATES _UT_LIBS)
list(REMOVE_DUPLICATES _UT_SOURCES)

add_executable(${_UT_TARGET} ${_UT_SOURCES})
add_dependencies(${_UT_TARGET} onnx onnx_proto googletest)

target_include_directories(${_UT_TARGET}
PUBLIC ${googletest_INCLUDE_DIRS}
${ONNX_INCLUDE_DIRS}
${PROTOBUF_INCLUDE_DIRS}
${ONNX_ROOT}
${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(${_UT_TARGET} ${_UT_LIBS} ${CMAKE_THREAD_LIBS_INIT})
if(TARGET protobuf::libprotobuf)
target_link_libraries(${_UT_TARGET} protobuf::libprotobuf)
else()
target_link_libraries(${_UT_TARGET} ${PROTOBUF_LIBRARIES})
endif()

if(WIN32)
target_compile_options(${_UT_TARGET}
PRIVATE /EHsc # exception handling - C++ may throw,
# extern "C" will not
)
add_msvc_runtime_flag(${_UT_TARGET})
endif()

if(MSVC)
target_compile_options(${_UT_TARGET}
PRIVATE /wd4146 # unary minus operator applied to
# unsigned type, result still
# unsigned from include\google\protob
# uf\wire_format_lite.h
)
endif()

set(TEST_ARGS)
if(ONNX_GENERATE_TEST_REPORTS)
# generate a report file next to the test program
list(
APPEND
TEST_ARGS
"--gtest_output=xml:$<SHELL_PATH:$<TARGET_FILE:${_UT_TARGET}>.$<CONFIG>.results.xml>"
)
endif()

add_test(NAME ${_UT_TARGET}
COMMAND ${_UT_TARGET} ${TEST_ARGS}
WORKING_DIRECTORY $<TARGET_FILE_DIR:${_UT_TARGET}>)

endfunction(AddTest)

addtest(TARGET ${UT_NAME} SOURCES ${${UT_NAME}_src} LIBS ${${UT_NAME}_libs})

set(TEST_DATA_SRC ${ONNX_ROOT}/onnx/test/cpp/testdata)
set(TEST_DATA_DES $<TARGET_FILE_DIR:${UT_NAME}>/testdata)

# Copy test data from source to destination.
add_custom_command(TARGET ${UT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${TEST_DATA_SRC}
${TEST_DATA_DES})

0 comments on commit c761845

Please sign in to comment.