【发布时间】:2020-02-27 10:06:27
【问题描述】:
编辑:尝试让它工作一天后,我只是在 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')
【问题讨论】:
-
整个 SConscipt 是您创建的吗?为什么使用 DefaultEnvironment()?本机/测试下有多少级目录?那里的源是由其他逻辑生成的,还是静态的(不是机器生成的)?
-
@bdbaddog,它是 godot 示例 c++ 项目附带的 SConscript 的略微修改版本。为什么不使用 DefaultEnvironment?在 native/test 下会有多级目录,它们会匹配我的 native/src 下的目录树。抱歉,如果这些是明显的问题,我对 make 和 cmake 非常熟悉,但似乎无法理解 scons
-
native/test下的文件是否有构建逻辑生成的?
标签: c++ googletest scons godot