From a2dd76f24e4049088c4cfbcb845ec4cf3cec69ac Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Sun, 21 Jul 2024 14:25:35 +0530 Subject: [PATCH 1/8] chore: add working swift c interop --- clients/swift/cac/Bridging-Header.h | 6 ++ clients/swift/cac/libcac_client.h | 33 +++++++ clients/swift/cac/main.swift | 131 ++++++++++++++++++++++++++++ clients/swift/default.nix | 20 +++++ flake.nix | 1 + 5 files changed, 191 insertions(+) create mode 100644 clients/swift/cac/Bridging-Header.h create mode 100644 clients/swift/cac/libcac_client.h create mode 100644 clients/swift/cac/main.swift create mode 100644 clients/swift/default.nix diff --git a/clients/swift/cac/Bridging-Header.h b/clients/swift/cac/Bridging-Header.h new file mode 100644 index 00000000..33a3fd76 --- /dev/null +++ b/clients/swift/cac/Bridging-Header.h @@ -0,0 +1,6 @@ +#ifndef CAC_Bridging_Header_h +#define CAC_Bridging_Header_h + +#include "libcac_client.h" + +#endif /* CAC_Bridging_Header_h */ diff --git a/clients/swift/cac/libcac_client.h b/clients/swift/cac/libcac_client.h new file mode 100644 index 00000000..7b562bd1 --- /dev/null +++ b/clients/swift/cac/libcac_client.h @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +typedef struct Arc_Client Arc_Client; + +int cac_last_error_length(void); + +const char *cac_last_error_message(void); + +void cac_free_string(char *s); + +int cac_new_client(const char *tenant, unsigned long update_frequency, const char *hostname); + +void cac_start_polling_update(const char *tenant); + +void cac_free_client(struct Arc_Client *ptr); + +struct Arc_Client *cac_get_client(const char *tenant); + +const char *cac_get_last_modified(struct Arc_Client *client); + +const char *cac_get_config(struct Arc_Client *client, + const char *filter_query, + const char *filter_prefix); + +const char *cac_get_resolved_config(struct Arc_Client *client, + const char *query, + const char *filter_keys, + const char *merge_strategy); + +const char *cac_get_default_config(struct Arc_Client *client, const char *filter_keys); diff --git a/clients/swift/cac/main.swift b/clients/swift/cac/main.swift new file mode 100644 index 00000000..a23ba083 --- /dev/null +++ b/clients/swift/cac/main.swift @@ -0,0 +1,131 @@ +import Foundation + +typealias UnknownClientPointer = OpaquePointer +enum MergeStrategy { + case MERGE + case REPLACE + + var show: String { + switch self { + case .MERGE: + return "MERGE" + case .REPLACE: + return "REPLACE" + } + } +} + +func createCacClient(tenant: String, frequency: UInt, hostname: String) -> Bool { + return tenant.withCString { t -> Bool in + return hostname.withCString { h -> Bool in + let resp = cac_new_client(t, frequency, h); + if (resp == 0) { + return true + } + return false + } + } +} + +func getCacClient(tenant: String) -> UnknownClientPointer? { + return tenant.withCString { t -> UnknownClientPointer? in + return cac_get_client(tenant) + } +} + +func cacStartPolling(tenant: String) { + tenant.withCString { t in + cac_start_polling_update(t) + } +} + +func getCacLastModified(client: UnknownClientPointer) -> String? { + let resp = cac_get_last_modified(client) + return resp.map { String(cString: $0) } +} + +func parseJson(jsonString: String) -> Any? { + if let jsonData = jsonString.data(using: .utf8) { + do { + return try JSONSerialization.jsonObject(with: jsonData, options: []) + } catch { + return nil + } + } + return nil +} + +// TODO: fix +func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Any? { + let keys = filterKeys.map { $0.joined(separator: "|") } + + return context.withCString { c -> Any? in + return MergeStrategy.MERGE.show.withCString { m -> Any? in + let rawData : UnsafePointer? + if let k = keys { + rawData = cac_get_resolved_config(client, c, k, m) + } else { + rawData = cac_get_resolved_config(client, c, nil, m) + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } + } +} + +func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]) -> Any? { + let keys = filterKeys.joined(separator: "|") + + return keys.withCString { k -> Any? in + let rawData = cac_get_default_config(client, keys) + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } +} + +func cacFreeClient(client: UnknownClientPointer) { + cac_free_client(client) +} + +func cacLastErrorMessage() -> String? { + return cac_last_error_message().map { String(cString: $0) } +} + + +let t = "test" +let f : UInt = 300 +let h = "http://localhost:8080" + +if (createCacClient(tenant: t, frequency: f, hostname: h)) { + if let client = getCacClient(tenant: t) { + // cacStartPolling(tenant: t) + // print("cacStartPolling started!") + + // let m = getCacLastModified(client: client) + let r = getResolvedConfig(client: client, context: "") + // let d = getDefaultConfig(client: client, filterKeys: []) + + if let val = r { + print("resolved: \(r)") + } else { + print("err-msg: \(cacLastErrorMessage())") + } + + // print("modified: \(m)") + + // print("default: \(d)") + + cacFreeClient(client: client) + } else { + print("getCacClient failed!") + } +} else { + print("createCacClient failed!") +} + +// func getFullConfigStateWithFilter(client: UnknownClientPointer, ) + +// let result = createCacClient(tenant: "naman", frequency: 3, hostname: "http://localhost:8080") +// let result = getCacClient(tenant: "naman") +// let result = cacStartPolling(tenant: "naman") +// let r4 = getCacLastModified(client: "naman") + +// print("Result from C function: \(r4)") diff --git a/clients/swift/default.nix b/clients/swift/default.nix new file mode 100644 index 00000000..902d56eb --- /dev/null +++ b/clients/swift/default.nix @@ -0,0 +1,20 @@ +{ + perSystem = { config, pkgs, self', lib, ... }: { + devShells.swift = let + compileCac = pkgs.writeShellScriptBin "compileCac" '' + swiftc cac/main.swift -lcac_client -import-objc-header cac/Bridging-Header.h -o cac-swift + ''; + in + pkgs.mkShell { + name = "superposition-swift-clients"; + shellHook = '' + export LIBRARY_PATH=${self'.packages.superposition}/lib + ''; + buildInputs = with pkgs; [ + swift + swiftPackages.Foundation + compileCac + ]; + }; + }; +} diff --git a/flake.nix b/flake.nix index 0c651811..6721b3db 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,7 @@ ./nix/pre-commit.nix ./clients/haskell ./nix/rust.nix + ./clients/swift ]; perSystem = { pkgs, self', config, ... }: { From fa30b5aa66e08b1d54364f24bac289621ae46955 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Tue, 23 Jul 2024 05:21:50 +0530 Subject: [PATCH 2/8] chore: add exp client + readme --- clients/swift/README.md | 20 ++++++ clients/swift/cac/Bridging-Header.h | 2 +- clients/swift/cac/libcac_client.h | 33 ---------- clients/swift/cac/main.swift | 61 ++++-------------- clients/swift/default.nix | 13 ++-- clients/swift/example/cac_example.swift | 41 +++++++++++++ clients/swift/example/exp_example.swift | 39 ++++++++++++ clients/swift/exp/Bridging-Header.h | 6 ++ clients/swift/exp/main.swift | 82 +++++++++++++++++++++++++ 9 files changed, 208 insertions(+), 89 deletions(-) create mode 100644 clients/swift/README.md delete mode 100644 clients/swift/cac/libcac_client.h create mode 100644 clients/swift/example/cac_example.swift create mode 100644 clients/swift/example/exp_example.swift create mode 100644 clients/swift/exp/Bridging-Header.h create mode 100644 clients/swift/exp/main.swift diff --git a/clients/swift/README.md b/clients/swift/README.md new file mode 100644 index 00000000..de5f66e3 --- /dev/null +++ b/clients/swift/README.md @@ -0,0 +1,20 @@ +## CAC and Experimentation clients in `Swift` + +- ## Walkthrough + - Create bridging file for each module + - add the `#include` statement for the C header file + - without xcode: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path (checkout : [default.nix][./default.nix]) + - with xcode: todo! + +- ## setup (nix) : + `nix develop .#swift` + +- ## compile without xcode (using nix) : + 1. to compile cac client: `compileCac` + 2. to compile exp client: `compileExp` + +- ## run using xcode : + todo! + +- ## run example : + todo! diff --git a/clients/swift/cac/Bridging-Header.h b/clients/swift/cac/Bridging-Header.h index 33a3fd76..3769e227 100644 --- a/clients/swift/cac/Bridging-Header.h +++ b/clients/swift/cac/Bridging-Header.h @@ -1,6 +1,6 @@ #ifndef CAC_Bridging_Header_h #define CAC_Bridging_Header_h -#include "libcac_client.h" +#include "../../../headers/libcac_client.h" #endif /* CAC_Bridging_Header_h */ diff --git a/clients/swift/cac/libcac_client.h b/clients/swift/cac/libcac_client.h deleted file mode 100644 index 7b562bd1..00000000 --- a/clients/swift/cac/libcac_client.h +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -#include - -typedef struct Arc_Client Arc_Client; - -int cac_last_error_length(void); - -const char *cac_last_error_message(void); - -void cac_free_string(char *s); - -int cac_new_client(const char *tenant, unsigned long update_frequency, const char *hostname); - -void cac_start_polling_update(const char *tenant); - -void cac_free_client(struct Arc_Client *ptr); - -struct Arc_Client *cac_get_client(const char *tenant); - -const char *cac_get_last_modified(struct Arc_Client *client); - -const char *cac_get_config(struct Arc_Client *client, - const char *filter_query, - const char *filter_prefix); - -const char *cac_get_resolved_config(struct Arc_Client *client, - const char *query, - const char *filter_keys, - const char *merge_strategy); - -const char *cac_get_default_config(struct Arc_Client *client, const char *filter_keys); diff --git a/clients/swift/cac/main.swift b/clients/swift/cac/main.swift index a23ba083..03d3cb51 100644 --- a/clients/swift/cac/main.swift +++ b/clients/swift/cac/main.swift @@ -1,6 +1,8 @@ import Foundation typealias UnknownClientPointer = OpaquePointer +typealias Value = [String: Any] + enum MergeStrategy { case MERGE case REPLACE @@ -44,10 +46,10 @@ func getCacLastModified(client: UnknownClientPointer) -> String? { return resp.map { String(cString: $0) } } -func parseJson(jsonString: String) -> Any? { +func parseJson(jsonString: String) -> Value? { if let jsonData = jsonString.data(using: .utf8) { do { - return try JSONSerialization.jsonObject(with: jsonData, options: []) + return try JSONSerialization.jsonObject(with: jsonData, options: []) as? Value } catch { return nil } @@ -56,14 +58,16 @@ func parseJson(jsonString: String) -> Any? { } // TODO: fix -func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Any? { +func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Value? { let keys = filterKeys.map { $0.joined(separator: "|") } - return context.withCString { c -> Any? in - return MergeStrategy.MERGE.show.withCString { m -> Any? in + return context.withCString { c -> Value? in + return MergeStrategy.MERGE.show.withCString { m -> Value? in let rawData : UnsafePointer? if let k = keys { - rawData = cac_get_resolved_config(client, c, k, m) + rawData = k.withCString { ck -> UnsafePointer? in + return cac_get_resolved_config(client, c, ck, m) + } } else { rawData = cac_get_resolved_config(client, c, nil, m) } @@ -72,10 +76,10 @@ func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys } } -func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]) -> Any? { +func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]) -> Value? { let keys = filterKeys.joined(separator: "|") - return keys.withCString { k -> Any? in + return keys.withCString { k -> Value? in let rawData = cac_get_default_config(client, keys) return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } } @@ -88,44 +92,3 @@ func cacFreeClient(client: UnknownClientPointer) { func cacLastErrorMessage() -> String? { return cac_last_error_message().map { String(cString: $0) } } - - -let t = "test" -let f : UInt = 300 -let h = "http://localhost:8080" - -if (createCacClient(tenant: t, frequency: f, hostname: h)) { - if let client = getCacClient(tenant: t) { - // cacStartPolling(tenant: t) - // print("cacStartPolling started!") - - // let m = getCacLastModified(client: client) - let r = getResolvedConfig(client: client, context: "") - // let d = getDefaultConfig(client: client, filterKeys: []) - - if let val = r { - print("resolved: \(r)") - } else { - print("err-msg: \(cacLastErrorMessage())") - } - - // print("modified: \(m)") - - // print("default: \(d)") - - cacFreeClient(client: client) - } else { - print("getCacClient failed!") - } -} else { - print("createCacClient failed!") -} - -// func getFullConfigStateWithFilter(client: UnknownClientPointer, ) - -// let result = createCacClient(tenant: "naman", frequency: 3, hostname: "http://localhost:8080") -// let result = getCacClient(tenant: "naman") -// let result = cacStartPolling(tenant: "naman") -// let r4 = getCacLastModified(client: "naman") - -// print("Result from C function: \(r4)") diff --git a/clients/swift/default.nix b/clients/swift/default.nix index 902d56eb..7e8fedab 100644 --- a/clients/swift/default.nix +++ b/clients/swift/default.nix @@ -2,19 +2,20 @@ perSystem = { config, pkgs, self', lib, ... }: { devShells.swift = let compileCac = pkgs.writeShellScriptBin "compileCac" '' - swiftc cac/main.swift -lcac_client -import-objc-header cac/Bridging-Header.h -o cac-swift + swiftc cac/main.swift -L../../target/debug -lcac_client -import-objc-header cac/Bridging-Header.h -o cac-swift + ''; + compileExp = pkgs.writeShellScriptBin "compileExp" '' + swiftc exp/main.swift example/exp_example.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/Bridging-Header.h -o exp-swift ''; in - pkgs.mkShell { + pkgs.mkShell { name = "superposition-swift-clients"; - shellHook = '' - export LIBRARY_PATH=${self'.packages.superposition}/lib - ''; buildInputs = with pkgs; [ swift swiftPackages.Foundation compileCac + compileExp ]; - }; + }; }; } diff --git a/clients/swift/example/cac_example.swift b/clients/swift/example/cac_example.swift new file mode 100644 index 00000000..45ce14bd --- /dev/null +++ b/clients/swift/example/cac_example.swift @@ -0,0 +1,41 @@ +import Foundation + +func main() { + let t = "test" + let f : UInt = 300 + let h = "http://localhost:8080" + + if (createCacClient(tenant: t, frequency: f, hostname: h)) { + print("createCacClient success!") + + if let client = getCacClient(tenant: t) { + print("getCacClient success!") + + let thread = Thread { + cacStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getCacLastModified(client: client) + let r = getResolvedConfig(client: client, context: "{}") + let d = getDefaultConfig(client: client, filterKeys: []) + + print("Last Modified: \(m)") + print("Resolved Config: \(r)") + print("Default Config: \(d)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + cacFreeClient(client: client) + print("Free client memory!") + } else { + print("getCacClient failed!") + } + } else { + print("createCacClient failed!") + } +} + +main() diff --git a/clients/swift/example/exp_example.swift b/clients/swift/example/exp_example.swift new file mode 100644 index 00000000..c92616a3 --- /dev/null +++ b/clients/swift/example/exp_example.swift @@ -0,0 +1,39 @@ +import Foundation + +func main() { + let t = "test" + let f : UInt = 300 + let h = "http://localhost:8080" + + if (createExptClient(tenant: t, frequency: f, hostname: h)) { + print("createExptClient successs!") + if let client = getExptClient(tenant: t) { + print("getExptClient successs!") + + let thread = Thread { + exptStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getApplicableVariant(client: client, context: "{}", toss: 1) + let r = getSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + let d = getRunningExperiments(client: client) + let v = getFilteredSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + print("Applicable Variants: \(r)") + print("Satisfied Experiments: \(m)") + print("Running Experiments: \(d)") + print("Filtered Satisfied Experiments: \(v)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + exptFreeClient(client: client) + print("Free client memory!") + } else { + print("getExptClient failed!") + } + } else { + print("createExptClient failed!") + } +} diff --git a/clients/swift/exp/Bridging-Header.h b/clients/swift/exp/Bridging-Header.h new file mode 100644 index 00000000..87d5f9c7 --- /dev/null +++ b/clients/swift/exp/Bridging-Header.h @@ -0,0 +1,6 @@ +#ifndef Experimentation_Bridging_Header_h +#define Experimentation_Bridging_Header_h + +#include "../../../headers/libexperimentation_client.h" + +#endif /* Experimentation_Bridging_Header_h */ diff --git a/clients/swift/exp/main.swift b/clients/swift/exp/main.swift new file mode 100644 index 00000000..66229248 --- /dev/null +++ b/clients/swift/exp/main.swift @@ -0,0 +1,82 @@ +import Foundation + +typealias UnknownClientPointer = OpaquePointer +typealias Value = [String: Any] + +func createExptClient(tenant: String, frequency: UInt, hostname: String) -> Bool { + return tenant.withCString { t -> Bool in + return hostname.withCString { h -> Bool in + let resp = expt_new_client(t, frequency, h); + if (resp == 0) { + return true + } + return false + } + } +} + +func getExptClient(tenant: String) -> UnknownClientPointer? { + return tenant.withCString { t -> UnknownClientPointer? in + return expt_get_client(tenant) + } +} + +func exptStartPolling(tenant: String) { + tenant.withCString { t in + expt_start_polling_update(t) + } +} + +func parseJson(jsonString: String) -> Value? { + if let jsonData = jsonString.data(using: .utf8) { + do { + return try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] + } catch { + return nil + } + } + return nil +} + +// TODO: fix +func getApplicableVariant(client: UnknownClientPointer, context: String, toss: Int16) -> Value? { + return context.withCString { c -> Value? in + let rawData = expt_get_applicable_variant(client, c, toss) + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } +} + +func getSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]) -> Value? { + let keys = filterPrefix.joined(separator: "|") + + return keys.withCString { k -> Value? in + return context.withCString { c -> Value? in + let rawData = expt_get_satisfied_experiments(client, c, k) + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } + } +} + +func getRunningExperiments(client: UnknownClientPointer) -> Value? { + let resp = expt_get_running_experiments(client) + return resp.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + + +func getFilteredSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]) -> Value? { + let keys = filterPrefix.joined(separator: "|") + return context.withCString { c -> Value? in + return keys.withCString { k -> Value? in + let resp = expt_get_filtered_satisfied_experiments(client, c, k) + return resp.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + } + } +} + +func exptFreeClient(client: UnknownClientPointer) { + expt_free_client(client) +} + +func exptLastErrorMessage() -> String? { + return expt_last_error_message().map { String(cString: $0) } +} From 8098682f5c0ca29859bde09bee5ac465bbe7ae95 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Tue, 23 Jul 2024 05:27:32 +0530 Subject: [PATCH 3/8] chore: cleanup --- clients/swift/README.md | 5 +++-- clients/swift/cac/main.swift | 1 - clients/swift/exp/main.swift | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/clients/swift/README.md b/clients/swift/README.md index de5f66e3..63b0f31e 100644 --- a/clients/swift/README.md +++ b/clients/swift/README.md @@ -3,15 +3,16 @@ - ## Walkthrough - Create bridging file for each module - add the `#include` statement for the C header file - - without xcode: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path (checkout : [default.nix][./default.nix]) + - without xcode: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path respectively. (checkout : [default.nix](default.nix)) - with xcode: todo! - ## setup (nix) : `nix develop .#swift` -- ## compile without xcode (using nix) : +- ## run without xcode (using nix) : 1. to compile cac client: `compileCac` 2. to compile exp client: `compileExp` + 3. use generated bins - ## run using xcode : todo! diff --git a/clients/swift/cac/main.swift b/clients/swift/cac/main.swift index 03d3cb51..65791835 100644 --- a/clients/swift/cac/main.swift +++ b/clients/swift/cac/main.swift @@ -57,7 +57,6 @@ func parseJson(jsonString: String) -> Value? { return nil } -// TODO: fix func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Value? { let keys = filterKeys.map { $0.joined(separator: "|") } diff --git a/clients/swift/exp/main.swift b/clients/swift/exp/main.swift index 66229248..ee307996 100644 --- a/clients/swift/exp/main.swift +++ b/clients/swift/exp/main.swift @@ -38,7 +38,6 @@ func parseJson(jsonString: String) -> Value? { return nil } -// TODO: fix func getApplicableVariant(client: UnknownClientPointer, context: String, toss: Int16) -> Value? { return context.withCString { c -> Value? in let rawData = expt_get_applicable_variant(client, c, toss) From dc4ed40da00088e56cb8c2581d959befa480ac42 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Thu, 25 Jul 2024 06:24:19 +0530 Subject: [PATCH 4/8] chore: resolve comments --- .gitignore | 3 +- ...ridging-Header.h => cac-bridging-header.h} | 0 clients/swift/cac/{main.swift => cac.swift} | 44 ++++++++++++++++--- clients/swift/default.nix | 15 ++++++- ...ridging-Header.h => exp-bridging-header.h} | 0 clients/swift/exp/{main.swift => exp.swift} | 17 ++++--- 6 files changed, 63 insertions(+), 16 deletions(-) rename clients/swift/cac/{Bridging-Header.h => cac-bridging-header.h} (100%) rename clients/swift/cac/{main.swift => cac.swift} (61%) rename clients/swift/exp/{Bridging-Header.h => exp-bridging-header.h} (100%) rename clients/swift/exp/{main.swift => exp.swift} (80%) diff --git a/.gitignore b/.gitignore index ba181d30..21790e6d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ backend/.env /result-* clients/haskell/result clients/haskell/dist-newstyle +clients/swift/*.out # dev bacon.toml docker-compose/localstack/export_cyphers.sh @@ -34,4 +35,4 @@ test_logs .cargo # pre-commit config .pre-commit-config.yaml -.cargo \ No newline at end of file +.cargo diff --git a/clients/swift/cac/Bridging-Header.h b/clients/swift/cac/cac-bridging-header.h similarity index 100% rename from clients/swift/cac/Bridging-Header.h rename to clients/swift/cac/cac-bridging-header.h diff --git a/clients/swift/cac/main.swift b/clients/swift/cac/cac.swift similarity index 61% rename from clients/swift/cac/main.swift rename to clients/swift/cac/cac.swift index 65791835..8785a570 100644 --- a/clients/swift/cac/main.swift +++ b/clients/swift/cac/cac.swift @@ -31,7 +31,7 @@ func createCacClient(tenant: String, frequency: UInt, hostname: String) -> Bool func getCacClient(tenant: String) -> UnknownClientPointer? { return tenant.withCString { t -> UnknownClientPointer? in - return cac_get_client(tenant) + return cac_get_client(t) } } @@ -75,13 +75,45 @@ func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys } } -func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]) -> Value? { - let keys = filterKeys.joined(separator: "|") +func getDefaultConfig(client: UnknownClientPointer, filterKeys: [String]? = nil) -> Value? { + let keys = filterKeys.map { $0.joined(separator: "|") } + let rawData : UnsafePointer? + if let k = keys { + rawData = k.withCString { ck -> UnsafePointer? in + return cac_get_default_config(client, ck) + } + } else { + rawData = cac_get_default_config(client, nil) + } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +} + +func getConfig(client: UnknownClientPointer, filterQuery: [String]?, filterPrefix: [String]?) -> Value? { + let query = filterQuery.map { $0.joined(separator: "|") } + let prefix = filterPrefix.map { $0.joined(separator: "|") } + let rawData : UnsafePointer? - return keys.withCString { k -> Value? in - let rawData = cac_get_default_config(client, keys) - return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } + if let q = query { + rawData = + q.withCString { qs -> UnsafePointer? in + if let p = prefix { + return p.withCString { ps -> UnsafePointer? in + return cac_get_config(client, qs, ps) + } + } else { + return cac_get_config(client, qs, nil) + } + } + } else { + if let p = prefix { + rawData = p.withCString { ps -> UnsafePointer? in + return cac_get_config(client, nil, ps) + } + } else { + rawData = cac_get_config(client, nil, nil) + } } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } } func cacFreeClient(client: UnknownClientPointer) { diff --git a/clients/swift/default.nix b/clients/swift/default.nix index 7e8fedab..57d78f6b 100644 --- a/clients/swift/default.nix +++ b/clients/swift/default.nix @@ -2,11 +2,21 @@ perSystem = { config, pkgs, self', lib, ... }: { devShells.swift = let compileCac = pkgs.writeShellScriptBin "compileCac" '' - swiftc cac/main.swift -L../../target/debug -lcac_client -import-objc-header cac/Bridging-Header.h -o cac-swift + swiftc cac/cac.swift -L../../target/debug -lcac_client -import-objc-header cac/cac-bridging-header.h -o cac-swift.out ''; compileExp = pkgs.writeShellScriptBin "compileExp" '' - swiftc exp/main.swift example/exp_example.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/Bridging-Header.h -o exp-swift + swiftc exp/exp.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/exp-bridging-header.h -o exp-swift.out ''; + # compileExample = pkgs.writeShellScriptBin "compileExample" '' + # mkdir example/modules + + # swiftc cac/cac.swift -L../../target/debug -lcac_client -import-objc-header cac/cac-bridging-header.h -emit-module -emit-module-path example/modules/cac.swiftmodule + + # swiftc exp/exp.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/exp-bridging-header.h -emit-module -emit-module-path example/modules/exp.swiftmodule + + # swiftc -I./example/modules example/cac_example.swift example/modules/cac.swiftmodule + # swiftc -I./example/modules example/exp_example.swift example/modules/exp.swiftmodule + # ''; in pkgs.mkShell { name = "superposition-swift-clients"; @@ -15,6 +25,7 @@ swiftPackages.Foundation compileCac compileExp + # compileExample ]; }; }; diff --git a/clients/swift/exp/Bridging-Header.h b/clients/swift/exp/exp-bridging-header.h similarity index 100% rename from clients/swift/exp/Bridging-Header.h rename to clients/swift/exp/exp-bridging-header.h diff --git a/clients/swift/exp/main.swift b/clients/swift/exp/exp.swift similarity index 80% rename from clients/swift/exp/main.swift rename to clients/swift/exp/exp.swift index ee307996..8bde8e48 100644 --- a/clients/swift/exp/main.swift +++ b/clients/swift/exp/exp.swift @@ -45,15 +45,18 @@ func getApplicableVariant(client: UnknownClientPointer, context: String, toss: I } } -func getSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]) -> Value? { - let keys = filterPrefix.joined(separator: "|") - - return keys.withCString { k -> Value? in - return context.withCString { c -> Value? in - let rawData = expt_get_satisfied_experiments(client, c, k) - return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } +func getSatisfiedExperiments(client: UnknownClientPointer, context: String, filterPrefix: [String]? = nil) -> Value? { + let keys = filterPrefix.map { $0.joined(separator: "|") } + let rawData = context.withCString { c -> UnsafeMutablePointer? in + if let k = keys { + return k.withCString { kc -> UnsafeMutablePointer? in + return expt_get_satisfied_experiments(client, c, kc) + } + } else { + return expt_get_satisfied_experiments(client, c, nil) } } + return rawData.map { String(cString: $0) }.flatMap { parseJson(jsonString: $0) } } func getRunningExperiments(client: UnknownClientPointer) -> Value? { From 147df32bcf4908818237c1a09473d27113320af6 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Sat, 27 Jul 2024 14:20:53 +0530 Subject: [PATCH 5/8] chore: add xcode project + nix-ed --- .gitignore | 1 + clients/swift/README.md | 27 +- clients/swift/cac/cac-bridging-header.h | 6 - clients/swift/default.nix | 22 +- clients/swift/example/cac_example.swift | 41 --- clients/swift/example/exp_example.swift | 39 -- clients/swift/exp/exp-bridging-header.h | 6 - clients/swift/swift.xcodeproj/project.pbxproj | 341 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + clients/swift/swift/Bridging-Header.h | 16 + clients/swift/{cac => swift}/cac.swift | 21 +- clients/swift/{exp => swift}/exp.swift | 21 +- clients/swift/swift/main.swift | 12 + clients/swift/swift/swift.entitlements | 8 + clients/swift/swift/test.swift | 90 +++++ clients/swift/swift/types.swift | 9 + clients/swift/swift/utils.swift | 19 + 18 files changed, 551 insertions(+), 143 deletions(-) delete mode 100644 clients/swift/cac/cac-bridging-header.h delete mode 100644 clients/swift/example/cac_example.swift delete mode 100644 clients/swift/example/exp_example.swift delete mode 100644 clients/swift/exp/exp-bridging-header.h create mode 100644 clients/swift/swift.xcodeproj/project.pbxproj create mode 100644 clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 clients/swift/swift/Bridging-Header.h rename clients/swift/{cac => swift}/cac.swift (90%) rename clients/swift/{exp => swift}/exp.swift (87%) create mode 100644 clients/swift/swift/main.swift create mode 100644 clients/swift/swift/swift.entitlements create mode 100644 clients/swift/swift/test.swift create mode 100644 clients/swift/swift/types.swift create mode 100644 clients/swift/swift/utils.swift diff --git a/.gitignore b/.gitignore index 21790e6d..bb2a750a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ backend/.env clients/haskell/result clients/haskell/dist-newstyle clients/swift/*.out +clients/swift/**/*/xcuserdata # dev bacon.toml docker-compose/localstack/export_cyphers.sh diff --git a/clients/swift/README.md b/clients/swift/README.md index 63b0f31e..a9eb8577 100644 --- a/clients/swift/README.md +++ b/clients/swift/README.md @@ -3,19 +3,20 @@ - ## Walkthrough - Create bridging file for each module - add the `#include` statement for the C header file - - without xcode: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path respectively. (checkout : [default.nix](default.nix)) - - with xcode: todo! + - `using nix`: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path respectively. (checkout : [default.nix](default.nix)) + - `using xcode`: create a command-line project in xcode and configure `-L`, `-l` and `-import-objc-header` flags using `Build Settings` in the GUI. Settings equivalent for each compiler flags (checkout : [project.pbxproj](swift.xcodeproj/project.pbxproj)) -- ## setup (nix) : - `nix develop .#swift` + - `-L` : `Library Search Paths` + - `-l` : `Other Linker Flags` + - `-import-objc-header` : `Objective-C Bridging Header` -- ## run without xcode (using nix) : - 1. to compile cac client: `compileCac` - 2. to compile exp client: `compileExp` - 3. use generated bins +- ## setup + - ### using nix : + 1. cd to `clients/swift` + 2. spawn devShell `nix develop .#swift` + 3. run `compileTest` + 4. use generated bins -- ## run using xcode : - todo! - -- ## run example : - todo! + - ### using xcode : + 1. open project (`clients/swift`) in xcode + 2. build & run the project diff --git a/clients/swift/cac/cac-bridging-header.h b/clients/swift/cac/cac-bridging-header.h deleted file mode 100644 index 3769e227..00000000 --- a/clients/swift/cac/cac-bridging-header.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef CAC_Bridging_Header_h -#define CAC_Bridging_Header_h - -#include "../../../headers/libcac_client.h" - -#endif /* CAC_Bridging_Header_h */ diff --git a/clients/swift/default.nix b/clients/swift/default.nix index 57d78f6b..8ae8c03d 100644 --- a/clients/swift/default.nix +++ b/clients/swift/default.nix @@ -1,19 +1,21 @@ { perSystem = { config, pkgs, self', lib, ... }: { devShells.swift = let - compileCac = pkgs.writeShellScriptBin "compileCac" '' - swiftc cac/cac.swift -L../../target/debug -lcac_client -import-objc-header cac/cac-bridging-header.h -o cac-swift.out - ''; - compileExp = pkgs.writeShellScriptBin "compileExp" '' - swiftc exp/exp.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/exp-bridging-header.h -o exp-swift.out + compileTest = pkgs.writeShellScriptBin "compileTest" '' + swiftc \ + swift/main.swift swift/cac.swift swift/exp.swift swift/types.swift swift/utils.swift swift/test.swift \ + -L../../target/debug \ + -lcac_client \ + -lexperimentation_client \ + -import-objc-header swift/Bridging-Header.h \ + -o test_client.out ''; + + # TODO: use nix to build module # compileExample = pkgs.writeShellScriptBin "compileExample" '' # mkdir example/modules - # swiftc cac/cac.swift -L../../target/debug -lcac_client -import-objc-header cac/cac-bridging-header.h -emit-module -emit-module-path example/modules/cac.swiftmodule - # swiftc exp/exp.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/exp-bridging-header.h -emit-module -emit-module-path example/modules/exp.swiftmodule - # swiftc -I./example/modules example/cac_example.swift example/modules/cac.swiftmodule # swiftc -I./example/modules example/exp_example.swift example/modules/exp.swiftmodule # ''; @@ -23,9 +25,7 @@ buildInputs = with pkgs; [ swift swiftPackages.Foundation - compileCac - compileExp - # compileExample + compileTest ]; }; }; diff --git a/clients/swift/example/cac_example.swift b/clients/swift/example/cac_example.swift deleted file mode 100644 index 45ce14bd..00000000 --- a/clients/swift/example/cac_example.swift +++ /dev/null @@ -1,41 +0,0 @@ -import Foundation - -func main() { - let t = "test" - let f : UInt = 300 - let h = "http://localhost:8080" - - if (createCacClient(tenant: t, frequency: f, hostname: h)) { - print("createCacClient success!") - - if let client = getCacClient(tenant: t) { - print("getCacClient success!") - - let thread = Thread { - cacStartPolling(tenant: t) - } - thread.start() - print("cacStartPolling: thread started!") - - let m = getCacLastModified(client: client) - let r = getResolvedConfig(client: client, context: "{}") - let d = getDefaultConfig(client: client, filterKeys: []) - - print("Last Modified: \(m)") - print("Resolved Config: \(r)") - print("Default Config: \(d)") - - thread.cancel() - print("cacStartPolling: thread closed!") - - cacFreeClient(client: client) - print("Free client memory!") - } else { - print("getCacClient failed!") - } - } else { - print("createCacClient failed!") - } -} - -main() diff --git a/clients/swift/example/exp_example.swift b/clients/swift/example/exp_example.swift deleted file mode 100644 index c92616a3..00000000 --- a/clients/swift/example/exp_example.swift +++ /dev/null @@ -1,39 +0,0 @@ -import Foundation - -func main() { - let t = "test" - let f : UInt = 300 - let h = "http://localhost:8080" - - if (createExptClient(tenant: t, frequency: f, hostname: h)) { - print("createExptClient successs!") - if let client = getExptClient(tenant: t) { - print("getExptClient successs!") - - let thread = Thread { - exptStartPolling(tenant: t) - } - thread.start() - print("cacStartPolling: thread started!") - - let m = getApplicableVariant(client: client, context: "{}", toss: 1) - let r = getSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) - let d = getRunningExperiments(client: client) - let v = getFilteredSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) - print("Applicable Variants: \(r)") - print("Satisfied Experiments: \(m)") - print("Running Experiments: \(d)") - print("Filtered Satisfied Experiments: \(v)") - - thread.cancel() - print("cacStartPolling: thread closed!") - - exptFreeClient(client: client) - print("Free client memory!") - } else { - print("getExptClient failed!") - } - } else { - print("createExptClient failed!") - } -} diff --git a/clients/swift/exp/exp-bridging-header.h b/clients/swift/exp/exp-bridging-header.h deleted file mode 100644 index 87d5f9c7..00000000 --- a/clients/swift/exp/exp-bridging-header.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef Experimentation_Bridging_Header_h -#define Experimentation_Bridging_Header_h - -#include "../../../headers/libexperimentation_client.h" - -#endif /* Experimentation_Bridging_Header_h */ diff --git a/clients/swift/swift.xcodeproj/project.pbxproj b/clients/swift/swift.xcodeproj/project.pbxproj new file mode 100644 index 00000000..425f1cb8 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.pbxproj @@ -0,0 +1,341 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 29FE37482C54DD7B00F7BE64 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37472C54DD7B00F7BE64 /* main.swift */; }; + 29FE374F2C54DD8C00F7BE64 /* cac.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE374E2C54DD8C00F7BE64 /* cac.swift */; }; + 29FE37512C54DD9500F7BE64 /* exp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37502C54DD9500F7BE64 /* exp.swift */; }; + 29FE37542C54DDDD00F7BE64 /* types.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37532C54DDDD00F7BE64 /* types.swift */; }; + 29FE37562C54DE7E00F7BE64 /* utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37552C54DE7E00F7BE64 /* utils.swift */; }; + 29FE37582C54DFA300F7BE64 /* test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29FE37572C54DFA300F7BE64 /* test.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 29FE37422C54DD7B00F7BE64 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 29FE37442C54DD7B00F7BE64 /* swift */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = swift; sourceTree = BUILT_PRODUCTS_DIR; }; + 29FE37472C54DD7B00F7BE64 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; + 29FE374E2C54DD8C00F7BE64 /* cac.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cac.swift; sourceTree = ""; }; + 29FE37502C54DD9500F7BE64 /* exp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = exp.swift; sourceTree = ""; }; + 29FE37522C54DDA200F7BE64 /* Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Bridging-Header.h"; sourceTree = ""; }; + 29FE37532C54DDDD00F7BE64 /* types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = types.swift; sourceTree = ""; }; + 29FE37552C54DE7E00F7BE64 /* utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = utils.swift; sourceTree = ""; }; + 29FE37572C54DFA300F7BE64 /* test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = test.swift; sourceTree = ""; }; + 29FE37592C54E10800F7BE64 /* swift.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = swift.entitlements; sourceTree = ""; }; + 29FE375A2C54E48800F7BE64 /* default.nix */ = {isa = PBXFileReference; lastKnownFileType = text; path = default.nix; sourceTree = ""; }; + 29FE375B2C54E7FD00F7BE64 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 29FE37412C54DD7B00F7BE64 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 29FE373B2C54DD7B00F7BE64 = { + isa = PBXGroup; + children = ( + 29FE375B2C54E7FD00F7BE64 /* README.md */, + 29FE375A2C54E48800F7BE64 /* default.nix */, + 29FE37462C54DD7B00F7BE64 /* swift */, + 29FE37452C54DD7B00F7BE64 /* Products */, + ); + sourceTree = ""; + }; + 29FE37452C54DD7B00F7BE64 /* Products */ = { + isa = PBXGroup; + children = ( + 29FE37442C54DD7B00F7BE64 /* swift */, + ); + name = Products; + sourceTree = ""; + }; + 29FE37462C54DD7B00F7BE64 /* swift */ = { + isa = PBXGroup; + children = ( + 29FE37592C54E10800F7BE64 /* swift.entitlements */, + 29FE37472C54DD7B00F7BE64 /* main.swift */, + 29FE374E2C54DD8C00F7BE64 /* cac.swift */, + 29FE37502C54DD9500F7BE64 /* exp.swift */, + 29FE37522C54DDA200F7BE64 /* Bridging-Header.h */, + 29FE37532C54DDDD00F7BE64 /* types.swift */, + 29FE37552C54DE7E00F7BE64 /* utils.swift */, + 29FE37572C54DFA300F7BE64 /* test.swift */, + ); + path = swift; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 29FE37432C54DD7B00F7BE64 /* swift */ = { + isa = PBXNativeTarget; + buildConfigurationList = 29FE374B2C54DD7B00F7BE64 /* Build configuration list for PBXNativeTarget "swift" */; + buildPhases = ( + 29FE37402C54DD7B00F7BE64 /* Sources */, + 29FE37412C54DD7B00F7BE64 /* Frameworks */, + 29FE37422C54DD7B00F7BE64 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = swift; + productName = swift; + productReference = 29FE37442C54DD7B00F7BE64 /* swift */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 29FE373C2C54DD7B00F7BE64 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1520; + LastUpgradeCheck = 1520; + TargetAttributes = { + 29FE37432C54DD7B00F7BE64 = { + CreatedOnToolsVersion = 15.2; + }; + }; + }; + buildConfigurationList = 29FE373F2C54DD7B00F7BE64 /* Build configuration list for PBXProject "swift" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 29FE373B2C54DD7B00F7BE64; + productRefGroup = 29FE37452C54DD7B00F7BE64 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 29FE37432C54DD7B00F7BE64 /* swift */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 29FE37402C54DD7B00F7BE64 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 29FE37562C54DE7E00F7BE64 /* utils.swift in Sources */, + 29FE37582C54DFA300F7BE64 /* test.swift in Sources */, + 29FE37482C54DD7B00F7BE64 /* main.swift in Sources */, + 29FE37512C54DD9500F7BE64 /* exp.swift in Sources */, + 29FE374F2C54DD8C00F7BE64 /* cac.swift in Sources */, + 29FE37542C54DDDD00F7BE64 /* types.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 29FE37492C54DD7B00F7BE64 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 13.5; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 29FE374A2C54DD7B00F7BE64 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MACOSX_DEPLOYMENT_TARGET = 13.5; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + }; + name = Release; + }; + 29FE374C2C54DD7B00F7BE64 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 32XH2DF5HY; + ENABLE_HARDENED_RUNTIME = YES; + LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../target/debug"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(SRCROOT)/../../target/debug"; + OTHER_LDFLAGS = ( + "-v", + "-lexperimentation_client", + "-lcac_client", + ); + "OTHER_LDFLAGS[arch=*]" = ( + "-v", + "-lexperimentation_client", + "-lcac_client", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + "SWIFT_OBJC_BRIDGING_HEADER[arch=*]" = "$(SRCROOT)/swift/Bridging-Header.h"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 29FE374D2C54DD7B00F7BE64 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 32XH2DF5HY; + ENABLE_HARDENED_RUNTIME = YES; + OTHER_LDFLAGS = ( + "-lexperimentation_client", + "-lcac_client", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 29FE373F2C54DD7B00F7BE64 /* Build configuration list for PBXProject "swift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29FE37492C54DD7B00F7BE64 /* Debug */, + 29FE374A2C54DD7B00F7BE64 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 29FE374B2C54DD7B00F7BE64 /* Build configuration list for PBXNativeTarget "swift" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 29FE374C2C54DD7B00F7BE64 /* Debug */, + 29FE374D2C54DD7B00F7BE64 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 29FE373C2C54DD7B00F7BE64 /* Project object */; +} diff --git a/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/clients/swift/swift.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/clients/swift/swift/Bridging-Header.h b/clients/swift/swift/Bridging-Header.h new file mode 100644 index 00000000..105f8640 --- /dev/null +++ b/clients/swift/swift/Bridging-Header.h @@ -0,0 +1,16 @@ +// +// Bridging-Header.h +// swift +// +// Created by naman agarwal on 27/07/24. +// + +#ifndef Bridging_Header_h +#define Bridging_Header_h + +// cac +#include "../../../headers/libcac_client.h" +// exp +#include "../../../headers/libexperimentation_client.h" + +#endif /* Bridging_Header_h */ diff --git a/clients/swift/cac/cac.swift b/clients/swift/swift/cac.swift similarity index 90% rename from clients/swift/cac/cac.swift rename to clients/swift/swift/cac.swift index 8785a570..14380b94 100644 --- a/clients/swift/cac/cac.swift +++ b/clients/swift/swift/cac.swift @@ -1,7 +1,11 @@ -import Foundation +// +// cac.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// -typealias UnknownClientPointer = OpaquePointer -typealias Value = [String: Any] +import Foundation enum MergeStrategy { case MERGE @@ -46,17 +50,6 @@ func getCacLastModified(client: UnknownClientPointer) -> String? { return resp.map { String(cString: $0) } } -func parseJson(jsonString: String) -> Value? { - if let jsonData = jsonString.data(using: .utf8) { - do { - return try JSONSerialization.jsonObject(with: jsonData, options: []) as? Value - } catch { - return nil - } - } - return nil -} - func getResolvedConfig(client: UnknownClientPointer, context: String, filterKeys: [String]? = nil) -> Value? { let keys = filterKeys.map { $0.joined(separator: "|") } diff --git a/clients/swift/exp/exp.swift b/clients/swift/swift/exp.swift similarity index 87% rename from clients/swift/exp/exp.swift rename to clients/swift/swift/exp.swift index 8bde8e48..1b701c38 100644 --- a/clients/swift/exp/exp.swift +++ b/clients/swift/swift/exp.swift @@ -1,7 +1,12 @@ +// +// exp.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + import Foundation -typealias UnknownClientPointer = OpaquePointer -typealias Value = [String: Any] func createExptClient(tenant: String, frequency: UInt, hostname: String) -> Bool { return tenant.withCString { t -> Bool in @@ -27,17 +32,6 @@ func exptStartPolling(tenant: String) { } } -func parseJson(jsonString: String) -> Value? { - if let jsonData = jsonString.data(using: .utf8) { - do { - return try JSONSerialization.jsonObject(with: jsonData, options: []) as? [String: Any] - } catch { - return nil - } - } - return nil -} - func getApplicableVariant(client: UnknownClientPointer, context: String, toss: Int16) -> Value? { return context.withCString { c -> Value? in let rawData = expt_get_applicable_variant(client, c, toss) @@ -82,3 +76,4 @@ func exptFreeClient(client: UnknownClientPointer) { func exptLastErrorMessage() -> String? { return expt_last_error_message().map { String(cString: $0) } } + diff --git a/clients/swift/swift/main.swift b/clients/swift/swift/main.swift new file mode 100644 index 00000000..6c598f46 --- /dev/null +++ b/clients/swift/swift/main.swift @@ -0,0 +1,12 @@ +// +// main.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +// unit tests +test_cac() +test_exp() diff --git a/clients/swift/swift/swift.entitlements b/clients/swift/swift/swift.entitlements new file mode 100644 index 00000000..8cc185af --- /dev/null +++ b/clients/swift/swift/swift.entitlements @@ -0,0 +1,8 @@ + + + + + com.apple.security.cs.disable-library-validation + + + diff --git a/clients/swift/swift/test.swift b/clients/swift/swift/test.swift new file mode 100644 index 00000000..10256b9a --- /dev/null +++ b/clients/swift/swift/test.swift @@ -0,0 +1,90 @@ +// +// test.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +// test CAC functions +func test_cac() { + let t = "dev" + let f : UInt = 300 + let h = "http://localhost:8080" + + print("\n") + if (createCacClient(tenant: t, frequency: f, hostname: h)) { + print("createCacClient success!") + + if let client = getCacClient(tenant: t) { + print("getCacClient success!") + + let thread = Thread { + cacStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getCacLastModified(client: client) + let r = getResolvedConfig(client: client, context: "{}") + let d = getDefaultConfig(client: client, filterKeys: []) + let c = getConfig(client: client, filterQuery: nil, filterPrefix: nil) + + print("Last Modified: \(m)") + print("Resolved Config: \(r)") + print("Default Config: \(d)") + print("Get Config: \(c)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + cacFreeClient(client: client) + print("Free client memory!") + } else { + print("getCacClient failed!") + } + } else { + print("createCacClient failed!") + } +} + +// test EXP functions +func test_exp() { + let t = "dev" + let f : UInt = 300 + let h = "http://localhost:8080" + + print("\n") + if (createExptClient(tenant: t, frequency: f, hostname: h)) { + print("createExptClient successs!") + if let client = getExptClient(tenant: t) { + print("getExptClient successs!") + + let thread = Thread { + exptStartPolling(tenant: t) + } + thread.start() + print("cacStartPolling: thread started!") + + let m = getApplicableVariant(client: client, context: "{}", toss: 1) + let r = getSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + let d = getRunningExperiments(client: client) + let v = getFilteredSatisfiedExperiments(client: client, context: "{}", filterPrefix: []) + print("Applicable Variants: \(r)") + print("Satisfied Experiments: \(m)") + print("Running Experiments: \(d)") + print("Filtered Satisfied Experiments: \(v)") + + thread.cancel() + print("cacStartPolling: thread closed!") + + exptFreeClient(client: client) + print("Free client memory!") + } else { + print("getExptClient failed!") + } + } else { + print("createExptClient failed!") + } +} diff --git a/clients/swift/swift/types.swift b/clients/swift/swift/types.swift new file mode 100644 index 00000000..fe6e248a --- /dev/null +++ b/clients/swift/swift/types.swift @@ -0,0 +1,9 @@ +// +// types.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +typealias UnknownClientPointer = OpaquePointer +typealias Value = [String: Any] diff --git a/clients/swift/swift/utils.swift b/clients/swift/swift/utils.swift new file mode 100644 index 00000000..9a9c91db --- /dev/null +++ b/clients/swift/swift/utils.swift @@ -0,0 +1,19 @@ +// +// utils.swift +// swift +// +// Created by naman agarwal on 27/07/24. +// + +import Foundation + +func parseJson(jsonString: String) -> Value? { + if let jsonData = jsonString.data(using: .utf8) { + do { + return try JSONSerialization.jsonObject(with: jsonData, options: []) as? Value + } catch { + return nil + } + } + return nil +} From 11d83b9e649e51ac6bc3bc3c90ee4c4c4fbece8a Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Sat, 3 Aug 2024 23:55:19 +0530 Subject: [PATCH 6/8] chore: add config file --- clients/swift/Config.xcconfig | 20 +++++ clients/swift/default.nix | 11 +-- clients/swift/swift.xcodeproj/project.pbxproj | 24 +++++- .../xcshareddata/xcschemes/swift.xcscheme | 78 +++++++++++++++++++ 4 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 clients/swift/Config.xcconfig create mode 100644 clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme diff --git a/clients/swift/Config.xcconfig b/clients/swift/Config.xcconfig new file mode 100644 index 00000000..c790a69f --- /dev/null +++ b/clients/swift/Config.xcconfig @@ -0,0 +1,20 @@ +// +// Config.xcconfig +// swift +// +// Created by naman agarwal on 03/08/24. +// + +// Configuration settings file format documentation can be found at: +// https://help.apple.com/xcode/#/dev745c5c974 + +SWIFT_FLAGS = $(inherited) -DSUPERPOSITION_LIB_PATH="/Users/naman.agarwal/dev2/superposition/target/debugs" + +// Library Search Paths +LIBRARY_SEARCH_PATHS = $(inherited) $(SUPERPOSITION_LIB_PATH) + +// Linker Flags +OTHER_LDFLAGS = $(inherited) -lcac_client -lexperimentation_client + +// Bridging Header +SWIFT_OBJC_BRIDGING_HEADER = $(SRCROOT)/swift/Bridging-Header.h diff --git a/clients/swift/default.nix b/clients/swift/default.nix index 8ae8c03d..d188bb71 100644 --- a/clients/swift/default.nix +++ b/clients/swift/default.nix @@ -4,21 +4,12 @@ compileTest = pkgs.writeShellScriptBin "compileTest" '' swiftc \ swift/main.swift swift/cac.swift swift/exp.swift swift/types.swift swift/utils.swift swift/test.swift \ - -L../../target/debug \ + -L$SUPERPOSITION_LIB_PATH \ -lcac_client \ -lexperimentation_client \ -import-objc-header swift/Bridging-Header.h \ -o test_client.out ''; - - # TODO: use nix to build module - # compileExample = pkgs.writeShellScriptBin "compileExample" '' - # mkdir example/modules - # swiftc cac/cac.swift -L../../target/debug -lcac_client -import-objc-header cac/cac-bridging-header.h -emit-module -emit-module-path example/modules/cac.swiftmodule - # swiftc exp/exp.swift -L../../target/debug -lexperimentation_client -import-objc-header exp/exp-bridging-header.h -emit-module -emit-module-path example/modules/exp.swiftmodule - # swiftc -I./example/modules example/cac_example.swift example/modules/cac.swiftmodule - # swiftc -I./example/modules example/exp_example.swift example/modules/exp.swiftmodule - # ''; in pkgs.mkShell { name = "superposition-swift-clients"; diff --git a/clients/swift/swift.xcodeproj/project.pbxproj b/clients/swift/swift.xcodeproj/project.pbxproj index 425f1cb8..13c1f9a5 100644 --- a/clients/swift/swift.xcodeproj/project.pbxproj +++ b/clients/swift/swift.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 29C786A02C5E54C400354E13 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = ""; }; 29FE37442C54DD7B00F7BE64 /* swift */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = swift; sourceTree = BUILT_PRODUCTS_DIR; }; 29FE37472C54DD7B00F7BE64 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 29FE374E2C54DD8C00F7BE64 /* cac.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cac.swift; sourceTree = ""; }; @@ -55,6 +56,7 @@ 29FE373B2C54DD7B00F7BE64 = { isa = PBXGroup; children = ( + 29C786A02C5E54C400354E13 /* Config.xcconfig */, 29FE375B2C54E7FD00F7BE64 /* README.md */, 29FE375A2C54E48800F7BE64 /* default.nix */, 29FE37462C54DD7B00F7BE64 /* swift */, @@ -157,6 +159,7 @@ /* Begin XCBuildConfiguration section */ 29FE37492C54DD7B00F7BE64 /* Debug */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; @@ -207,19 +210,25 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MACOSX_DEPLOYMENT_TARGET = 13.5; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; + OTHER_LDFLAGS = "$(inherited)"; + "OTHER_SWIFT_FLAGS[arch=*]" = ""; SDKROOT = macosx; SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OBJC_BRIDGING_HEADER = "$(inherited)"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; }; name = Debug; }; 29FE374A2C54DD7B00F7BE64 /* Release */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; @@ -264,24 +273,29 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; MACOSX_DEPLOYMENT_TARGET = 13.5; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; + OTHER_LDFLAGS = "$(inherited)"; SDKROOT = macosx; SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OBJC_BRIDGING_HEADER = "$(inherited)"; }; name = Release; }; 29FE374C2C54DD7B00F7BE64 /* Debug */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; buildSettings = { CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 32XH2DF5HY; ENABLE_HARDENED_RUNTIME = YES; - LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../target/debug"; - "LIBRARY_SEARCH_PATHS[arch=*]" = "$(SRCROOT)/../../target/debug"; + LIBRARY_SEARCH_PATHS = "$(inherited)"; + "LIBRARY_SEARCH_PATHS[arch=*]" = "$(inherited)"; OTHER_LDFLAGS = ( "-v", "-lexperimentation_client", @@ -300,11 +314,13 @@ }; 29FE374D2C54DD7B00F7BE64 /* Release */ = { isa = XCBuildConfiguration; + baseConfigurationReference = 29C786A02C5E54C400354E13 /* Config.xcconfig */; buildSettings = { CODE_SIGN_ENTITLEMENTS = swift/swift.entitlements; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = 32XH2DF5HY; ENABLE_HARDENED_RUNTIME = YES; + LIBRARY_SEARCH_PATHS = "$(inherited)"; OTHER_LDFLAGS = ( "-lexperimentation_client", "-lcac_client", @@ -324,7 +340,7 @@ 29FE374A2C54DD7B00F7BE64 /* Release */, ); defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; + defaultConfigurationName = Debug; }; 29FE374B2C54DD7B00F7BE64 /* Build configuration list for PBXNativeTarget "swift" */ = { isa = XCConfigurationList; @@ -333,7 +349,7 @@ 29FE374D2C54DD7B00F7BE64 /* Release */, ); defaultConfigurationIsVisible = 0; - defaultConfigurationName = Release; + defaultConfigurationName = Debug; }; /* End XCConfigurationList section */ }; diff --git a/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme b/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme new file mode 100644 index 00000000..97859273 --- /dev/null +++ b/clients/swift/swift.xcodeproj/xcshareddata/xcschemes/swift.xcscheme @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From eddae5efc25d9d9f938322bf3a9b64aca699bb4b Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Sun, 4 Aug 2024 00:37:16 +0530 Subject: [PATCH 7/8] chore: use include path --- clients/swift/Config.xcconfig | 7 ++++++- clients/swift/default.nix | 1 + clients/swift/swift/Bridging-Header.h | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/clients/swift/Config.xcconfig b/clients/swift/Config.xcconfig index c790a69f..a27c793f 100644 --- a/clients/swift/Config.xcconfig +++ b/clients/swift/Config.xcconfig @@ -8,11 +8,16 @@ // Configuration settings file format documentation can be found at: // https://help.apple.com/xcode/#/dev745c5c974 -SWIFT_FLAGS = $(inherited) -DSUPERPOSITION_LIB_PATH="/Users/naman.agarwal/dev2/superposition/target/debugs" +// Change env manually +// SUPERPOSITION_LIB_PATH=$(SRCROOT)/../../target/debug +// SUPERPOSITION_INCLUDE_PATH=$(SRCROOT)/../../headers // Library Search Paths LIBRARY_SEARCH_PATHS = $(inherited) $(SUPERPOSITION_LIB_PATH) +// Add your header search paths +HEADER_SEARCH_PATHS = $(inherited) $(SUPERPOSITION_INCLUDE_PATH) + // Linker Flags OTHER_LDFLAGS = $(inherited) -lcac_client -lexperimentation_client diff --git a/clients/swift/default.nix b/clients/swift/default.nix index d188bb71..695a6b16 100644 --- a/clients/swift/default.nix +++ b/clients/swift/default.nix @@ -5,6 +5,7 @@ swiftc \ swift/main.swift swift/cac.swift swift/exp.swift swift/types.swift swift/utils.swift swift/test.swift \ -L$SUPERPOSITION_LIB_PATH \ + -I$SUPERPOSITION_INCLUDE_PATH \ -lcac_client \ -lexperimentation_client \ -import-objc-header swift/Bridging-Header.h \ diff --git a/clients/swift/swift/Bridging-Header.h b/clients/swift/swift/Bridging-Header.h index 105f8640..f8f187a6 100644 --- a/clients/swift/swift/Bridging-Header.h +++ b/clients/swift/swift/Bridging-Header.h @@ -9,8 +9,8 @@ #define Bridging_Header_h // cac -#include "../../../headers/libcac_client.h" +#include "libcac_client.h" // exp -#include "../../../headers/libexperimentation_client.h" +#include "libexperimentation_client.h" #endif /* Bridging_Header_h */ From e580ea71cea3a4a8aeeedfa4b3bbedbf585a4c80 Mon Sep 17 00:00:00 2001 From: Naman Agarwal Date: Sun, 4 Aug 2024 00:41:36 +0530 Subject: [PATCH 8/8] chore: update readme --- clients/swift/README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clients/swift/README.md b/clients/swift/README.md index a9eb8577..eeb88e4d 100644 --- a/clients/swift/README.md +++ b/clients/swift/README.md @@ -3,12 +3,13 @@ - ## Walkthrough - Create bridging file for each module - add the `#include` statement for the C header file - - `using nix`: use `swiftc` to compile modules using `-L`, `-l` and `-import-objc-header` flags to specify object files search dir, object-module name and bridging file path respectively. (checkout : [default.nix](default.nix)) - - `using xcode`: create a command-line project in xcode and configure `-L`, `-l` and `-import-objc-header` flags using `Build Settings` in the GUI. Settings equivalent for each compiler flags (checkout : [project.pbxproj](swift.xcodeproj/project.pbxproj)) + - `using nix`: use `swiftc` to compile modules using `-L`, `-l`, `I` and `-import-objc-header` flags to specify object files search dir, object-module name, header search path and bridging file path respectively. (checkout : [default.nix](default.nix)) + - `using xcode`: create a command-line project in xcode and configure `-L`, `-l` and `-import-objc-header` flags using `Build Settings` in the GUI. Settings equivalent for each compiler flags (checkout : [Config](Config.xcconfig)) - - `-L` : `Library Search Paths` - - `-l` : `Other Linker Flags` - - `-import-objc-header` : `Objective-C Bridging Header` + - `-L` : `LIBRARY_SEARCH_PATHS` + - `-l` : `OTHER_LDFLAGS` + - `-import-objc-header` : `SWIFT_OBJC_BRIDGING_HEADER` + - `I` : `HEADER_SEARCH_PATHS` - ## setup - ### using nix :