Skip to content

Commit c526129

Browse files
committed
Merge branch 'master' into gwynne/improve-processinfo-osverstring
2 parents a36bdd0 + 0ded41e commit c526129

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+689
-305
lines changed

CMakeLists.txt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,39 @@ if(NOT BUILD_SHARED_LIBS)
6868
install(TARGETS CoreFoundation CFXMLInterface CFURLSessionInterface
6969
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
7070
endif()
71+
72+
set(swift_lib_dir "lib/swift")
73+
if(NOT BUILD_SHARED_LIBS)
74+
set(swift_lib_dir "lib/swift_static")
75+
endif()
76+
7177
# TODO(compnerd) install as a Framework as that is how swift actually is built
7278
install(DIRECTORY
7379
${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers/
7480
DESTINATION
75-
lib/swift/CoreFoundation
81+
${swift_lib_dir}/CoreFoundation
7682
FILES_MATCHING PATTERN "*.h")
7783
install(FILES
78-
CoreFoundation/Base.subproj/module.map
84+
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>module.map
7985
DESTINATION
80-
lib/swift/CoreFoundation)
86+
${swift_lib_dir}/CoreFoundation)
8187
install(DIRECTORY
8288
${CMAKE_CURRENT_BINARY_DIR}/CFURLSessionInterface.framework/Headers/
8389
DESTINATION
84-
lib/swift/CFURLSessionInterface
90+
${swift_lib_dir}/CFURLSessionInterface
8591
FILES_MATCHING PATTERN "*.h")
8692
install(FILES
87-
CoreFoundation/URL.subproj/module.map
93+
CoreFoundation/URL.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>module.map
8894
DESTINATION
89-
lib/swift/CFURLSessionInterface)
95+
${swift_lib_dir}/CFURLSessionInterface)
9096
install(DIRECTORY
9197
${CMAKE_CURRENT_BINARY_DIR}/CFXMLInterface.framework/Headers/
9298
DESTINATION
93-
lib/swift/CFXMLInterface
99+
${swift_lib_dir}/CFXMLInterface
94100
FILES_MATCHING PATTERN "*.h")
95101
install(FILES
96-
CoreFoundation/Parsing.subproj/module.map
102+
CoreFoundation/Parsing.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>module.map
97103
DESTINATION
98-
lib/swift/CFXMLInterface)
104+
${swift_lib_dir}/CFXMLInterface)
99105

100106
add_subdirectory(cmake/modules)

CoreFoundation/Base.subproj/CFAvailability.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@
135135
#define __CF_ENUM_FIXED_IS_AVAILABLE (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && (__has_feature(objc_fixed_enum) || __has_extension(cxx_fixed_enum)))
136136

137137
#if __CF_ENUM_FIXED_IS_AVAILABLE
138-
#define __CF_NAMED_ENUM(_type, _name) enum __CF_ENUM_ATTRIBUTES _name : _type _name; enum _name : _type
138+
#define __CF_NAMED_ENUM(_type, _name) int __CF_ENUM_ ## _name; enum __CF_ENUM_ATTRIBUTES _name : _type; typedef enum _name _name; enum _name : _type
139139
#define __CF_ANON_ENUM(_type) enum __CF_ENUM_ATTRIBUTES : _type
140-
#define CF_CLOSED_ENUM(_type, _name) enum __CF_CLOSED_ENUM_ATTRIBUTES _name : _type _name; enum _name : _type
140+
#define CF_CLOSED_ENUM(_type, _name) int __CF_ENUM_ ## _name; enum __CF_CLOSED_ENUM_ATTRIBUTES _name : _type; typedef enum _name _name; enum _name : _type
141141
#if (__cplusplus)
142142
#define CF_OPTIONS(_type, _name) _type _name; enum __CF_OPTIONS_ATTRIBUTES : _type
143143
#else
144-
#define CF_OPTIONS(_type, _name) enum __CF_OPTIONS_ATTRIBUTES _name : _type _name; enum _name : _type
144+
#define CF_OPTIONS(_type, _name) int __CF_OPTIONS_ ## _name; enum __CF_OPTIONS_ATTRIBUTES _name : _type; typedef enum _name _name; enum _name : _type
145145
#endif
146146
#else
147147
#define __CF_NAMED_ENUM(_type, _name) _type _name; enum

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,21 @@ CF_PRIVATE Boolean _CFReadBytesFromFile(CFAllocatorRef alloc, CFURLRef url, void
164164

165165
CF_PRIVATE Boolean _CFWriteBytesToFile(CFURLRef url, const void *bytes, CFIndex length) {
166166
int fd = -1;
167-
int mode;
168167
struct statinfo statBuf;
169168
char path[CFMaxPathSize];
170169
if (!CFURLGetFileSystemRepresentation(url, true, (uint8_t *)path, CFMaxPathSize)) {
171170
return false;
172171
}
173172

174173
int no_hang_fd = openAutoFSNoWait();
175-
mode = 0666;
174+
int mode = 0666;
176175
if (0 == stat(path, &statBuf)) {
177176
mode = statBuf.st_mode;
178177
} else if (thread_errno() != ENOENT) {
179178
closeAutoFSNoWait(no_hang_fd);
180179
return false;
181180
}
182-
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|CF_OPENFLGS, 0666);
181+
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|CF_OPENFLGS, mode);
183182
if (fd < 0) {
184183
closeAutoFSNoWait(no_hang_fd);
185184
return false;

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -113,44 +113,36 @@ const char **_CFGetProcessPath(void) {
113113
return &__CFProcessPath;
114114
}
115115

116-
#if TARGET_OS_WIN32
116+
static inline void _CFSetProgramNameFromPath(const char *path) {
117+
__CFProcessPath = strdup(path);
118+
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
119+
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
120+
}
121+
117122
const char *_CFProcessPath(void) {
118123
if (__CFProcessPath) return __CFProcessPath;
124+
125+
#if TARGET_OS_WIN32
119126
wchar_t buf[CFMaxPathSize] = {0};
120127
DWORD rlen = GetModuleFileNameW(NULL, buf, sizeof(buf) / sizeof(buf[0]));
121128
if (0 < rlen) {
122129
char asciiBuf[CFMaxPathSize] = {0};
123130
int res = WideCharToMultiByte(CP_UTF8, 0, buf, rlen, asciiBuf, sizeof(asciiBuf) / sizeof(asciiBuf[0]), NULL, NULL);
124131
if (0 < res) {
125-
__CFProcessPath = strdup(asciiBuf);
126-
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
127-
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
132+
_CFSetProgramNameFromPath(asciiBuf);
128133
}
129134
}
130135
if (!__CFProcessPath) {
131136
__CFProcessPath = "";
132137
__CFprogname = __CFProcessPath;
133138
}
134139
return __CFProcessPath;
135-
}
136-
#endif
137-
138-
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
139-
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
140-
return pthread_main_np() == 1;
141-
}
142-
#endif
143-
144-
#if TARGET_OS_MAC
145-
const char *_CFProcessPath(void) {
146-
if (__CFProcessPath) return __CFProcessPath;
140+
#elif TARGET_OS_MAC
147141
#if TARGET_OS_OSX
148142
if (!__CFProcessIsRestricted()) {
149143
const char *path = (char *)__CFgetenv("CFProcessPath");
150144
if (path) {
151-
__CFProcessPath = strdup(path);
152-
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
153-
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
145+
_CFSetProgramNameFromPath(path);
154146
return __CFProcessPath;
155147
}
156148
}
@@ -160,9 +152,7 @@ const char *_CFProcessPath(void) {
160152
uint32_t size = CFMaxPathSize;
161153
char buffer[size];
162154
if (0 == _NSGetExecutablePath(buffer, &size)) {
163-
__CFProcessPath = strdup(buffer);
164-
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
165-
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
155+
_CFSetProgramNameFromPath(buffer);
166156
}
167157
}
168158

@@ -171,6 +161,38 @@ const char *_CFProcessPath(void) {
171161
__CFprogname = __CFProcessPath;
172162
}
173163
return __CFProcessPath;
164+
#elif TARGET_OS_LINUX
165+
char buf[CFMaxPathSize + 1];
166+
167+
ssize_t res = readlink("/proc/self/exe", buf, CFMaxPathSize);
168+
if (res > 0) {
169+
// null terminate, readlink does not
170+
buf[res] = 0;
171+
_CFSetProgramNameFromPath(buf);
172+
} else {
173+
__CFProcessPath = "";
174+
__CFprogname = __CFProcessPath;
175+
}
176+
return __CFProcessPath;
177+
#else // TARGET_OS_BSD
178+
if (!__CFProcessIsRestricted()) {
179+
char *path = getenv("_");
180+
if (path != NULL) {
181+
_CFSetProgramNameFromPath(path);
182+
return __CFProcessPath;
183+
}
184+
}
185+
186+
// We don't yet have anything left to try.
187+
__CFProcessPath = "";
188+
__CFprogname = __CFProcessPath;
189+
return __CFProcessPath;
190+
#endif
191+
}
192+
193+
#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_BSD
194+
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
195+
return pthread_main_np() == 1;
174196
}
175197
#endif
176198

@@ -185,24 +207,6 @@ const char *_CFProcessPath(void) {
185207
Boolean _CFIsMainThread(void) {
186208
return syscall(SYS_gettid) == getpid();
187209
}
188-
189-
const char *_CFProcessPath(void) {
190-
if (__CFProcessPath) return __CFProcessPath;
191-
char buf[CFMaxPathSize + 1];
192-
193-
ssize_t res = readlink("/proc/self/exe", buf, CFMaxPathSize);
194-
if (res > 0) {
195-
// null terminate, readlink does not
196-
buf[res] = 0;
197-
__CFProcessPath = strdup(buf);
198-
__CFprogname = strrchr(__CFProcessPath, PATH_SEP);
199-
__CFprogname = (__CFprogname ? __CFprogname + 1 : __CFProcessPath);
200-
} else {
201-
__CFProcessPath = "";
202-
__CFprogname = __CFProcessPath;
203-
}
204-
return __CFProcessPath;
205-
}
206210
#endif
207211

208212
CF_PRIVATE CFStringRef _CFProcessNameString(void) {

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,7 @@ const char *_NSPrintForDebugger(void *cf) {
17271727
if (!desc) {
17281728
return "<no description>";
17291729
}
1730+
CFRelease(desc);
17301731
const char *cheapResult = CFStringGetCStringPtr((CFTypeRef)cf, kCFStringEncodingUTF8);
17311732
if (cheapResult) {
17321733
return cheapResult;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module CoreFoundation [extern_c] [system] {
2+
umbrella header "CoreFoundation.h"
3+
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }
4+
5+
link "CoreFoundation"
6+
link "uuid"
7+
}

CoreFoundation/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ find_package(Threads REQUIRED)
2727
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
2828
find_package(LibXml2 REQUIRED)
2929
find_package(CURL CONFIG)
30-
if(NOT CURL_FOUND)
30+
if(CURL_FOUND)
31+
set(CURL_VERSION_STRING ${CURL_VERSION})
32+
else()
3133
find_package(CURL REQUIRED)
3234
endif()
3335
find_package(ICU COMPONENTS uc i18n REQUIRED)

CoreFoundation/Locale.subproj/CFDateIntervalFormatter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void updateFormatter(CFDateIntervalFormatterRef dif) {
132132

133133
char localeBuffer[100] = {0};
134134
CFStringGetCString(localeID, localeBuffer, 100, kCFStringEncodingUTF8);
135-
135+
136136
UniChar timeZoneID[100] = {0};
137137
CFTimeZoneRef timeZone = dif->_timeZone;
138138
if (timeZone) {
@@ -179,6 +179,7 @@ static void updateFormatter(CFDateIntervalFormatterRef dif) {
179179
#endif
180180

181181
CFRelease(locale);
182+
CFRelease(localeID);
182183
CFRelease(timeZone);
183184
}
184185
}

CoreFoundation/NumberDate.subproj/CFTimeZone.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,13 @@ static CFIndex __CFBSearchTZPeriods(CFTimeZoneRef tz, CFAbsoluteTime at) {
485485

486486

487487
CF_INLINE int32_t __CFDetzcode(const unsigned char *bufp) {
488-
int32_t result = (bufp[0] & 0x80) ? ~0L : 0L;
488+
// `result` is uint32_t to avoid undefined behaviour of shifting left negative values
489+
uint32_t result = (bufp[0] & 0x80) ? ~0L : 0L;
489490
result = (result << 8) | (bufp[0] & 0xff);
490491
result = (result << 8) | (bufp[1] & 0xff);
491492
result = (result << 8) | (bufp[2] & 0xff);
492493
result = (result << 8) | (bufp[3] & 0xff);
493-
return result;
494+
return (int32_t)result;
494495
}
495496

496497
CF_INLINE void __CFEntzcode(int32_t value, unsigned char *bufp) {
@@ -1594,6 +1595,8 @@ CFTimeZoneRef CFTimeZoneCreateWithName(CFAllocatorRef allocator, CFStringRef nam
15941595
#endif
15951596
CFRelease(dict);
15961597
if (CFEqual(CFSTR(""), name)) {
1598+
if (baseURL) CFRelease(baseURL);
1599+
if (data) CFRelease(data);
15971600
return NULL;
15981601
}
15991602
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module CFXMLInterface [extern_c] [system] {
2+
umbrella header "CFXMLInterface.h"
3+
4+
link "CFXMLInterface"
5+
link "xml2"
6+
}

CoreFoundation/Preferences.subproj/CFPreferences.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ CF_PRIVATE CFStringRef _CFPreferencesGetByHostIdentifierString(void) {
169169

170170
static unsigned long __CFSafeLaunchLevel = 0;
171171

172-
static CFURLRef _preferencesDirectoryForUserHostSafetyLevel(CFStringRef userName, CFStringRef hostName, unsigned long safeLevel) {
173-
CFAllocatorRef alloc = __CFPreferencesAllocator();
172+
static CFURLRef _preferencesCreateDirectoryForUserHostSafetyLevel(CFStringRef userName, CFStringRef hostName, unsigned long safeLevel) {
174173
CFURLRef location = NULL;
175174

176175
CFKnownLocationUser user;
@@ -197,7 +196,7 @@ static CFURLRef _preferencesDirectoryForUserHostSafetyLevel(CFStringRef userName
197196
}
198197

199198
static CFURLRef _preferencesDirectoryForUserHost(CFStringRef userName, CFStringRef hostName) {
200-
return _preferencesDirectoryForUserHostSafetyLevel(userName, hostName, __CFSafeLaunchLevel);
199+
return _preferencesCreateDirectoryForUserHostSafetyLevel(userName, hostName, __CFSafeLaunchLevel);
201200
}
202201

203202
static Boolean __CFPreferencesWritesXML = true;
@@ -282,15 +281,13 @@ void CFPreferencesSetMultiple(CFDictionaryRef keysToSet, CFArrayRef keysToRemove
282281

283282
CFTypeRef *keys = NULL;
284283
CFTypeRef *values;
285-
CFIndex numOfKeysToSet = 0;
286284

287285
domain = _CFPreferencesStandardDomain(appName, user, host);
288286
if (!domain) return;
289287

290288
CFAllocatorRef alloc = CFGetAllocator(domain);
291289

292290
if (keysToSet && (count = CFDictionaryGetCount(keysToSet))) {
293-
numOfKeysToSet = count;
294291
keys = (CFTypeRef *)CFAllocatorAllocate(alloc, 2*count*sizeof(CFTypeRef), 0);
295292
if (keys) {
296293
values = &(keys[count]);
@@ -385,7 +382,7 @@ const CFRuntimeClass __CFPreferencesDomainClass = {
385382
};
386383

387384
/* We spend a lot of time constructing these prefixes; we should cache. REW, 7/19/99 */
388-
static CFStringRef _CFPreferencesCachePrefixForUserHost(CFStringRef userName, CFStringRef hostName) {
385+
static CFStringRef _CFPreferencesCreateCachePrefixForUserHost(CFStringRef userName, CFStringRef hostName) {
389386
if (userName == kCFPreferencesAnyUser && hostName == kCFPreferencesAnyHost) {
390387
return (CFStringRef)CFRetain(CFSTR("*/*/"));
391388
}
@@ -410,7 +407,7 @@ static CFStringRef _CFPreferencesCachePrefixForUserHost(CFStringRef userName,
410407

411408
// It would be nice if we could remember the key for "well-known" combinations, so we're not constantly allocing more strings.... - REW 2/3/99
412409
static CFStringRef _CFPreferencesStandardDomainCacheKey(CFStringRef domainName, CFStringRef userName, CFStringRef hostName) {
413-
CFStringRef prefix = _CFPreferencesCachePrefixForUserHost(userName, hostName);
410+
CFStringRef prefix = _CFPreferencesCreateCachePrefixForUserHost(userName, hostName);
414411
CFStringRef result = NULL;
415412

416413
if (prefix) {
@@ -424,7 +421,7 @@ static CFURLRef _CFPreferencesURLForStandardDomainWithSafetyLevel(CFStringRef do
424421
CFURLRef theURL = NULL;
425422
CFAllocatorRef prefAlloc = __CFPreferencesAllocator();
426423
#if TARGET_OS_OSX || TARGET_OS_WIN32 || TARGET_OS_LINUX
427-
CFURLRef prefDir = _preferencesDirectoryForUserHostSafetyLevel(userName, hostName, safeLevel);
424+
CFURLRef prefDir = _preferencesCreateDirectoryForUserHostSafetyLevel(userName, hostName, safeLevel);
428425
CFStringRef appName;
429426
CFStringRef fileName;
430427
Boolean mustFreeAppName = false;
@@ -462,12 +459,12 @@ static CFURLRef _CFPreferencesURLForStandardDomainWithSafetyLevel(CFStringRef do
462459
#elif TARGET_OS_WIN32
463460
theURL = CFURLCreateWithFileSystemPathRelativeToBase(prefAlloc, fileName, kCFURLWindowsPathStyle, false, prefDir);
464461
#endif
465-
if (prefDir) CFRelease(prefDir);
466462
CFRelease(fileName);
467463
}
468464
#else
469465
//#error Do not know where to store NSUserDefaults on this platform
470466
#endif
467+
if (prefDir) CFRelease(prefDir);
471468
return theURL;
472469
}
473470

@@ -607,7 +604,7 @@ CF_PRIVATE CFArrayRef _CFPreferencesCreateDomainList(CFStringRef userName, CFSt
607604
cachedDomains = (CFPreferencesDomainRef *)(cachedDomainKeys + cnt);
608605
CFDictionaryGetKeysAndValues(domainCache, (const void **)cachedDomainKeys, (const void **)cachedDomains);
609606
__CFUnlock(&domainCacheLock);
610-
suffix = _CFPreferencesCachePrefixForUserHost(userName, hostName);
607+
suffix = _CFPreferencesCreateCachePrefixForUserHost(userName, hostName);
611608
suffixLen = CFStringGetLength(suffix);
612609

613610
for (idx = 0; idx < cnt; idx ++) {
@@ -738,9 +735,8 @@ static void freeVolatileDomain(CFAllocatorRef allocator, CFTypeRef context, voi
738735
CFRelease((CFTypeRef)domain);
739736
}
740737

741-
static CFTypeRef fetchVolatileValue(CFTypeRef context, void *domain, CFStringRef key) {
742-
CFTypeRef result = CFDictionaryGetValue((CFMutableDictionaryRef )domain, key);
743-
if (result) CFRetain(result);
738+
static CFTypeRef fetchVolatileValue(CFTypeRef context, void *domain, CFStringRef key) {
739+
CFTypeRef result = CFDictionaryGetValue((CFMutableDictionaryRef )domain, key);
744740
return result;
745741
}
746742

0 commit comments

Comments
 (0)