From 517fe4e4b427b066a5c8e984118b648461888d86 Mon Sep 17 00:00:00 2001 From: valord577 Date: Tue, 15 Sep 2026 22:56:09 +0800 Subject: [PATCH] win msvc build for libsqlite3 --- .github/workflows/sqlite3.yml | 2 +- build_v2.py | 23 ++++++++++++++++++++++- modules/sqlite3.py | 27 ++++++++++++++++++++++----- x_utils.py | 6 +++--- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/.github/workflows/sqlite3.yml b/.github/workflows/sqlite3.yml index 52a8e68..54c2aa3 100644 --- a/.github/workflows/sqlite3.yml +++ b/.github/workflows/sqlite3.yml @@ -27,4 +27,4 @@ jobs: uses: ./.github/workflows/_matrix.yml with: disable_static: true - select_platforms: "macosx, linux, android" + select_platforms: "macosx, linux, android, win-msvc" diff --git a/build_v2.py b/build_v2.py index 83eb9c8..8c12653 100755 --- a/build_v2.py +++ b/build_v2.py @@ -217,6 +217,8 @@ def __init__(self, # - API Level 35: android.media.MediaCodec#PARAMETER_KEY_QP_OFFSET_MAP self.android_api_level: "int" = 28 + self.win32_sdk_version: "str" = '' + self.win32_compat_vers: "str" = '19.00' # -D_MSC_VER=1900 self.win32_msvc_env_native: "dict[str, str]" = {} self.win32_msvc_env_target: "dict[str, str]" = {} @@ -507,13 +509,32 @@ def _setctx_win32_msvc( msvc_dir, msvc_devshell = x.win32_msvc_detect() state.win32_msvc_env_native = x.win32_msvc_dump_env(msvc_dir, msvc_devshell, x.NATIVE_ARCH) - state.win32_msvc_env_target = x.win32_msvc_dump_env(msvc_dir, msvc_devshell, state.target_arch) + state.win32_msvc_env_target = x.win32_msvc_dump_env(msvc_dir, msvc_devshell, state.target_arch, state.win32_sdk_version) state.llvm_triple = { 'arm64': f'aarch64-pc-windows-msvc', 'amd64': f'x86_64-pc-windows-msvc', }[state.target_arch] + state.cc.extend([ + f'clang', f'--driver-mode=cl', + f'--target={state.llvm_triple}', + f'-fms-compatibility-version={state.win32_compat_vers}', + ]) + if state.win32_sdk_version: + state.cc.extend([ + f'/winsdkversion', state.win32_sdk_version, + ]) + if state.target_arch == 'amd64': + state.cc.extend(['-march=x86-64-v2']) + if state.target_arch == 'arm64': + state.cc.extend(['-march=armv8-a']) + state.ar.extend(['llvm-ar']) + state.ar.extend(['llvm-nm']) + state.ldflags.extend(['/clang:-fuse-ld=lld']) + state.objcopy.extend(['llvm-objcopy']) + state.objcopy.extend(['llvm-windres']) + # cmake toolchain file state.extra_cmake.extend([ '-G', 'Ninja', diff --git a/modules/sqlite3.py b/modules/sqlite3.py index 365b354..8a7ef23 100755 --- a/modules/sqlite3.py +++ b/modules/sqlite3.py @@ -66,12 +66,19 @@ def _build_step_0(): '-DSQLITE_OMIT_SHARED_CACHE', '-DSQLITE_OMIT_UTF16', '-DSQLITE_STRICT_SUBTYPE=1', - ] + [ - '-std=c11', '-fPIC', '-Wall', '-Wextra', - '-shared', '-v', '-O3', '-DNDEBUG', - '-ffunction-sections', '-fdata-sections', - '-pthread', ] + if ctx.args.target_plat != 'win-msvc': + args.extend([ + '-std=c11', '-fPIC', '-Wall', '-Wextra', + '-shared', '-v', '-O3', '-DNDEBUG', + '-ffunction-sections', '-fdata-sections', + '-pthread', + ]) + else: + args.extend([ + '/std:c11', '-DNDEBUG', '/W4', '/Gw', '/Gy', '/MT', '/LD', + '/clang:-v', '/clang:-O3', + ]) output = (Path(ctx.args.pkg_inst_dir)) if ctx.args.target_plat in {'linux', 'android'}: @@ -83,6 +90,16 @@ def _build_step_0(): '-Wl,--icf=safe', '-Wl,-rpath,$ORIGIN', '-o', output.as_posix(), f'-Wl,--soname={output.name}', '-lm', ]) + elif ctx.args.target_plat == 'win-msvc': + output = (output / 'lib' / 'libsqlite3.dll'); \ + output.parent.mkdir(parents=True, exist_ok=True) + args.extend([ + '-Xlinker', '/OPT:REF', + '-Xlinker', '/OPT:ICF', + '-Xlinker', '/WX', + '-Xlinker', '/Brepro', + '-o', output.as_posix(), + ]) elif ctx.args.target_plat == 'win-mingw': pass else: # apple platform diff --git a/x_utils.py b/x_utils.py index c38c918..8270e67 100644 --- a/x_utils.py +++ b/x_utils.py @@ -258,7 +258,7 @@ def win32_msvc_detect() -> "tuple[Path, Path]": logv(f'load msvc devshell: "{msvc_devshell.as_posix()}"') return (msvc_dir, msvc_devshell) -def win32_msvc_dump_env(msvc_dir: Path, msvc_devshell: Path, target_arch: str) -> "dict[str, str]": +def win32_msvc_dump_env(msvc_dir: Path, msvc_devshell: Path, target_arch: str, winsdk_version: "str | None" = None) -> "dict[str, str]": if NATIVE_PLAT != 'windows': loge(f'only works on windows, host os: {NATIVE_PLAT}') @@ -267,7 +267,8 @@ def win32_msvc_dump_env(msvc_dir: Path, msvc_devshell: Path, target_arch: str) - # Only supports access to the VS DevShell from PowerShell # - https://learn.microsoft.com/visualstudio/ide/reference/command-prompt-powershell#developer-powershell - vs_devshell_arg = f'-host_arch={NATIVE_ARCH} -arch={target_arch}' + vs_devshell_arg = f'-host_arch={NATIVE_ARCH} -arch={target_arch} -no_logo' + if winsdk_version: vs_devshell_arg += f' -winsdk={winsdk_version}' pwsh_script_blk = f'Import-Module "{msvc_devshell}"; ' pwsh_script_blk += f'Enter-VsDevShell -VsInstallPath "{msvc_dir}" -SkipAutomaticLocation -DevCmdArguments "{vs_devshell_arg}"; ' @@ -275,7 +276,6 @@ def win32_msvc_dump_env(msvc_dir: Path, msvc_devshell: Path, target_arch: str) - run_as_subprocess(['pwsh', '-WorkingDirectory', PROJ_ROOT, '-NonInteractive', - '-NoProfileLoadTime', '-ExecutionPolicy', 'Bypass', '-Command', pwsh_script_blk ])