From b220f119ceb35be21e12d067f1980af7a63c704e Mon Sep 17 00:00:00 2001 From: ClausKlein Date: Wed, 10 Jul 2024 15:00:37 +0200 Subject: [PATCH] Use import std on macos too --- CMakeLists.txt | 108 ++++++++++++++++++-------------- asio-module/AsioConfig.cmake.in | 37 ++++++----- asio-module/CMakeLists.txt | 11 +--- main.hpp | 2 +- 4 files changed, 85 insertions(+), 73 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ecd09c7..60799f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.28...3.30) +cmake_minimum_required(VERSION 3.28.4...3.30) project(modules_benchmark LANGUAGES CXX) find_package(OpenSSL REQUIRED) @@ -9,14 +9,30 @@ option(USE_MODULES "on/off" ON) # Always use libc++ set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_EXTENSIONS OFF) -add_compile_options(-stdlib=libc++ -ftime-trace) +add_compile_options(-stdlib=libc++) add_link_options(-stdlib=libc++) +set(LLVM_LIBC_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/stdlib/) + +if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28.4 AND CMAKE_GENERATOR STREQUAL "Ninja") + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 17.0) + set(USE_MODULE TRUE) + # see https://releases.llvm.org/15.0.0/projects/libcxx/docs/ReleaseNotes.html + add_compile_options(-fexperimental-library) + add_link_options(-lc++experimental) + add_compile_options(-ftime-trace) + if(APPLE) + set(LLVM_LIBC_SOURCE /usr/local/Cellar/llvm/18.1.8/share/libc++/v1/) + endif() + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 14.0) + set(USE_MODULE TRUE) + endif() +endif() # Disable module scanning if(USE_MODULES) - set(CMAKE_CXX_SCAN_FOR_MODULES ON) + set(CMAKE_CXX_SCAN_FOR_MODULES ON) else() - set(CMAKE_CXX_SCAN_FOR_MODULES OFF) + set(CMAKE_CXX_SCAN_FOR_MODULES OFF) endif() if(PROJECT_IS_TOP_LEVEL) @@ -32,65 +48,65 @@ elseif(ASIO_HAS_SNPRINTF) list(APPEND CPPdefinitions ASIO_HAS_SNPRINTF) endif() - # Build the stdlib module -function (add_stdlib_module NAME) - add_library(${NAME}) - target_sources(${NAME} PUBLIC - FILE_SET CXX_MODULES FILES - stdlib/std.cppm - stdlib/std.compat.cppm - ) - target_compile_features(${NAME} PUBLIC cxx_std_23) - target_compile_definitions(${NAME} PUBLIC _LIBCPP_HAS_NO_LOCALIZATION) - target_compile_options(${NAME} PRIVATE -Wno-reserved-module-identifier) +function(add_stdlib_module NAME) + add_library(${NAME}) + # cmake-format: off + target_sources(${NAME} PUBLIC + FILE_SET CXX_MODULES + BASE_DIRS ${LLVM_LIBC_SOURCE} + FILES + ${LLVM_LIBC_SOURCE}std.cppm + ${LLVM_LIBC_SOURCE}std.compat.cppm + ) + # cmake-format: on + target_compile_features(${NAME} PUBLIC cxx_std_23) + target_compile_definitions(${NAME} PUBLIC _LIBCPP_HAS_NO_LOCALIZATION) + target_compile_options(${NAME} PRIVATE -Wno-reserved-module-identifier) endfunction() if(USE_MODULES) - # Find the Asio package containing the function to build the module - find_package(Asio REQUIRED HINTS stagedir/lib) + # Find the Asio package containing the function to build the module + find_package(Asio REQUIRED HINTS stagedir/lib) - # Build the stdlib module - if(NOT APPLE) - add_stdlib_module(stdlib) - endif() + # Build the stdlib module + add_stdlib_module(stdlib) - # Build the asio module - add_asio_module(asio) + # Build the asio module + add_asio_module(asio) else() - # Asio interface library - add_library(asio INTERFACE) - target_compile_definitions(asio INTERFACE ${CPPdefinitions}) - target_include_directories(asio INTERFACE asio-module/include) - target_compile_features(asio INTERFACE cxx_std_23) - target_link_libraries(asio INTERFACE OpenSSL::SSL OpenSSL::Crypto) + # Asio interface library + add_library(asio INTERFACE) + target_compile_definitions(asio INTERFACE ${CPPdefinitions}) + target_include_directories(asio INTERFACE asio-module/include) + target_compile_features(asio INTERFACE cxx_std_23) + target_link_libraries(asio INTERFACE OpenSSL::SSL OpenSSL::Crypto) endif() # Executable -add_executable(server main.cpp main.hpp - main2.cpp - main3.cpp - main4.cpp - main5.cpp - main6.cpp - main7.cpp +add_executable( + server + main.cpp + main.hpp + main2.cpp + main3.cpp + main4.cpp + main5.cpp + main6.cpp + main7.cpp ) target_compile_features(server PRIVATE cxx_std_23) if(USE_MODULES) - if(APPLE) - target_link_libraries(server PRIVATE asio) - else() - target_link_libraries(server PRIVATE stdlib asio) - endif() - target_compile_definitions(server PRIVATE USE_MODULES) - #XXX target_compile_options(server PRIVATE -Wno-deprecated-declarations) + target_link_libraries(server PRIVATE stdlib asio) + target_compile_definitions(server PRIVATE USE_MODULES) + #XXX target_compile_options(server PRIVATE -Wno-deprecated-declarations) else() - target_link_libraries(server PRIVATE asio) + target_link_libraries(server PRIVATE asio) endif() enable_testing() if(APPLE) - add_test(NAME server COMMAND dyld_info server) + add_test(NAME server COMMAND dyld_info server) else() - add_test(NAME server COMMAND ldd server) + add_test(NAME server COMMAND ldd server) endif() diff --git a/asio-module/AsioConfig.cmake.in b/asio-module/AsioConfig.cmake.in index e29ae38..f4d5bdf 100644 --- a/asio-module/AsioConfig.cmake.in +++ b/asio-module/AsioConfig.cmake.in @@ -1,22 +1,25 @@ - include(CMakeFindDependencyMacro) find_dependency(OpenSSL) -function (add_asio_module NAME) - set(ASIO_ROOT @CMAKE_INSTALL_PREFIX@) - message("Root is: ${ASIO_ROOT}") - add_library(${NAME}) - target_include_directories(${NAME} PRIVATE ${ASIO_ROOT}/include) - target_compile_features(${NAME} PUBLIC cxx_std_23) - target_compile_options(${NAME} PRIVATE -Wno-include-angled-in-module-purview) - if(DEFINED CPPdefinitions) - target_compile_definitions(${NAME} PUBLIC ${CPPdefinitions}) - endif() - target_sources(${NAME} - PUBLIC - FILE_SET modules_public TYPE CXX_MODULES BASE_DIRS ${ASIO_ROOT} FILES - ${ASIO_ROOT}/module/asio.cxx - ) - target_link_libraries(${NAME} PUBLIC OpenSSL::SSL OpenSSL::Crypto) +function(add_asio_module NAME) + set(ASIO_ROOT @CMAKE_INSTALL_PREFIX@) + message("Root is: ${ASIO_ROOT}") + add_library(${NAME}) + target_include_directories(${NAME} PRIVATE ${ASIO_ROOT}/include) + target_compile_features(${NAME} PUBLIC cxx_std_23) + target_compile_options(${NAME} PRIVATE -Wno-include-angled-in-module-purview) + if(DEFINED CPPdefinitions) + target_compile_definitions(${NAME} PUBLIC ${CPPdefinitions}) + endif() + # cmake-format: off + target_sources(${NAME} PUBLIC + FILE_SET modules_public + TYPE CXX_MODULES + BASE_DIRS ${ASIO_ROOT} + FILES + ${ASIO_ROOT}/module/asio.cxx + ) + # cmake-format: on + target_link_libraries(${NAME} PUBLIC OpenSSL::SSL OpenSSL::Crypto) endfunction() diff --git a/asio-module/CMakeLists.txt b/asio-module/CMakeLists.txt index 4525cce..3ae8b5e 100644 --- a/asio-module/CMakeLists.txt +++ b/asio-module/CMakeLists.txt @@ -6,11 +6,7 @@ install(DIRECTORY include DESTINATION .) install(DIRECTORY module DESTINATION .) include(CMakePackageConfigHelpers) -write_basic_package_version_file( - AsioConfigVersion.cmake - VERSION 1.0.0 - COMPATIBILITY AnyNewerVersion -) +write_basic_package_version_file(AsioConfigVersion.cmake VERSION 1.0.0 COMPATIBILITY AnyNewerVersion) # see https://discourse.cmake.org/t/exit-with-error-if-user-attempt-to-override-cmake-install-prefix-during-installation/10020/3 # if(NOT CMAKE_INSTALL_PREFIX STREQUAL "@CMAKE_INSTALL_PREFIX@") @@ -18,7 +14,4 @@ write_basic_package_version_file( # endif() configure_file(AsioConfig.cmake.in AsioConfig.cmake @ONLY) -install( - FILES ${CMAKE_CURRENT_BINARY_DIR}/AsioConfig.cmake - DESTINATION lib/cmake/Asio -) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/AsioConfig.cmake DESTINATION lib/cmake/Asio) diff --git a/main.hpp b/main.hpp index 5a99c08..d687a8f 100644 --- a/main.hpp +++ b/main.hpp @@ -1,5 +1,5 @@ #ifdef USE_MODULES -# ifndef __APPLE__ +# ifndef XXX__APPLE__ import std; import std.compat; # else