From 639d340f46ba30e8110e3f8be36129e7ceb38c1b Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Wed, 8 Mar 2023 20:54:30 +0000 Subject: [PATCH] builder: Setup the cache file for serene binary --- CMakeLists.txt | 1 + cmake/caches/serene_dev.cmake | 2 ++ serene/CMakeLists.txt | 47 +++++++++++++++++++++++++---------- serene/src/serene.cpp | 34 ++++++++++++++++++++++++- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0479d8e..131c678 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) -Wdouble-promotion -Wformat=2) + add_link_options(-fuse-ld=lld) # if ((SERENE_USE_LIBCXX) AND (NOT (SERENE_TOOLCHAIN_PATH))) # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -v") # set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi") diff --git a/cmake/caches/serene_dev.cmake b/cmake/caches/serene_dev.cmake index 11c5231..069ea0f 100644 --- a/cmake/caches/serene_dev.cmake +++ b/cmake/caches/serene_dev.cmake @@ -45,6 +45,8 @@ set(CMAKE_CXX_COMPILER "${SERENE_LLVM_BIN}/clang++" CACHE PATH "") # a mistake set(BDWgc_DIR "${SERENE_PKG_DIR}/bdwgc.${BDWGC_VERSION}") +set(MUSL_DIR "${SERENE_PKG_DIR}/musl.${MUSL_VERSION}/" CACHE PATH "") + set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "") set(SERENE_CCACHE_DIR "$ENV{HOME}/.ccache" CACHE STRING "") set(CMAKE_BUILD_TYPE "Debug") diff --git a/serene/CMakeLists.txt b/serene/CMakeLists.txt index 6140ee5..8b1b3f4 100644 --- a/serene/CMakeLists.txt +++ b/serene/CMakeLists.txt @@ -49,8 +49,7 @@ set_target_properties(serene PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} # Warn on unused libs LINK_WHAT_YOU_USE TRUE - # LTO support - INTERPROCEDURAL_OPTIMIZATION TRUE) +) if (CPP_20_SUPPORT) target_compile_features(serene PRIVATE cxx_std_20) @@ -64,9 +63,21 @@ target_include_directories(serene ${CMAKE_CURRENT_SOURCE_DIR}/include ) + # We don't want the generated files from table gen # to be treated as local since the contain warnings -target_include_directories(serene SYSTEM PRIVATE ${PROJECT_BINARY_DIR}/include) +target_include_directories(serene SYSTEM PRIVATE + ${PROJECT_BINARY_DIR}/include + # ${SERENE_LLVM_DIR}/include/x86_64-unknown-linux-gnu/c++/v1 + # ${SERENE_LLVM_DIR}/include/c++/v1 + #${MUSL_DIR}/include +) + +target_link_directories(serene + PRIVATE + # ${SERENE_LLVM_DIR}/lib + # ${MUSL_DIR}/lib +) # Autogenerate the `config.h` file configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/serene/config.h.in include/serene/config.h) @@ -74,9 +85,17 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/serene/config.h.in include/se target_compile_options(serene PRIVATE + -stdlib=libc++ # LLVM has it's own RTTI -fno-rtti -fno-builtin-strlen + # Don't link against system's default libc++ or libstdc++ + # -nostdinc++ + # -nostdlib++ + + # Do not link any default library, We want to be explicit + # -nodefaultlibs + # -nostdinc # Dedicate a section to each function, so the linker # can do a better job on dead code elimination @@ -106,14 +125,13 @@ target_compile_options(serene target_link_options(serene PRIVATE - -fuse-ld=lld - -Wl,-gc-sections - + -stdlib=libc++ + -lc++abi + -Wl,--gc-sections $<$:-s> $<$:-fsanitize=address> $<$:-static-libsan> - # Do not link against shared libraries --static ) @@ -126,15 +144,18 @@ if(SERENE_ENABLE_TIDY) set_target_properties(serene PROPERTIES CXX_CLANG_TIDY ${CLANG_TIDY_PATH}) endif() - - -# TODO: Setup the THINLTO on release if(SERENE_ENABLE_THINLTO) + include(CheckIPOSupported) + # Optional IPO. Do not use IPO if it's not supported by compiler. + check_ipo_supported(RESULT result OUTPUT output) + if(result) + set_property(TARGET serene PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + else() + message(WARNING "IPO is not supported: ${output}") + message(WARNING "Make sure to use lld") + endif() endif() -# target_link_libraries(serene -# PRIVATE -# ) include(GNUInstallDirs) diff --git a/serene/src/serene.cpp b/serene/src/serene.cpp index 4c63786..1878c78 100644 --- a/serene/src/serene.cpp +++ b/serene/src/serene.cpp @@ -16,4 +16,36 @@ * along with this program. If not, see . */ -int main() { return 0; } + +#include +#include +#include + +struct Vec3 +{ + int x, y, z; + + // following constructor is no longer needed since C++20 + Vec3(int x = 0, int y = 0, int z = 0) noexcept : x(x), y(y), z(z) { } + + friend std::ostream& operator<<(std::ostream& os, const Vec3& v) + { + return os << "{ x=" << v.x << ", y=" << v.y << ", z=" << v.z << " }"; + } +}; + +int main() +{ + // Use the default constructor. + std::unique_ptr v1 = std::make_unique(); + // Use the constructor that matches these arguments + std::unique_ptr v2 = std::make_unique(0,1,2); + // Create a unique_ptr to an array of 5 elements + std::unique_ptr v3 = std::make_unique(5); + + std::cout << "make_unique(): " << *v1 << '\n' + << "make_unique(0,1,2): " << *v2 << '\n' + << "make_unique(5): "; + for (int i = 0; i < 5; i++) + std::cout << std::setw(i ? 30 : 0) << v3[static_cast(i)] << '\n'; +}