Skip to content

Commit

Permalink
[Core] Add a new CMake option to control linking against static C/C++
Browse files Browse the repository at this point in the history
runtime.

Also update dependencies script with new flag for selecting
static/dynamic linking to the CRT.

See also: #349
  • Loading branch information
lakulish committed Jul 15, 2024
1 parent 807b894 commit 9eeb450
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
8 changes: 7 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ if (IPL_OS_WINDOWS AND IPL_CPU_X64)
option(STEAMAUDIO_BUILD_DOCS "Build documentation." ON)
endif()

# If true, statically link against the C/C++ runtime library.
option(STEAMAUDIO_STATIC_RUNTIME "Statically link against the C/C++ runtime library." ON)

if ((IPL_OS_WINDOWS OR IPL_OS_LINUX) AND (IPL_CPU_X86 OR IPL_CPU_X64))
option(STEAMAUDIO_ENABLE_AVX "Enable AVX intrinsics." ON)
endif()
Expand Down Expand Up @@ -157,7 +160,10 @@ if (IPL_OS_WINDOWS)
)
add_compile_options(
/EHsc /MP /W3 /Zi
$<IF:$<CONFIG:Debug>,/MTd,/MT>
$<$<AND:$<CONFIG:Debug>,$<BOOL:${STEAMAUDIO_STATIC_RUNTIME}>>:/MTd>
$<$<AND:$<CONFIG:Debug>,$<NOT:$<BOOL:${STEAMAUDIO_STATIC_RUNTIME}>>>:/MDd>
$<$<AND:$<NOT:$<CONFIG:Debug>>,$<BOOL:${STEAMAUDIO_STATIC_RUNTIME}>>:/MT>
$<$<AND:$<NOT:$<CONFIG:Debug>>,$<NOT:$<BOOL:${STEAMAUDIO_STATIC_RUNTIME}>>>:/MD>
$<$<CONFIG:Debug>:/RTC1>
$<$<NOT:$<CONFIG:Debug>>:/Ox>
$<$<CONFIG:Release>:/GL>
Expand Down
33 changes: 23 additions & 10 deletions core/build/get_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def fetch_dependency(name, dep, platform):
def toolchain(suffix):
return os.path.join(os.getcwd(), 'build', 'toolchain_' + suffix + '.cmake')

def configure_cmake(name, cmake_layers, platform, cmake, vs_version, ndk_path, debug):
def configure_cmake(name, cmake_layers, platform, cmake, vs_version, ndk_path, debug, shared_crt):
src_dir = os.path.join(os.getcwd(), 'deps-build', name, 'src', name)
build_dir = os.path.join(os.getcwd(), 'deps-build', name, 'build', platform)
install_dir = os.path.join(os.getcwd(), 'deps-build', name, 'install', platform)
Expand All @@ -486,16 +486,28 @@ def configure_cmake(name, cmake_layers, platform, cmake, vs_version, ndk_path, d

if platform == 'windows-x86':
cmake_args += ['-G', vs_generator_name(vs_version), '-A', 'Win32']
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
if not shared_crt:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
else:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MD', '-DCMAKE_CXX_FLAGS_RELEASE=/MD']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MDd', '-DCMAKE_CXX_FLAGS_DEBUG=/MDd']
elif platform == 'windows-x64':
cmake_args += ['-G', vs_generator_name(vs_version), '-A', 'x64']
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
if not shared_crt:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
else:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MD', '-DCMAKE_CXX_FLAGS_RELEASE=/MD']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MDd', '-DCMAKE_CXX_FLAGS_DEBUG=/MDd']
elif platform == 'windows-arm64':
cmake_args += ['-G', vs_generator_name(vs_version), '-A', 'ARM64']
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
if not shared_crt:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MT', '-DCMAKE_CXX_FLAGS_RELEASE=/MT']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MTd', '-DCMAKE_CXX_FLAGS_DEBUG=/MTd']
else:
cmake_args += ['-DCMAKE_C_FLAGS_RELEASE=/MD', '-DCMAKE_CXX_FLAGS_RELEASE=/MD']
cmake_args += ['-DCMAKE_C_FLAGS_DEBUG=/MDd', '-DCMAKE_CXX_FLAGS_DEBUG=/MDd']
elif platform == 'linux-x86':
cmake_args += ['-G', 'Unix Makefiles']
cmake_args += ['-DCMAKE_C_FLAGS=-m32', '-DCMAKE_CXX_FLAGS=-m32', '-DCMAKE_SHARED_LINKER_FLAGS=-m32', '-DCMAKE_EXE_LINKER_FLAGS=-m32']
Expand Down Expand Up @@ -597,7 +609,7 @@ def configure_custom(name, custom, platform, cmake, vs_version):

stamp_configure(name, platform, stamp)

def configure_dependency(name, dep, platform, cmake, vs_version, ndk_path, debug):
def configure_dependency(name, dep, platform, cmake, vs_version, ndk_path, debug, shared_crt):
configure = dep.get("configure", {})
if len(configure) == 0:
return
Expand All @@ -606,7 +618,7 @@ def configure_dependency(name, dep, platform, cmake, vs_version, ndk_path, debug
custom = configure.get("custom", None)

if cmake_layers is not None:
configure_cmake(name, cmake_layers, platform, cmake, vs_version, ndk_path, debug)
configure_cmake(name, cmake_layers, platform, cmake, vs_version, ndk_path, debug, shared_crt)
elif custom is not None:
configure_custom(name, custom, platform, cmake, vs_version)

Expand Down Expand Up @@ -790,6 +802,7 @@ def toposort(deps):
parser.add_argument('--debug', help = "Build debug binaries.", action='store_true', default=False)
parser.add_argument('--toolsonly', help = "Build tool dependencies only.", action='store_true', default=False)
parser.add_argument('--libsonly', help = "Build library dependencies only.", action='store_true', default=False)
parser.add_argument('--sharedcrt', help = "Link to shared C/C++ runtime library. (Windows only)", action='store_true', default=False)
args = parser.parse_args()

if args.platform in ['osx', 'ios']:
Expand Down Expand Up @@ -898,7 +911,7 @@ def toposort(deps):

try:
fetch_dependency(name, dep, platform)
configure_dependency(name, dep, platform, cmake, vs_version, args.ndk, debug)
configure_dependency(name, dep, platform, cmake, vs_version, args.ndk, debug, args.sharedcrt)
build_dependency(name, dep, platform, cmake, vs_version, args.ndk, debug)
install_dependency(name, dep, platform, cmake, debug)
copy_dependency(name, dep, platform)
Expand Down

0 comments on commit 9eeb450

Please sign in to comment.