Scons environment seems to be clobbered after adding static library

151 Views Asked by At

I'm building a Godot extension, and here is my Scons file at commit 8907014:

#!/usr/bin/env python
import os
import sys

env = SConscript("godot-cpp/SConstruct")

# For reference:
# - CCFLAGS are compilation flags shared between C and C++
# - CFLAGS are for C-specific compilation flags
# - CXXFLAGS are for C++-specific compilation flags
# - CPPFLAGS are for pre-processor flags
# - CPPDEFINES are for pre-processor defines
# - LINKFLAGS are for linking flags

llamacpp_path = ARGUMENTS.get('llamacpp_path', 'llama.cpp')  # llama.cpp is a directory, not a cpp file :)

# Specify where to find headers and libraries
env.Append(CPPPATH=[
    "src/",
    llamacpp_path,
    os.path.join(llamacpp_path, 'common'),
    # "godot-cpp/include/godot_cpp/classes/"  # Add this path for wrapped.hpp
])
sources = Glob("src/*.cpp")

# Include wrapped.cpp in your sources
# sources.append("godot-cpp/src/classes/wrapped.cpp")

# add 'pthread' to libraries
libraries = ['pthread']
env.Append(LIBS=libraries)

# Object files from the specified path
object_files = [
    os.path.join(llamacpp_path, 'ggml-alloc.o'),
    os.path.join(llamacpp_path, 'k_quants.o'),
    os.path.join(llamacpp_path, 'ggml.o'),
    os.path.join(llamacpp_path, 'common.o'),
    os.path.join(llamacpp_path, 'llama.o')
]

# Create a static library from the object files
static_lib = env.StaticLibrary('llama', object_files)

# Link the shared library against the static library
if env["platform"] == "macos":
    library = env.SharedLibrary(
        "the-game/bin/libgdllm.{}.{}.framework/libgdllm.{}.{}".format(
            env["platform"], env["target"], env["platform"], env["target"]
        ),
        source=sources,
        LIBS=[static_lib]
    )
else:
    library = env.SharedLibrary(
        "the-game/bin/libgdllm{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
        LIBS=[static_lib]
    )

Default(library)

When the GDExtension loads in the engine, it errors with undefined godot-cpp symbols. This worked previously before I added the llama static library:

static_lib = env.StaticLibrary('llama', object_files)

Without the llama static library, I got undefined llama symbols.

I'm new to Scons, so to my eyes it looks like that adding the llama static library broke the godot-cpp environment.

Given that llama and godot-cpp don't depend on each other, it's not a linking order issue.

I also tried to give godot-cpp and llama separate environments, but I'm still getting undefined godot-cpp symbols after that.

To recap:

  • I got the basic GDExtension example working
  • Then I added llama as a dependency, but got undefined llama symbols at runtime
  • I changed llama to be a static library, but now get undefined godot-cpp symbols at runtime
  • I fixed the first godot-cpp undefined symbol by adding the lines for wrapped.hpp and wrapped.cpp (now commented), but then it just moves onto the next undefined godot-cpp symbol. Is this the way to go? But, then, what is the point of the godot-cpp SConscript environment?
1

There are 1 best solutions below

1
Mats Wichmann On

Trimming away, you've done this:

    library = env.SharedLibrary(
        "the-game/bin/libgdllm{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
        source=sources,
        LIBS=[static_lib]
    )

Passing keyword arguments to a builder creates what SCons calls an "override environment" - like an overlay - values are pulled from the override in preference to the base environment. In the override, you've set LIBS to a list containing just static_lib, meaning that build doesn't see any original value of LIBS - in other words, you're covering up whatever the call to SConscript("godot-cpp/SConstruct") set up for env["LIBS"] (which you then appended pthread to). You need to add your library (static or shared should make no difference), which is a little tricky. One approach might be to fetch it beforehand, something like:

shliblist = env['LIBS'] + [static_lib]
library = env.SharedLibrary(... LIBS=shliblist ...)

or, if you think it won't hurt anything else, you can just env.Append like you did for the addition of pthread.

Additional comments:

# llama.cpp is a directory, not a cpp file :)

Really recommend you don't do that... the chance of SCons (or someome else) misinterpreting that exists - the fact you need that comment at all is a hint.

# Specify where to find headers and libraries
env.Append(CPPPATH=[   ...

CPPPATH is just for headers. If you want to set library search paths, you need to use LIBPATH (see https://scons.org/doc/production/HTML/scons-man.html#cv-LIBPATH)