Skip to content

Commit ab6bb0d

Browse files
committed
APPLE: Use System API for Temporary Directory location
1 parent aa9efa2 commit ab6bb0d

4 files changed

Lines changed: 59 additions & 7 deletions

File tree

pxr/base/arch/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ set(CMAKE_INCLUDE_CURRENT_DIR OFF)
44
set(PXR_PREFIX pxr/base)
55
set(PXR_PACKAGE arch)
66

7-
set(PLATFORM_LIBS ${M_LIB})
7+
set(ARCH_PLATFORM_LIBS ${M_LIB})
8+
set(ARCH_PLATFORM_HEADERS "")
9+
set(ARCH_PLATFORM_CPPFILES "")
810
if(WIN32)
9-
set(PLATFORM_LIBS Ws2_32.lib Dbghelp.lib ${PLATFORM_LIBS})
11+
set(ARCH_PLATFORM_LIBS Ws2_32.lib Dbghelp.lib ${ARCH_PLATFORM_LIBS})
12+
elseif (APPLE)
13+
set(ARCH_PLATFORM_HEADERS darwin.h)
14+
set(ARCH_PLATFORM_CPPFILES darwin.mm)
15+
set(ARCH_PLATFORM_LIBS "-framework Foundation" ${ARCH_PLATFORM_LIBS})
1016
endif()
1117

1218
pxr_library(arch
1319
LIBRARIES
1420
${CMAKE_DL_LIBS}
15-
${PLATFORM_LIBS}
21+
${ARCH_PLATFORM_LIBS}
1622

1723
PUBLIC_CLASSES
1824
align
@@ -51,10 +57,12 @@ pxr_library(arch
5157
PRIVATE_HEADERS
5258
testArchAbi.h
5359
testArchUtil.h
60+
${ARCH_PLATFORM_HEADERS}
5461

5562
CPPFILES
5663
assumptions.cpp
5764
initConfig.cpp
65+
${ARCH_PLATFORM_CPPFILES}
5866

5967
DOXYGEN_FILES
6068
overview.dox

pxr/base/arch/darwin.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright 2025 Pixar
3+
//
4+
// Licensed under the terms set forth in the LICENSE.txt file available at
5+
// https://openusd.org/license.
6+
//
7+
#ifndef PXR_ARCH_DARWIN_H
8+
#define PXR_ARCH_DARWIN_H
9+
#include "pxr/pxr.h"
10+
11+
PXR_NAMESPACE_OPEN_SCOPE
12+
13+
const char* Arch_DarwinGetTemporaryDirectory();
14+
15+
PXR_NAMESPACE_CLOSE_SCOPE
16+
#endif // PXR_ARCH_DARWIN_H

pxr/base/arch/darwin.mm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright 2025 Pixar
3+
//
4+
// Licensed under the terms set forth in the LICENSE.txt file available at
5+
// https://openusd.org/license.
6+
//
7+
8+
#include "pxr/base/arch/darwin.h"
9+
#import <Foundation/Foundation.h>
10+
#include <string>
11+
12+
PXR_NAMESPACE_OPEN_SCOPE
13+
14+
15+
const char* Arch_DarwinGetTemporaryDirectory() {
16+
char* temporaryDirectory;
17+
18+
const char *nsTmpDir = [NSTemporaryDirectory() UTF8String];
19+
temporaryDirectory = new char[strlen(nsTmpDir) + 1];
20+
strcpy(temporaryDirectory, nsTmpDir);
21+
22+
return temporaryDirectory;
23+
}
24+
25+
PXR_NAMESPACE_CLOSE_SCOPE

pxr/base/arch/fileSystem.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include <unistd.h>
4242
#endif
4343

44+
#if defined(ARCH_OS_DARWIN)
45+
#include "pxr/base/arch/darwin.h"
46+
#endif
47+
4448
PXR_NAMESPACE_OPEN_SCOPE
4549

4650
using std::pair;
@@ -730,6 +734,9 @@ Arch_InitTmpDir()
730734
// Strip the trailing slash
731735
tmpPath[sizeOfPath-1] = 0;
732736
_TmpDir = _strdup(ArchWindowsUtf16ToUtf8(tmpPath).c_str());
737+
#elif defined(ARCH_OS_DARWIN)
738+
// On Apple platforms, we use the system APIs to get the designated temp directory
739+
_TmpDir = Arch_DarwinGetTemporaryDirectory();
733740
#else
734741
const std::string tmpdir = ArchGetEnv("TMPDIR");
735742
if (!tmpdir.empty()) {
@@ -738,11 +745,7 @@ Arch_InitTmpDir()
738745
// set, the following call will leak a string.
739746
_TmpDir = strdup(tmpdir.c_str());
740747
} else {
741-
#if defined(ARCH_OS_DARWIN)
742-
_TmpDir = "/tmp";
743-
#else
744748
_TmpDir = "/var/tmp";
745-
#endif
746749
}
747750
#endif
748751
}

0 commit comments

Comments
 (0)