@@ -135,6 +135,7 @@ export async function downloadWindowsSdk(
135135export async function getWindowsCrossCompileArgs ( runtime : string ) : Promise < {
136136 success : boolean ;
137137 args : string [ ] ;
138+ env : Record < string , string > ;
138139 error ?: string ;
139140} > {
140141 const sdkPath = getXwinSdkPath ( ) ;
@@ -143,12 +144,15 @@ export async function getWindowsCrossCompileArgs(runtime: string): Promise<{
143144
144145 // 检查 SDK 是否存在
145146 const crtPath = path . join ( splatPath , 'crt' ) ;
146- const sdkLibPath = path . join ( splatPath , 'sdk' , 'lib' , 'ucrt' , arch ) ;
147+ const crtLibPath = path . join ( splatPath , 'crt' , 'lib' , arch ) ;
148+ const sdkUmPath = path . join ( splatPath , 'sdk' , 'lib' , 'um' , arch ) ;
149+ const sdkUcrtPath = path . join ( splatPath , 'sdk' , 'lib' , 'ucrt' , arch ) ;
147150
148151 if ( ! fs . existsSync ( crtPath ) ) {
149152 return {
150153 success : false ,
151154 args : [ ] ,
155+ env : { } ,
152156 error : 'Windows SDK not found. Please download it first.' ,
153157 } ;
154158 }
@@ -159,35 +163,57 @@ export async function getWindowsCrossCompileArgs(runtime: string): Promise<{
159163 return {
160164 success : false ,
161165 args : [ ] ,
166+ env : { } ,
162167 error : 'lld-link is not installed. Please install LLD: brew install lld' ,
163168 } ;
164169 }
165170
171+ // 获取 lld-link 路径
172+ const lldPath = await getLldLinkPath ( ) ;
173+
166174 // 构建 MSBuild 参数
167175 const args : string [ ] = [
168176 '-p:DisableUnsupportedError=true' ,
169- '-p:CppLinker=lld-link' ,
170177 '-p:AcceptVSBuildToolsLicense=true' ,
171178 // 禁用 SourceLink 以避免 lld-link 路径解析问题
172179 '-p:EnableSourceLink=false' ,
173180 '-p:EnableSourceControlManagerQueries=false' ,
174181 ] ;
175182
176- // 添加链接器参数 (通过 LinkerArg)
177- // 这些参数会被传递给 lld-link
178- const linkerArgs = [
179- `/vctoolsdir:${ path . join ( splatPath , 'crt' ) } ` ,
180- `/winsdkdir:${ path . join ( splatPath , 'sdk' ) } ` ,
181- `/LIBPATH:${ path . join ( splatPath , 'crt' , 'lib' , arch ) } /` ,
182- `/LIBPATH:${ path . join ( splatPath , 'sdk' , 'lib' , 'um' , arch ) } /` ,
183- ] ;
184-
185- // 将链接器参数添加到 MSBuild
186- for ( const arg of linkerArgs ) {
187- args . push ( `-p:LinkerArg="${ arg } "` ) ;
183+ // 设置 C++ 链接器
184+ if ( lldPath ) {
185+ args . push ( `-p:CppLinker=${ lldPath } ` ) ;
186+ } else {
187+ args . push ( '-p:CppLinker=lld-link' ) ;
188188 }
189189
190- return { success : true , args } ;
190+ // 构建链接器参数 - 使用 /LIBPATH 指定库搜索路径
191+ const libPaths = [
192+ crtLibPath ,
193+ sdkUmPath ,
194+ sdkUcrtPath ,
195+ ] . filter ( p => fs . existsSync ( p ) ) ;
196+
197+ // 通过 IlcAdditionalLinkArgs 传递链接器参数
198+ const linkerArgs = libPaths . map ( p => `/LIBPATH:"${ p } "` ) . join ( ' ' ) ;
199+ args . push ( `-p:IlcAdditionalLinkArgs=${ linkerArgs } ` ) ;
200+
201+ // 构建 LIB 环境变量 - lld-link 需要这个来查找库
202+ const libEnvPaths = libPaths . join ( path . delimiter ) ;
203+
204+ // 添加 lld-link 到 PATH
205+ const brewPrefix = process . arch === 'arm64' ? '/opt/homebrew' : '/usr/local' ;
206+ const lldBinPath = path . join ( brewPrefix , 'opt' , 'lld' , 'bin' ) ;
207+ const pathEnv = fs . existsSync ( lldBinPath )
208+ ? `${ lldBinPath } :${ process . env [ 'PATH' ] || '' } `
209+ : process . env [ 'PATH' ] || '' ;
210+
211+ const env : Record < string , string > = {
212+ 'LIB' : libEnvPaths ,
213+ 'PATH' : pathEnv ,
214+ } ;
215+
216+ return { success : true , args, env } ;
191217}
192218
193219/**
0 commit comments