编辑:尝试使其工作一天后,我只是用cmake重新编写了它。使用godot native实际上并不需要以任何方式使用Scons。
我有一个带有一些本机(c ++)脚本的godot项目。我想为C ++类添加google-test单元测试。
scons对我来说是全新的,我很难查找文档。使用现有的SConstruct文件,如何添加一个新的google测试目标,该目标使用我的native/test
目录(多层子目录)中的源文件,并链接到我现有的库?
我想使用VariantDir将目标文件保留在测试源目录之外。
测试目标行在底部附近,我包括了整个文件作为上下文。
#!python
import os, subprocess
opts = Variables([], ARGUMENTS)
projectName = 'mygame'
projectPrettyName = 'MyGame'
# Gets the standard flags CC, CCX, etc.
env = DefaultEnvironment()
# compilation database support
env.Tool("compilation_db")
env.Alias("compiledb", env.CompilationDatabase('compile_commands.json'))
# Define our options
opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release']))
opts.Add(EnumVariable('platform', "Compilation platform", 'linux', ['', 'windows', 'x11', 'linux', 'osx']))
opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", 'linux', ['', 'windows', 'x11', 'linux', 'osx']))
opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'yes'))
opts.Add(PathVariable('target_path', 'The path where the lib is installed.', projectName + '.godot/bin/'))
opts.Add(PathVariable('target_name', 'The library name.', 'lib' + projectName, PathVariable.PathAccept))
# Local dependency paths, adapt them to your setup
godot_headers_path = "godot-cpp/godot_headers/"
cpp_bindings_path = "godot-cpp/"
cpp_library = "libgodot-cpp"
# only support 64 at this time..
bits = 64
# Updates the environment with the option variables.
opts.Update(env)
# Process some arguments
if env['use_llvm']:
env['CC'] = 'clang'
env['CXX'] = 'clang++'
if env['p'] != '':
env['platform'] = env['p']
if env['platform'] == '':
print("No valid target platform selected.")
quit();
# Check our platform specifics
if env['platform'] == "osx":
env['target_path'] += 'osx/'
cpp_library += '.osx'
if env['target'] in ('debug', 'd'):
env.Append(CCFLAGS=['-g', '-O2', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
else:
env.Append(CCFLAGS=['-g', '-O3', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64'])
elif env['platform'] in ('x11', 'linux'):
env['target_path'] += 'x11/'
cpp_library += '.linux'
if env['target'] in ('debug', 'd'):
env.Append(CCFLAGS=['-fPIC', '-g3', '-Og'])
env.Append(CXXFLAGS=['-std=c++17'])
else:
env.Append(CCFLAGS=['-fPIC', '-g', '-O3'])
env.Append(CXXFLAGS=['-std=c++17'])
elif env['platform'] == "windows":
env['target_path'] += 'win64/'
cpp_library += '.windows'
# This makes sure to keep the session environment variables on windows,
# that way you can run scons in a vs 2017 prompt and it will find all the required tools
env.Append(ENV=os.environ)
env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS'])
env.Append(CCFLAGS=['-W3', '-GR'])
if env['target'] in ('debug', 'd'):
env.Append(CPPDEFINES=['_DEBUG'])
env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
env.Append(LINKFLAGS=['-DEBUG'])
else:
env.Append(CPPDEFINES=['NDEBUG'])
env.Append(CCFLAGS=['-O2', '-EHsc', '-MD'])
if env['target'] in ('release', 'r'):
cpp_library += '.release'
else:
cpp_library += '.debug'
cpp_library += '.' + str(bits)
# make sure our binding library is properly includes
env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/'])
env.Append(LIBPATH=[cpp_bindings_path + 'bin/'])
env.Append(LIBS=[cpp_library])
env.Append(CPPPATH=['native/include/'])
env.VariantDir('#build/', 'native/src', duplicate=0)
sources = Glob('#build/**.cpp')
libPath = target=env['target_path'] + env['target_name']
library = env.SharedLibrary(libPath, source=sources)
# Test target description: Help needed here
env.VariantDir('#tbuild/', 'native/test', duplicate=0) # ???
testsources = Glob('#tbuild/*cpp') # ???
tests = env.Program(???)
Default(library, 'compiledb')
以下是我们如何使用Google测试的示例。我们在程序代码所在的位置添加了一个SConscript文件。 SConsctruct正在调用该脚本。:
env.USE_GTEST()
env.Append(LIBS = libraries)
#Creates the binary
prg = env.buildProgram('ts_test', ['ts_test.cpp'])
Alias('but', prg)
# Execute the test seting the resulting XML as a target.
# Note how using the previous target as source means that test will ONLY RUN if the source has changed
# and if it's source changed, and so on. So a unit-test will not run if it's tested code did not change.
# OPTIONS: Change test execution options here. Do "--help" on your unit-test binary for a complete list.
# Example: Use this to run 10 times only tests with name that starts with 'memory'.
# testOptions = ' --gtest_repeat=10 --gtest_filter=memory*
testOptions = ' --gtest_color=yes'
Alias('rut', env.Command(target = 'unit_test_result.xml',
source = prg,
action = [ 'cd ' + env.getStripCwd() + ';' + os.path.join(env['LOCALROOT'],'$SOURCE') + testOptions + ' --gtest_output=xml:' + os.path.join(env['LOCALROOT'],'$TARGET')] ) )
def USE_GTEST(env):
gtestRoot = get_third_party_root(env, 'GTEST_ROOT')
incDir = os.path.join(gtestRoot, 'include')
libs = []
if env['PLATFORM'] == 'posix':
libDir = os.path.join(gtestRoot, 'lib')
libs.append('gtest')
libs.append('gtest_main')
libs.append('pthread')
else: # Windows
libDir = os.path.join(gtestRoot, 'msvc', '2010', 'gtest-md')
if isDebug(env):
libs.append('gtestd')
libs.append('gtest_maind')
if env['CONFIG'] == 'win32':
libDir = os.path.join(libDir, 'Win32-Debug')
else:
libDir = os.path.join(libDir, 'x64-Debug')
else:
libs.append('gtest')
libs.append('gtest_main')
if env['CONFIG'] == 'win32':
libDir = os.path.join(libDir, 'Win32-Release')
else:
libDir = os.path.join(libDir, 'x64-Release')
env.Append(CPPPATH = [incDir],
LIBPATH = [libDir],
LIBS = libs)