From 67bbf646d886d67ccda4d4ed1ca815efa83c7b39 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 15 Apr 2021 18:59:19 -0700 Subject: [PATCH 1/2] build: add a CMake based build for Atomics This adds an initial implementation of a CMake based build system. --- CMakeLists.txt | 37 ++++++++ Sources/Atomics/CMakeLists.txt | 31 ++++++ Sources/CMakeLists.txt | 11 +++ Sources/_AtomicsShims/CMakeLists.txt | 19 ++++ .../_AtomicsShims/include/module.modulemap | 3 + cmake/modules/SwiftSupport.cmake | 94 +++++++++++++++++++ 6 files changed, 195 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Sources/Atomics/CMakeLists.txt create mode 100644 Sources/CMakeLists.txt create mode 100644 Sources/_AtomicsShims/CMakeLists.txt create mode 100644 Sources/_AtomicsShims/include/module.modulemap create mode 100644 cmake/modules/SwiftSupport.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ad522f4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2021 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +cmake_minimum_required(VERSION 3.16) +project(swift-atomics + LANGUAGES C Swift) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules) + +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) +set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) + +if(CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin) + option(BUILD_SHARED_LIBS "Build shared libraries by default" YES) +endif() + +include(CTest) +include(SwiftSupport) + +add_subdirectory(Sources) +# if(BUILD_TESTING) +# add_subdirectory(Tests) +# endif() + +get_property(SWIFT_ATOMICS_EXPORTS GLOBAL PROPERTY SWIFT_ATOMICS_EXPORTS) +export(TARGETS ${SWIFT_ATOMICS_EXPORTS} + NAMESPACE SwiftAtomics:: + FILE swift-atomics-config.cmake + EXPORT_LINK_INTERFACE_LIBRARIES) diff --git a/Sources/Atomics/CMakeLists.txt b/Sources/Atomics/CMakeLists.txt new file mode 100644 index 0000000..abb5250 --- /dev/null +++ b/Sources/Atomics/CMakeLists.txt @@ -0,0 +1,31 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2021 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(Atomics + autogenerated/AtomicBool.swift + autogenerated/AtomicLazyReference.swift + autogenerated/HighLevelTypes.swift + autogenerated/IntegerConformances.swift + autogenerated/PointerConformances.swift + AtomicInteger.swift + AtomicMemoryOrderings.swift + AtomicOptional.swift + AtomicRawRepresentable.swift + AtomicStrongReference.swift + AtomicValue.swift + DoubleWord.swift) +set_target_properties(Atomics PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) +target_compile_definitions(Atomics PRIVATE + ENABLE_DOUBLEWIDE_ATOMICS) +target_link_libraries(Atomics PUBLIC + _AtomicsShims) + +_install_target(Atomics) +set_property(GLOBAL APPEND PROPERTY SWIFT_ATOMICS_EXPORTS Atomics) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt new file mode 100644 index 0000000..d37b0ab --- /dev/null +++ b/Sources/CMakeLists.txt @@ -0,0 +1,11 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2020 Apple Inc. and the Swift Numerics project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_subdirectory(_AtomicsShims) +add_subdirectory(Atomics) diff --git a/Sources/_AtomicsShims/CMakeLists.txt b/Sources/_AtomicsShims/CMakeLists.txt new file mode 100644 index 0000000..daf1a52 --- /dev/null +++ b/Sources/_AtomicsShims/CMakeLists.txt @@ -0,0 +1,19 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2021 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +add_library(_AtomicsShims STATIC + src/_AtomicsShims.c) +target_include_directories(_AtomicsShims PUBLIC + include) +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64") + target_compile_options(_AtomicsShims PUBLIC + "$<$:SHELL:-Xcc -mcx16>") +endif() + +set_property(GLOBAL APPEND PROPERTY SWIFT_ATOMICS_EXPORTS _AtomicsShims) diff --git a/Sources/_AtomicsShims/include/module.modulemap b/Sources/_AtomicsShims/include/module.modulemap new file mode 100644 index 0000000..9155f5d --- /dev/null +++ b/Sources/_AtomicsShims/include/module.modulemap @@ -0,0 +1,3 @@ +module _AtomicsShims { + header "_AtomicsShims.h" +} diff --git a/cmake/modules/SwiftSupport.cmake b/cmake/modules/SwiftSupport.cmake new file mode 100644 index 0000000..900f4c0 --- /dev/null +++ b/cmake/modules/SwiftSupport.cmake @@ -0,0 +1,94 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2021 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +# 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 "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() + + 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) +endfunction() From 907a24f5f288b8d601cb1a5400cf2b771e7be8d2 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 29 Apr 2021 14:41:55 -0700 Subject: [PATCH 2/2] Add the ability to run tests via CMake This adds the ability to run the test suite via CMake + Ninja. --- CMakeLists.txt | 6 +++--- Tests/CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Tests/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index ad522f4..807c9b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,9 +26,9 @@ include(CTest) include(SwiftSupport) add_subdirectory(Sources) -# if(BUILD_TESTING) -# add_subdirectory(Tests) -# endif() +if(BUILD_TESTING) + add_subdirectory(Tests) +endif() get_property(SWIFT_ATOMICS_EXPORTS GLOBAL PROPERTY SWIFT_ATOMICS_EXPORTS) export(TARGETS ${SWIFT_ATOMICS_EXPORTS} diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..35ead34 --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,33 @@ +#[[ +This source file is part of the Swift Atomics Open Source Project + +Copyright (c) 2021 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin) + find_package(dispatch CONFIG) + find_package(Foundation CONFIG) +endif() +find_package(XCTest CONFIG) + +add_executable(AtomicsTestBundle + AtomicsTests/main.swift + AtomicsTests/autogenerated/Basics.swift + AtomicsTests/DoubleWord.swift + AtomicsTests/LifetimeTracked.swift + AtomicsTests/LockFreeQueue.swift + AtomicsTests/LockFreeSingleConsumerStack.swift + AtomicsTests/StrongReferenceRace.swift + AtomicsTests/StrongReferenceShuffle.swift + AtomicsTests/UnsafeAtomicLazyReference.swift) +target_link_libraries(AtomicsTestBundle PRIVATE + Atomics + XCTest + $<$>:dispatch> + $<$>:Foundation>) + +add_test(NAME SwiftAtomics + COMMAND AtomicsTestBundle)