Skip to content

Commit

Permalink
Use import std on macos too
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Jul 10, 2024
1 parent de8e1a7 commit b220f11
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 73 deletions.
108 changes: 62 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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()
37 changes: 20 additions & 17 deletions asio-module/AsioConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -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()
11 changes: 2 additions & 9 deletions asio-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ 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@")
# message(FATAL_ERROR "wrong CMAKE_INSTALL_PREFIX")
# 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)
2 changes: 1 addition & 1 deletion main.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifdef USE_MODULES
# ifndef __APPLE__
# ifndef XXX__APPLE__
import std;
import std.compat;
# else
Expand Down

0 comments on commit b220f11

Please sign in to comment.