Files
OpenZen/native/CMakeLists.txt
T
Shirona1337 ecc29d4e82 build(native): enable per-TU parallel compilation (cmake --parallel + /MP)
Before: MSBuild defaulted to a single process per project, so each
vcxproj compiled its .cpp files sequentially. Local clean rebuild of
OpenZen.dll + OpenZenLoader.exe measured ~14.6 s.

After: same cold rebuild measures ~4.8 s — 3x faster — by combining

- `cmake --build --parallel <Runtime.runtime.availableProcessors()>`
  in the buildNative task, which forwards a project-level `-m:N` to
  MSBuild and gets DLL / Loader to build concurrently where the
  dependency graph allows.
- `/MP` on the MSVC compile options, which is the actual win: it lets
  cl.exe spawn one compiler process per available core within a
  single vcxproj. The DLL+Loader together have ~13 .cpp files; this
  is where the bulk of wall-time was being spent serially.

CI runners get the same speed-up for free.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 01:06:02 +08:00

50 lines
1.8 KiB
CMake

cmake_minimum_required(VERSION 3.20)
# CMP0091 NEW lets us drive the MSVC runtime via MSVC_RUNTIME_LIBRARY
# instead of patching CMAKE_CXX_FLAGS_*. CMake 3.15+ default, set
# explicitly so the build stays predictable across CMake versions.
cmake_policy(SET CMP0091 NEW)
project(OpenZenNative LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Statically link the MSVC C/C++ runtime into both the loader EXE and the
# injected DLL. This matters most for the DLL: our manual mapper resolves
# import addresses locally and assumes the imported DLL has the same base
# in both processes - true for kernel32/user32/ntdll (KnownDLLs) but NOT
# for vcruntime140/ucrtbase, so the mapped DLL would otherwise call CRT
# functions through wild pointers and crash the target Java process. /MT
# eliminates those imports entirely.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
if(NOT DEFINED ENV{JAVA_HOME})
message(FATAL_ERROR "JAVA_HOME must be set so we can locate jni.h / jvmti.h")
endif()
set(JDK_INCLUDE "$ENV{JAVA_HOME}/include")
include_directories(${JDK_INCLUDE} ${JDK_INCLUDE}/win32)
if(NOT WIN32)
message(FATAL_ERROR "OpenZen native build only supports Windows")
endif()
add_compile_definitions(
UNICODE
_UNICODE
WIN32_LEAN_AND_MEAN
NOMINMAX
)
if(MSVC)
# /MP enables cl.exe-level multi-process compilation: every translation
# unit inside a single vcxproj compiles in parallel. Without it, MSBuild
# only parallelises *across* projects (the -j N forwarded by
# `cmake --build --parallel` controls that), so a single project with
# a dozen .cpp files would still compile serially.
add_compile_options(/W3 /permissive- /utf-8 /EHsc /MP)
endif()
add_subdirectory(dll)
add_subdirectory(loader)