From 19c4db1b566028a5705f5639f963c7485e4c640b Mon Sep 17 00:00:00 2001 From: Shibo Tao <62922815+T8T9@users.noreply.github.com> Date: Sun, 21 Jun 2020 22:05:03 +0800 Subject: [PATCH] don't re-generate header file if content doesn't change (#25130) * don't re-generate header file if content doesn't change. test=develop * add copy_if_different function. test=develop --- .gitignore | 1 + CMakeLists.txt | 2 +- cmake/generic.cmake | 16 +++++++ paddle/fluid/framework/ir/CMakeLists.txt | 5 ++- .../analysis/ir_passes/CMakeLists.txt | 4 +- paddle/fluid/operators/CMakeLists.txt | 7 ++- paddle/fluid/operators/jit/CMakeLists.txt | 7 ++- paddle/fluid/pybind/CMakeLists.txt | 45 +++++++++++++------ 8 files changed, 65 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 4a5c35bbc2e..749832c3930 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ paddle/fluid/op_use_default_grad_maker_PR.spec build/ build_doc/ *.user +*.tmp .vscode .idea diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f62a3c9327..6fd6556922a 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_F # cmake 3.12, 3.13, 3.14 will append gcc link options to nvcc, and nvcc doesn't recognize them. if(WITH_GPU AND (${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.12) AND (${CMAKE_VERSION} VERSION_LESS 3.15)) - message(FATAL_ERROR "cmake ${CMAKE_VERSION} is not supported when WITH_GPU=ON because of bug https://tinyurl.com/ydbb9m7n. " + message(FATAL_ERROR "cmake ${CMAKE_VERSION} is not supported when WITH_GPU=ON because of bug https://cmake.org/pipermail/cmake/2018-September/068195.html. " "You can use cmake 3.16 (recommended), 3.10, 3.11, 3.15 or 3.17. Please refer to the install document: https://cmake.org/install/") endif() diff --git a/cmake/generic.cmake b/cmake/generic.cmake index 4be0dcfd69c..bc299baee19 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -820,3 +820,19 @@ function(brpc_library TARGET_NAME) cc_library("${TARGET_NAME}_proto" SRCS "${brpc_proto_srcs}") cc_library("${TARGET_NAME}" SRCS "${brpc_library_SRCS}" DEPS "${TARGET_NAME}_proto" "${brpc_library_DEPS}") endfunction() + +# copy_if_different from src_file to dst_file before barrier_target. +function(copy_if_different src_file dst_file barrier_target) + # this is a dummy target, should always be run to update ${pybind_file_final} + add_custom_target(before_${barrier_target} ALL + DEPENDS before_${barrier_target}_custom_command + ) + add_dependencies(${barrier_target} before_${barrier_target}) + + add_custom_command( + OUTPUT before_${barrier_target}_custom_command + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file} + COMMENT "copy_if_different ${dst_file}" + VERBATIM + ) +endfunction() diff --git a/paddle/fluid/framework/ir/CMakeLists.txt b/paddle/fluid/framework/ir/CMakeLists.txt index 70662e9f32e..81bd8a4adf4 100644 --- a/paddle/fluid/framework/ir/CMakeLists.txt +++ b/paddle/fluid/framework/ir/CMakeLists.txt @@ -1,8 +1,11 @@ -set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) +set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h.tmp) +set(pass_file_final ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) file(WRITE ${pass_file} "// Generated by the paddle/fluid/framework/ir/CMakeLists.txt. DO NOT EDIT!\n\n") file(APPEND ${pass_file} "\#pragma once\n") file(APPEND ${pass_file} "\#include \"paddle/fluid/framework/ir/pass.h\"\n") +copy_if_different(${pass_file} ${pass_file_final} extern_glog) + add_subdirectory(fuse_optimizer_ops_pass) add_subdirectory(memory_optimize_pass) add_subdirectory(multi_devices_graph_pass) diff --git a/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt b/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt index 7fcb07d020b..e35178428cc 100644 --- a/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt +++ b/paddle/fluid/inference/analysis/ir_passes/CMakeLists.txt @@ -7,7 +7,7 @@ if (WITH_GPU AND TENSORRT_FOUND) subgraph_util tensorrt_subgraph_pass CACHE INTERNAL "") - set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) + set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h.tmp) file(APPEND ${pass_file} "USE_PASS(tensorrt_subgraph_pass);\n") set(INFER_IR_PASSES ${INFER_IR_PASSES} tensorrt_subgraph_pass CACHE INTERNAL "") endif() @@ -15,7 +15,7 @@ endif() if (WITH_LITE) cc_library(lite_subgraph_pass SRCS lite_subgraph_pass.cc DEPS ${analysis_deps} subgraph_util lite_op_teller) set(analysis_deps ${analysis_deps} subgraph_util lite_subgraph_pass CACHE INTERNAL "") - set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h) + set(pass_file ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h.tmp) file(APPEND ${pass_file} "USE_PASS(lite_subgraph_pass);\n") set(INFER_IR_PASSES ${INFER_IR_PASSES} lite_subgraph_pass CACHE INTERNAL "") cc_test(lite_subgraph_pass_tester SRCS lite_subgraph_pass_tester.cc DEPS lite_subgraph_pass gtest glog) diff --git a/paddle/fluid/operators/CMakeLists.txt b/paddle/fluid/operators/CMakeLists.txt index d5f1b528da5..40ed5869c27 100644 --- a/paddle/fluid/operators/CMakeLists.txt +++ b/paddle/fluid/operators/CMakeLists.txt @@ -3,8 +3,11 @@ include(operators) # clean cache and pybind_file content first when rebuild unset(GLOB_OP_LIB CACHE) unset(OP_LIBRARY CACHE) -set(pybind_file ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h CACHE INTERNAL "pybind.h file") -file(WRITE ${pybind_file} "// Generated by the paddle/fluid/operator/CMakeLists.txt. DO NOT EDIT!\n\n") +set(pybind_file ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h.tmp CACHE INTERNAL "pybind.h file") +set(pybind_file_final ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h) +file(WRITE ${pybind_file} "// Generated by the paddle/fluid/operators/CMakeLists.txt. DO NOT EDIT!\n\n") + +copy_if_different(${pybind_file} ${pybind_file_final} operator) add_subdirectory(math) add_subdirectory(controlflow) diff --git a/paddle/fluid/operators/jit/CMakeLists.txt b/paddle/fluid/operators/jit/CMakeLists.txt index 2f116d0f74a..ce1b566c0b8 100644 --- a/paddle/fluid/operators/jit/CMakeLists.txt +++ b/paddle/fluid/operators/jit/CMakeLists.txt @@ -1,6 +1,7 @@ -set(jit_file ${PADDLE_BINARY_DIR}/paddle/fluid/operators/jit/kernels.h) -file(WRITE ${jit_file} "// Generated by the paddle/fluid/operators/jit/CMakeLists.txt. DO NOT EDIT!\n\n") +set(jit_file ${PADDLE_BINARY_DIR}/paddle/fluid/operators/jit/kernels.h.tmp) +set(jit_file_final ${PADDLE_BINARY_DIR}/paddle/fluid/operators/jit/kernels.h) +file(WRITE ${jit_file} "// Generated by the paddle/fluid/operators/jit/CMakeLists.txt. DO NOT EDIT!\n\n") file(APPEND ${jit_file} "\#pragma once\n") file(APPEND ${jit_file} "\#include \"paddle/fluid/operators/jit/helper.h\"\n") file(APPEND ${jit_file} "\#include \"paddle/fluid/operators/jit/registry.h\"\n\n") @@ -11,6 +12,8 @@ file(GLOB jit_kernel_cc_srcs RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "*.cc") list(REMOVE_ITEM jit_kernel_cc_srcs test.cc benchmark.cc) cc_library(jit_kernel_base SRCS ${jit_kernel_cc_srcs} DEPS ${JIT_KERNEL_DEPS}) +copy_if_different(${jit_file} ${jit_file_final} jit_kernel_base) + # refer must go first add_subdirectory(refer) add_subdirectory(more) diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 6f47d312d2a..b98dad60935 100644 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -66,23 +66,34 @@ if(WITH_PYTHON) get_property (os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) target_link_libraries(op_function_generator ${os_dependency_modules}) + set(impl_file ${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h) + set(tmp_impl_file ${impl_file}.tmp) + if(WIN32) - add_custom_target(op_function_cmd - COMMAND "${CMAKE_BINARY_DIR}/paddle/fluid/pybind/${CMAKE_BUILD_TYPE}/op_function_generator" - "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") - add_dependencies(op_function_cmd op_function_generator) + add_custom_command(TARGET op_function_generator + POST_BUILD + COMMAND "${CMAKE_BINARY_DIR}/paddle/fluid/pybind/${CMAKE_BUILD_TYPE}/op_function_generator" + "${tmp_impl_file}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_impl_file} ${impl_file} + COMMENT "copy_if_different ${impl_file}" + VERBATIM + ) + if(${CBLAS_PROVIDER} STREQUAL MKLML) - add_custom_command(TARGET op_function_generator POST_BUILD + add_custom_command(TARGET op_function_generator + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) else(${CBLAS_PROVIDER} STREQUAL EXTERN_OPENBLAS) - add_custom_command(TARGET op_function_generator POST_BUILD + add_custom_command(TARGET op_function_generator + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${OPENBLAS_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) endif() if(WITH_MKLDNN) - add_custom_command(TARGET op_function_generator POST_BUILD + add_custom_command(TARGET op_function_generator + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) endif() @@ -91,19 +102,25 @@ if(WITH_PYTHON) # copy these *.so to current directory and append current directory to # LD_LIBRARY_PATH. This is different with Windows platformm, which search # *.dll in current directory automatically. - add_custom_target(op_function_cmd - COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:." + add_custom_command(TARGET op_function_generator + POST_BUILD + COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:." "${CMAKE_CURRENT_BINARY_DIR}/op_function_generator" - "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") - add_dependencies(op_function_cmd op_function_generator) + "${tmp_impl_file}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${tmp_impl_file} ${impl_file} + COMMENT "copy_if_different ${impl_file}" + VERBATIM + ) if(WITH_MKL) - add_custom_command(TARGET op_function_generator POST_BUILD + add_custom_command(TARGET op_function_generator + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR} ) endif(WITH_MKL) if(WITH_MKLDNN) - add_custom_command(TARGET op_function_generator POST_BUILD + add_custom_command(TARGET op_function_generator + PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR} ) endif(WITH_MKLDNN) @@ -126,5 +143,5 @@ if(WITH_PYTHON) get_property (os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) target_link_libraries(paddle_pybind ${os_dependency_modules}) - add_dependencies(paddle_pybind op_function_cmd) + add_dependencies(paddle_pybind op_function_generator) endif(WITH_PYTHON) -- GitLab