Skip to content

Commit 63ec702

Browse files
authored
Merge pull request #9 from yim-lee/cmake
Set up CMake build
2 parents a79b838 + 80f3571 commit 63ec702

File tree

10 files changed

+338
-4
lines changed

10 files changed

+338
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ Package.resolved
1111
DerivedData
1212
.swiftpm
1313
**/gyb.pyc
14+
/out
1415

1516
.*.sw[nop]

CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftASN1 open source project
4+
##
5+
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
cmake_minimum_required(VERSION 3.15.1)
16+
17+
project(SwiftASN1
18+
LANGUAGES Swift)
19+
20+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
21+
22+
include(SwiftSupport)
23+
24+
if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
25+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
26+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
27+
else()
28+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
29+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
30+
endif()
31+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
32+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
33+
34+
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
35+
36+
if(BUILD_SHARED_LIBS)
37+
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
38+
endif()
39+
40+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
41+
find_package(Foundation CONFIG)
42+
endif()
43+
44+
add_subdirectory(Sources)
45+
add_subdirectory(cmake/modules)

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ let package = Package(
2424
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
2525
],
2626
targets: [
27-
.target(name: "SwiftASN1"),
27+
.target(
28+
name: "SwiftASN1",
29+
exclude: ["CMakeLists.txt"]
30+
),
2831
.testTarget(name: "SwiftASN1Tests", dependencies: ["SwiftASN1"]),
2932
]
3033
)

Sources/CMakeLists.txt

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

Sources/SwiftASN1/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftASN1 open source project
4+
##
5+
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
add_library(SwiftASN1
16+
"ASN1.swift"
17+
"Basic ASN1 Types/ASN1Any.swift"
18+
"Basic ASN1 Types/ASN1BitString.swift"
19+
"Basic ASN1 Types/ASN1Boolean.swift"
20+
"Basic ASN1 Types/ASN1Identifier.swift"
21+
"Basic ASN1 Types/ASN1Integer.swift"
22+
"Basic ASN1 Types/ASN1Null.swift"
23+
"Basic ASN1 Types/ASN1OctetString.swift"
24+
"Basic ASN1 Types/ASN1Strings.swift"
25+
"Basic ASN1 Types/ArraySliceBigint.swift"
26+
"Basic ASN1 Types/GeneralizedTime.swift"
27+
"Basic ASN1 Types/ObjectIdentifier.swift"
28+
"Basic ASN1 Types/PEMDocument.swift"
29+
"Basic ASN1 Types/TimeUtilities.swift"
30+
"Basic ASN1 Types/UTCTime.swift"
31+
"Errors.swift")
32+
33+
target_link_libraries(SwiftASN1 PUBLIC
34+
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
35+
set_target_properties(SwiftASN1 PROPERTIES
36+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
37+
38+
_install_target(SwiftASN1)
39+
set_property(GLOBAL APPEND PROPERTY SWIFT_ASN1_EXPORTS SwiftASN1)

cmake/modules/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftASN1 open source project
4+
##
5+
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
set(SWIFT_ASN1_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftASN1Exports.cmake)
16+
configure_file(SwiftASN1Config.cmake.in
17+
${CMAKE_CURRENT_BINARY_DIR}/SwiftASN1Config.cmake)
18+
19+
get_property(SWIFT_ASN1_EXPORTS GLOBAL PROPERTY SWIFT_ASN1_EXPORTS)
20+
export(TARGETS ${SWIFT_ASN1_EXPORTS} FILE ${SWIFT_ASN1_EXPORTS_FILE})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftASN1 open source project
4+
##
5+
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
if(NOT TARGET SwiftASN1)
16+
include(@SWIFT_ASN1_EXPORTS_FILE@)
17+
endif()

cmake/modules/SwiftSupport.cmake

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

scripts/soundness.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
##
44
## This source file is part of the SwiftASN1 open source project
55
##
6-
## Copyright (c) 2022 Apple Inc. and the SwiftASN1 project authors
6+
## Copyright (c) 2022-2023 Apple Inc. and the SwiftASN1 project authors
77
## Licensed under Apache License v2.0
88
##
99
## See LICENSE.txt for license information
@@ -31,7 +31,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
3131

3232
function replace_acceptable_years() {
3333
# this needs to replace all acceptable forms with 'YEARS'
34-
sed -e 's/20[12][789012]-20[12][89012]/YEARS/' -e 's/20[12][89012]/YEARS/'
34+
sed -e 's/20[12][789012]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/'
3535
}
3636

3737
printf "=> Checking for unacceptable language... "
@@ -53,10 +53,20 @@ if git grep --color=never -i "${unacceptable_terms[@]}" -- . ":(exclude)CODE_OF_
5353
fi
5454
printf "\033[0;32mokay.\033[0m\n"
5555

56+
printf "=> Detecting changes in source files for CMake build\n"
57+
FIRST_OUT="$(git status --porcelain)"
58+
out=$($here/update_cmakelists.sh 2>&1)
59+
SECOND_OUT="$(git status --porcelain)"
60+
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
61+
printf "\033[0;31mThere are source file changes! Have you added or renamed source files? Or did you forget to run 'update_cmakelists.sh' and commit changes?\033[0m\n"
62+
exit 1
63+
fi
64+
printf "\033[0;32mokay.\033[0m\n"
65+
5666
printf "=> Checking license headers\n"
5767
tmp=$(mktemp /tmp/.swift-asn1-soundness_XXXXXX)
5868

59-
for language in swift-or-c bash dtrace python; do
69+
for language in swift-or-c bash dtrace python cmake; do
6070
printf " * $language... "
6171
declare -a matching_files
6272
declare -a exceptions
@@ -137,6 +147,24 @@ EOF
137147
* SPDX-License-Identifier: Apache-2.0
138148
*
139149
*===----------------------------------------------------------------------===*/
150+
EOF
151+
;;
152+
cmake)
153+
matching_files=( -name 'SwiftSupport.cmake' -o -name 'CMakeLists.txt' )
154+
cat > "$tmp" <<"EOF"
155+
##===----------------------------------------------------------------------===##
156+
##
157+
## This source file is part of the SwiftASN1 open source project
158+
##
159+
## Copyright (c) YEARS Apple Inc. and the SwiftASN1 project authors
160+
## Licensed under Apache License v2.0
161+
##
162+
## See LICENSE.txt for license information
163+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
164+
##
165+
## SPDX-License-Identifier: Apache-2.0
166+
##
167+
##===----------------------------------------------------------------------===##
140168
EOF
141169
;;
142170
*)

scripts/update_cmakelists.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftASN1 open source project
5+
##
6+
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
set -eu
17+
18+
here=$(pwd)
19+
20+
case "$(uname -s)" in
21+
Darwin)
22+
find=gfind # brew install findutils
23+
;;
24+
*)
25+
find=find
26+
;;
27+
esac
28+
29+
function update_cmakelists_source() {
30+
src_root="$here/Sources/$1"
31+
32+
# Build an array with the rest of the arguments
33+
shift
34+
src_exts=("$@")
35+
echo "Finding source files (${src_exts[@]}) under $src_root"
36+
37+
num_exts=${#src_exts[@]}
38+
39+
# Build file extensions argument for `find`
40+
declare -a exts_arg
41+
exts_arg+=(-name "${src_exts[0]}")
42+
for (( i=1; i<$num_exts; i++ ));
43+
do
44+
exts_arg+=(-o -name "${src_exts[$i]}")
45+
done
46+
47+
# Wrap quotes around each filename since it might contain spaces
48+
srcs=$($find "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort)
49+
echo "$srcs"
50+
51+
# Update list of source files in CMakeLists.txt
52+
# The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line;
53+
# the second is so that we can pass in a variable to replace with.
54+
perl -pi -e 'BEGIN { undef $/; $replace = shift } s/add_library\(([^\n]+)\n([^\)]+)/add_library\($1\n$replace/' "$srcs" "$src_root/CMakeLists.txt"
55+
echo "Updated $src_root/CMakeLists.txt"
56+
}
57+
58+
update_cmakelists_source "SwiftASN1" "*.swift"

0 commit comments

Comments
 (0)