mirror of
https://github.com/1dot13/source.git
synced 2026-08-05 14:00:23 +02:00
The tree compiles and links under clang-cl, so make that a configuration
anyone can ask for rather than a command line to reconstruct:
MSVC_DIRECTORY=/path/to/msvc cmake --preset '1dot13 Release clang-cl'
It is a check on the source, not a port -- MSVC still builds what ships. What
it buys is a second front end over every translation unit, which is how the
inline assembly with no operand size and the resource string with a raw 0xA9
were found, and it runs natively: clang-cl, lld-link, llvm-rc and llvm-lib
need no Wine, so a full JA2.exe takes a fraction of the time.
The toolchain file names every MSVC and SDK include directory, because
clang-cl does not read INCLUDE and LIB the way cl does, and picks the newest
version of each tree rather than pinning one. MSVC_DIRECTORY says where they
live and is reported plainly when it is missing or wrong.
build a bindable Bink import library when not building with MSVC
The shipped binkw32.lib is an old ordinal-based long-format import library.
MSVC's link.exe binds it, but clang-cl's lld-link does not: it leaves every
Bink import unresolved, so at run time the calls jump into arbitrary code and
crash the game at startup, six access violations deep in unrelated code before
the game's own handler gives up.
Rebuild the import library from binkw32.def with llvm-dlltool on any front end
other than MSVC. The library imports by ordinal, for the reasons binkw32.def
records: binkw32.dll exports decorated stdcall names that keep their leading
underscore (_BinkOpen@8), and llvm-dlltool strips that underscore from the
imported name whatever --no-leading-underscore says, so a by-name import would
look up a name the DLL does not export and Wine would substitute a builtin stub
that aborts ("unimplemented function binkw32.dll.BinkSetSoundSystem@8").
Ordinals have no such ambiguity and match the retail DLL. MSVC keeps binding the
shipped library, so the retail build is untouched.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
refactor toolchain
make the preset template something CMake will read
The template carries two // comments. JSON has no comments and neither does
the preset format, so CMake refuses the whole file:
CMakePresets.json:10: Missing '}' or object member name
Which means the file CopyUserPresetTemplate drops into the source directory
has never worked as handed over -- "cmake --preset '1dot13 Release'" fails
until you delete the comments yourself.
The text they carried is worth keeping, so it moves to the description field
the format does have, and cmake --preset '1dot13 Release' now configures.
92 lines
3.4 KiB
CMake
92 lines
3.4 KiB
CMake
# Usage:
|
|
# MSVC_SDK=/path/to/msvc cmake --toolchain cmake/toolchains/clang-cl.cmake ...
|
|
#
|
|
# MSVC_SDK is where the cross headers and libraries live: the directory holding
|
|
# vc/ and kits/, which for msvc-wine is the root it was installed to. It comes
|
|
# from the environment.
|
|
|
|
set(CMAKE_SYSTEM_NAME Windows)
|
|
set(CMAKE_SYSTEM_PROCESSOR x86)
|
|
|
|
set(MSVC_SDK "$ENV{MSVC_SDK}")
|
|
if(NOT IS_DIRECTORY "${MSVC_SDK}/vc/tools/msvc"
|
|
OR NOT IS_DIRECTORY "${MSVC_SDK}/kits/10/include")
|
|
message(FATAL_ERROR
|
|
"MSVC_SDK (${MSVC_SDK}) does not hold vc/tools/msvc and kits/10/include. "
|
|
"Point it at the MSVC installation, e.g. MSVC_SDK=/home/you/msvc")
|
|
endif()
|
|
|
|
# Both trees are versioned directories; take the newest of each rather than
|
|
# pinning a version that goes stale the next time the toolchain is updated.
|
|
function(_newest_subdirectory result dir)
|
|
file(GLOB versions "${dir}/*")
|
|
list(SORT versions)
|
|
list(POP_BACK versions newest)
|
|
set(${result} "${newest}" PARENT_SCOPE)
|
|
endfunction()
|
|
|
|
_newest_subdirectory(_msvc "${MSVC_SDK}/vc/tools/msvc")
|
|
_newest_subdirectory(_sdk "${MSVC_SDK}/kits/10/include")
|
|
get_filename_component(_sdk_version "${_sdk}" NAME)
|
|
set(_sdk_lib "${MSVC_SDK}/kits/10/lib/${_sdk_version}")
|
|
|
|
# Find every tool here rather than trusting PATH, so the choice of compiler,
|
|
# linker and rc is the toolchain file's alone.
|
|
foreach(tool clang-cl lld-link llvm-rc llvm-lib llvm-mt llvm-dlltool)
|
|
string(TOUPPER "${tool}" _variable)
|
|
string(REPLACE "-" "_" _variable "${_variable}")
|
|
# An absolute path: CMake resolves a bare name against the source directory.
|
|
find_program(${_variable} NAMES ${tool} REQUIRED)
|
|
endforeach()
|
|
|
|
set(CMAKE_C_COMPILER "${CLANG_CL}")
|
|
set(CMAKE_CXX_COMPILER "${CLANG_CL}")
|
|
set(CMAKE_LINKER "${LLD_LINK}")
|
|
set(CMAKE_RC_COMPILER "${LLVM_RC}")
|
|
set(CMAKE_AR "${LLVM_LIB}")
|
|
set(CMAKE_MT "${LLVM_MT}")
|
|
|
|
# clang-cl does not read INCLUDE and LIB the way cl does, so every directory
|
|
# has to be named. -imsvc rather than -I marks them as system headers, which
|
|
# keeps warnings about the SDK out of the build log.
|
|
set(_includes
|
|
"${_msvc}/atlmfc/include"
|
|
"${_msvc}/include"
|
|
"${_sdk}/shared"
|
|
"${_sdk}/ucrt"
|
|
"${_sdk}/um"
|
|
"${_sdk}/winrt"
|
|
"${_sdk}/km"
|
|
)
|
|
|
|
set(_flags "--target=i386-pc-windows-msvc")
|
|
foreach(directory ${_includes})
|
|
string(APPEND _flags " -imsvc ${directory}")
|
|
endforeach()
|
|
set(CMAKE_C_FLAGS_INIT "${_flags}")
|
|
set(CMAKE_CXX_FLAGS_INIT "${_flags}")
|
|
|
|
# The resource compiler is preprocessed by clang-cl, which needs the same
|
|
# headers again: ja2.rc includes windows.h. It takes -I, not -imsvc.
|
|
set(_rc_flags "")
|
|
foreach(directory ${_includes})
|
|
string(APPEND _rc_flags " -I ${directory}")
|
|
endforeach()
|
|
set(CMAKE_RC_FLAGS_INIT "${_rc_flags}")
|
|
|
|
set(CMAKE_EXE_LINKER_FLAGS_INIT
|
|
"/libpath:${_msvc}/lib/x86 /libpath:${_sdk_lib}/ucrt/x86 /libpath:${_sdk_lib}/um/x86")
|
|
|
|
# lld-link cannot bind the Bink import library the game ships, so rebuild a
|
|
# bindable one from binkw32.def (which explains why) and hand it to the top-level
|
|
# CMakeLists through binkw32_lib. MSVC keeps using the shipped library.
|
|
execute_process(
|
|
COMMAND "${LLVM_DLLTOOL}" -m i386 --no-leading-underscore
|
|
-d "${CMAKE_CURRENT_LIST_DIR}/../../binkw32.def"
|
|
-l "${CMAKE_BINARY_DIR}/binkw32.lib"
|
|
RESULT_VARIABLE _binkw32DlltoolResult)
|
|
if(NOT _binkw32DlltoolResult EQUAL 0)
|
|
message(FATAL_ERROR "llvm-dlltool failed to build the binkw32 import library")
|
|
endif()
|
|
set(binkw32_lib "${CMAKE_BINARY_DIR}/binkw32.lib")
|