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 33 34 35
    LIB_DIRS = [
        "math", 'utils', 'parameter', "gserver", "api", "cuda", "pserver",
        "trainer"
    ]
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 52 53
            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 已提交
54
            self.with_coverage = PaddleLDFlag.cmake_bool(WITH_COVERALLS)
Z
zhangjinchao01 已提交
55 56 57 58 59 60
            self.gflags_libs = GFLAGS_LIBRARIES
            self.gflags_location = GFLAGS_LOCATION
            self.cblas_libs = CBLAS_LIBRARIES
            self.curt = CUDA_LIBRARIES

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

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

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

        def libs_str(self):
            libs = [
L
liaogang 已提交
78
                whole_start,
Z
zhangjinchao01 已提交
79
                "-lpaddle_gserver",
L
liaogang 已提交
80
                whole_end,
Z
zhangjinchao01 已提交
81 82 83 84 85 86 87 88 89 90
                "-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 已提交
91
                self.normalize_flag(self.zlib),
Z
zhangjinchao01 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
                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 已提交
105 106
            if self.with_coverage:
                libs.append("-fprofile-arcs")
Z
zhangjinchao01 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
            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
120 121 122 123
            elif cmake_flag in [
                    "gflags-shared", "gflags-static", "gflags_nothreads-shared",
                    "gflags_nothreads-static"
            ]:  # special for gflags
Z
zhangjinchao01 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                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
            """
139 140
            if cmake_str in ["FALSE", "OFF", "NO"] or cmake_str.endswith(
                    "-NOTFOUND"):
Z
zhangjinchao01 已提交
141 142 143
                return False
            else:
                return True
144

Y
Yu Yang 已提交
145 146 147 148 149
        def c_flag(self):
            if self.with_coverage:
                return ["-fprofile-arcs", "-ftest-coverage", "-O0", "-g"]
            else:
                return None
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