Set up CMake build

This commit is contained in:
Yim Lee committed 2023-01-25 13:49:19 -08:00
1 parent a79b83898c
commit 0f873377cd
9 files changed
+315 -2

No files matched your search

+1
View File
@@ -11,5 +11,6 @@ Package.resolved
DerivedData
.swiftpm
**/gyb.pyc
/out
.*.sw[nop]
+45
View File
@@ -0,0 +1,45 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
cmake_minimum_required(VERSION 3.15.1)
project(SwiftASN1
LANGUAGES Swift)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
include(SwiftSupport)
if(CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
else()
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
if(BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
endif()
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
find_package(Foundation CONFIG)
endif()
add_subdirectory(Sources)
add_subdirectory(cmake/modules)
+15
View File
@@ -0,0 +1,15 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
add_subdirectory(SwiftASN1)
+39
View File
@@ -0,0 +1,39 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
add_library(SwiftASN1
"ASN1.swift"
"Basic ASN1 Types/ASN1Any.swift"
"Basic ASN1 Types/ASN1BitString.swift"
"Basic ASN1 Types/ASN1Boolean.swift"
"Basic ASN1 Types/ASN1Identifier.swift"
"Basic ASN1 Types/ASN1Integer.swift"
"Basic ASN1 Types/ASN1Null.swift"
"Basic ASN1 Types/ASN1OctetString.swift"
"Basic ASN1 Types/ASN1Strings.swift"
"Basic ASN1 Types/ArraySliceBigint.swift"
"Basic ASN1 Types/GeneralizedTime.swift"
"Basic ASN1 Types/ObjectIdentifier.swift"
"Basic ASN1 Types/PEMDocument.swift"
"Basic ASN1 Types/TimeUtilities.swift"
"Basic ASN1 Types/UTCTime.swift"
"Errors.swift")
target_link_libraries(SwiftASN1 PUBLIC
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
set_target_properties(SwiftASN1 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
_install_target(SwiftASN1)
set_property(GLOBAL APPEND PROPERTY SWIFT_ASN1_EXPORTS SwiftASN1)
+20
View File
@@ -0,0 +1,20 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set(SWIFT_ASN1_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftASN1Exports.cmake)
configure_file(SwiftASN1Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/SwiftASN1Config.cmake)
get_property(SWIFT_ASN1_EXPORTS GLOBAL PROPERTY SWIFT_ASN1_EXPORTS)
export(TARGETS ${SWIFT_ASN1_EXPORTS} FILE ${SWIFT_ASN1_EXPORTS_FILE})
+17
View File
@@ -0,0 +1,17 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
if(NOT TARGET SwiftASN1)
include(@SWIFT_ASN1_EXPORTS_FILE@)
endif()
+108
View File
@@ -0,0 +1,108 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
# Returns the architecture name in a variable
#
# Usage:
# get_swift_host_arch(result_var_name)
#
# Sets ${result_var_name} with the converted architecture name derived from
# CMAKE_SYSTEM_PROCESSOR.
function(get_swift_host_arch result_var_name)
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set("${result_var_name}" "x86_64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
set("${result_var_name}" "aarch64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set("${result_var_name}" "aarch64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
set("${result_var_name}" "s390x" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
set("${result_var_name}" "armv6" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
set("${result_var_name}" "armv7" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
set("${result_var_name}" "armv7" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
set("${result_var_name}" "x86_64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
set("${result_var_name}" "itanium" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
set("${result_var_name}" "i686" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
set("${result_var_name}" "i686" PARENT_SCOPE)
else()
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
endfunction()
# Returns the os name in a variable
#
# Usage:
# get_swift_host_os(result_var_name)
#
#
# Sets ${result_var_name} with the converted OS name derived from
# CMAKE_SYSTEM_NAME.
function(get_swift_host_os result_var_name)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(${result_var_name} macosx PARENT_SCOPE)
else()
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
endif()
endfunction()
function(_install_target module)
get_swift_host_os(swift_os)
get_target_property(type ${module} TYPE)
if(type STREQUAL STATIC_LIBRARY)
set(swift swift_static)
else()
set(swift swift)
endif()
install(TARGETS ${module}
ARCHIVE DESTINATION lib/${swift}/${swift_os}
LIBRARY DESTINATION lib/${swift}/${swift_os}
RUNTIME DESTINATION bin)
if(type STREQUAL EXECUTABLE)
return()
endif()
get_swift_host_arch(swift_arch)
get_target_property(module_name ${module} Swift_MODULE_NAME)
if(NOT module_name)
set(module_name ${module})
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
RENAME ${swift_arch}.swiftdoc)
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
RENAME ${swift_arch}.swiftmodule)
else()
install(FILES
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
$<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${swift_arch})
endif()
endfunction()
+12 -2
View File
@@ -3,7 +3,7 @@
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2022 Apple Inc. and the SwiftASN1 project authors
## Copyright (c) 2022-2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
@@ -31,7 +31,7 @@ here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/20[12][789012]-20[12][89012]/YEARS/' -e 's/20[12][89012]/YEARS/'
sed -e 's/20[12][789012]-20[12][890123]/YEARS/' -e 's/20[12][890123]/YEARS/'
}
printf "=> Checking for unacceptable language... "
@@ -53,6 +53,16 @@ if git grep --color=never -i "${unacceptable_terms[@]}" -- . ":(exclude)CODE_OF_
fi
printf "\033[0;32mokay.\033[0m\n"
printf "=> Detecting changes in source files for CMake build\n"
FIRST_OUT="$(git status --porcelain)"
out=$($here/update_cmakelists.sh 2>&1)
SECOND_OUT="$(git status --porcelain)"
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
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"
exit 1
fi
printf "\033[0;32mokay.\033[0m\n"
printf "=> Checking license headers\n"
tmp=$(mktemp /tmp/.swift-asn1-soundness_XXXXXX)
+58
View File
@@ -0,0 +1,58 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftASN1 open source project
##
## Copyright (c) 2023 Apple Inc. and the SwiftASN1 project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftASN1 project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
set -eu
here=$(pwd)
case "$(uname -s)" in
Darwin)
find=gfind # brew install findutils
;;
*)
find=find
;;
esac
function update_cmakelists_source() {
src_root="$here/Sources/$1"
# Build an array with the rest of the arguments
shift
src_exts=("$@")
echo "Finding source files (${src_exts[@]}) under $src_root"
num_exts=${#src_exts[@]}
# Build file extensions argument for `find`
declare -a exts_arg
exts_arg+=(-name "${src_exts[0]}")
for (( i=1; i<$num_exts; i++ ));
do
exts_arg+=(-o -name "${src_exts[$i]}")
done
# Wrap quotes around each filename since it might contain spaces
srcs=$($find "${src_root}" -type f \( "${exts_arg[@]}" \) -printf ' "%P"\n' | LC_ALL=POSIX sort)
echo "$srcs"
# Update list of source files in CMakeLists.txt
# The first part in `BEGIN` (i.e., `undef $/;`) is for working with multi-line;
# the second is so that we can pass in a variable to replace with.
perl -pi -e 'BEGIN { undef $/; $replace = shift } s/add_library\(([^\n]+)\n([^\)]+)/add_library\($1\n$replace/' "$srcs" "$src_root/CMakeLists.txt"
echo "Updated $src_root/CMakeLists.txt"
}
update_cmakelists_source "SwiftASN1" "*.swift"