util.cmake 5.2 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
        if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
28 29 30
            if(IOS AND NOT IOS_ENABLE_BITCODE)
                list(APPEND LIBS "-undefined dynamic_lookup")
            endif()
L
liaogang 已提交
31
        endif()
Y
Yu Yang 已提交
32
        list(REVERSE libsInArgn)
L
liaogang 已提交
33
        target_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
            ${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 已提交
50
        target_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
51 52 53
                "-Wl,--start-group"
                ${LIBS}
                "-Wl,--end-group")
L
liaogang 已提交
54
    endif()
Z
zhangjinchao01 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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.
#
70
# It will handle WITH_PYTHON etc.
Z
zhangjinchao01 已提交
71
function(link_paddle_exe TARGET_NAME)
B
backyes 已提交
72 73 74 75
    if(WITH_RDMA)
        generate_rdma_links()
    endif()

Z
zhangjinchao01 已提交
76
    target_circle_link_libraries(${TARGET_NAME}
Y
Yu Yang 已提交
77
        ARCHIVE_START
Z
zhangjinchao01 已提交
78
        paddle_gserver
H
hedaoyuan 已提交
79
        paddle_function
Y
Yu Yang 已提交
80
        ARCHIVE_END
81 82 83
        paddle_pserver
        paddle_trainer_lib
        paddle_network
Z
zhangjinchao01 已提交
84 85 86 87 88
        paddle_math
        paddle_utils
        paddle_parameter
        paddle_proto
        paddle_cuda
89
        paddle_optimizer
L
liaogang 已提交
90
        ${EXTERNAL_LIBS}
Z
zhangjinchao01 已提交
91
        ${CMAKE_THREAD_LIBS_INIT}
L
liaogang 已提交
92 93 94
        ${CMAKE_DL_LIBS}
        ${RDMA_LD_FLAGS}
        ${RDMA_LIBS})
X
xuwei06 已提交
95

96 97 98 99
    if(ANDROID)
        target_link_libraries(${TARGET_NAME} log)
    endif(ANDROID)

100 101 102 103
    if(WITH_MKLDNN AND WITH_MKLML AND MKLDNN_IOMP_DIR)
      target_link_libraries(${TARGET_NAME} "-L${MKLDNN_IOMP_DIR} -liomp5 -Wl,--as-needed")
    endif()

104
    add_dependencies(${TARGET_NAME} ${external_project_dependencies})
Z
zhangjinchao01 已提交
105 106 107 108 109 110 111 112
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})
H
hedaoyuan 已提交
113
    target_link_libraries(${TARGET_NAME}
H
hedaoyuan 已提交
114
                          paddle_test_main
115
                          paddle_test_util
H
hedaoyuan 已提交
116
                          ${GTEST_LIBRARIES})
Z
zhangjinchao01 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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})
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()

146
# Creates C resources file from files in given resource file
147 148 149
function(create_resources res_file output_file)
  add_custom_command(
    OUTPUT ${output_file}
150 151
    COMMAND python ARGS ${PADDLE_SOURCE_DIR}/cmake/make_resource.py ${res_file} ${output_file}
    DEPENDS ${res_file} ${PADDLE_SOURCE_DIR}/cmake/make_resource.py)
152
endfunction()
X
xuwei06 已提交
153 154 155 156 157


# Create a python unittest using run_python_tests.sh,
# which takes care of making correct running environment
function(add_python_test TEST_NAME)
158 159 160 161 162 163 164 165
    foreach(arg ${ARGN})
        get_filename_component(py_fn ${arg} NAME_WE)
        set(TRG_NAME ${TEST_NAME}_${py_fn})
        add_test(NAME ${TRG_NAME}
                COMMAND env PYTHONPATH=${PADDLE_PYTHON_PACKAGE_DIR}
                python2 ${arg}
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    endforeach()
X
xuwei06 已提交
166
endfunction()