|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: split-file %s %t --leading-lines |
| 3 | + |
| 4 | +/// Build Swift dylib and compatibility header. |
| 5 | +// RUN: %target-build-swift-dylib(%t/%target-library-name(Lib)) %t/Lib.swift \ |
| 6 | +// RUN: -emit-module-path %t/Lib.swiftmodule \ |
| 7 | +// RUN: -emit-clang-header-path %t/cdecl.h \ |
| 8 | +// RUN: -import-objc-header %t/BridgingHeader.h \ |
| 9 | +// RUN: -enable-experimental-feature CDecl \ |
| 10 | +// RUN: -enable-experimental-feature CImplementation |
| 11 | +// RUN: %target-codesign %t/%target-library-name(Lib) |
| 12 | + |
| 13 | +/// Build a C client against cdecl.h. |
| 14 | +// RUN: %clang-no-modules %t/Client.c -o %t/a.out \ |
| 15 | +// RUN: -I %clang-include-dir -Werror -isysroot %sdk \ |
| 16 | +// RUN: -I %t -l Lib -L %t %target-rpath(%t) |
| 17 | +// RUN: %target-codesign %t/a.out |
| 18 | +// RUN: %target-run %t/a.out > %t/run.log |
| 19 | +// RUN: %FileCheck %t/Client.c --check-prefix=PRINTS --input-file %t/run.log |
| 20 | + |
| 21 | +/// Build a Swift client against cdecl.h. |
| 22 | +// RUN: %target-build-swift %t/Client.swift -o %t/a.out \ |
| 23 | +// RUN: -I %t -l Lib -L %t %target-rpath(%t) |
| 24 | +// RUN: %target-codesign %t/a.out |
| 25 | +// RUN: %target-run %t/a.out > %t/run.log |
| 26 | +// RUN: %FileCheck %t/Client.swift --check-prefix=PRINTS --input-file %t/run.log |
| 27 | + |
| 28 | +// REQUIRES: swift_feature_CDecl |
| 29 | +// REQUIRES: swift_feature_CImplementation |
| 30 | +// REQUIRES: executable_test |
| 31 | + |
| 32 | +//--- BridgingHeader.h |
| 33 | +#include <stddef.h> |
| 34 | +#include <stdbool.h> |
| 35 | + |
| 36 | +extern int simple(int x, int y); |
| 37 | + |
| 38 | +extern void primitiveTypes(ptrdiff_t i, int ci, long l, char c, float f, double d, bool b); |
| 39 | + |
| 40 | +extern void sameName(); |
| 41 | + |
| 42 | +__attribute__((swift_name("renamed_swiftSide(_:)"))) |
| 43 | +extern void renamed_clangSide(ptrdiff_t arg); |
| 44 | + |
| 45 | +//--- Lib.swift |
| 46 | + |
| 47 | +@cdecl @implementation |
| 48 | +public func simple(_ x: Int32, _ y: Int32) -> Int32 { |
| 49 | + print(x, y) |
| 50 | + return x |
| 51 | +} |
| 52 | + |
| 53 | +@implementation @cdecl |
| 54 | +public func primitiveTypes(_ i: Int, _ ci: CInt, _ l: CLong, _ c: CChar, _ f: Float, _ d: Double, _ b: Bool) { |
| 55 | + print(i, ci, l, c, f, d, b) |
| 56 | +} |
| 57 | + |
| 58 | +@cdecl(sameName) @implementation |
| 59 | +public func sameName() { |
| 60 | + print("sameName") |
| 61 | +} |
| 62 | + |
| 63 | +@cdecl(renamed_clangSide) @implementation |
| 64 | +public func renamed_swiftSide(_ arg: Int) { |
| 65 | + print(arg) |
| 66 | +} |
| 67 | + |
| 68 | +//--- Client.c |
| 69 | + |
| 70 | +#include "cdecl.h" |
| 71 | +#include "BridgingHeader.h" |
| 72 | + |
| 73 | +int main() { |
| 74 | + ptrdiff_t x = simple(42, 43); |
| 75 | + // PRINTS: 42 43 |
| 76 | + primitiveTypes(1, 2, 3, 'a', 1.0f, 2.0, true); |
| 77 | + // PRINTS: 1 2 3 97 1.0 2.0 true |
| 78 | + sameName(); |
| 79 | + // PRINTS: sameName |
| 80 | + renamed_clangSide(11); |
| 81 | + // PRINTS: 11 |
| 82 | +} |
| 83 | + |
| 84 | +//--- Client.swift |
| 85 | +import Lib |
| 86 | + |
| 87 | +let _ = simple(42, 43) |
| 88 | +// PRINTS: 42 43 |
| 89 | +primitiveTypes(1, 2, 3, 97, 1.0, 2.0, true) |
| 90 | +// PRINTS: 1 2 3 97 1.0 2.0 true |
| 91 | +sameName(); |
| 92 | +// PRINTS: sameName |
| 93 | +renamed_swiftSide(11) |
| 94 | +// PRINTS: 11 |
0 commit comments