提交 9fb6a017 编写于 作者: J jinhui yuan

use a single CMakeLists.txt

上级 1b8d3118
build_oneflow
build_third_party
third_party
build
third_party
\ No newline at end of file
# Minimum CMake required
cmake_minimum_required(VERSION 3.5)
option(BUILD_THIRD_PARTY "Build third party or oneflow" OFF)
# Project
project(oneflow C CXX)
if (BUILD_THIRD_PARTY)
project(third_party C CXX)
else()
project(oneflow C CXX)
endif()
enable_testing()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if (WIN32)
set(CMAKE_BUILD_TYPE Debug)
endif()
set(oneflow_src_dir ${PROJECT_SOURCE_DIR}/oneflow)
set(oneflow_cmake_dir ${PROJECT_SOURCE_DIR}/cmake)
if (WIN32)
else()
list(APPEND CUDA_NVCC_FLAGS "-std=c++11 -w")
endif()
option(PREPARE_THIRD_PARTY "Whether include the third party codes in the solution" OFF)
set(THIRD_PARTY_DIR "${PROJECT_SOURCE_DIR}/third_party")
if (NOT EXISTS ${THIRD_PARTY_DIR})
message(FATAL_ERROR "Please set third party directory by: -DTHIRD_PARTY_DIR=/your/third_party/path")
endif()
set(CMAKE_BUILD_TYPE Debug)
add_definitions(-DNOMINMAX -D_WIN32_WINNT=0x0A00 -DLANG_CXX11 -DCOMPILER_MSVC -D__VERSION__=\"MSVC\")
add_definitions(-DWIN32 -DOS_WIN -D_MBCS -DWIN64 -DWIN32_LEAN_AND_MEAN -DNOGDI -DPLATFORM_WINDOWS)
add_definitions(/bigobj /nologo /EHsc /GF /FC /MP /Gm-)
add_definitions(-DGOOGLE_GLOG_DLL_DECL=)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
# Different set for different platform
if (MSVC)
foreach(flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
......@@ -37,12 +33,14 @@ if (MSVC)
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif()
endforeach()
endif()
if (UNIX)
else()
list(APPEND CUDA_NVCC_FLAGS "-std=c++11 -w")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wno-sign-compare")
endif()
set(THIRD_PARTY_DIR "${PROJECT_SOURCE_DIR}/third_party"
CACHE PATH "Where the third party headers and libs are put")
# Modules
list(APPEND CMAKE_MODULE_PATH ${oneflow_cmake_dir}/third_party)
list(APPEND CMAKE_MODULE_PATH ${oneflow_cmake_dir})
......@@ -50,72 +48,10 @@ include(util)
include(proto2cpp)
include(third_party)
# main cpp
list(APPEND of_main_cc ${oneflow_src_dir}/compiler/compiler.cpp)
list(APPEND of_main_cc ${oneflow_src_dir}/runtime/elf_runner.cpp)
# source_group
SUBDIRLIST(subdir_list ${oneflow_src_dir})
foreach(subdir ${subdir_list})
file(GLOB subdir_headers "${oneflow_src_dir}/${subdir}/*.h")
file(GLOB subdir_cuda_headers "${oneflow_src_dir}/${subdir}/*.cuh")
file(GLOB subdir_obj_cpps "${oneflow_src_dir}/${subdir}/*.cpp")
file(GLOB subdir_obj_cus "${oneflow_src_dir}/${subdir}/*.cu")
file(GLOB subdir_test_cpps "${oneflow_src_dir}/${subdir}/*_test.cpp")
file(GLOB subdir_protos "${oneflow_src_dir}/${subdir}/*.proto")
foreach(test_cpp ${subdir_test_cpps})
list(REMOVE_ITEM subdir_obj_cpps ${test_cpp})
endforeach()
foreach(main_cpp ${of_main_cc})
list(REMOVE_ITEM subdir_obj_cpps ${main_cpp})
endforeach()
source_group(${subdir} FILES ${subdir_headers} ${subdir_cuda_headers} ${subdir_obj_cpps} ${subdir_obj_cus} {subdir_protos})
list(APPEND of_all_obj_cc ${subdir_headers} ${subdir_cuda_headers} ${subdir_obj_cpps} ${subdir_obj_cus})
list(APPEND of_all_proto ${subdir_protos})
list(APPEND of_all_test_cc ${subdir_test_cpps})
endforeach()
# proto obj lib
foreach(proto_name ${of_all_proto})
file(RELATIVE_PATH proto_rel_name ${oneflow_src_dir} ${proto_name})
list(APPEND of_all_rel_protos ${proto_rel_name})
endforeach()
RELATIVE_PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS
${oneflow_src_dir}
${of_all_rel_protos})
cuda_add_library(of_protoobj ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(of_protoobj ${oneflow_third_party_libs})
# cc obj lib
include_directories(${oneflow_src_dir})
include_directories(${PROJECT_BINARY_DIR})
cuda_add_library(of_ccobj ${of_all_obj_cc})
target_link_libraries(of_ccobj ${oneflow_third_party_libs})
add_dependencies(of_ccobj of_protoobj)
if(APPLE)
set(of_libs -Wl,-force_load of_ccobj of_protoobj)
elseif(UNIX)
set(of_libs -Wl,--whole-archive of_ccobj of_protoobj -Wl,--no-whole-archive)
elseif(WIN32)
set(of_libs of_ccobj of_protoobj)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /WHOLEARCHIVE:of_ccobj")
if (NOT BUILD_THIRD_PARTY)
include(oneflow)
else()
add_custom_target(prepare_oneflow_third_party ALL
DEPENDS ${oneflow_third_party_dependencies})
endif()
# build main
foreach(cc ${of_main_cc})
get_filename_component(main_name ${cc} NAME_WE)
cuda_add_executable(${main_name} ${cc})
target_link_libraries(${main_name} ${of_libs} ${oneflow_third_party_libs})
endforeach()
# build test
foreach(cc ${of_all_test_cc})
get_filename_component(test_name ${cc} NAME_WE)
string(CONCAT test_exe_name ${test_name} exe)
cuda_add_executable(${test_exe_name} ${cc})
target_link_libraries(${test_exe_name} ${of_libs} ${oneflow_third_party_libs})
add_test(NAME ${test_name} COMMAND ${test_exe_name})
endforeach()
del build_oneflow
mkdir build_oneflow
cd ./build_oneflow
mkdir build
cd ./build
cmake .. -A x64
msbuild /p:Configuration=Debug ALL_BUILD.vcxproj
cd ..
......
rm -rf build_oneflow
mkdir build_oneflow
cd ./build_oneflow
mkdir build
cd ./build
cmake ..
make -j
cd ..
......
rm -rf build_oneflow
mkdir build_oneflow
cd ./build_oneflow
mkdir build
cd ./build
cmake .. -G Xcode -DCMAKE_BUILD_TYPE=Debug
make -j
cd ..
......
del build_third_party
mkdir build_third_party
cd ./build_third_party
cmake ../cmake/third_party -A x64
msbuild /p:Configuration=Debug ALL_BUILD.vcxproj
del build
mkdir build
cd ./build
cmake .. -A x64 -DBUILD_THIRD_PARTY=ON
msbuild /p:Configuration=Debug prepare_oneflow_third_party.vcxproj
del CMakeCache.txt
cd ..
rm -rf build_third_party
mkdir build_third_party
cd ./build_third_party
cmake ../cmake/third_party
rm -rf build
mkdir build
cd ./build
cmake .. -DBUILD_THIRD_PARTY=ON
make -j
rm CMakeCache.txt
cd ..
rm -rf build_third_party
mkdir build_third_party
cd ./build_third_party
cmake ../cmake/third_party -G Xcode -DCMAKE_BUILD_TYPE=Debug
rm -rf build
mkdir build
cd ./build
cmake .. -G Xcode -DCMAKE_BUILD_TYPE=Debug -DBUILD_THIRD_PARTY=ON
make -j
rm CMakeCache.txt
cd ..
# main cpp
list(APPEND of_main_cc ${oneflow_src_dir}/compiler/compiler.cpp)
list(APPEND of_main_cc ${oneflow_src_dir}/runtime/elf_runner.cpp)
# source_group
SUBDIRLIST(subdir_list ${oneflow_src_dir})
foreach(subdir ${subdir_list})
file(GLOB subdir_headers "${oneflow_src_dir}/${subdir}/*.h")
file(GLOB subdir_cuda_headers "${oneflow_src_dir}/${subdir}/*.cuh")
file(GLOB subdir_obj_cpps "${oneflow_src_dir}/${subdir}/*.cpp")
file(GLOB subdir_obj_cus "${oneflow_src_dir}/${subdir}/*.cu")
file(GLOB subdir_test_cpps "${oneflow_src_dir}/${subdir}/*_test.cpp")
file(GLOB subdir_protos "${oneflow_src_dir}/${subdir}/*.proto")
foreach(test_cpp ${subdir_test_cpps})
list(REMOVE_ITEM subdir_obj_cpps ${test_cpp})
endforeach()
foreach(main_cpp ${of_main_cc})
list(REMOVE_ITEM subdir_obj_cpps ${main_cpp})
endforeach()
source_group(${subdir} FILES ${subdir_headers} ${subdir_cuda_headers} ${subdir_obj_cpps} ${subdir_obj_cus} {subdir_protos})
list(APPEND of_all_obj_cc ${subdir_headers} ${subdir_cuda_headers} ${subdir_obj_cpps} ${subdir_obj_cus})
list(APPEND of_all_proto ${subdir_protos})
list(APPEND of_all_test_cc ${subdir_test_cpps})
endforeach()
# proto obj lib
foreach(proto_name ${of_all_proto})
file(RELATIVE_PATH proto_rel_name ${oneflow_src_dir} ${proto_name})
list(APPEND of_all_rel_protos ${proto_rel_name})
endforeach()
RELATIVE_PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS
${oneflow_src_dir}
${of_all_rel_protos})
cuda_add_library(of_protoobj ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(of_protoobj ${oneflow_third_party_libs})
# cc obj lib
include_directories(${oneflow_src_dir})
include_directories(${PROJECT_BINARY_DIR})
cuda_add_library(of_ccobj ${of_all_obj_cc})
target_link_libraries(of_ccobj ${oneflow_third_party_libs})
add_dependencies(of_ccobj of_protoobj)
if(APPLE)
set(of_libs -Wl,-force_load of_ccobj of_protoobj)
elseif(UNIX)
set(of_libs -Wl,--whole-archive of_ccobj of_protoobj -Wl,--no-whole-archive)
elseif(WIN32)
set(of_libs of_ccobj of_protoobj)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /WHOLEARCHIVE:of_ccobj")
endif()
# build main
foreach(cc ${of_main_cc})
get_filename_component(main_name ${cc} NAME_WE)
cuda_add_executable(${main_name} ${cc})
target_link_libraries(${main_name} ${of_libs} ${oneflow_third_party_libs})
endforeach()
# build test
foreach(cc ${of_all_test_cc})
get_filename_component(test_name ${cc} NAME_WE)
string(CONCAT test_exe_name ${test_name} exe)
cuda_add_executable(${test_exe_name} ${cc})
target_link_libraries(${test_exe_name} ${of_libs} ${oneflow_third_party_libs})
add_test(NAME ${test_name} COMMAND ${test_exe_name})
endforeach()
......@@ -2,14 +2,6 @@ if (NOT WIN32)
find_package(Threads)
endif()
if(WIN32)
add_definitions(-DNOMINMAX -D_WIN32_WINNT=0x0A00 -DLANG_CXX11 -DCOMPILER_MSVC -D__VERSION__=\"MSVC\")
add_definitions(-DWIN32 -DOS_WIN -D_MBCS -DWIN64 -DWIN32_LEAN_AND_MEAN -DNOGDI -DPLATFORM_WINDOWS)
add_definitions(/bigobj /nologo /EHsc /GF /FC /MP /Gm-)
add_definitions(-DGOOGLE_GLOG_DLL_DECL=)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
endif()
include(zlib)
include(protobuf)
include(googletest)
......
......@@ -24,7 +24,7 @@ foreach(LIBRARY_NAME ${GFLAGS_LIBRARY_NAMES})
list(APPEND GFLAGS_BUILD_STATIC_LIBRARIES ${GFLAGS_BUILD_LIBRARY_DIR}/${LIBRARY_NAME})
endforeach()
if (PREPARE_THIRD_PARTY)
if (BUILD_THIRD_PARTY)
ExternalProject_Add(gflags
PREFIX gflags
......@@ -55,4 +55,4 @@ add_custom_target(gflags_copy_libs_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GFLAGS_BUILD_STATIC_LIBRARIES} ${GFLAGS_LIBRARY_DIR}
DEPENDS gflags_create_library_dir)
endif(PREPARE_THIRD_PARTY)
endif(BUILD_THIRD_PARTY)
......@@ -31,7 +31,7 @@ set (GLOG_PUBLIC_H
${CMAKE_CURRENT_BINARY_DIR}/glog/src/glog/src/glog/log_severity.h
)
if(PREPARE_THIRD_PARTY)
if(BUILD_THIRD_PARTY)
ExternalProject_Add(glog
PREFIX glog
......@@ -70,4 +70,4 @@ add_custom_target(glog_copy_libs_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GLOG_BUILD_STATIC_LIBRARIES} ${GLOG_LIBRARY_DIR}
DEPENDS glog_create_library_dir)
endif(PREPARE_THIRD_PARTY)
\ No newline at end of file
endif(BUILD_THIRD_PARTY)
\ No newline at end of file
......@@ -23,7 +23,7 @@ foreach(LIBRARY_NAME ${GOOGLETEST_LIBRARY_NAMES})
list(APPEND GOOGLETEST_BUILD_STATIC_LIBRARIES ${GOOGLETEST_BUILD_LIBRARY_DIR}/${LIBRARY_NAME})
endforeach()
if(PREPARE_THIRD_PARTY)
if(BUILD_THIRD_PARTY)
ExternalProject_Add(googletest
PREFIX googletest
......@@ -55,4 +55,4 @@ add_custom_target(googletest_copy_libs_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GOOGLETEST_BUILD_STATIC_LIBRARIES} ${GOOGLETEST_LIBRARY_DIR}
DEPENDS googletest_create_library_dir)
endif(PREPARE_THIRD_PARTY)
\ No newline at end of file
endif(BUILD_THIRD_PARTY)
\ No newline at end of file
......@@ -29,7 +29,7 @@ foreach(LIBRARY_NAME ${GRPC_LIBRARY_NAMES})
list(APPEND GRPC_BUILD_STATIC_LIBRARIES ${GRPC_BUILD_LIBRARY_DIR}/${LIBRARY_NAME})
endforeach()
if(PREPARE_THIRD_PARTY)
if(BUILD_THIRD_PARTY)
ExternalProject_Add(grpc
PREFIX grpc
......@@ -67,4 +67,4 @@ add_custom_target(grpc_copy_libs_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${GRPC_BUILD_STATIC_LIBRARIES} ${GRPC_LIBRARY_DIR}
DEPENDS grpc_create_library_dir)
endif(PREPARE_THIRD_PARTY)
\ No newline at end of file
endif(BUILD_THIRD_PARTY)
\ No newline at end of file
......@@ -31,7 +31,7 @@ endforeach()
set(PROTOBUF_BUILD_PROTOC_EXECUTABLE ${PROTOBUF_BUILD_LIBRARY_DIR}/${PROTOC_EXECUTABLE_NAME})
set(PROTOBUF_PROTOC_EXECUTABLE ${PROTOBUF_BINARY_DIR}/${PROTOC_EXECUTABLE_NAME})
if (PREPARE_THIRD_PARTY)
if (BUILD_THIRD_PARTY)
ExternalProject_Add(protobuf
PREFIX protobuf
......@@ -81,4 +81,4 @@ add_custom_target(protobuf_copy_binary_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PROTOBUF_BUILD_PROTOC_EXECUTABLE} ${PROTOBUF_BINARY_DIR}
DEPENDS protobuf_create_binary_dir)
endif(PREPARE_THIRD_PARTY)
\ No newline at end of file
endif(BUILD_THIRD_PARTY)
\ No newline at end of file
......@@ -28,7 +28,7 @@ set(ZLIB_HEADERS
"${ZLIB_INSTALL}/include/zlib.h"
)
if(PREPARE_THIRD_PARTY)
if(BUILD_THIRD_PARTY)
ExternalProject_Add(zlib
PREFIX zlib
......@@ -65,4 +65,4 @@ add_custom_target(zlib_copy_libs_to_destination
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ZLIB_BUILD_STATIC_LIBRARIES} ${ZLIB_LIBRARY_DIR}
DEPENDS zlib_create_library_dir)
endif(PREPARE_THIRD_PARTY)
\ No newline at end of file
endif(BUILD_THIRD_PARTY)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册