允许给定程序的 scons 编译失败

问题描述 投票:0回答:1

我有一个 SConstruct 文件来驱动 C++ 库的编译,并编译许多测试程序来检查其有效性。为了构建测试程序,以下 SConstruct 片段可以正常工作:

test_sources = glob.glob(r"test/unit_tests/*.cpp")
env.Append(RPATH=root_dir)
for tsource in test_sources:
  ttarget = tsource.replace('.cpp', '.out')
  env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')

但是,某些测试不应编译,即它们被故意设计为产生编译错误。我将如何测试编译错误?我尝试将编译命令放在 try/ except 块中,即

    test_sources = glob.glob(r"test/unit_tests/*.cpp")
    env.Append(RPATH=root_dir)
    for tsource in test_sources:
        ttarget = tsource.replace('_', '-').replace('.cpp', '.out')
        if 'mock' in os.path.basename(tsource):
            # filenames including 'mock' should not compile
            try:
                env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')
                sys.exit(1)
            except:
                pass
        else:
            env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')

但它不起作用(scons 因错误退出)

scons: Reading SConscript files ...
running with -j 2
scons: done reading SConscript files.
scons: Building targets ...
g++ -o test/unit-tests/month.out -Wl,-rpath=/home/xanthos/Software/ggdatetime test/unit_tests/month.o -L. libdatetime.so.0.1.0
g++ -o test/unit_tests/year_mock_1.o -c -std=c++17 -Wall -Wextra -Werror -pedantic -W -Wshadow -Winline -O2 -march=native --std=c++17 -Isrc test/unit_tests/year_mock_1.cpp
g++ -o test/unit_tests/year_mock_2.o -c -std=c++17 -Wall -Wextra -Werror -pedantic -W -Wshadow -Winline -O2 -march=native --std=c++17 -Isrc test/unit_tests/year_mock_2.cpp
test/unit_tests/year_mock_1.cpp: In function 'int main()':
test/unit_tests/year_mock_1.cpp:9:6: error: no match for 'operator==' (operand types are 'dso::year' and 'int')
    9 |   y1 == 2024;
      |   ~~ ^~ ~~~~
      |   |     |
      |   |     int
      |   dso::year
In file included from test/unit_tests/year_mock_1.cpp:1:
src/dtfund.hpp:1616:16: note: candidate: 'template<class DType, class, class> constexpr bool dso::operator==(DType, DType)'
 1616 | constexpr bool operator==(DType a, DType b) noexcept {
      |                ^~~~~~~~
src/dtfund.hpp:1616:16: note:   template argument deduction/substitution failed:
test/unit_tests/year_mock_1.cpp:9:9: note:   deduced conflicting types for parameter 'DType' ('dso::year' and 'int')
    9 |   y1 == 2024;
      |         ^~~~
scons: *** [test/unit_tests/year_mock_1.o] Error 1
test/unit_tests/year_mock_2.cpp: In function 'int main()':
test/unit_tests/year_mock_2.cpp:9:6: error: no match for 'operator+' (operand types are 'dso::year' and 'int')
    9 |   y1 + 2024;
      |   ~~ ^ ~~~~
      |   |    |
      |   |    int
      |   dso::year
In file included from test/unit_tests/year_mock_2.cpp:1:
src/dtfund.hpp:1751:17: note: candidate: 'template<class DType, class, class> constexpr DType dso::operator+(DType, DType)'
 1751 | constexpr DType operator+(DType _a, DType _b) noexcept {
      |                 ^~~~~~~~
src/dtfund.hpp:1751:17: note:   template argument deduction/substitution failed:
test/unit_tests/year_mock_2.cpp:9:8: note:   deduced conflicting types for parameter 'DType' ('dso::year' and 'int')
    9 |   y1 + 2024;
      |        ^~~~
scons: *** [test/unit_tests/year_mock_2.o] Error 1
scons: building terminated because of errors.
c++ automated-tests scons
1个回答
0
投票

试试这个:

test_sources = glob.glob(r"test/unit_tests/*.cpp")

test_env = env.Clone()

test_env['CXXCOM'] = "-"+test_env['CXXCOM']
test_env['SHCXXCOM'] = "-"+test_env['SHCXXCOM']
test_env['SHLINKCOM'] = "-"+test_env['SHLINKCOM']
test_env['LINKCOM'] = "-"+test_env['LINKCOM']

test_env.Append(RPATH=root_dir)
for tsource in test_sources:
  ttarget = tsource.replace('.cpp', '.out')
  test_env.Program(target=ttarget, source=tsource, CPPPATH='src/', LIBS=vlib, LIBPATH='.')

在任何操作前加上

-
将忽略返回值。 因此,我们克隆您的原始环境,并且仅修改 C++ 编译和链接命令(对于共享对象,您可能可以将其范围缩小到仅 SH* 环境,但我认为这应该可行。

有关操作对象的更多信息,请参阅:https://scons.org/doc/production/HTML/scons-man.html#action_objects

© www.soinside.com 2019 - 2024. All rights reserved.