diff --git a/.gitignore b/.gitignore index 79c39f772596dc96f198ae0a7ffc952afdf6484c..85ad57ec4ea0cd270fa2b22c3f10665575ef596c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ -build_oneflow -build_third_party -third_party - +build +third_party \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2159997fd8f0b7bcf1f11f977ea5a59297edc3d6..c04d360958965e5b77d0976675fa64840807a33e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,30 @@ # 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() diff --git a/build_oneflow.bat b/build_oneflow.bat index 4f756253955abebfce9b35b057d7744a32147078..360c5283a73dd0ee9807f8c897687537900d06b4 100644 --- a/build_oneflow.bat +++ b/build_oneflow.bat @@ -1,6 +1,5 @@ -del build_oneflow -mkdir build_oneflow -cd ./build_oneflow +mkdir build +cd ./build cmake .. -A x64 msbuild /p:Configuration=Debug ALL_BUILD.vcxproj cd .. diff --git a/build_oneflow.sh b/build_oneflow.sh index fd0f67685a3f10373b9349f7c76ba8f14d4dd4b5..f8d524d71f233fc985accce2a90dd72bfb579667 100644 --- a/build_oneflow.sh +++ b/build_oneflow.sh @@ -1,6 +1,5 @@ -rm -rf build_oneflow -mkdir build_oneflow -cd ./build_oneflow +mkdir build +cd ./build cmake .. make -j cd .. diff --git a/build_oneflow_xcode.sh b/build_oneflow_xcode.sh index 128ab969941ce1bb3004d22ef893761a82f6c03e..8a67efac3a1b580cb50b2aaf3a4d6cfe7b4db5dc 100644 --- a/build_oneflow_xcode.sh +++ b/build_oneflow_xcode.sh @@ -1,6 +1,5 @@ -rm -rf build_oneflow -mkdir build_oneflow -cd ./build_oneflow +mkdir build +cd ./build cmake .. -G Xcode -DCMAKE_BUILD_TYPE=Debug make -j cd .. diff --git a/build_third_party.bat b/build_third_party.bat index df38ab306266bf0ec62b578f6996be607ef14f4c..76c693bcb44c7fcb9444246244276c349492e12e 100644 --- a/build_third_party.bat +++ b/build_third_party.bat @@ -1,7 +1,8 @@ -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 .. diff --git a/build_third_party.sh b/build_third_party.sh index 2d5a4e63cc6fc4f0a49ee92eab92d9fcb04ee8bd..dac55a5276a8da818209ebf956d95073c538218e 100644 --- a/build_third_party.sh +++ b/build_third_party.sh @@ -1,7 +1,8 @@ -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 .. diff --git a/build_third_party_xcode.sh b/build_third_party_xcode.sh index 91f2822f9a3056044d480ad0d315d9caca82cf73..208ebc89548469f2b77bde86c2df50c6343be723 100644 --- a/build_third_party_xcode.sh +++ b/build_third_party_xcode.sh @@ -1,7 +1,8 @@ -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 .. diff --git a/cmake/oneflow.cmake b/cmake/oneflow.cmake new file mode 100644 index 0000000000000000000000000000000000000000..64a536eaafbc051c0dca7cf15e57771f5be1fb7f --- /dev/null +++ b/cmake/oneflow.cmake @@ -0,0 +1,69 @@ +# 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() diff --git a/cmake/third_party.cmake b/cmake/third_party.cmake index a405350965b9f458924ae68ee8b1646a41beeffc..88d90c4f7c6cb2c83b23daacc42ec1ae1d977a39 100644 --- a/cmake/third_party.cmake +++ b/cmake/third_party.cmake @@ -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) diff --git a/cmake/third_party/gflags.cmake b/cmake/third_party/gflags.cmake index 67ccaf05f14e3b57a0591f05d74b5352f60a8aee..1fe2a9036b8391b01687223d752626d2de1d26fd 100644 --- a/cmake/third_party/gflags.cmake +++ b/cmake/third_party/gflags.cmake @@ -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) diff --git a/cmake/third_party/glog.cmake b/cmake/third_party/glog.cmake index be643070ded47905dbe914a0191e3fd2b529683d..9c1c129d3e1b45d6e54349fc1499db7382b870d4 100644 --- a/cmake/third_party/glog.cmake +++ b/cmake/third_party/glog.cmake @@ -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 diff --git a/cmake/third_party/googletest.cmake b/cmake/third_party/googletest.cmake index cdc788e66aeed31a75e348884828bf6549f8e553..e89b6ab0345f8c086e7b4e676d883ed16a133cdc 100644 --- a/cmake/third_party/googletest.cmake +++ b/cmake/third_party/googletest.cmake @@ -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 diff --git a/cmake/third_party/grpc.cmake b/cmake/third_party/grpc.cmake index a509ec558cae35bcb06cd159f1f269dc05d17dce..b9ad007e8d6a522232d94ca47c4946be7018724f 100644 --- a/cmake/third_party/grpc.cmake +++ b/cmake/third_party/grpc.cmake @@ -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 diff --git a/cmake/third_party/protobuf.cmake b/cmake/third_party/protobuf.cmake index 08ee3dc8470ca0e0584589a9d4045ae0dfcbb934..378ecab063bfe8deb7f540766419b73d85b00ad5 100644 --- a/cmake/third_party/protobuf.cmake +++ b/cmake/third_party/protobuf.cmake @@ -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 diff --git a/cmake/third_party/zlib.cmake b/cmake/third_party/zlib.cmake index 1770555f8631e08519c8dc61e435c363d12ec872..fcdac7ae32de6fb5becb9c0b471d5c03c457f279 100644 --- a/cmake/third_party/zlib.cmake +++ b/cmake/third_party/zlib.cmake @@ -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