-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoonlight-dependencies.cmake
More file actions
453 lines (404 loc) · 17 KB
/
Copy pathmoonlight-dependencies.cmake
File metadata and controls
453 lines (404 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
include_guard(GLOBAL)
# Normalize FFmpeg configure probes that incorrectly succeed against the nxdk toolchain.
function(_moonlight_patch_ffmpeg_config_header ffmpeg_config_header)
if(NOT EXISTS "${ffmpeg_config_header}")
return()
endif()
file(READ "${ffmpeg_config_header}" ffmpeg_config_text)
foreach(ffmpeg_disabled_probe
IN ITEMS
HAVE_ALIGNED_MALLOC
HAVE_EXP2
HAVE_EXP2F
HAVE_LLRINT
HAVE_LLRINTF
HAVE_LOG2
HAVE_LOG2F
HAVE_LRINT
HAVE_LRINTF
HAVE_MEMALIGN
HAVE_MMAP
HAVE_POSIX_MEMALIGN
HAVE_RINT
HAVE_RINTF
HAVE_SCHED_GETAFFINITY
HAVE_STRERROR_R
HAVE_SYSCTL)
string(REGEX REPLACE
"#define ${ffmpeg_disabled_probe} [0-9]+"
"#define ${ffmpeg_disabled_probe} 0"
ffmpeg_config_text
"${ffmpeg_config_text}")
endforeach()
file(WRITE "${ffmpeg_config_header}" "${ffmpeg_config_text}")
endfunction()
# Validate FFmpeg source and support files before attempting a configure.
function(_moonlight_validate_xbox_ffmpeg_inputs ffmpeg_source_dir)
if(NOT EXISTS "${ffmpeg_source_dir}/configure")
message(FATAL_ERROR
"FFmpeg source directory not found: ${ffmpeg_source_dir}\n"
"Run: git submodule update --init --recursive")
endif()
foreach(ffmpeg_support_file IN LISTS ARGN)
if(NOT EXISTS "${ffmpeg_support_file}")
message(FATAL_ERROR "Required FFmpeg support file not found: ${ffmpeg_support_file}")
endif()
endforeach()
endfunction()
# Read the FFmpeg source revision used in the rebuild signature.
function(_moonlight_get_ffmpeg_revision out_var ffmpeg_source_dir)
execute_process(
COMMAND git -C "${ffmpeg_source_dir}" rev-parse HEAD
OUTPUT_VARIABLE ffmpeg_revision
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE ffmpeg_revision_result
)
if(NOT ffmpeg_revision_result EQUAL 0)
set(ffmpeg_revision unknown)
endif()
set(${out_var} "${ffmpeg_revision}" PARENT_SCOPE)
endfunction()
# Compute the FFmpeg rebuild signature from source and support-file inputs.
function(_moonlight_compute_ffmpeg_signature out_var nxdk_dir ffmpeg_source_dir)
set(ffmpeg_cc_wrapper "${ARGV3}")
set(ffmpeg_cxx_wrapper "${ARGV4}")
set(ffmpeg_compat_header "${ARGV5}")
_moonlight_get_ffmpeg_revision(ffmpeg_revision "${ffmpeg_source_dir}")
file(SHA256 "${ffmpeg_cc_wrapper}" ffmpeg_cc_wrapper_hash)
file(SHA256 "${ffmpeg_cxx_wrapper}" ffmpeg_cxx_wrapper_hash)
file(SHA256 "${ffmpeg_compat_header}" ffmpeg_compat_header_hash)
file(SHA256 "${CMAKE_SOURCE_DIR}/cmake/moonlight-dependencies.cmake" ffmpeg_dependency_manifest_hash)
set(signature_inputs
"FFMPEG_REVISION=${ffmpeg_revision}"
"NXDK_DIR=${nxdk_dir}"
"FFMPEG_PROFILE=h264-mpeg2-h263-opus-xbox"
"FFMPEG_TARGET_OS=none"
"FFMPEG_ARCH=x86"
"FFMPEG_CC_WRAPPER_SHA256=${ffmpeg_cc_wrapper_hash}"
"FFMPEG_CXX_WRAPPER_SHA256=${ffmpeg_cxx_wrapper_hash}"
"FFMPEG_COMPAT_HEADER_SHA256=${ffmpeg_compat_header_hash}"
"FFMPEG_DEPENDENCY_MANIFEST_SHA256=${ffmpeg_dependency_manifest_hash}")
list(JOIN signature_inputs "\n" signature_text)
string(SHA256 signature "${signature_text}")
set(${out_var} "${signature}" PARENT_SCOPE)
endfunction()
# Determine whether FFmpeg must be rebuilt from the saved signature and expected outputs.
function(_moonlight_should_rebuild_ffmpeg out_var signature_file signature)
set(need_rebuild FALSE)
if(NOT EXISTS "${signature_file}")
set(need_rebuild TRUE)
else()
file(READ "${signature_file}" saved_signature)
string(STRIP "${saved_signature}" saved_signature)
if(NOT saved_signature STREQUAL signature)
set(need_rebuild TRUE)
endif()
endif()
_moonlight_has_missing_output(ffmpeg_missing_output ${ARGN})
if(ffmpeg_missing_output)
set(need_rebuild TRUE)
endif()
set(${out_var} "${need_rebuild}" PARENT_SCOPE)
endfunction()
# Convert FFmpeg build paths to shell paths for the active host platform.
function(_moonlight_get_ffmpeg_shell_paths out_var)
set(ffmpeg_source_dir "${ARGV1}")
set(ffmpeg_install_dir "${ARGV2}")
set(ffmpeg_build_dir "${ARGV3}")
set(nxdk_dir "${ARGV4}")
set(ffmpeg_cc_wrapper "${ARGV5}")
set(ffmpeg_cxx_wrapper "${ARGV6}")
if(CMAKE_HOST_WIN32)
_moonlight_to_msys_path(ffmpeg_source_shell_path "${ffmpeg_source_dir}")
_moonlight_to_msys_path(ffmpeg_install_shell_path "${ffmpeg_install_dir}")
_moonlight_to_msys_path(ffmpeg_build_shell_path "${ffmpeg_build_dir}")
_moonlight_to_msys_path(nxdk_shell_path "${nxdk_dir}")
_moonlight_to_msys_path(ffmpeg_cc_wrapper_shell_path "${ffmpeg_cc_wrapper}")
_moonlight_to_msys_path(ffmpeg_cxx_wrapper_shell_path "${ffmpeg_cxx_wrapper}")
else()
set(ffmpeg_source_shell_path "${ffmpeg_source_dir}")
set(ffmpeg_install_shell_path "${ffmpeg_install_dir}")
set(ffmpeg_build_shell_path "${ffmpeg_build_dir}")
set(nxdk_shell_path "${nxdk_dir}")
set(ffmpeg_cc_wrapper_shell_path "${ffmpeg_cc_wrapper}")
set(ffmpeg_cxx_wrapper_shell_path "${ffmpeg_cxx_wrapper}")
endif()
set(ffmpeg_shell_paths
"${ffmpeg_source_shell_path}"
"${ffmpeg_install_shell_path}"
"${ffmpeg_build_shell_path}"
"${nxdk_shell_path}"
"${ffmpeg_cc_wrapper_shell_path}"
"${ffmpeg_cxx_wrapper_shell_path}")
set(${out_var} "${ffmpeg_shell_paths}" PARENT_SCOPE)
endfunction()
# Compose FFmpeg configure arguments for the nxdk target.
function(_moonlight_get_ffmpeg_configure_args out_var)
set(ffmpeg_source_shell_path "${ARGV1}")
set(ffmpeg_install_shell_path "${ARGV2}")
set(ffmpeg_cc_shell_path "sh ${ARGV3}")
set(ffmpeg_cxx_shell_path "sh ${ARGV4}")
set(ffmpeg_configure_args
sh
"${ffmpeg_source_shell_path}/configure"
"--prefix=${ffmpeg_install_shell_path}"
--enable-cross-compile
--arch=x86
--cpu=i686
--target-os=none
"--cc=${ffmpeg_cc_shell_path}"
"--cxx=${ffmpeg_cxx_shell_path}"
--ar=llvm-ar
--ranlib=llvm-ranlib
--nm=llvm-nm
--enable-static
--disable-shared
--disable-autodetect
--disable-asm
--disable-inline-asm
--disable-x86asm
--disable-debug
--disable-doc
--disable-programs
--disable-network
--disable-everything
--disable-avdevice
--disable-avfilter
--disable-avformat
--disable-iconv
--disable-zlib
--disable-bzlib
--disable-lzma
--disable-sdl2
--disable-symver
--disable-runtime-cpudetect
--disable-pthreads
--disable-w32threads
--disable-os2threads
--disable-hwaccels
--enable-avcodec
--enable-avutil
--enable-swscale
--enable-swresample
--enable-parser=h264
--enable-parser=h263
--enable-parser=mpegvideo
--enable-decoder=h264
--enable-decoder=h263
--enable-decoder=h263i
--enable-decoder=h263p
--enable-decoder=mpeg2video
--enable-decoder=opus)
set(${out_var} "${ffmpeg_configure_args}" PARENT_SCOPE)
endfunction()
# Execute an FFmpeg command in MSYS2 with nxdk paths ahead of the host PATH.
function(_moonlight_run_windows_ffmpeg_command description nxdk_shell_path ffmpeg_build_shell_path)
_moonlight_get_windows_msys2_shell(msys2_shell)
_moonlight_join_shell_command(ffmpeg_command ${ARGN})
_moonlight_shell_quote(quoted_nxdk_shell_path "${nxdk_shell_path}")
_moonlight_shell_quote(quoted_ffmpeg_build_shell_path "${ffmpeg_build_shell_path}")
string(CONCAT ffmpeg_script
"unset MAKEFLAGS MFLAGS GNUMAKEFLAGS MAKELEVEL; "
"unset MSYS2_ARG_CONV_EXCL; "
"export NXDK_DIR=${quoted_nxdk_shell_path}; "
"export PATH=\"$NXDK_DIR/bin:$PATH\"; "
"cd ${quoted_ffmpeg_build_shell_path}; "
"exec ${ffmpeg_command}")
execute_process(
COMMAND "${msys2_shell}" -defterm -here -no-start -mingw64 -c "${ffmpeg_script}"
RESULT_VARIABLE ffmpeg_command_result
)
if(NOT ffmpeg_command_result EQUAL 0)
message(FATAL_ERROR "${description} failed with exit code ${ffmpeg_command_result}")
endif()
endfunction()
# Rebuild and install FFmpeg for the Xbox target.
function(_moonlight_rebuild_xbox_ffmpeg nxdk_dir ffmpeg_source_dir ffmpeg_build_dir ffmpeg_install_dir)
set(ffmpeg_cc_wrapper "${ARGV4}")
set(ffmpeg_cxx_wrapper "${ARGV5}")
set(signature_file "${ARGV6}")
set(signature "${ARGV7}")
file(REMOVE_RECURSE "${ffmpeg_build_dir}" "${ffmpeg_install_dir}")
file(MAKE_DIRECTORY "${ffmpeg_build_dir}")
_moonlight_get_ffmpeg_shell_paths(
ffmpeg_shell_paths
"${ffmpeg_source_dir}"
"${ffmpeg_install_dir}"
"${ffmpeg_build_dir}"
"${nxdk_dir}"
"${ffmpeg_cc_wrapper}"
"${ffmpeg_cxx_wrapper}")
list(GET ffmpeg_shell_paths 0 ffmpeg_source_shell_path)
list(GET ffmpeg_shell_paths 1 ffmpeg_install_shell_path)
list(GET ffmpeg_shell_paths 2 ffmpeg_build_shell_path)
list(GET ffmpeg_shell_paths 3 nxdk_shell_path)
list(GET ffmpeg_shell_paths 4 ffmpeg_cc_wrapper_shell_path)
list(GET ffmpeg_shell_paths 5 ffmpeg_cxx_wrapper_shell_path)
_moonlight_get_ffmpeg_configure_args(
ffmpeg_configure_args
"${ffmpeg_source_shell_path}"
"${ffmpeg_install_shell_path}"
"${ffmpeg_cc_wrapper_shell_path}"
"${ffmpeg_cxx_wrapper_shell_path}")
if(CMAKE_HOST_WIN32)
_moonlight_run_windows_ffmpeg_command(
"FFmpeg configure"
"${nxdk_shell_path}"
"${ffmpeg_build_shell_path}"
${ffmpeg_configure_args})
else()
moonlight_run_nxdk_command(
"FFmpeg configure"
"${nxdk_dir}"
"${ffmpeg_build_dir}"
${ffmpeg_configure_args})
endif()
set(ffmpeg_config_header "${ffmpeg_build_dir}/config.h")
_moonlight_patch_ffmpeg_config_header("${ffmpeg_config_header}")
if(CMAKE_HOST_WIN32)
_moonlight_run_windows_ffmpeg_command(
"FFmpeg build"
"${nxdk_shell_path}"
"${ffmpeg_build_shell_path}"
make
-j4
install)
else()
moonlight_run_nxdk_command(
"FFmpeg build"
"${nxdk_dir}"
"${ffmpeg_build_dir}"
make
-j4
install)
endif()
file(WRITE "${signature_file}" "${signature}\n")
endfunction()
# Prepare the static FFmpeg libraries used by the Xbox streaming runtime.
function(moonlight_prepare_xbox_ffmpeg nxdk_dir)
set(ffmpeg_source_dir "${CMAKE_SOURCE_DIR}/third-party/ffmpeg")
set(ffmpeg_cc_wrapper "${CMAKE_SOURCE_DIR}/scripts/ffmpeg-nxdk-cc.sh")
set(ffmpeg_cxx_wrapper "${CMAKE_SOURCE_DIR}/scripts/ffmpeg-nxdk-cxx.sh")
set(ffmpeg_compat_header "${CMAKE_SOURCE_DIR}/src/_nxdk_compat/ffmpeg_compat.h")
_moonlight_validate_xbox_ffmpeg_inputs(
"${ffmpeg_source_dir}"
"${ffmpeg_cc_wrapper}"
"${ffmpeg_cxx_wrapper}"
"${ffmpeg_compat_header}")
set(ffmpeg_state_dir "${CMAKE_BINARY_DIR}/third-party/ffmpeg")
set(ffmpeg_build_dir "${ffmpeg_state_dir}/build")
set(ffmpeg_install_dir "${ffmpeg_state_dir}/install")
set(signature_file "${ffmpeg_state_dir}/build.signature")
set(required_outputs
"${ffmpeg_install_dir}/include/libavcodec/avcodec.h"
"${ffmpeg_install_dir}/lib/libavcodec.a"
"${ffmpeg_install_dir}/lib/libavutil.a"
"${ffmpeg_install_dir}/lib/libswscale.a"
"${ffmpeg_install_dir}/lib/libswresample.a")
file(MAKE_DIRECTORY "${ffmpeg_state_dir}")
_moonlight_compute_ffmpeg_signature(
signature
"${nxdk_dir}"
"${ffmpeg_source_dir}"
"${ffmpeg_cc_wrapper}"
"${ffmpeg_cxx_wrapper}"
"${ffmpeg_compat_header}")
_moonlight_should_rebuild_ffmpeg(
need_rebuild
"${signature_file}"
"${signature}"
${required_outputs})
if(need_rebuild)
message(STATUS "Preparing FFmpeg for Xbox at ${ffmpeg_build_dir}")
_moonlight_rebuild_xbox_ffmpeg(
"${nxdk_dir}"
"${ffmpeg_source_dir}"
"${ffmpeg_build_dir}"
"${ffmpeg_install_dir}"
"${ffmpeg_cc_wrapper}"
"${ffmpeg_cxx_wrapper}"
"${signature_file}"
"${signature}")
else()
message(STATUS "Using existing FFmpeg Xbox outputs from ${ffmpeg_install_dir}")
endif()
set(MOONLIGHT_FFMPEG_INCLUDE_DIR "${ffmpeg_install_dir}/include" PARENT_SCOPE)
set(MOONLIGHT_FFMPEG_LIBRARIES
"${ffmpeg_install_dir}/lib/libavcodec.a"
"${ffmpeg_install_dir}/lib/libswscale.a"
"${ffmpeg_install_dir}/lib/libswresample.a"
"${ffmpeg_install_dir}/lib/libavutil.a"
PARENT_SCOPE)
endfunction()
# Prepare dependencies that are common to multiple Moonlight components
macro(MOONLIGHT_PREPARE_COMMON_DEPENDENCIES)
include(GetOpenSSL REQUIRED)
if(NOT TARGET moonlight-openssl)
add_library(moonlight-openssl INTERFACE)
add_library(Moonlight::OpenSSL ALIAS moonlight-openssl)
target_link_libraries(moonlight-openssl
INTERFACE
OpenSSL::SSL
OpenSSL::Crypto)
endif()
set(ENET_NO_INSTALL ON CACHE BOOL "Do not install libraries built for enet" FORCE)
set(_moonlight_restore_build_shared_libs FALSE)
if(DEFINED BUILD_SHARED_LIBS)
set(_moonlight_restore_build_shared_libs TRUE)
set(_moonlight_saved_build_shared_libs "${BUILD_SHARED_LIBS}")
endif()
set(BUILD_SHARED_LIBS OFF)
if(NOT TARGET moonlight-common-c)
add_subdirectory(
"${CMAKE_SOURCE_DIR}/third-party/moonlight-common-c"
"${CMAKE_BINARY_DIR}/third-party/moonlight-common-c"
)
endif()
if(_moonlight_restore_build_shared_libs)
set(BUILD_SHARED_LIBS "${_moonlight_saved_build_shared_libs}")
else()
unset(BUILD_SHARED_LIBS)
endif()
if(TARGET moonlight-common-c AND DEFINED MOONLIGHT_OPENSSL_EXTERNAL_TARGET)
if(TARGET ${MOONLIGHT_OPENSSL_EXTERNAL_TARGET})
add_dependencies(moonlight-common-c ${MOONLIGHT_OPENSSL_EXTERNAL_TARGET})
endif()
endif()
if(TARGET moonlight-common-c
AND CMAKE_C_COMPILER_ID STREQUAL "GNU")
target_compile_options(moonlight-common-c PRIVATE -Wno-error=cast-function-type)
endif()
if(MOONLIGHT_BUILD_KIND STREQUAL "XBOX")
if(NOT DEFINED NXDK_DIR OR NXDK_DIR STREQUAL "")
message(FATAL_ERROR "NXDK_DIR must be defined before preparing Xbox dependencies")
endif()
set(MOONLIGHT_NXDK_NET_INCLUDE_DIR "${NXDK_DIR}/lib/net")
set(MOONLIGHT_NXDK_LIBC_EXTENSIONS_DIR "${NXDK_DIR}/lib/xboxrt/libc_extensions")
set(MOONLIGHT_NXDK_LWIP_POSIX_COMPAT_DIR "${NXDK_DIR}/lib/net/lwip/src/include/compat/posix")
set(MOONLIGHT_NXDK_LWIP_MDNS_SOURCES
"${NXDK_DIR}/lib/net/lwip/src/apps/mdns/mdns.c"
"${NXDK_DIR}/lib/net/lwip/src/apps/mdns/mdns_domain.c"
"${NXDK_DIR}/lib/net/lwip/src/apps/mdns/mdns_out.c"
)
if(TARGET enet)
target_compile_definitions(enet PRIVATE NXDK)
target_link_libraries(enet PUBLIC NXDK::NXDK NXDK::Net)
target_include_directories(enet PRIVATE
"${MOONLIGHT_NXDK_NET_INCLUDE_DIR}"
"${MOONLIGHT_NXDK_LIBC_EXTENSIONS_DIR}"
"${MOONLIGHT_NXDK_LWIP_POSIX_COMPAT_DIR}")
target_compile_options(enet PRIVATE -Wno-unused-function -Wno-error=unused-function)
endif()
if(TARGET moonlight-common-c)
target_compile_definitions(moonlight-common-c PRIVATE NXDK)
target_include_directories(moonlight-common-c PRIVATE
"${MOONLIGHT_NXDK_NET_INCLUDE_DIR}"
"${MOONLIGHT_NXDK_LIBC_EXTENSIONS_DIR}"
"${MOONLIGHT_NXDK_LWIP_POSIX_COMPAT_DIR}")
target_compile_options(moonlight-common-c PRIVATE
-Wno-unused-function
-Wno-error=unused-function)
endif()
moonlight_prepare_xbox_ffmpeg("${NXDK_DIR}")
endif()
endmacro()