util.cmake 6.1 KB
Newer Older
L
liaogang 已提交
1
# Some common routine for paddle compile.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8

# target_circle_link_libraries
# Link libraries to target which has circle dependencies.
#
# First Argument: target name want to be linked with libraries
# Rest Arguments: libraries which link together.
function(target_circle_link_libraries TARGET_NAME)
L
liaogang 已提交
9
    if(APPLE)
Y
Yu Yang 已提交
10 11 12 13
        set(LIBS)
        set(inArchive OFF)
        set(libsInArgn)

L
liaogang 已提交
14
        foreach(arg ${ARGN})
Y
Yu Yang 已提交
15 16 17 18 19 20 21 22 23 24 25
            if(${arg} STREQUAL "ARCHIVE_START")
                set(inArchive ON)
            elseif(${arg} STREQUAL "ARCHIVE_END")
                set(inArchive OFF)
            else()
                if(inArchive)
                    list(APPEND LIBS "-Wl,-force_load")
                endif()
                list(APPEND LIBS ${arg})
                list(APPEND libsInArgn ${arg})
            endif()
L
liaogang 已提交
26
        endforeach()
L
liaogang 已提交
27 28 29
        if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
            list(APPEND LIBS "-undefined dynamic_lookup")
        endif()
Y
Yu Yang 已提交
30
        list(REVERSE libsInArgn)
L
liaogang 已提交
31
        target_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
            ${LIBS}
            ${libsInArgn})

    else()  # LINUX
        set(LIBS)

        foreach(arg ${ARGN})
            if(${arg} STREQUAL "ARCHIVE_START")
                list(APPEND LIBS "-Wl,--whole-archive")
            elseif(${arg} STREQUAL "ARCHIVE_END")
                list(APPEND LIBS "-Wl,--no-whole-archive")
            else()
                list(APPEND LIBS ${arg})
            endif()
        endforeach()

L
liaogang 已提交
48
        target_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
49 50 51
                "-Wl,--start-group"
                ${LIBS}
                "-Wl,--end-group")
L
liaogang 已提交
52
    endif()
Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
endfunction()

# compile_cu_as_cpp
# Make a cu file compiled as C++
# Arguments: Source files
macro(compile_cu_as_cpp)
    foreach(s ${ARGN})
        set_source_files_properties(${s} PROPERTIES LANGUAGE CXX)
        set_source_files_properties(${s} PROPERTIES COMPILE_FLAGS "-x c++")
    endforeach()
endmacro()

# link_paddle_exe
# add paddle library for a paddle executable, such as trainer, pserver.
#
68
# It will handle WITH_PYTHON etc.
Z
zhangjinchao01 已提交
69
function(link_paddle_exe TARGET_NAME)
B
backyes 已提交
70 71 72 73
    if(WITH_RDMA)
        generate_rdma_links()
    endif()

Z
zhangjinchao01 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86
    if(WITH_METRIC)
        if(WITH_GPU)
            set(METRIC_LIBS paddle_metric_learning paddle_dserver_lib metric metric_cpu)
        else()
            set(METRIC_LIBS paddle_metric_learning paddle_dserver_lib metric_cpu)
        endif()
    else()
        set(METRIC_LIBS "")
    endif()

    if(PADDLE_WITH_INTERNAL)
        set(INTERAL_LIBS paddle_internal_gserver paddle_internal_parameter)
        target_circle_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
87
            ARCHIVE_START
Z
zhangjinchao01 已提交
88 89
            paddle_internal_gserver
            paddle_internal_owlqn
Y
Yu Yang 已提交
90
            ARCHIVE_END
Z
zhangjinchao01 已提交
91 92 93 94 95 96
            paddle_internal_parameter)
    else()
        set(INTERAL_LIBS "")
    endif()

    target_circle_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
97
        ARCHIVE_START
Z
zhangjinchao01 已提交
98 99
        paddle_gserver
        ${METRIC_LIBS}
Y
Yu Yang 已提交
100
        ARCHIVE_END
Z
zhangjinchao01 已提交
101 102 103 104 105 106 107 108 109 110
        paddle_pserver
        paddle_trainer_lib
        paddle_network
        paddle_math
        paddle_utils
        paddle_parameter
        paddle_proto
        paddle_cuda
        ${METRIC_LIBS}
        ${PROTOBUF_LIBRARY}
111 112
        ${LIBGLOG_LIBRARY}
        ${GFLAGS_LIBRARIES}
Z
zhangjinchao01 已提交
113 114
        ${CMAKE_THREAD_LIBS_INIT}
        ${CBLAS_LIBS}
Y
Yu Yang 已提交
115
        ${ZLIB_LIBRARIES}
Z
zhangjinchao01 已提交
116
        ${INTERAL_LIBS}
Y
Yu Yang 已提交
117
        ${CMAKE_DL_LIBS})
B
backyes 已提交
118 119 120 121 122 123

    if(WITH_RDMA)
        target_link_libraries(${TARGET_NAME}
            ${RDMA_LD_FLAGS}
            ${RDMA_LIBS})
    endif()
X
xuwei06 已提交
124

Z
zhangjinchao01 已提交
125 126 127 128 129 130
    if(WITH_PYTHON)
        target_link_libraries(${TARGET_NAME}
            ${PYTHON_LIBRARIES})
    endif()

    if(WITH_GPU)
X
xuwei06 已提交
131
        if(NOT WITH_DSO OR WITH_METRIC)
Z
zhangjinchao01 已提交
132 133
            target_link_libraries(${TARGET_NAME}
                ${CUDNN_LIBRARY}
X
xuwei06 已提交
134
                ${CUDA_curand_LIBRARY})
Z
zhangjinchao01 已提交
135 136 137 138 139 140 141 142
            CUDA_ADD_CUBLAS_TO_TARGET(${TARGET_NAME})
        endif()

        check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME )
        if(HAVE_CLOCK_GETTIME)
            target_link_libraries(${TARGET_NAME} rt)
        endif()
    endif()
143 144 145 146 147

    if(NOT WITH_DSO)
        target_link_libraries(${TARGET_NAME}
            ${WARPCTC_LIBRARY})
    endif()
Z
zhangjinchao01 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
endfunction()

# link_paddle_test
# Link a paddle unittest for target
# TARGET_NAME: the unittest target name
# Rest Arguemnts: not used.
function(link_paddle_test TARGET_NAME)
    link_paddle_exe(${TARGET_NAME})
    target_link_libraries(${TARGET_NAME} ${GTEST_MAIN_LIBRARIES}
        ${GTEST_LIBRARIES})
endfunction()

# add_unittest_without_exec
#
# create a paddle unittest. not specifically define how to run this unittest.
# TARGET_NAME: the unittest target name, same as executable file name
# Rest Arguments: the source files to compile this unittest.
macro(add_unittest_without_exec TARGET_NAME)
    add_executable(${TARGET_NAME} ${ARGN})
    link_paddle_test(${TARGET_NAME})
    add_style_check_target(${TARGET_NAME} ${ARGN})
endmacro()

# add_unittest
# create a paddle unittest and just to execute this binary to make unittest.
#
# TARGET_NAME: the unittest target name, same as executable file name
# Rest Arguments: the source files to compile this unittest.
macro(add_unittest TARGET_NAME)
    add_unittest_without_exec(${TARGET_NAME} ${ARGN})
    add_test(${TARGET_NAME} ${TARGET_NAME})
endmacro()

# add_simple_unittest
# create a paddle unittest with file name. It just compile ${TARGET_NAME}.cpp to
# ${TARGET_NAME} and then execute it.
macro(add_simple_unittest TARGET_NAME)
    add_unittest(${TARGET_NAME} ${TARGET_NAME}.cpp)
endmacro()

188 189 190 191 192 193 194 195 196 197 198 199 200
# Creates C resources file from files in given resource file
function(create_resources res_file output)
    # Create empty output file
    file(WRITE ${output} "")
    # Get short filename
    string(REGEX MATCH "([^/]+)$" filename ${res_file})
    # Replace filename spaces & extension separator for C compatibility
    string(REGEX REPLACE "\\.| |-" "_" filename ${filename})
    # Read hex data from file
    file(READ ${res_file} filedata HEX)
    # Convert hex data for C compatibility
    string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
    # Append data to output file
X
xuwei06 已提交
201
    file(APPEND ${output} "const unsigned char ${filename}[] = {${filedata}0};\nconst unsigned ${filename}_size = sizeof(${filename});\n")
202
endfunction()