Last active
April 20, 2024 05:21
-
-
Save niw/81a4b80297b97ea825c6e8cc38179712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 6ac1a2feb7143a8bc0e57af5c8dc57cbbb05d85f | |
Author: Yoshimasa Niwa <[email protected]> | |
Date: Fri Apr 19 22:14:55 2024 -0700 | |
wip | |
diff --git a/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h b/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h | |
index 911e7d7..ed3f909 100644 | |
--- a/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h | |
+++ b/3_bidirectional_cxx_interop/include/fibonacci/fibonacci.h | |
@@ -9,9 +9,9 @@ | |
// | |
//===----------------------------------------------------------------------===// | |
-#ifndef FIBONACCI_FIBONACCI_H | |
-#define FIBONACCI_FIBONACCI_H | |
+#ifndef FIBONACCICPP_FIBONACCI_H | |
+#define FIBONACCICPP_FIBONACCI_H | |
int fibonacci_cpp(int x); | |
-#endif // FIBONACCI_FIBONACCI_H | |
+#endif // FIBONACCICPP_FIBONACCI_H | |
diff --git a/3_bidirectional_cxx_interop/include/fibonacci/module.modulemap b/3_bidirectional_cxx_interop/include/fibonacci/module.modulemap | |
index 6d39a19..2ae0142 100644 | |
--- a/3_bidirectional_cxx_interop/include/fibonacci/module.modulemap | |
+++ b/3_bidirectional_cxx_interop/include/fibonacci/module.modulemap | |
@@ -1,3 +1,3 @@ | |
-module Fibonacci { | |
+module FibonacciCpp { | |
header "fibonacci.h" | |
} | |
diff --git a/3_bidirectional_cxx_interop/lib/fibonacci/CMakeLists.txt b/3_bidirectional_cxx_interop/lib/fibonacci/CMakeLists.txt | |
index ab9842d..69587df 100644 | |
--- a/3_bidirectional_cxx_interop/lib/fibonacci/CMakeLists.txt | |
+++ b/3_bidirectional_cxx_interop/lib/fibonacci/CMakeLists.txt | |
@@ -15,7 +15,7 @@ | |
# generated header path) | |
_swift_generate_cxx_header_target( | |
fibonacci_swift_h | |
- SwiftFibonacci | |
+ Fibonacci | |
"${CMAKE_CURRENT_BINARY_DIR}/include/fibonacci/fibonacci-swift.h" | |
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/fibonacci.swift" | |
SEARCH_PATHS "${PINGPONG_INCLUDE_DIR}") | |
@@ -34,9 +34,8 @@ _swift_generate_cxx_header_target( | |
# flag is propagated to users of the library. Emitting the flag is gated by | |
# the COMPILE_LANGUAGE cmake generator expression to ensure that the flag is | |
# only passed to the Swift compiler and not the C++ compiler. | |
-add_library(fibonacci STATIC fibonacci.swift fibonacci.cpp) | |
-add_dependencies(fibonacci fibonacci_swift_h) | |
-target_include_directories(fibonacci PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include") | |
-set_target_properties(fibonacci PROPERTIES Swift_MODULE_NAME "SwiftFibonacci") | |
-target_compile_options(fibonacci PUBLIC | |
+add_library(Fibonacci STATIC fibonacci.swift fibonacci.cpp) | |
+add_dependencies(Fibonacci fibonacci_swift_h) | |
+target_include_directories(Fibonacci PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include") | |
+target_compile_options(Fibonacci PUBLIC | |
"$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>") | |
diff --git a/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.cpp b/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.cpp | |
index d915589..7364d92 100644 | |
--- a/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.cpp | |
+++ b/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.cpp | |
@@ -15,5 +15,5 @@ | |
int fibonacci_cpp(int x) { | |
std::cout << "x [cpp]: " << x << std::endl; | |
if (x <= 1) return 1; | |
- return SwiftFibonacci::fibonacciSwift(x - 1) + SwiftFibonacci::fibonacciSwift(x - 2); | |
+ return Fibonacci::fibonacciSwift(x - 1) + Fibonacci::fibonacciSwift(x - 2); | |
} | |
diff --git a/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.swift b/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.swift | |
index 449794e..d84f038 100644 | |
--- a/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.swift | |
+++ b/3_bidirectional_cxx_interop/lib/fibonacci/fibonacci.swift | |
@@ -9,7 +9,8 @@ | |
// | |
//===----------------------------------------------------------------------===// | |
-import Fibonacci | |
+import FibonacciCpp | |
+import SwiftUI | |
public func fibonacciSwift(_ x: CInt) -> CInt { | |
print("x [swift]: \(x)") | |
@@ -18,3 +19,26 @@ public func fibonacciSwift(_ x: CInt) -> CInt { | |
} | |
return fibonacci_cpp(x - 1) + fibonacci_cpp(x - 2) | |
} | |
+ | |
+public func run() { | |
+ FibonacciApp.main() | |
+} | |
+ | |
+struct FibonacciApp: App { | |
+ var body: some Scene { | |
+ WindowGroup { | |
+ ContentView() | |
+ } | |
+ } | |
+} | |
+ | |
+struct ContentView: View { | |
+ var body: some View { | |
+ VStack { | |
+ Text("Fibonacci") | |
+ .padding() | |
+ Text("Fibonacci(5) = \(fibonacci_cpp(5))") | |
+ .padding() | |
+ } | |
+ } | |
+} | |
diff --git a/3_bidirectional_cxx_interop/src/CMakeLists.txt b/3_bidirectional_cxx_interop/src/CMakeLists.txt | |
index 42c68ec..e5734d4 100644 | |
--- a/3_bidirectional_cxx_interop/src/CMakeLists.txt | |
+++ b/3_bidirectional_cxx_interop/src/CMakeLists.txt | |
@@ -6,7 +6,7 @@ | |
# See https://swift.org/LICENSE.txt for license information | |
add_executable(fibonacci_swift fibonacci.swift) | |
-target_link_libraries(fibonacci_swift PRIVATE fibonacci) | |
+target_link_libraries(fibonacci_swift PRIVATE Fibonacci) | |
add_executable(fibonacci_cpp fibonacci.cpp) | |
-target_link_libraries(fibonacci_cpp PRIVATE fibonacci) | |
+target_link_libraries(fibonacci_cpp PRIVATE Fibonacci) | |
diff --git a/3_bidirectional_cxx_interop/src/fibonacci.cpp b/3_bidirectional_cxx_interop/src/fibonacci.cpp | |
index 2be7f1a..926b11d 100644 | |
--- a/3_bidirectional_cxx_interop/src/fibonacci.cpp | |
+++ b/3_bidirectional_cxx_interop/src/fibonacci.cpp | |
@@ -13,6 +13,6 @@ | |
#include <iostream> | |
int main(int argc, char ** argv) { | |
- std::cout << SwiftFibonacci::fibonacciSwift(5) << std::endl; | |
+ Fibonacci::run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment