677 lines
26 KiB
CMake
677 lines
26 KiB
CMake
# VoidLibCAutoVLC.cmake
|
|
#
|
|
# Automatic fat-VLC generation for targets that link against 'void'.
|
|
#
|
|
# Usage in a CMakeLists.txt:
|
|
#
|
|
# include(VoidLibCAutoVLC) # in root, or done via add_subdirectory
|
|
#
|
|
# # Define the VLC source as a SHARED library, link void:
|
|
# add_library(mylib SHARED src.c)
|
|
# target_link_libraries(mylib PRIVATE void)
|
|
# set_target_properties(mylib PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
|
#
|
|
# # For executable VLC (.vle), set the entry point before the hook fires:
|
|
# voidlibc_vlc_executable(mylib main)
|
|
#
|
|
# # Register the DEFER hook for this directory (call once per CMakeLists.txt):
|
|
# voidlibc_enable_auto_vlc()
|
|
#
|
|
# The hook fires at the end of the directory, scans all SHARED/STATIC targets
|
|
# that link 'void', and creates:
|
|
# <bindir>/<target>.vls or <bindir>/<target>.vle
|
|
# via zig cc (preferred) or clang cross-compilation for x86_64 Linux+Windows+macOS.
|
|
# Falls back to host compiler for Linux-only .vls when no cross-compiler is found.
|
|
#
|
|
# Compile options propagation: the hook reads COMPILE_OPTIONS and
|
|
# INCLUDE_DIRECTORIES from the target (filtering out generator expressions)
|
|
# and forwards them to every per-platform compile invocation.
|
|
|
|
cmake_minimum_required(VERSION 3.19) # cmake_language(DEFER)
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Internal: resolve the voidlink executable target.
|
|
# Works whether voidlink is a build-tree target or an installed IMPORTED target.
|
|
# Sets <tgt_var> (name for generator expressions) and <dep_var> (DEPENDS list).
|
|
# ---------------------------------------------------------------------------
|
|
function(_voidlibc_resolve_voidlink tgt_var dep_var)
|
|
if(TARGET voidlink)
|
|
set(${tgt_var} "voidlink" PARENT_SCOPE)
|
|
set(${dep_var} "voidlink" PARENT_SCOPE)
|
|
elseif(TARGET VoidLibC::voidlink)
|
|
set(${tgt_var} "VoidLibC::voidlink" PARENT_SCOPE)
|
|
set(${dep_var} "VoidLibC::voidlink" PARENT_SCOPE)
|
|
else()
|
|
message(FATAL_ERROR
|
|
"VoidLibC auto-VLC: voidlink not found. "
|
|
"Either build from source or install VoidLibC.")
|
|
endif()
|
|
endfunction()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Internal: detect cross-compiler once, cache results.
|
|
# ---------------------------------------------------------------------------
|
|
function(_voidlibc_detect_cross)
|
|
if(DEFINED CACHE{_VOIDLIBC_CC_KIND})
|
|
return()
|
|
endif()
|
|
|
|
find_program(_VOIDLIBC_ZIG_EXE zig)
|
|
find_program(_VOIDLIBC_CLANG_EXE clang)
|
|
|
|
if(_VOIDLIBC_ZIG_EXE)
|
|
set(_VOIDLIBC_CC_KIND "zig" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_BIN "${_VOIDLIBC_ZIG_EXE};cc" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_LINUX "x86_64-linux-musl" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_WINDOWS "x86_64-windows-gnu" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_MACOS "x86_64-macos-none" CACHE INTERNAL "")
|
|
message(STATUS "VoidLibC auto-VLC: using zig cc (${_VOIDLIBC_ZIG_EXE})")
|
|
elseif(_VOIDLIBC_CLANG_EXE)
|
|
set(_VOIDLIBC_CC_KIND "clang" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_BIN "${_VOIDLIBC_CLANG_EXE}" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_LINUX "x86_64-linux-gnu" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_WINDOWS "x86_64-pc-windows-msvc" CACHE INTERNAL "")
|
|
set(_VOIDLIBC_CC_MACOS "x86_64-apple-macos12" CACHE INTERNAL "")
|
|
message(STATUS "VoidLibC auto-VLC: using clang (${_VOIDLIBC_CLANG_EXE})")
|
|
else()
|
|
set(_VOIDLIBC_CC_KIND "host" CACHE INTERNAL "")
|
|
message(STATUS "VoidLibC auto-VLC: no cross-compiler found; producing Linux-only .vls")
|
|
endif()
|
|
endfunction()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Internal: filter a list, removing items that start with $< (generator exprs).
|
|
# Result written to <out_var> in caller scope.
|
|
# ---------------------------------------------------------------------------
|
|
function(_voidlibc_filter_genex out_var)
|
|
set(_result "")
|
|
foreach(_item IN LISTS ARGN)
|
|
if(NOT "${_item}" MATCHES "^\\$<")
|
|
list(APPEND _result "${_item}")
|
|
endif()
|
|
endforeach()
|
|
set(${out_var} "${_result}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Internal: given a target and output dir, create all custom commands/targets
|
|
# that produce <target>.vls or <target>.vle.
|
|
# ---------------------------------------------------------------------------
|
|
function(_voidlibc_make_vlc _tgt _outdir)
|
|
_voidlibc_detect_cross()
|
|
|
|
# Collect sources.
|
|
get_target_property(_srcs ${_tgt} SOURCES)
|
|
if(NOT _srcs)
|
|
return()
|
|
endif()
|
|
|
|
# Platform filter: VOIDLIBC_VLC_PLATFORMS property lists which OS sections
|
|
# to include. Defaults to all three when a cross-compiler is available,
|
|
# or "linux" only when falling back to the host compiler.
|
|
get_target_property(_plat_prop ${_tgt} VOIDLIBC_VLC_PLATFORMS)
|
|
if(_plat_prop)
|
|
set(_platforms "${_plat_prop}")
|
|
elseif(_VOIDLIBC_CC_KIND STREQUAL "host")
|
|
set(_platforms "linux")
|
|
else()
|
|
set(_platforms "linux;windows;macos")
|
|
endif()
|
|
|
|
# Determine output extension and entry flag.
|
|
get_target_property(_entry ${_tgt} VOIDLIBC_VLC_ENTRY)
|
|
if(_entry)
|
|
set(_ext ".vle")
|
|
set(_entry_args "--entry" "${_entry}")
|
|
else()
|
|
set(_ext ".vls")
|
|
set(_entry_args "")
|
|
endif()
|
|
|
|
set(_out_vlc "${_outdir}/${_tgt}${_ext}")
|
|
|
|
# Collect compile options and include dirs from the target (no genex).
|
|
get_target_property(_raw_opts ${_tgt} COMPILE_OPTIONS)
|
|
if(NOT _raw_opts)
|
|
set(_raw_opts "")
|
|
endif()
|
|
_voidlibc_filter_genex(_compile_opts ${_raw_opts})
|
|
|
|
get_target_property(_raw_incs ${_tgt} INCLUDE_DIRECTORIES)
|
|
if(NOT _raw_incs)
|
|
set(_raw_incs "")
|
|
endif()
|
|
_voidlibc_filter_genex(_raw_incs_clean ${_raw_incs})
|
|
set(_inc_flags "")
|
|
foreach(_d IN LISTS _raw_incs_clean)
|
|
list(APPEND _inc_flags "-I${_d}")
|
|
endforeach()
|
|
|
|
# Per-platform object lists.
|
|
set(_linux_objs "")
|
|
set(_windows_objs "")
|
|
set(_macos_objs "")
|
|
|
|
foreach(_src IN LISTS _srcs)
|
|
# Make source path absolute.
|
|
get_filename_component(_src_abs "${_src}" ABSOLUTE
|
|
BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
|
|
get_filename_component(_src_base "${_src}" NAME_WE)
|
|
set(_stem "${_tgt}_${_src_base}")
|
|
|
|
# Emit compile commands only for the requested platforms.
|
|
if("linux" IN_LIST _platforms)
|
|
set(_obj_linux "${_outdir}/${_stem}_linux.o")
|
|
if(_VOIDLIBC_CC_KIND STREQUAL "zig")
|
|
add_custom_command(OUTPUT "${_obj_linux}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
-target ${_VOIDLIBC_CC_LINUX}
|
|
-O2 -fPIC ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_linux}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] zig cc → Linux musl: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
elseif(_VOIDLIBC_CC_KIND STREQUAL "clang")
|
|
add_custom_command(OUTPUT "${_obj_linux}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
--target=${_VOIDLIBC_CC_LINUX}
|
|
-O2 -fPIC ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_linux}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] clang → Linux: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
else()
|
|
add_custom_command(OUTPUT "${_obj_linux}"
|
|
COMMAND ${CMAKE_C_COMPILER}
|
|
-O2 -fPIC ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_linux}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] host → Linux: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
endif()
|
|
list(APPEND _linux_objs "${_obj_linux}")
|
|
endif()
|
|
|
|
if("windows" IN_LIST _platforms AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
set(_obj_windows "${_outdir}/${_stem}_windows.obj")
|
|
if(_VOIDLIBC_CC_KIND STREQUAL "zig")
|
|
add_custom_command(OUTPUT "${_obj_windows}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
-target ${_VOIDLIBC_CC_WINDOWS}
|
|
-O2 ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_windows}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] zig cc → Windows GNU: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
else()
|
|
add_custom_command(OUTPUT "${_obj_windows}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
--target=${_VOIDLIBC_CC_WINDOWS}
|
|
-O2 ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_windows}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] clang → Windows: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
endif()
|
|
list(APPEND _windows_objs "${_obj_windows}")
|
|
endif()
|
|
|
|
if("macos" IN_LIST _platforms AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
set(_obj_macos "${_outdir}/${_stem}_macos.o")
|
|
if(_VOIDLIBC_CC_KIND STREQUAL "zig")
|
|
add_custom_command(OUTPUT "${_obj_macos}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
-target ${_VOIDLIBC_CC_MACOS}
|
|
-O2 -fPIC ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_macos}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] zig cc → macOS: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
else()
|
|
add_custom_command(OUTPUT "${_obj_macos}"
|
|
COMMAND ${_VOIDLIBC_CC_BIN}
|
|
--target=${_VOIDLIBC_CC_MACOS}
|
|
-O2 -fPIC ${_compile_opts} ${_inc_flags}
|
|
-c "${_src_abs}" -o "${_obj_macos}"
|
|
DEPENDS "${_src_abs}"
|
|
COMMENT "[VLC] clang → macOS: ${_src_base}.c"
|
|
VERBATIM
|
|
)
|
|
endif()
|
|
list(APPEND _macos_objs "${_obj_macos}")
|
|
endif()
|
|
endforeach()
|
|
|
|
# Build the fuse/pack-elf command with only the active platform groups.
|
|
_voidlibc_resolve_voidlink(_vl_tgt _vl_dep)
|
|
set(_fuse_cmd $<TARGET_FILE:${_vl_tgt}>)
|
|
set(_fuse_deps ${_vl_dep})
|
|
set(_fuse_platform_args "")
|
|
set(_fuse_comment_platforms "")
|
|
|
|
if(_linux_objs)
|
|
list(APPEND _fuse_platform_args --linux ${_linux_objs})
|
|
list(APPEND _fuse_deps ${_linux_objs})
|
|
string(APPEND _fuse_comment_platforms "Linux")
|
|
endif()
|
|
if(_windows_objs)
|
|
list(APPEND _fuse_platform_args --windows ${_windows_objs})
|
|
list(APPEND _fuse_deps ${_windows_objs})
|
|
string(APPEND _fuse_comment_platforms "+Windows")
|
|
endif()
|
|
if(_macos_objs)
|
|
list(APPEND _fuse_platform_args --macos ${_macos_objs})
|
|
list(APPEND _fuse_deps ${_macos_objs})
|
|
string(APPEND _fuse_comment_platforms "+macOS")
|
|
endif()
|
|
|
|
# Single platform → pack-elf; multiple → fuse.
|
|
list(LENGTH _platforms _nplat)
|
|
if(_nplat EQUAL 1 AND "linux" IN_LIST _platforms)
|
|
add_custom_command(OUTPUT "${_out_vlc}"
|
|
COMMAND $<TARGET_FILE:${_vl_tgt}>
|
|
pack-elf --os linux
|
|
${_linux_objs}
|
|
${_entry_args}
|
|
-o "${_out_vlc}"
|
|
DEPENDS ${_fuse_deps}
|
|
COMMENT "[VLC] pack-elf → ${_tgt}${_ext} (${_fuse_comment_platforms})"
|
|
VERBATIM
|
|
)
|
|
else()
|
|
add_custom_command(OUTPUT "${_out_vlc}"
|
|
COMMAND $<TARGET_FILE:${_vl_tgt}>
|
|
fuse
|
|
${_fuse_platform_args}
|
|
${_entry_args}
|
|
-o "${_out_vlc}"
|
|
DEPENDS ${_fuse_deps}
|
|
COMMENT "[VLC] fuse → ${_tgt}${_ext} (${_fuse_comment_platforms})"
|
|
VERBATIM
|
|
)
|
|
endif()
|
|
|
|
add_custom_target(${_tgt}_vlc ALL DEPENDS "${_out_vlc}")
|
|
endfunction()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Internal: DEFER callback — scans the directory for VLC candidates.
|
|
# ---------------------------------------------------------------------------
|
|
function(_voidlibc_finalize_directory)
|
|
get_property(_targets
|
|
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
|
PROPERTY BUILDSYSTEM_TARGETS
|
|
)
|
|
foreach(_tgt IN LISTS _targets)
|
|
get_target_property(_type ${_tgt} TYPE)
|
|
# Only process library types; skip executables and interface targets.
|
|
if(NOT _type MATCHES "^(SHARED_LIBRARY|STATIC_LIBRARY|OBJECT_LIBRARY)$")
|
|
continue()
|
|
endif()
|
|
# Check for 'void' or 'VoidLibC::void' in LINK_LIBRARIES.
|
|
get_target_property(_libs ${_tgt} LINK_LIBRARIES)
|
|
if(NOT _libs)
|
|
continue()
|
|
endif()
|
|
if("void" IN_LIST _libs OR "VoidLibC::void" IN_LIST _libs)
|
|
_voidlibc_make_vlc(${_tgt} "${CMAKE_CURRENT_BINARY_DIR}")
|
|
endif()
|
|
endforeach()
|
|
endfunction()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Public API
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# voidlibc_enable_auto_vlc()
|
|
# Call once per CMakeLists.txt that defines VLC-producing targets.
|
|
# Registers a DEFER hook that runs after all targets in this directory
|
|
# are defined and creates <target>.vls / <target>.vle for each one that
|
|
# links 'void'.
|
|
macro(voidlibc_enable_auto_vlc)
|
|
cmake_language(DEFER
|
|
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
|
|
CALL _voidlibc_finalize_directory
|
|
)
|
|
endmacro()
|
|
|
|
# voidlibc_vlc_executable(<target> <entry-symbol>)
|
|
# Mark a SHARED library target to produce an executable .vle instead of
|
|
# a shared-library .vls. Must be called before voidlibc_enable_auto_vlc().
|
|
macro(voidlibc_vlc_executable _tgt _entry)
|
|
set_target_properties(${_tgt} PROPERTIES VOIDLIBC_VLC_ENTRY "${_entry}")
|
|
endmacro()
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# High-level host-libc delegation API (Phase D)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
# _void_build_one_platform(<platform> <cc_kind> <cc_bin> <target_triple>
|
|
# <sources_abs> <stem_prefix> <outdir>
|
|
# <compile_opts> <inc_flags>
|
|
# <obj_list_var>)
|
|
#
|
|
# Internal helper: compile each source for one platform and append objects to <obj_list_var>.
|
|
function(_void_build_one_platform _plat _cc_kind _cc_bin _triple
|
|
_srcs _stem_pfx _outdir
|
|
_compile_opts _inc_flags
|
|
_obj_list_var)
|
|
set(_objs "")
|
|
set(_ext ".o")
|
|
if(_plat STREQUAL "windows")
|
|
set(_ext ".obj")
|
|
endif()
|
|
|
|
foreach(_src IN LISTS _srcs)
|
|
get_filename_component(_base "${_src}" NAME_WE)
|
|
set(_obj "${_outdir}/${_stem_pfx}_${_base}_${_plat}${_ext}")
|
|
|
|
if(_cc_kind STREQUAL "zig")
|
|
set(_fPIC "")
|
|
if(NOT _plat STREQUAL "windows")
|
|
set(_fPIC "-fPIC")
|
|
endif()
|
|
add_custom_command(OUTPUT "${_obj}"
|
|
COMMAND ${_cc_bin}
|
|
-target ${_triple}
|
|
-O2 ${_fPIC} ${_compile_opts} ${_inc_flags}
|
|
-c "${_src}" -o "${_obj}"
|
|
DEPENDS "${_src}"
|
|
COMMENT "[VLC] zig cc → ${_plat}: ${_base}"
|
|
VERBATIM
|
|
)
|
|
else()
|
|
set(_fPIC "")
|
|
if(NOT _plat STREQUAL "windows")
|
|
set(_fPIC "-fPIC")
|
|
endif()
|
|
add_custom_command(OUTPUT "${_obj}"
|
|
COMMAND ${_cc_bin}
|
|
--target=${_triple}
|
|
-O2 ${_fPIC} ${_compile_opts} ${_inc_flags}
|
|
-c "${_src}" -o "${_obj}"
|
|
DEPENDS "${_src}"
|
|
COMMENT "[VLC] clang → ${_plat}: ${_base}"
|
|
VERBATIM
|
|
)
|
|
endif()
|
|
list(APPEND _objs "${_obj}")
|
|
endforeach()
|
|
|
|
set(${_obj_list_var} "${_objs}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
# void_add_universal_library(<tgt>
|
|
# SOURCES <src>...
|
|
# [TARGETS linux|windows|macos...]
|
|
# [LINUX_NEEDS <soname>...] # DT_NEEDED entries for Linux section
|
|
# [WINDOWS_NEEDS <dll>...] # import DLLs for Windows section
|
|
# [MACOS_NEEDS <install-name>...] # LC_LOAD_DYLIB entries for macOS section
|
|
# [LINUX_SYMLIBS <sym=lib>...] # --symlib overrides for Linux
|
|
# [WINDOWS_SYMLIBS <sym=lib>...] # --symlib overrides for Windows
|
|
# [MACOS_SYMLIBS <sym=lib>...] # --symlib overrides for macOS
|
|
# [STRICT_LIBS] # promote unresolved symbols to errors
|
|
# )
|
|
#
|
|
# Cross-compiles SOURCES for each requested target OS and fuses them into
|
|
# <CMAKE_CURRENT_BINARY_DIR>/<tgt>.vls via voidlink.
|
|
function(void_add_universal_library _tgt)
|
|
cmake_parse_arguments(_V "STRICT_LIBS"
|
|
""
|
|
"SOURCES;TARGETS;LINUX_NEEDS;WINDOWS_NEEDS;MACOS_NEEDS;LINUX_SYMLIBS;WINDOWS_SYMLIBS;MACOS_SYMLIBS"
|
|
${ARGN}
|
|
)
|
|
|
|
if(NOT _V_SOURCES)
|
|
message(FATAL_ERROR "void_add_universal_library(${_tgt}): SOURCES required")
|
|
endif()
|
|
|
|
_voidlibc_detect_cross()
|
|
_voidlibc_resolve_voidlink(_vl_tgt _vl_dep)
|
|
|
|
# Default platforms.
|
|
if(NOT _V_TARGETS)
|
|
if(_VOIDLIBC_CC_KIND STREQUAL "host")
|
|
set(_V_TARGETS "linux")
|
|
else()
|
|
set(_V_TARGETS "linux;windows;macos")
|
|
endif()
|
|
endif()
|
|
|
|
# Absolute source paths.
|
|
set(_srcs_abs "")
|
|
foreach(_s IN LISTS _V_SOURCES)
|
|
get_filename_component(_sa "${_s}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
|
|
list(APPEND _srcs_abs "${_sa}")
|
|
endforeach()
|
|
|
|
set(_outdir "${CMAKE_CURRENT_BINARY_DIR}")
|
|
set(_out_vls "${_outdir}/${_tgt}.vls")
|
|
|
|
# Build per-platform object lists.
|
|
set(_linux_objs "")
|
|
set(_windows_objs "")
|
|
set(_macos_objs "")
|
|
|
|
if("linux" IN_LIST _V_TARGETS)
|
|
_void_build_one_platform("linux"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_LINUX}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_linux_objs)
|
|
endif()
|
|
if("windows" IN_LIST _V_TARGETS AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
_void_build_one_platform("windows"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_WINDOWS}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_windows_objs)
|
|
endif()
|
|
if("macos" IN_LIST _V_TARGETS AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
_void_build_one_platform("macos"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_MACOS}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_macos_objs)
|
|
endif()
|
|
|
|
# Build voidlink fuse command.
|
|
set(_fuse_args "")
|
|
set(_fuse_deps ${_vl_dep})
|
|
set(_comment_plats "")
|
|
|
|
set(_strict_arg "")
|
|
if(_V_STRICT_LIBS)
|
|
set(_strict_arg "--strict-libs")
|
|
endif()
|
|
|
|
if(_linux_objs)
|
|
list(APPEND _fuse_args --linux ${_linux_objs})
|
|
list(APPEND _fuse_deps ${_linux_objs})
|
|
foreach(_n IN LISTS _V_LINUX_NEEDS)
|
|
list(APPEND _fuse_args --linux-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _V_LINUX_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "Linux")
|
|
endif()
|
|
if(_windows_objs)
|
|
list(APPEND _fuse_args --windows ${_windows_objs})
|
|
list(APPEND _fuse_deps ${_windows_objs})
|
|
foreach(_n IN LISTS _V_WINDOWS_NEEDS)
|
|
list(APPEND _fuse_args --windows-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _V_WINDOWS_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "+Windows")
|
|
endif()
|
|
if(_macos_objs)
|
|
list(APPEND _fuse_args --macos ${_macos_objs})
|
|
list(APPEND _fuse_deps ${_macos_objs})
|
|
foreach(_n IN LISTS _V_MACOS_NEEDS)
|
|
list(APPEND _fuse_args --macos-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _V_MACOS_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "+macOS")
|
|
endif()
|
|
|
|
add_custom_command(OUTPUT "${_out_vls}"
|
|
COMMAND $<TARGET_FILE:${_vl_tgt}>
|
|
fuse ${_fuse_args} ${_strict_arg}
|
|
-o "${_out_vls}"
|
|
DEPENDS ${_fuse_deps}
|
|
COMMENT "[VLC] fuse → ${_tgt}.vls (${_comment_plats})"
|
|
VERBATIM
|
|
)
|
|
add_custom_target(${_tgt} ALL DEPENDS "${_out_vls}")
|
|
endfunction()
|
|
|
|
# void_add_universal_executable(<tgt>
|
|
# ENTRY <symbol>
|
|
# SOURCES <src>...
|
|
# [same optional keyword args as void_add_universal_library]
|
|
# )
|
|
#
|
|
# Like void_add_universal_library but produces <tgt>.vle with an entry point.
|
|
function(void_add_universal_executable _tgt)
|
|
cmake_parse_arguments(_VE "STRICT_LIBS"
|
|
"ENTRY"
|
|
"SOURCES;TARGETS;LINUX_NEEDS;WINDOWS_NEEDS;MACOS_NEEDS;LINUX_SYMLIBS;WINDOWS_SYMLIBS;MACOS_SYMLIBS"
|
|
${ARGN}
|
|
)
|
|
|
|
if(NOT _VE_ENTRY)
|
|
message(FATAL_ERROR "void_add_universal_executable(${_tgt}): ENTRY required")
|
|
endif()
|
|
if(NOT _VE_SOURCES)
|
|
message(FATAL_ERROR "void_add_universal_executable(${_tgt}): SOURCES required")
|
|
endif()
|
|
|
|
_voidlibc_detect_cross()
|
|
_voidlibc_resolve_voidlink(_vl_tgt _vl_dep)
|
|
|
|
if(NOT _VE_TARGETS)
|
|
if(_VOIDLIBC_CC_KIND STREQUAL "host")
|
|
set(_VE_TARGETS "linux")
|
|
else()
|
|
set(_VE_TARGETS "linux;windows;macos")
|
|
endif()
|
|
endif()
|
|
|
|
set(_srcs_abs "")
|
|
foreach(_s IN LISTS _VE_SOURCES)
|
|
get_filename_component(_sa "${_s}" ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
|
|
list(APPEND _srcs_abs "${_sa}")
|
|
endforeach()
|
|
|
|
set(_outdir "${CMAKE_CURRENT_BINARY_DIR}")
|
|
set(_out_vle "${_outdir}/${_tgt}.vle")
|
|
|
|
set(_linux_objs "")
|
|
set(_windows_objs "")
|
|
set(_macos_objs "")
|
|
|
|
if("linux" IN_LIST _VE_TARGETS)
|
|
_void_build_one_platform("linux"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_LINUX}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_linux_objs)
|
|
endif()
|
|
if("windows" IN_LIST _VE_TARGETS AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
_void_build_one_platform("windows"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_WINDOWS}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_windows_objs)
|
|
endif()
|
|
if("macos" IN_LIST _VE_TARGETS AND NOT _VOIDLIBC_CC_KIND STREQUAL "host")
|
|
_void_build_one_platform("macos"
|
|
"${_VOIDLIBC_CC_KIND}" "${_VOIDLIBC_CC_BIN}"
|
|
"${_VOIDLIBC_CC_MACOS}"
|
|
"${_srcs_abs}" "${_tgt}" "${_outdir}" "" ""
|
|
_macos_objs)
|
|
endif()
|
|
|
|
set(_fuse_args "--entry" "${_VE_ENTRY}")
|
|
set(_fuse_deps ${_vl_dep})
|
|
set(_comment_plats "")
|
|
set(_strict_arg "")
|
|
if(_VE_STRICT_LIBS)
|
|
set(_strict_arg "--strict-libs")
|
|
endif()
|
|
|
|
if(_linux_objs)
|
|
list(APPEND _fuse_args --linux ${_linux_objs})
|
|
list(APPEND _fuse_deps ${_linux_objs})
|
|
foreach(_n IN LISTS _VE_LINUX_NEEDS)
|
|
list(APPEND _fuse_args --linux-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _VE_LINUX_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "Linux")
|
|
endif()
|
|
if(_windows_objs)
|
|
list(APPEND _fuse_args --windows ${_windows_objs})
|
|
list(APPEND _fuse_deps ${_windows_objs})
|
|
foreach(_n IN LISTS _VE_WINDOWS_NEEDS)
|
|
list(APPEND _fuse_args --windows-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _VE_WINDOWS_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "+Windows")
|
|
endif()
|
|
if(_macos_objs)
|
|
list(APPEND _fuse_args --macos ${_macos_objs})
|
|
list(APPEND _fuse_deps ${_macos_objs})
|
|
foreach(_n IN LISTS _VE_MACOS_NEEDS)
|
|
list(APPEND _fuse_args --macos-need "${_n}")
|
|
endforeach()
|
|
foreach(_sl IN LISTS _VE_MACOS_SYMLIBS)
|
|
list(APPEND _fuse_args --symlib "${_sl}")
|
|
endforeach()
|
|
string(APPEND _comment_plats "+macOS")
|
|
endif()
|
|
|
|
add_custom_command(OUTPUT "${_out_vle}"
|
|
COMMAND $<TARGET_FILE:${_vl_tgt}>
|
|
fuse ${_fuse_args} ${_strict_arg}
|
|
-o "${_out_vle}"
|
|
DEPENDS ${_fuse_deps}
|
|
COMMENT "[VLC] fuse → ${_tgt}.vle (${_comment_plats})"
|
|
VERBATIM
|
|
)
|
|
add_custom_target(${_tgt} ALL DEPENDS "${_out_vle}")
|
|
endfunction()
|
|
|
|
# void_symbol_library(<sym> <linux-lib> <windows-lib> <macos-lib>)
|
|
# Append per-symbol host library overrides to the _VOID_{LINUX,WINDOWS,MACOS}_SYMLIBS
|
|
# lists in the caller's scope. Pass these lists to void_add_universal_library via
|
|
# LINUX_SYMLIBS / WINDOWS_SYMLIBS / MACOS_SYMLIBS.
|
|
#
|
|
# Example:
|
|
# void_symbol_library(my_platform_func
|
|
# myplatlib.so myplatlib.dll myplatlib.dylib)
|
|
# void_add_universal_library(mylib SOURCES foo.c
|
|
# LINUX_SYMLIBS ${_VOID_LINUX_SYMLIBS}
|
|
# WINDOWS_SYMLIBS ${_VOID_WINDOWS_SYMLIBS}
|
|
# MACOS_SYMLIBS ${_VOID_MACOS_SYMLIBS}
|
|
# )
|
|
macro(void_symbol_library _sym _linux_lib _win_lib _macos_lib)
|
|
list(APPEND _VOID_LINUX_SYMLIBS "${_sym}=${_linux_lib}")
|
|
list(APPEND _VOID_WINDOWS_SYMLIBS "${_sym}=${_win_lib}")
|
|
list(APPEND _VOID_MACOS_SYMLIBS "${_sym}=${_macos_lib}")
|
|
endmacro()
|