Skip to content

Commit

Permalink
Statically link mingw/msvc runtime libraries on Windows
Browse files Browse the repository at this point in the history
Co-authored-by: David Snopek <dsnopek@gmail.com>
  • Loading branch information
feiyunw and dsnopek committed Aug 8, 2023
1 parent 5834e16 commit 54515da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 2 additions & 0 deletions tools/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def generate(env):

if env["optimize"] == "debug" or env["optimize"] == "none":
env.Append(CCFLAGS=["/MDd", "/Od"])
elif env["use_static_cpp"]:
env.Append(CCFLAGS=["/MT"])
else:
env.Append(CCFLAGS=["/MD"])

Expand Down
33 changes: 23 additions & 10 deletions tools/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
def options(opts):
opts.Add(BoolVariable("use_mingw", "Use the MinGW compiler instead of MSVC - only effective on Windows", False))
opts.Add(BoolVariable("use_clang_cl", "Use the clang driver instead of MSVC - only effective on Windows", False))
opts.Add(BoolVariable("use_static_cpp", "Link MinGW/MSVC C++ runtime libraries statically", True))


def exists(env):
Expand Down Expand Up @@ -45,6 +46,18 @@ def generate(env):
env["SHLIBPREFIX"] = ""
# Want dll suffix
env["SHLIBSUFFIX"] = ".dll"

env.Append(CCFLAGS=["-Wwrite-strings"])
if env["use_static_cpp"]:
env.Append(
LINKFLAGS=[
"-static",
"-Wl,--no-undefined",
"-static-libgcc",
"-static-libstdc++",
]
)

# Long line hack. Use custom spawn, quick AR append (to avoid files with the same names to override each other).
my_spawn.configure(env)

Expand All @@ -60,15 +73,15 @@ def generate(env):
# Want dll suffix
env["SHLIBSUFFIX"] = ".dll"

# These options are for a release build even using target=debug
env.Append(CCFLAGS=["-O3", "-Wwrite-strings"])
env.Append(
LINKFLAGS=[
"--static",
"-Wl,--no-undefined",
"-static-libgcc",
"-static-libstdc++",
]
)
env.Append(CCFLAGS=["-Wwrite-strings"])
if env["use_static_cpp"]:
env.Append(
LINKFLAGS=[
"-static",
"-Wl,--no-undefined",
"-static-libgcc",
"-static-libstdc++",
]
)

env.Append(CPPDEFINES=["WINDOWS_ENABLED"])

0 comments on commit 54515da

Please sign in to comment.