paddle_ld_flags.py 5.4 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
    from paddle_api_config import *
    import os.path
L
liaogang 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
    import platform

    system = platform.system().lower()
    is_osx = (system == 'darwin')
    is_win = (system == 'windows')
    is_lin = (system == 'linux')

    if is_lin:
        whole_start = "-Wl,--whole-archive"
        whole_end = "-Wl,--no-whole-archive"
    elif is_osx:
        whole_start = ""
        whole_end = ""
Z
zhangjinchao01 已提交
31

32
    LIB_DIRS = [
H
hedaoyuan 已提交
33 34
        "math", 'function', 'utils', 'parameter', "gserver", "api", "cuda",
        "pserver", "trainer"
35
    ]
Z
zhangjinchao01 已提交
36 37 38 39 40 41 42 43
    PARENT_LIB_DIRS = ['proto']

    class PaddleLDFlag(object):
        def __init__(self):
            self.paddle_build_dir = PADDLE_BUILD_DIR
            self.paddle_build_dir = os.path.abspath(self.paddle_build_dir)
            self.with_gpu = PaddleLDFlag.cmake_bool(WITH_GPU)
            self.protolib = PROTOBUF_LIB
B
backyes 已提交
44
            self.zlib = ZLIB_LIB
Z
zhangjinchao01 已提交
45 46 47 48 49 50 51
            self.thread = CMAKE_THREAD_LIB
            self.dl_libs = CMAKE_DL_LIBS
            self.with_python = PaddleLDFlag.cmake_bool(WITH_PYTHON)
            self.python_libs = PYTHON_LIBRARIES

            self.glog_libs = LIBGLOG_LIBRARY

Y
Yu Yang 已提交
52
            self.with_coverage = PaddleLDFlag.cmake_bool(WITH_COVERALLS)
Z
zhangjinchao01 已提交
53 54 55 56 57 58
            self.gflags_libs = GFLAGS_LIBRARIES
            self.gflags_location = GFLAGS_LOCATION
            self.cblas_libs = CBLAS_LIBRARIES
            self.curt = CUDA_LIBRARIES

        def ldflag_str(self):
59 60
            return " ".join(
                [self.libs_dir_str(), self.parent_dir_str(), self.libs_str()])
Z
zhangjinchao01 已提交
61 62 63

        def libs_dir_str(self):
            libdirs = LIB_DIRS
64 65 66
            return " ".join(
                map(lambda x: "-L" + os.path.join(self.paddle_build_dir, x),
                    libdirs))
Z
zhangjinchao01 已提交
67 68 69

        def parent_dir_str(self):
            libdirs = PARENT_LIB_DIRS
70 71 72
            return " ".join(
                map(lambda x: "-L" + os.path.join(self.paddle_build_dir, '..', x),
                    libdirs))
Z
zhangjinchao01 已提交
73 74 75

        def libs_str(self):
            libs = [
L
liaogang 已提交
76
                whole_start,
Z
zhangjinchao01 已提交
77
                "-lpaddle_gserver",
H
hedaoyuan 已提交
78
                "-lpaddle_function",
L
liaogang 已提交
79
                whole_end,
Z
zhangjinchao01 已提交
80 81 82 83 84 85 86 87 88 89
                "-lpaddle_pserver",
                "-lpaddle_trainer_lib",
                "-lpaddle_network",
                '-lpaddle_parameter',
                "-lpaddle_math",
                '-lpaddle_utils',
                "-lpaddle_proto",
                "-lpaddle_cuda",
                "-lpaddle_api",
                self.normalize_flag(self.protolib),
90 91
                self.normalize_flag(self.glog_libs),
                self.normalize_flag(self.gflags_libs),
B
backyes 已提交
92
                self.normalize_flag(self.zlib),
Z
zhangjinchao01 已提交
93 94 95 96 97 98 99 100 101
                self.normalize_flag(self.thread),
                self.normalize_flag(self.dl_libs),
                self.normalize_flag(self.cblas_libs),
            ]

            if self.with_python:
                libs.append(self.normalize_flag(self.python_libs))
            if self.with_gpu:
                libs.append(self.normalize_flag(self.curt))
Y
Yu Yang 已提交
102 103
            if self.with_coverage:
                libs.append("-fprofile-arcs")
Z
zhangjinchao01 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
            return " ".join(filter(lambda l: len(l) != 0, libs))

        def normalize_flag(self, cmake_flag):
            """
            CMake flag string to ld flag
            :type cmake_flag: str
            """
            if ";" in cmake_flag:
                return " ".join(map(self.normalize_flag, cmake_flag.split(";")))
            if cmake_flag.startswith("/"):  # is a path
                return cmake_flag
            elif cmake_flag.startswith("-l"):  # normal link command
                return cmake_flag
117 118 119 120
            elif cmake_flag in [
                    "gflags-shared", "gflags-static", "gflags_nothreads-shared",
                    "gflags_nothreads-static"
            ]:  # special for gflags
Z
zhangjinchao01 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                assert PaddleLDFlag.cmake_bool(self.gflags_location)
                return self.gflags_location
            elif len(cmake_flag) != 0:
                return "".join(["-l", cmake_flag])
            else:
                return ""

        @staticmethod
        def cmake_bool(cmake_str):
            """
            CMake bool string to bool
            :param cmake_str: cmake boolean string
            :type cmake_str: str
            :rtype: bool
            """
136 137
            if cmake_str in ["FALSE", "OFF", "NO"] or cmake_str.endswith(
                    "-NOTFOUND"):
Z
zhangjinchao01 已提交
138 139 140
                return False
            else:
                return True
141

Y
Yu Yang 已提交
142 143
        def c_flag(self):
            if self.with_coverage:
L
liaogang 已提交
144 145 146 147
                return [
                    "-fprofile-arcs", "-ftest-coverage", "-O0", "-g",
                    "-std=c++11"
                ]
Y
Yu Yang 已提交
148
            else:
L
liaogang 已提交
149
                return ["-std=c++11"]
Z
zhangjinchao01 已提交
150
except ImportError:
151

Z
zhangjinchao01 已提交
152 153 154
    class PaddleLDFlag(object):
        def ldflag_str(self):
            pass
155

Y
Yu Yang 已提交
156 157
        def c_flag(self):
            pass