Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
37e2174
adding swift wrapper
absaroj Jun 2, 2023
70984ff
moving files to sources
absaroj Jun 2, 2023
ca4c6b8
moving files to proper places
absaroj Jun 2, 2023
80790d2
adding module for header expose and importing it in swift wrapper
absaroj Jun 2, 2023
3b91ddc
correcting files for build errors and other changes
absaroj Jun 2, 2023
94f9691
adding module for objc
absaroj Jun 2, 2023
408fc8f
swift build passing'
absaroj Jun 2, 2023
1791586
adding readme
absaroj Jun 2, 2023
c24eb4a
changing the package name to 1dsswiftwrapper
absaroj Jun 6, 2023
e7e43ef
changing target name
absaroj Jun 8, 2023
477b036
adding pr suggestions
absaroj Jun 8, 2023
eb85e7f
adding build.sh changes
absaroj Jun 9, 2023
d76b833
cmake list file changes
absaroj Jun 9, 2023
2927818
creating static lib
absaroj Jun 9, 2023
85d380a
adding swift wrappers build and linking
absaroj Jun 9, 2023
da20709
adding more logic for building and producing sample app
absaroj Jun 9, 2023
bf52749
adding code for swiftc invoking
absaroj Jun 9, 2023
7b69738
adding comments
absaroj Jun 9, 2023
753bde7
adding change to create the sample app
absaroj Jun 9, 2023
fde4743
linking and includding modules in swiftc to build the sample exe: bei…
absaroj Jun 9, 2023
e96ab38
adding comments
absaroj Jun 9, 2023
df89625
removing self
absaroj Jun 9, 2023
2a129d7
compiling all files frrom sample
absaroj Jun 9, 2023
84585b4
removing extraline
absaroj Jun 9, 2023
37e75e1
correcting the output folder of swift build
absaroj Jun 11, 2023
d149e89
correcting build output folder paths
absaroj Jun 12, 2023
a536f12
Merge branch 'main' into absaroj/local_build
lalitb Jun 13, 2023
2b3105d
Merge branch 'main' into absaroj/local_build
lalitb Jun 13, 2023
8151b28
adding typealias file to stop importing the objcmodule in clients (#1…
absaroj Jun 16, 2023
9b31864
Merge branch 'main' into absaroj/local_build
absaroj Jun 16, 2023
1167d04
Merge branch 'main' into absaroj/local_build
absaroj Jun 20, 2023
d8d8b4a
Merge branch 'main' into absaroj/local_build
absaroj Jun 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 99 additions & 26 deletions wrappers/swift/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
#######################################################################################################
#############################################################################################################
# Generates project files for Swift wrapper files and generates executable based on the main.swift file.
#
# Dependecies:
# Please build Obj-c dependencies required by Swift by running
# ./build.sh clean release
# in the source folder to generate the required Obj-C library. It should be installed at /usr/local/lib
#######################################################################################################
# in the source root folder to generate the required Obj-C library. It should be installed at /usr/local/lib.
#############################################################################################################

# ------------------------------------------------------------------------------------------------------------
# 1. Locate all the required static libs.
# 2. Run swift build to build swift wrappers package.
# 3. Combine all object files from swift wrappers to make a static lib.
# 4. Invoke swiftc compiler to compile the sample swift code while including the modules (wrappers and objc)
# and linking the static libs to generate the executable.
# ------------------------------------------------------------------------------------------------------------

CMAKE_MINIMUM_REQUIRED(VERSION 3.15)

PROJECT(Swift_Wrapper)
message("${CMAKE_CURRENT_SOURCE_DIR}")
enable_language(Swift)
# Set build and path related variables
string(TOLOWER ${CMAKE_BUILD_TYPE} LOWER_BUILD_TYPE)
set(PROJECT_NAME "OneDSSwift")
set(SWIFT_MODULE_PATH "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/${LOWER_BUILD_TYPE}")
set(SWIFT_OBJECT_OUTPUT_FOLDER "${SWIFT_MODULE_PATH}/${PROJECT_NAME}.build") # Swift object file output path: debug/OneDSSwift.build/*.o
set(SWIFT_STATIC_LIB "${CMAKE_BINARY_DIR}/StaticLibs/libonedsswift.a") # Putting newly created static libs in this folder
set(OBJC_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Modules")
set(INSTALLED_LIB_PATH "/usr/local/lib")

PROJECT(${PROJECT_NAME})

# Set flags and paths
# Set flags
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} -import-objc-header ObjCModule-Bridging-Header.h")
set(INSTALLED_LIB_PATH "/usr/local/lib")

# Add system libs
find_library(SYSTEM_CONFIGURATION SystemConfiguration)
find_package(ZLIB REQUIRED)
find_library(NETWORK_FRAMEWORK Network)

# Add libmat.a library which has Obj-C dependencies built needed by Swift
# Add libmat.a library which has Obj-C dependencies built needed by Swift wrappers
find_library(MAT_LIB libmat.a PATHS ${INSTALLED_LIB_PATH})
find_library(SQLITE3_LIB libsqlite3.a PATH ${INSTALLED_LIB_PATH})

# Check and bail out if required libs don't exist
if(NOT SQLITE3_LIB)
set(SQLITE3_LIB "sqlite3")
endif()
Expand All @@ -35,19 +49,78 @@ if(NOT MAT_LIB)
message(FATAL_ERROR, "libmat.a not found at /usr/local/lib. Please run ./build.sh at root and install libmat.a.")
endif()

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "dir='${dir}'")
endforeach()

# Add source files to the target
file(GLOB SWIFT_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.swift")
add_executable(swift_sample ${SWIFT_FILES})

target_link_libraries(swift_sample
${MAT_LIB}
${SYSTEM_CONFIGURATION}
${SQLITE3_LIB}
stdc++
ZLIB::ZLIB
${NETWORK_FRAMEWORK})
# --------------------------------------------
# Swift wrappers: Build and generate static lib
# --------------------------------------------

# Run swift build to compile and generate object files for swift
message("\nRunning swift build...")
execute_process(
COMMAND swift build
-c ${LOWER_BUILD_TYPE}
--scratch-path ${CMAKE_BINARY_DIR}/${PROJECT_NAME}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE SWIFT_BUILD_RESULT
)

if(SWIFT_BUILD_RESULT EQUAL 0)
message("Swift build succeeded!")

# Combine all the generated object files from swift code into static lib: lib onedsswift .a
# swift build does not generate a single static lib file
file(GLOB SWIFT_OBJECT_FILES "${SWIFT_OBJECT_OUTPUT_FOLDER}/*.o")
execute_process(
COMMAND ar rcs ${SWIFT_STATIC_LIB} ${SWIFT_OBJECT_FILES}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

if(EXISTS ${SWIFT_STATIC_LIB})
message("Swift wrapper static lib created at: ${SWIFT_STATIC_LIB}")
else()
message(FATAL_ERROR, "Swift wrapper static lib creation failed.")
endif()

else()
message(FATAL_ERROR, "Swift build failed with error code: ${SWIFT_BUILD_RESULT}")
endif()

# --------------------------
# End: Swift wrappers build
# --------------------------

# -------------------------------------------
# SwiftC: Invoke and generate sample_app exe
# -------------------------------------------

# Add swift files (outside of swift package) to the target
file(GLOB SWIFT_FILES "${CMAKE_CURRENT_SOURCE_DIR}/Sample/*.swift")

# Collect all libs to be linked into single variable
set(INCLUDE_LIBS
${MAT_LIB} # libmat.a contains ObjC symbols
${SWIFT_STATIC_LIB} # libonedsswift.a contains swift wrapper symbols
"-lC++"
)

# Collect all the modules to be included
set(INCLUDE_MODULES
"-I${OBJC_MODULE_DIR}" # Needed for Enums and others declared in ObjC headers
"-I${SWIFT_MODULE_PATH}" # Swift wrappers module OneDSSwift, generated after the build
)

# Invoke swiftc to create the exe
message("\nRunning swiftc...")
execute_process(
COMMAND swiftc
-o ${CMAKE_BINARY_DIR}/sample_app
${SWIFT_FILES} # Files to compile
${INCLUDE_MODULES} # Modules to include
${INCLUDE_LIBS} # Static libs to link
RESULT_VARIABLE SWIFTC_RESULT
)

if(EXISTS ${CMAKE_BINARY_DIR}/sample_app)
message("Executable is created at: ${CMAKE_BINARY_DIR}/sample_app")
else()
message(FATALError, "Executable creation failed.")
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// SPDX-License-Identifier: Apache-2.0
//

import OneDSSwift // Swift Package containing the wrappers over ObjC

let props = EventProperties(name:"TestEvent")
props.setProperty("PropName", withValue: ["Type":"SwiftWrappers"])
props.setProperty("PropWithPII", withInt64Value: Int64(30), withPiiKind: ODWPiiKind.distinguishedName)
print (props.properties)
print (props.piiTags)
props.setProperty("PropWithPII", withInt64Value: Int64(30), withPiiKind: PIIKind.distinguishedName)
print(props.properties())
4 changes: 2 additions & 2 deletions wrappers/swift/Sources/OneDSSwift/EventProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class EventProperties {
- name: Name of the event as a `String`.
*/
public init(name: String) {
self.odwEventProperties = ODWEventProperties(name: name)
odwEventProperties = ODWEventProperties(name: name)
}

/**
Expand Down Expand Up @@ -265,4 +265,4 @@ public final class EventProperties {
odwEventProperties.setProperty("EventInfo.PrivTags", withUInt64Value: UInt64(privTags.rawValue))
odwEventProperties.setProperty("EventInfo.Level", withUInt8Value: UInt8(privLevel.rawValue))
}
}
}
28 changes: 28 additions & 0 deletions wrappers/swift/Sources/OneDSSwift/ObjCTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//

/// Contains alias for the types declared in the ObjC header files to make them available
/// as part of the swift package module.
/// To avoid clients not have to import ObjCModule explicitly.

/// Check corresponding header file for the doc of each type.

import ObjCModule

// ODWEventProperties.h
public typealias PIIKind = ODWPiiKind
public typealias PrivacyDataType = ODWPrivacyDataType
public typealias DiagnosticLevel = ODWDiagLevel

// ODWLogger.h
public typealias TraceLevel = ODWTraceLevel
public typealias SessionState = ODWSessionState

// ODWLogManager.h
public typealias TransmissionProfile = ODWTransmissionProfile
public typealias FlushStatus = ODWStatus

// ODWPrivacyGuard.h
public typealias DataConcernType = ODWDataConcernType
21 changes: 18 additions & 3 deletions wrappers/swift/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
#!/bin/bash

export PATH=/usr/local/bin:$PATH
mkdir out

# Build flavor
BUILD_TYPE="debug"

if [ "$1" == "release" ]; then
BUILD_TYPE="$1"
fi

if [ "$1" == "clean" ] || [ "$2" == "clean" ]; then
# Clean the output folders
echo "Cleaning output folders..."
swift package clean
rm -Rf out
fi

# Generate the build files and run build
mkdir -p out/StaticLibs
cd out
cmake .. -G Xcode # Generate swift project files for xcode
xcodebuild -quiet
cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..