@@ -193,6 +193,18 @@ async function readCmakeSharedLibraryTarget(
193193 return sharedLibrary ;
194194}
195195
196+ function getCompilerPath (
197+ name : "clang" | "clang++" ,
198+ rootBuildPath : string ,
199+ allowMissing ?: boolean ,
200+ ) {
201+ const result = path . join ( rootBuildPath , "bin" , name ) ;
202+ if ( ! allowMissing ) {
203+ assert ( fs . existsSync ( result ) , `Expected ${ name } link to exist: ${ result } ` ) ;
204+ }
205+ return result ;
206+ }
207+
196208export const platform : Platform < Triplet [ ] , AppleOpts > = {
197209 id : "apple" ,
198210 name : "Apple" ,
@@ -245,16 +257,29 @@ export const platform: Platform<Triplet[], AppleOpts> = {
245257 } ,
246258 async configure (
247259 triplets ,
248- { source, build, define, weakNodeApiLinkage, cmakeJs } ,
249- spawn ,
260+ { source, build, define, weakNodeApiLinkage, cmakeJs, ccachePath } ,
250261 ) {
262+ // When using ccache, we're creating symlinks for the clang and clang++ binaries to the ccache binary
263+ // This is needed for ccache to understand it's being invoked as clang and clang++ respectively.
264+ if ( ccachePath ) {
265+ const buildBinPath = path . join ( build , "bin" ) ;
266+ await fs . promises . mkdir ( buildBinPath , { recursive : true } ) ;
267+ await fs . promises . symlink (
268+ ccachePath ,
269+ getCompilerPath ( "clang" , build , true ) ,
270+ ) ;
271+ await fs . promises . symlink (
272+ ccachePath ,
273+ getCompilerPath ( "clang++" , build , true ) ,
274+ ) ;
275+ }
251276 // Ideally, we would generate a single Xcode project supporting all architectures / platforms
252277 // However, CMake's Xcode generator does not support that well, so we generate one project per triplet
253278 // Specifically, the linking of weak-node-api breaks, since the sdk / arch specific framework
254279 // from the xcframework is picked at configure time, not at build time.
255280 // See https://gitlab.kitware.com/cmake/cmake/-/issues/21752#note_1717047 for more information.
256281 await Promise . all (
257- triplets . map ( async ( { triplet } ) => {
282+ triplets . map ( async ( { triplet, spawn } ) => {
258283 const buildPath = getBuildPath ( build , triplet ) ;
259284 // We want to use the CMake File API to query information later
260285 // TODO: Or do we?
@@ -272,16 +297,14 @@ export const platform: Platform<Triplet[], AppleOpts> = {
272297 "Xcode" ,
273298 ...toDefineArguments ( [
274299 ...define ,
275- ... ( weakNodeApiLinkage ? [ getWeakNodeApiVariables ( "apple" ) ] : [ ] ) ,
276- ... ( cmakeJs ? [ getCmakeJSVariables ( "apple" ) ] : [ ] ) ,
300+ weakNodeApiLinkage ? getWeakNodeApiVariables ( "apple" ) : { } ,
301+ cmakeJs ? getCmakeJSVariables ( "apple" ) : { } ,
277302 {
278303 CMAKE_SYSTEM_NAME : CMAKE_SYSTEM_NAMES [ triplet ] ,
279304 CMAKE_OSX_SYSROOT : XCODE_SDK_NAMES [ triplet ] ,
280305 CMAKE_OSX_ARCHITECTURES : APPLE_ARCHITECTURES [ triplet ] ,
281306 // Passing a linker flag to increase the header pad size to allow renaming the install name when linking it into the app.
282307 CMAKE_SHARED_LINKER_FLAGS : "-Wl,-headerpad_max_install_names" ,
283- } ,
284- {
285308 // Setting the output directories works around an issue with Xcode generator
286309 // where an unexpanded variable would emitted in the artifact paths.
287310 // This is okay, since we're generating per triplet build directories anyway.
@@ -296,7 +319,7 @@ export const platform: Platform<Triplet[], AppleOpts> = {
296319 } ,
297320 async build (
298321 { spawn, triplet } ,
299- { build, target, configuration, appleBundleIdentifier } ,
322+ { build, target, configuration, appleBundleIdentifier, ccachePath } ,
300323 ) {
301324 // We expect the final application to sign these binaries
302325 if ( target . length > 1 ) {
@@ -305,6 +328,13 @@ export const platform: Platform<Triplet[], AppleOpts> = {
305328
306329 const buildPath = getBuildPath ( build , triplet ) ;
307330
331+ const compilerPaths = ccachePath
332+ ? {
333+ clang : getCompilerPath ( "clang" , build ) ,
334+ clangPlusPlus : getCompilerPath ( "clang++" , build ) ,
335+ }
336+ : null ;
337+
308338 const sharedLibrary = await readCmakeSharedLibraryTarget (
309339 buildPath ,
310340 configuration ,
@@ -345,6 +375,12 @@ export const platform: Platform<Triplet[], AppleOpts> = {
345375 configuration ,
346376 "-destination" ,
347377 DESTINATION_BY_TRIPLET [ triplet ] ,
378+ ...( compilerPaths
379+ ? [
380+ `CC=${ compilerPaths . clang } ` ,
381+ `CXX=${ compilerPaths . clangPlusPlus } ` ,
382+ ]
383+ : [ ] ) ,
348384 ] ,
349385 buildPath ,
350386 ) ;
0 commit comments