From f8d5fd6f9b2e52d54eca2fb1eca376a085b39943 Mon Sep 17 00:00:00 2001 From: Shibo Tao <62922815+T8T9@users.noreply.github.com> Date: Tue, 23 Jun 2020 11:47:38 +0800 Subject: [PATCH] generate dummy file using cmake configure_file function to avoid re-generating it. (#25161) * generate dummy file using cmake configure_file function to avoid re-generating it. test=develop * add cmake/dummy.c.in. test=develop --- CMakeLists.txt | 2 +- cmake/cblas.cmake | 7 ++-- cmake/dummy.c.in | 3 ++ cmake/external/mkldnn.cmake | 7 ++-- cmake/generic.cmake | 65 +++++++++++++++++++++++++++---------- 5 files changed, 57 insertions(+), 27 deletions(-) create mode 100644 cmake/dummy.c.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fd6556922a..b39e48505c6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ include(system) project(paddle CXX C) include(init) +include(generic) # simplify cmake module # enable language CUDA # TODO(Shibo Tao): remove find_package(CUDA) completely. @@ -199,7 +200,6 @@ if(WITH_PROFILER) add_definitions(-DWITH_GPERFTOOLS) endif() -include(generic) # simplify cmake module include(ccache) # set ccache for compilation include(util) # set unittest and link libs include(version) # set PADDLE_VERSION diff --git a/cmake/cblas.cmake b/cmake/cblas.cmake index b51225dd4d4..32042864be4 100644 --- a/cmake/cblas.cmake +++ b/cmake/cblas.cmake @@ -22,9 +22,7 @@ # CBLAS_LIBS # a list of libraries should be linked by paddle. # # Each library should be full path to object file. -set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/cblas_dummy.c) -file(WRITE ${dummyfile} "const char *dummy_cblas = \"${dummyfile}\";") -add_library(cblas STATIC ${dummyfile}) +generate_dummy_static_lib(LIB_NAME "cblas" GENERATOR "cblas.cmake") if(WITH_LIBXSMM) target_link_libraries(cblas ${LIBXSMM_LIBS}) @@ -129,4 +127,5 @@ endif() include_directories(${CBLAS_INC_DIR}) if(NOT ${CBLAS_PROVIDER} STREQUAL MKLML) target_link_libraries(cblas ${CBLAS_LIBRARIES}) -endif() \ No newline at end of file +endif() + diff --git a/cmake/dummy.c.in b/cmake/dummy.c.in new file mode 100644 index 00000000000..833533603f9 --- /dev/null +++ b/cmake/dummy.c.in @@ -0,0 +1,3 @@ +// Generated by @dummy_GENERATOR@. DO NOT EDIT!!! + +const char *dummy = "@dummy_CONTENT@"; diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 69e9481c6fa..98bbf59cefb 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -30,7 +30,7 @@ if(CMAKE_INSTALL_LIBDIR MATCHES ".*lib64$") SET(LIBDIR "lib64") endif() -MESSAGE(STATUS "Set ${MKLDNN_INSTALL_DIR}/l${LIBDIR} to runtime path") +MESSAGE(STATUS "Set ${MKLDNN_INSTALL_DIR}/${LIBDIR} to runtime path") SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLDNN_INSTALL_DIR}/${LIBDIR}") @@ -109,9 +109,8 @@ add_definitions(-DPADDLE_WITH_MKLDNN) # generate a static dummy target to track mkldnn dependencies # for cc_library(xxx SRCS xxx.c DEPS mkldnn) -SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mkldnn_dummy.c) -FILE(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";") -ADD_LIBRARY(mkldnn STATIC ${dummyfile}) +generate_dummy_static_lib(LIB_NAME "mkldnn" GENERATOR "mkldnn.cmake") + TARGET_LINK_LIBRARIES(mkldnn ${MKLDNN_LIB} ${MKLML_IOMP_LIB}) ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT}) diff --git a/cmake/generic.cmake b/cmake/generic.cmake index bc299baee19..a47ffa71b6c 100644 --- a/cmake/generic.cmake +++ b/cmake/generic.cmake @@ -185,9 +185,9 @@ function(merge_static_libs TARGET_NAME) COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS} DEPENDS ${libs}) - # Generate dummy staic lib - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + # Generate dummy static lib + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs") + target_link_libraries(${TARGET_NAME} ${libs_deps}) foreach(lib ${libs}) @@ -227,8 +227,8 @@ function(merge_static_libs TARGET_NAME) DEPENDS ${libs} ${target_OBJS}) # Generate dummy staic lib - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs") + target_link_libraries(${TARGET_NAME} ${libs_deps}) # Get the file name of the generated library @@ -246,10 +246,9 @@ function(merge_static_libs TARGET_NAME) add_custom_command(OUTPUT ${target_SRCS} COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS} DEPENDS ${libs}) - # Generate dummy staic lib - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs") + target_link_libraries(${TARGET_NAME} ${libs_deps}) foreach(lib ${libs}) @@ -278,9 +277,7 @@ function(cc_library TARGET_NAME) if(cc_library_SHARED OR cc_library_shared) # build *.so add_library(${TARGET_NAME} SHARED ${cc_library_SRCS}) elseif(cc_library_INTERFACE OR cc_library_interface) - set(target_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c) - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:cc_library") else() add_library(${TARGET_NAME} STATIC ${cc_library_SRCS}) find_fluid_modules(${TARGET_NAME}) @@ -329,9 +326,9 @@ function(cc_library TARGET_NAME) else(cc_library_SRCS) if(cc_library_DEPS) list(REMOVE_DUPLICATES cc_library_DEPS) - set(target_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c) - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:cc_library") + target_link_libraries(${TARGET_NAME} ${cc_library_DEPS}) else() message(FATAL_ERROR "Please specify source files or libraries in cc_library(${TARGET_NAME} ...).") @@ -435,9 +432,8 @@ function(nv_library TARGET_NAME) else(nv_library_SRCS) if (nv_library_DEPS) list(REMOVE_DUPLICATES nv_library_DEPS) - set(target_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c) - file(WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \"${target_SRCS}\";") - add_library(${TARGET_NAME} STATIC ${target_SRCS}) + generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:nv_library") + target_link_libraries(${TARGET_NAME} ${nv_library_DEPS}) add_dependencies(${TARGET_NAME} ${nv_library_DEPS}) else() @@ -823,7 +819,7 @@ 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} + # this is a dummy target, should always be run to update ${dst_file} add_custom_target(before_${barrier_target} ALL DEPENDS before_${barrier_target}_custom_command ) @@ -836,3 +832,36 @@ function(copy_if_different src_file dst_file barrier_target) VERBATIM ) endfunction() + +# create a dummy source file, then create a static library. +# LIB_NAME should be the static lib name. +# FILE_PATH should be the dummy source file path. +# GENERATOR should be the file name invoke this function. +# CONTENT should be some helpful info. +# example: generate_dummy_static_lib(mylib FILE_PATH /path/to/dummy.c GENERATOR mylib.cmake CONTENT "helpful info") +function(generate_dummy_static_lib) + set(options "") + set(oneValueArgs LIB_NAME FILE_PATH GENERATOR CONTENT) + set(multiValueArgs "") + cmake_parse_arguments(dummy "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + if(NOT dummy_LIB_NAME) + message(FATAL_ERROR "You must provide a static lib name.") + endif() + if(NOT dummy_FILE_PATH) + set(dummy_FILE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${dummy_LIB_NAME}_dummy.c") + endif() + if(NOT dummy_GENERATOR) + message(FATAL_ERROR "You must provide a generator file name.") + endif() + # if ${dummy_GENERATOR} contains "/", it may be a file path + if(NOT ${dummy_GENERATOR} MATCHES ".*/.*") + set(dummy_GENERATOR "${CMAKE_CURRENT_LIST_DIR}/${dummy_GENERATOR}") + endif() + if(NOT dummy_CONTENT) + set(dummy_CONTENT "${dummy_FILE_PATH} for lib ${dummy_LIB_NAME}") + endif() + + configure_file(${PROJECT_SOURCE_DIR}/cmake/dummy.c.in ${dummy_FILE_PATH} @ONLY) + add_library(${dummy_LIB_NAME} STATIC ${dummy_FILE_PATH}) +endfunction() + -- GitLab