Skip to content

Commit 19e18a6

Browse files
yim-leecompnerd
authored andcommitted
Add CMake build files
Motivation: This is to support swiftlang/swift-package-manager#3202, which adds swift-crypto as a dependency to SwiftPM. Modifications: Add CMake files. Result: Can build swift-crypto with cmake.
1 parent 79542a8 commit 19e18a6

File tree

9 files changed

+298
-0
lines changed

9 files changed

+298
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ DerivedData
1313
**/gyb.pyc
1414

1515
.*.sw[nop]
16+
17+
CMakeFiles
18+
CMakeCache.txt
19+
cmake_*.cmake
20+
*.ninja

CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
cmake_minimum_required(VERSION 3.15.1)
12+
13+
project(swift-crypto
14+
LANGUAGES C Swift)
15+
16+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
17+
18+
include(SwiftSupport)
19+
20+
if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
21+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
22+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
23+
else()
24+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
25+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
26+
endif()
27+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
28+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
29+
30+
add_subdirectory(Sources)
31+
add_subdirectory(cmake/modules)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
add_library(CCryptoBoringSSL INTERFACE)
12+
13+
target_include_directories(CCryptoBoringSSL INTERFACE
14+
include)
15+
16+
set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSL)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
add_library(CCryptoBoringSSLShims STATIC shims.c)
12+
13+
target_include_directories(CCryptoBoringSSLShims PUBLIC
14+
include)
15+
16+
target_link_libraries(CCryptoBoringSSLShims PUBLIC
17+
CCryptoBoringSSL)
18+
19+
set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS CCryptoBoringSSLShims)

Sources/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
add_subdirectory(CCryptoBoringSSL)
12+
add_subdirectory(CCryptoBoringSSLShims)
13+
add_subdirectory(Crypto)

Sources/Crypto/CMakeLists.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
add_library(Crypto
12+
AEADs/AES/GCM/AES-GCM.swift
13+
AEADs/AES/GCM/BoringSSL/AES-GCM_boring.swift
14+
AEADS/ChachaPoly/ChaChaPoly.swift
15+
AEADS/ChachaPoly/BoringSSL/ChaChaPoly_boring.swift
16+
AEADS/Cipher.swift
17+
AEADS/Nonces.swift
18+
ASN1/ASN1.swift
19+
"ASN1/Basic ASN1 Types/ArraySliceBigint.swift"
20+
"ASN1/Basic ASN1 Types/ASN1BitString.swift"
21+
"ASN1/Basic ASN1 Types/ASN1Identifier.swift"
22+
"ASN1/Basic ASN1 Types/ASN1Integer.swift"
23+
"ASN1/Basic ASN1 Types/ASN1OctetString.swift"
24+
"ASN1/Basic ASN1 Types/ObjectIdentifier.swift"
25+
ASN1/ECDSASignature.swift
26+
ASN1/PEMDocument.swift
27+
ASN1/PKCS8PrivateKey.swift
28+
ASN1/SEC1PrivateKey.swift
29+
ASN1/SubjectPublicKeyInfo.swift
30+
CryptoKitErrors.swift
31+
Digests/BoringSSL/Digest_boring.swift
32+
Digests/Digest.swift
33+
Digests/Digests.swift
34+
Digests/HashFunctions_SHA2.swift
35+
Digests/HashFunctions.swift
36+
Insecure/Insecure_HashFunctions.swift
37+
Insecure/Insecure.swift
38+
"Key Agreement/BoringSSL/ECDH_boring.swift"
39+
"Key Agreement/DH.swift"
40+
"Key Agreement/ECDH.swift"
41+
"Key Derivation/HKDF.swift"
42+
Keys/EC/BoringSSL/Ed25519_boring.swift
43+
Keys/EC/BoringSSL/EllipticCurve_boring.swift
44+
Keys/EC/BoringSSL/EllipticCurvePoint_boring.swift
45+
Keys/EC/BoringSSL/NISTCurvesKeys_boring.swift
46+
Keys/EC/BoringSSL/X25519Keys_boring.swift
47+
Keys/EC/Curve25519.swift
48+
Keys/EC/Ed25519.swift
49+
Keys/EC/NISTCurvesKeys.swift
50+
Keys/EC/X25519Keys.swift
51+
Keys/Symmetric/SymmetricKeys.swift
52+
"Message Authentication Codes/HMAC/HMAC.swift"
53+
"Message Authentication Codes/MACFunctions.swift"
54+
"Message Authentication Codes/MessageAuthenticationCode.swift"
55+
PRF/AES.swift
56+
Signatures/BoringSSL/ECDSA_boring.swift
57+
Signatures/BoringSSL/ECDSASignature_boring.swift
58+
Signatures/BoringSSL/EdDSA_boring.swift
59+
Signatures/ECDSA.swift
60+
Signatures/EdDSA.swift
61+
Signatures/Signature.swift
62+
Util/BoringSSL/ArbitraryPrecisionInteger_boring.swift
63+
Util/BoringSSL/CryptoKitErrors_boring.swift
64+
Util/BoringSSL/FiniteFieldArithmeticContext_boring.swift
65+
Util/BoringSSL/RNG_boring.swift
66+
Util/BoringSSL/SafeCompare_boring.swift
67+
Util/BoringSSL/Zeroization_boring.swift
68+
Util/PrettyBytes.swift
69+
Util/SafeCompare.swift
70+
Util/SecureBytes.swift
71+
Util/Zeroization.swift)
72+
73+
target_link_libraries(Crypto PUBLIC
74+
CCryptoBoringSSL
75+
CCryptoBoringSSLShims)
76+
77+
set_target_properties(Crypto PROPERTIES
78+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
79+
80+
_install_target(Crypto)
81+
set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS Crypto)

cmake/modules/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
set(CRYPTO_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/CryptoExports.cmake)
12+
configure_file(CryptoConfig.cmake.in
13+
${CMAKE_CURRENT_BINARY_DIR}/CryptoConfig.cmake)
14+
15+
get_property(CRYPTO_EXPORTS GLOBAL PROPERTY CRYPTO_EXPORTS)
16+
export(TARGETS ${CRYPTO_EXPORTS} FILE ${CRYPTO_EXPORTS_FILE})

cmake/modules/CryptoConfig.cmake.in

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
if(NOT TARGET Crypto)
12+
include(@CRYPTO_EXPORTS_FILE@)
13+
endif()

cmake/modules/SwiftSupport.cmake

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# This source file is part of the SwiftCrypto open source project
2+
#
3+
# Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
4+
# Licensed under Apache License v2.0
5+
#
6+
# See LICENSE.txt for license information
7+
# See CONTRIBUTORS.md for the list of SwiftCrypto project authors
8+
#
9+
# SPDX-License-Identifier: Apache-2.0
10+
11+
# Returns the architecture name in a variable
12+
#
13+
# Usage:
14+
# get_swift_host_arch(result_var_name)
15+
#
16+
# Sets ${result_var_name} with the converted architecture name derived from
17+
# CMAKE_SYSTEM_PROCESSOR.
18+
function(get_swift_host_arch result_var_name)
19+
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
20+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
21+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
22+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
23+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
24+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
25+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
26+
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
27+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
28+
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
29+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
30+
set("${result_var_name}" "s390x" PARENT_SCOPE)
31+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
32+
set("${result_var_name}" "armv6" PARENT_SCOPE)
33+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
34+
set("${result_var_name}" "armv7" PARENT_SCOPE)
35+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
36+
set("${result_var_name}" "armv7" PARENT_SCOPE)
37+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
38+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
39+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
40+
set("${result_var_name}" "itanium" PARENT_SCOPE)
41+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
42+
set("${result_var_name}" "i686" PARENT_SCOPE)
43+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
44+
set("${result_var_name}" "i686" PARENT_SCOPE)
45+
else()
46+
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
47+
endif()
48+
endfunction()
49+
50+
# Returns the os name in a variable
51+
#
52+
# Usage:
53+
# get_swift_host_os(result_var_name)
54+
#
55+
#
56+
# Sets ${result_var_name} with the converted OS name derived from
57+
# CMAKE_SYSTEM_NAME.
58+
function(get_swift_host_os result_var_name)
59+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
60+
set(${result_var_name} macosx PARENT_SCOPE)
61+
else()
62+
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
63+
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
64+
endif()
65+
endfunction()
66+
67+
function(_install_target module)
68+
get_swift_host_os(swift_os)
69+
get_target_property(type ${module} TYPE)
70+
71+
if(type STREQUAL STATIC_LIBRARY)
72+
set(swift swift_static)
73+
else()
74+
set(swift swift)
75+
endif()
76+
77+
install(TARGETS ${module}
78+
ARCHIVE DESTINATION lib/${swift}/${swift_os}
79+
LIBRARY DESTINATION lib/${swift}/${swift_os}
80+
RUNTIME DESTINATION bin)
81+
if(type STREQUAL EXECUTABLE)
82+
return()
83+
endif()
84+
85+
get_swift_host_arch(swift_arch)
86+
get_target_property(module_name ${module} Swift_MODULE_NAME)
87+
if(NOT module_name)
88+
set(module_name ${module})
89+
endif()
90+
91+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
92+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
93+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
94+
RENAME ${swift_arch}.swiftdoc)
95+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
96+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
97+
RENAME ${swift_arch}.swiftmodule)
98+
else()
99+
install(FILES
100+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
101+
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
102+
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
103+
endif()
104+
endfunction()

0 commit comments

Comments
 (0)