paddle_ld_flags.py 5.4 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved
#
# 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 33 34 35 36 37 38 39 40

    LIB_DIRS = ["math", 'utils', 'parameter', "gserver", "api", "cuda", "pserver", "trainer"]
    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 已提交
41
            self.zlib = ZLIB_LIB
Z
zhangjinchao01 已提交
42 43 44 45 46 47 48 49 50
            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.with_glog = PaddleLDFlag.cmake_bool(WITH_GLOG)
            self.glog_libs = LIBGLOG_LIBRARY

            self.with_gflags = PaddleLDFlag.cmake_bool(WITH_GFLAGS)
Y
Yu Yang 已提交
51
            self.with_coverage = PaddleLDFlag.cmake_bool(WITH_COVERALLS)
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            self.gflags_libs = GFLAGS_LIBRARIES
            self.gflags_location = GFLAGS_LOCATION
            self.cblas_libs = CBLAS_LIBRARIES
            self.curt = CUDA_LIBRARIES

        def ldflag_str(self):
            return " ".join([self.libs_dir_str(),
                             self.parent_dir_str(),
                             self.libs_str()])

        def libs_dir_str(self):
            libdirs = LIB_DIRS
            return " ".join(map(lambda x: "-L" + os.path.join(self.paddle_build_dir, x),
                                libdirs))

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

        def libs_str(self):
            libs = [
L
liaogang 已提交
74
                whole_start,
Z
zhangjinchao01 已提交
75
                "-lpaddle_gserver",
L
liaogang 已提交
76
                whole_end,
Z
zhangjinchao01 已提交
77 78 79 80 81 82 83 84 85 86
                "-lpaddle_pserver",
                "-lpaddle_trainer_lib",
                "-lpaddle_network",
                '-lpaddle_parameter',
                "-lpaddle_math",
                '-lpaddle_utils',
                "-lpaddle_proto",
                "-lpaddle_cuda",
                "-lpaddle_api",
                self.normalize_flag(self.protolib),
B
backyes 已提交
87
                self.normalize_flag(self.zlib),
Z
zhangjinchao01 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
                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_glog:
                libs.append(self.normalize_flag(self.glog_libs))
            if self.with_gflags:
                libs.append(self.normalize_flag(self.gflags_libs))
            if self.with_gpu:
                libs.append(self.normalize_flag(self.curt))
Y
Yu Yang 已提交
101 102
            if self.with_coverage:
                libs.append("-fprofile-arcs")
Z
zhangjinchao01 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
            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
            elif cmake_flag in ["gflags-shared",
                                "gflags-static",
                                "gflags_nothreads-shared",
                                "gflags_nothreads-static"]:  # special for gflags
                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
            """
            if cmake_str in ["FALSE", "OFF", "NO"] or cmake_str.endswith("-NOTFOUND"):
                return False
            else:
                return True
Y
Yu Yang 已提交
139 140 141 142 143
        def c_flag(self):
            if self.with_coverage:
                return ["-fprofile-arcs", "-ftest-coverage", "-O0", "-g"]
            else:
                return None
Z
zhangjinchao01 已提交
144 145 146 147
except ImportError:
    class PaddleLDFlag(object):
        def ldflag_str(self):
            pass
Y
Yu Yang 已提交
148 149
        def c_flag(self):
            pass