diff --git a/.gitignore b/.gitignore index e10443c..8061269 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,6 @@ Package.resolved DerivedData .swiftpm **/gyb.pyc +/out .*.sw[nop] diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fb5e8ea --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt new file mode 100644 index 0000000..e576336 --- /dev/null +++ b/Sources/CMakeLists.txt @@ -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) diff --git a/Sources/SwiftASN1/CMakeLists.txt b/Sources/SwiftASN1/CMakeLists.txt new file mode 100644 index 0000000..56db064 --- /dev/null +++ b/Sources/SwiftASN1/CMakeLists.txt @@ -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 + $<$>: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) diff --git a/cmake/modules/CMakeLists.txt b/cmake/modules/CMakeLists.txt new file mode 100644 index 0000000..2c862af --- /dev/null +++ b/cmake/modules/CMakeLists.txt @@ -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}) diff --git a/cmake/modules/SwiftASN1Config.cmake.in b/cmake/modules/SwiftASN1Config.cmake.in new file mode 100644 index 0000000..91e376f --- /dev/null +++ b/cmake/modules/SwiftASN1Config.cmake.in @@ -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() diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake new file mode 100644 index 0000000..4627630 --- /dev/null +++ b/cmake/modules/SwiftSupport.cmake @@ -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 $/${module_name}.swiftdoc + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftdoc) + install(FILES $/${module_name}.swiftmodule + DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule + RENAME ${swift_arch}.swiftmodule) + else() + install(FILES + $/${module_name}.swiftdoc + $/${module_name}.swiftmodule + DESTINATION lib/${swift}/${swift_os}/${swift_arch}) + endif() +endfunction() diff --git a/scripts/soundness.sh b/scripts/soundness.sh index 8679232..546692e 100755 --- a/scripts/soundness.sh +++ b/scripts/soundness.sh @@ -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) diff --git a/scripts/update_cmakelists.sh b/scripts/update_cmakelists.sh new file mode 100755 index 0000000..0120619 --- /dev/null +++ b/scripts/update_cmakelists.sh @@ -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"