Skip to content

Commit

Permalink
build: various cmake cleanups (#657)
Browse files Browse the repository at this point in the history
* Those changes allows to use src/CMakeLists.txt directly
  within a project that has its own toolchain and options.
* factorize arm and x86 cmake code,
* allow unix and android build for any kind of arm combination
* Introduce NCNN_CMAKE_VERBOSE option to reduce cmake verbosity
* use modern cmake for OpenMP detection and usage
  • Loading branch information
ayounes-nviso authored and nihui committed Nov 20, 2018
1 parent 250f6b8 commit a2611f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ option(NCNN_OPENCV "minimal opencv structure emulation" OFF)
option(NCNN_BENCHMARK "print benchmark information for every layer" OFF)
option(NCNN_PIXEL "convert and resize from/to image pixel" ON)
option(NCNN_PIXEL_ROTATE "rotate image pixel orientation" OFF)
option(NCNN_CMAKE_VERBOSE "print verbose cmake messages" OFF)

if(NCNN_OPENMP)
find_package(OpenMP)
if(OpenMP_CXX_FOUND OR OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# For CMake < 3.9, we need to make the target ourselves
if(NOT TARGET OpenMP::OpenMP_CXX)
find_package(Threads REQUIRED)
add_library(OpenMP::OpenMP_CXX IMPORTED INTERFACE)
set_property(TARGET OpenMP::OpenMP_CXX
PROPERTY INTERFACE_COMPILE_OPTIONS ${OpenMP_CXX_FLAGS})
# Only works if the same flag is passed to the linker; use CMake 3.9+ otherwise (Intel, AppleClang)
set_property(TARGET OpenMP::OpenMP_CXX
PROPERTY INTERFACE_LINK_LIBRARIES ${OpenMP_CXX_FLAGS} Threads::Threads)
endif()
endif()

Expand Down
49 changes: 25 additions & 24 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,38 @@ macro(ncnn_add_layer class)
option(WITH_LAYER_${name} "build with layer ${name}" ON)
endif()

message("WITH_LAYER_${name} = ${WITH_LAYER_${name}}")
if(NCNN_CMAKE_VERBOSE)
message(STATUS "WITH_LAYER_${name} = ${WITH_LAYER_${name}}")
endif()

if(WITH_LAYER_${name})
list(APPEND ncnn_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/layer/${name}.cpp")

# look for arch specific implementation and append source
# optimized implementation for armv7 aarch64
if((ANDROID AND ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a"))
OR (ANDROID AND ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64"))
OR (UNIX AND ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l"))
OR (UNIX AND ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64"))
OR (IOS AND ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "armv7"))
OR (IOS AND ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "arm64"))
OR (IOS AND ("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "armv7;arm64")))
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/layer/arm/${name}_arm.cpp")
list(APPEND ncnn_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/layer/arm/${name}_arm.cpp")
set(WITH_LAYER_${name}_arm 1)
endif()
# optimized implementation for armv7, aarch64 or x86
if((IOS AND ${CMAKE_OSX_ARCHITECTURES} MATCHES "arm")
OR (CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm|aarch64)"))
set(arch arm)
else()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/layer/x86/${name}_x86.cpp")
list(APPEND ncnn_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/layer/x86/${name}_x86.cpp")
set(WITH_LAYER_${name}_x86 1)
set(arch x86)
endif()
set(LAYER_SRC ${CMAKE_CURRENT_SOURCE_DIR}/layer/${arch}/${name}_${arch}.cpp)
if(EXISTS ${LAYER_SRC})
set(WITH_LAYER_${name}_${arch} 1)
list(APPEND ncnn_SRCS ${LAYER_SRC})
if(NCNN_CMAKE_VERBOSE)
message(STATUS "Adding layer: ${LAYER_SRC}")
endif()
endif()
endif()

# generate layer_declaration and layer_registry file
if(WITH_LAYER_${name})
if(WITH_LAYER_${name}_arm)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/layer_declaration.h
"extern Layer* ${class}_arm_layer_creator();\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/layer_registry.h
"#if NCNN_STRING\n{\"${class}\",${class}_arm_layer_creator},\n#else\n{${class}_arm_layer_creator},\n#endif\n")
elseif(WITH_LAYER_${name}_x86)
if(WITH_LAYER_${name}_${arch})
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/layer_declaration.h
"extern Layer* ${class}_x86_layer_creator();\n")
"extern Layer* ${class}_${arch}_layer_creator();\n")
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/layer_registry.h
"#if NCNN_STRING\n{\"${class}\",${class}_x86_layer_creator},\n#else\n{${class}_x86_layer_creator},\n#endif\n")
"#if NCNN_STRING\n{\"${class}\",${class}_${arch}_layer_creator},\n#else\n{${class}_${arch}_layer_creator},\n#endif\n")
else()
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/layer_declaration.h
"extern Layer* ${class}_layer_creator();\n")
Expand Down Expand Up @@ -155,6 +149,13 @@ ncnn_add_layer(PSROIPooling)

add_library(ncnn STATIC ${ncnn_SRCS})

if(NCNN_OPENMP AND OpenMP_CXX_FOUND)
if(NCNN_CMAKE_VERBOSE)
message("Building with OpenMP")
endif()
target_link_libraries(ncnn PUBLIC OpenMP::OpenMP_CXX)
endif()

if(COVERAGE)
target_compile_options(ncnn PRIVATE --coverage)
endif()
Expand Down

0 comments on commit a2611f4

Please sign in to comment.