test_check_abi.py 5.3 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
3 4 5
# 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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9 10 11 12 13 14 15
# 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.

import os
16
import unittest
17 18 19 20 21 22 23
import warnings

import paddle.utils.cpp_extension.extension_utils as utils


class TestABIBase(unittest.TestCase):
    def test_environ(self):
24 25 26 27 28
        compiler_list = ['gcc', 'cl']
        for compiler in compiler_list:
            for flag in ['1', 'True', 'true']:
                os.environ['PADDLE_SKIP_CHECK_ABI'] = flag
                self.assertTrue(utils.check_abi_compatibility(compiler))
29 30 31 32 33 34 35

    def del_environ(self):
        key = 'PADDLE_SKIP_CHECK_ABI'
        if key in os.environ:
            del os.environ[key]


36
class TestCheckCompiler(TestABIBase):
37 38 39
    def test_expected_compiler(self):
        if utils.OS_NAME.startswith('linux'):
            gt = ['gcc', 'g++', 'gnu-c++', 'gnu-cc']
40 41 42 43 44 45
        elif utils.IS_WINDOWS:
            gt = ['cl']
        elif utils.OS_NAME.startswith('darwin'):
            gt = ['clang', 'clang++']

        self.assertListEqual(utils._expected_compiler_current_platform(), gt)
46

47
    def test_compiler_version(self):
48 49 50
        # clear environ
        self.del_environ()
        if utils.OS_NAME.startswith('linux'):
51 52 53
            compiler = 'g++'
        elif utils.IS_WINDOWS:
            compiler = 'cl'
54 55
        else:
            compiler = 'clang'
56 57 58 59 60

        # Linux: all CI gcc version > 5.4.0
        # Windows: all CI MSVC version > 19.00.24215
        # Mac: clang has no version limitation, always return true
        self.assertTrue(utils.check_abi_compatibility(compiler, verbose=True))
61 62 63 64

    def test_wrong_compiler_warning(self):
        # clear environ
        self.del_environ()
Z
Zhou Wei 已提交
65
        compiler = 'python'  # fake wrong compiler
66 67 68 69 70 71 72 73
        if not utils.IS_WINDOWS:
            with warnings.catch_warnings(record=True) as error:
                flag = utils.check_abi_compatibility(compiler, verbose=True)
                # check return False
                self.assertFalse(flag)
                # check Compiler Compatibility WARNING
                self.assertTrue(len(error) == 1)
                self.assertTrue(
74 75
                    "Compiler Compatibility WARNING" in str(error[0].message)
                )
76 77 78 79 80 81 82 83 84 85 86 87

    def test_exception_windows(self):
        # clear environ
        self.del_environ()
        compiler = 'fake compiler'  # fake command
        if utils.IS_WINDOWS:
            with warnings.catch_warnings(record=True) as error:
                flag = utils.check_abi_compatibility(compiler, verbose=True)
                # check return False
                self.assertFalse(flag)
                # check ABI Compatibility WARNING
                self.assertTrue(len(error) == 1)
88 89 90 91
                self.assertTrue(
                    "Failed to check compiler version for"
                    in str(error[0].message)
                )
92

93
    def test_exception_linux(self):
94 95 96 97
        # clear environ
        self.del_environ()
        compiler = 'python'  # fake command
        if utils.OS_NAME.startswith('linux'):
98

99 100 101 102 103 104 105 106 107 108 109 110
            def fake():
                return [compiler]

            # mock a fake function
            raw_func = utils._expected_compiler_current_platform
            utils._expected_compiler_current_platform = fake
            with warnings.catch_warnings(record=True) as error:
                flag = utils.check_abi_compatibility(compiler, verbose=True)
                # check return False
                self.assertFalse(flag)
                # check ABI Compatibility WARNING
                self.assertTrue(len(error) == 1)
111 112 113 114
                self.assertTrue(
                    "Failed to check compiler version for"
                    in str(error[0].message)
                )
115 116 117 118

            # restore
            utils._expected_compiler_current_platform = raw_func

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    def test_exception_mac(self):
        # clear environ
        self.del_environ()
        compiler = 'python'  # fake command
        if utils.OS_NAME.startswith('darwin'):

            def fake():
                return [compiler]

            # mock a fake function
            raw_func = utils._expected_compiler_current_platform
            utils._expected_compiler_current_platform = fake
            with warnings.catch_warnings(record=True) as error:
                flag = utils.check_abi_compatibility(compiler, verbose=True)
                # check return True
                self.assertTrue(flag)
                # check ABI Compatibility without WARNING
                self.assertTrue(len(error) == 0)

            # restore
            utils._expected_compiler_current_platform = raw_func

141 142 143 144

class TestRunCMDException(unittest.TestCase):
    def test_exception(self):
        for verbose in [True, False]:
145
            with self.assertRaisesRegex(RuntimeError, "Failed to run command"):
146 147 148 149 150 151
                cmd = "fake cmd"
                utils.run_cmd(cmd, verbose)


if __name__ == '__main__':
    unittest.main()