Skip to content

Commit 93cb627

Browse files
authored
Merge pull request #12705 from compnerd/SwiftDemangle
SwiftDemangle: build on non-Darwin targets
2 parents b382fa1 + 7e888d4 commit 93cb627

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ function(_add_swift_library_single target name)
719719
# On platforms that use ELF binaries we add markers for metadata sections in
720720
# the shared libraries using these object files. This wouldn't be necessary
721721
# if the link was done by the swift binary: rdar://problem/19007002
722-
if("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
722+
if(SWIFTLIB_TARGET_LIBRARY AND
723+
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
723724
if("${libkind}" STREQUAL "SHARED")
724725
set(arch_subdir "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}")
725726

lib/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ add_subdirectory(PrintAsObjC)
3232
add_subdirectory(RemoteAST)
3333
add_subdirectory(Sema)
3434
add_subdirectory(Serialization)
35-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
36-
add_subdirectory(SwiftDemangle)
37-
endif()
35+
add_subdirectory(SwiftDemangle)
3836
add_subdirectory(SIL)
3937
add_subdirectory(SILGen)
4038
add_subdirectory(SILOptimizer)

lib/SwiftDemangle/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ add_swift_library(swiftDemangle SHARED
22
SwiftDemangle.cpp
33
MangleHack.cpp
44
LINK_LIBRARIES swiftDemangling)
5+
if(NOT APPLE)
6+
target_link_libraries(swiftDemangle PRIVATE bsd)
7+
endif()
58

69
swift_install_in_component(compiler
710
TARGETS swiftDemangle

lib/SwiftDemangle/MangleHack.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,39 @@
1717
//
1818
//===----------------------------------------------------------------------===//
1919

20-
#include "swift/Strings.h"
2120
#include "swift/SwiftDemangle/MangleHack.h"
21+
#include "swift/Strings.h"
2222
#include <cassert>
23-
#include <cstring>
23+
#include <cstdarg>
2424
#include <cstdio>
25+
#include <cstdlib>
26+
#include <cstring>
27+
28+
#if defined(_WIN32)
29+
static int vasprintf(char **strp, const char *fmt, va_list ap) {
30+
int len = _vscprintf(fmt, ap);
31+
if (len < 0)
32+
return len;
33+
char *buffer = static_cast<char *>(malloc(len + 1));
34+
if (buffer == nullptr)
35+
return -1;
36+
int result = vsprintf(buffer, fmt, ap);
37+
if (result < 0) {
38+
free(buffer);
39+
return -1;
40+
}
41+
*strp = buffer;
42+
return result;
43+
}
44+
45+
static int asprintf(char **strp, const char *fmt, ...) {
46+
va_list args;
47+
va_start(args, fmt);
48+
int result = vasprintf(strp, fmt, args);
49+
va_end(args);
50+
return result;
51+
}
52+
#endif
2553

2654
const char *
2755
_swift_mangleSimpleClass(const char *module, const char *class_) {

lib/SwiftDemangle/SwiftDemangle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include "swift/Demangling/Demangle.h"
1919
#include "swift/SwiftDemangle/SwiftDemangle.h"
20+
#if defined(__linux__) || defined(_WIN32)
21+
#include <bsd/string.h>
22+
#endif
2023

2124
static size_t swift_demangle_getDemangledName_Options(const char *MangledName,
2225
char *OutputBuffer, size_t Length,

0 commit comments

Comments
 (0)