paddle_ld_flags.py 5.2 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
            self.gflags_location = GFLAGS_LOCATION
            self.cblas_libs = CBLAS_LIBRARIES
            self.curt = CUDA_LIBRARIES

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

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

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

        def libs_str(self):
            libs = [
L
liaogang 已提交
75
                whole_start,
Z
zhangjinchao01 已提交
76
                "-lpaddle_gserver",
H
hedaoyuan 已提交
77
                "-lpaddle_function",
L
liaogang 已提交
78
                whole_end,
Z
zhangjinchao01 已提交
79 80 81 82 83 84 85 86 87 88
                "-lpaddle_pserver",
                "-lpaddle_trainer_lib",
                "-lpaddle_network",
                '-lpaddle_parameter',
                "-lpaddle_math",
                '-lpaddle_utils',
                "-lpaddle_proto",
                "-lpaddle_cuda",
                "-lpaddle_api",
                self.normalize_flag(self.protolib),
89
                self.normalize_flag(self.glog_libs),
90
                self.normalize_flag("gflags"),
B
backyes 已提交
91
                self.normalize_flag(self.zlib),
Z
zhangjinchao01 已提交
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_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
            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
116
            elif cmake_flag == "gflags":  # special for gflags
Z
zhangjinchao01 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                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
            """
132 133
            if cmake_str in ["FALSE", "OFF", "NO"] or cmake_str.endswith(
                    "-NOTFOUND"):
Z
zhangjinchao01 已提交
134 135 136
                return False
            else:
                return True
137

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

Z
zhangjinchao01 已提交
148 149 150
    class PaddleLDFlag(object):
        def ldflag_str(self):
            pass
151

Y
Yu Yang 已提交
152 153
        def c_flag(self):
            pass