Skip to content

Commit

Permalink
Merge pull request #126 from aminya/ [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya authored May 21, 2022
2 parents afc8099 + f2350ed commit 7e7b982
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 30 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ jobs:
compiler: "msvc"
cmake: 3.18.0
vcvarsall: false
exclude:
- os: "windows-2022"
compiler: "gcc"
cmake: true
vcvarsall: true
steps:
- uses: actions/checkout@v2
- name: Cache
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test/build
.cache
.cache/
test/build/
install/
test_install/build/
44 changes: 44 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Launch lldb",
"program": "${workspaceRoot}/test/build/Debug/main",
"args": [],
"cwd": "${workspaceRoot}"
},
{
"name": "Attach lldb",
"type": "lldb",
"request": "attach",
"pid": "${command:pickMyProcess}"
},
{
"name": "Launch gdb",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/test/build/Debug/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
]
}
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,24 @@ option(FEATURE_DOCS "Enable the docs" OFF)
set(ENABLE_CLANG_TIDY OFF)
set(ENABLE_CPPCHECK OFF)
set(ENABLE_SANITIZER_ADDRESS OFF)
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR OFF)
if(FEATURE_TESTS)
set(ENABLE_CLANG_TIDY "ENABLE_CLANG_TIDY")
set(ENABLE_CPPCHECK "ENABLE_CPPCHECK")
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if(# not windows
NOT
"${CMAKE_SYSTEM_NAME}"
STREQUAL
"Windows"
# or is MSVC and has run vcvarsall
OR (MSVC AND "${index_of_vs_install_dir}" STREQUAL "-1"))
if(NOT
"${CMAKE_SYSTEM_NAME}"
STREQUAL
"Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if(MSVC AND "${index_of_vs_install_dir}" STREQUAL "-1")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
endif()
endif()
endif()
Expand Down
7 changes: 7 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ words:
- msvc
- msys
- multilib
- municode
- mylib
- myprogram
- myproj
Expand Down Expand Up @@ -103,3 +104,9 @@ words:
- Wuseless
- xcrun
- Yahyaabadi
- "ARGN"
- "CMAKE"
- "fargs"
- "shlib"
- "vcpkg"
- cppdbg
24 changes: 24 additions & 0 deletions src/DetectCompiler.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# includes a separate CMakeLists.txt file to detect the CXX/C compilers before project is called
# Using a separate file ensures that the current scope is not contaminated by the variable
execute_process(
COMMAND cmake -S "${ProjectOptions_SRC_DIR}/detect_compiler" -B "${CMAKE_CURRENT_BINARY_DIR}/detect_compiler" -G
"${CMAKE_GENERATOR}" "--log-level=ERROR" "-Wno-dev" OUTPUT_QUIET)

# parse the detected compilers from the cache
set(cache_variables
CMAKE_CXX_COMPILER
CMAKE_CXX_COMPILER_ID
CMAKE_C_COMPILER
CMAKE_C_COMPILER_ID
CMAKE_SYSTEM_PROCESSOR
CMAKE_HOST_SYSTEM_PROCESSOR)
foreach(cache_var ${cache_variables})
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/detect_compiler/CMakeCache.txt" "DETECTED_${cache_var}"
REGEX "^${cache_var}:STRING=(.*)$")
string(
REGEX
REPLACE "^${cache_var}:STRING=(.*)$"
"\\1"
"DETECTED_${cache_var}"
"${DETECTED_${cache_var}}")
endforeach()
4 changes: 4 additions & 0 deletions src/Index.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include("${ProjectOptions_SRC_DIR}/Sanitizers.cmake")
include("${ProjectOptions_SRC_DIR}/Doxygen.cmake")
include("${ProjectOptions_SRC_DIR}/StaticAnalyzers.cmake")
include("${ProjectOptions_SRC_DIR}/VCEnvironment.cmake")
include("${ProjectOptions_SRC_DIR}/MinGW.cmake")

# find msvc on windows if required. Should be called before run_vcpkg and run_conan to be effective
find_msvc()
Expand Down Expand Up @@ -122,6 +123,9 @@ macro(project_options)
# Link this 'library' to set the c++ standard / compile-time options requested
add_library(project_options INTERFACE)

# fix mingw
mingw_unicode(project_options)

if(NOT
"${ProjectOptions_ENABLE_IPO}"
STREQUAL
Expand Down
105 changes: 105 additions & 0 deletions src/MinGW.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
include_guard()

# detect mingw
function(is_mingw value)
set(_value OFF)
if("${MINGW}" STREQUAL "True" OR (WIN32 AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"
OR "${DETECTED_CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")))
set(_value ON)
endif()
set(${value}
"${_value}"
PARENT_SCOPE)
endfunction()

# configure mingw toolchain for vcpkg
# if mingw, use the correct triplet (otherwise it will fail to link libraries)
macro(configure_mingw_vcpkg)
if(WIN32 AND NOT MSVC)

# detect mingw if not already done
if(NOT
"${MINGW}"
STREQUAL
"True")
include("${ProjectOptions_SRC_DIR}/DetectCompiler.cmake")
endif()

is_mingw(_is_mingw)
if(${_is_mingw})
include("${ProjectOptions_SRC_DIR}/Utilities.cmake")
detect_architecture(_arch)
string(TOLOWER "${_arch}" _arch)

# https://github.com/microsoft/vcpkg/blob/4b766c1cd17205e1b768c4fadfd5f867c1d0510e/scripts/buildsystems/vcpkg.cmake#L340
set(MINGW TRUE)

# https://github.com/microsoft/vcpkg/blob/7aa1a14c5f5707373b73e909ed6aa12b7bae8ee7/scripts/cmake/vcpkg_common_definitions.cmake#L54
set(VCPKG_CMAKE_SYSTEM_NAME
"MinGW"
CACHE STRING "")
set(VCPKG_TARGET_IS_MINGW
TRUE
CACHE STRING "")

# choose between static or dynamic
set(MINGW_LINKAGE)
if(BUILD_SHARED_LIBS)
set(MINGW_LINKAGE "dynamic")
message(
STATUS
"Enabled dynamic toolchain for mingw. Make sure that the mingw64/bin directory is on the PATH when running the final executables."
)
else()
set(MINGW_LINKAGE "static")
endif()

set(VCPKG_LIBRARY_LINKAGE
${MINGW_LINKAGE}
CACHE STRING "")
set(VCPKG_CRT_LINKAGE
${MINGW_LINKAGE}
CACHE STRING "")

# Based on the docs https://github.com/microsoft/vcpkg/blob/master/docs/users/mingw.md (but it doesn't work!)
set(VCPKG_DEFAULT_TRIPLET
"${_arch}-mingw-${MINGW_LINKAGE}"
CACHE STRING "Default triplet for vcpkg")
set(VCPKG_DEFAULT_HOST_TRIPLET
"${_arch}-mingw-${MINGW_LINKAGE}"
CACHE STRING "Default target triplet for vcpkg")
set($ENV{VCPKG_DEFAULT_TRIPLET} "${_arch}-mingw-${MINGW_LINKAGE}")
set($ENV{VCPKG_DEFAULT_HOST_TRIPLET} "${_arch}-mingw-${MINGW_LINKAGE}")
endif()
endif()
endmacro()

# corrects the mingw toolchain type after including the vcpkg toolchain
# Requires to be called in the same scope as configure_mingw_vcpkg()
macro(configure_mingw_vcpkg_after)
if(WIN32 AND MINGW)
set(Z_VCPKG_TARGET_TRIPLET_PLAT mingw-${MINGW_LINKAGE})

include("${ProjectOptions_SRC_DIR}/Utilities.cmake")
detect_architecture(_arch)
string(TOLOWER "${_arch}" _arch)
set(Z_VCPKG_TARGET_TRIPLET_ARCH ${_arch})

set(VCPKG_TARGET_TRIPLET
"${Z_VCPKG_TARGET_TRIPLET_ARCH}-${Z_VCPKG_TARGET_TRIPLET_PLAT}"
CACHE STRING "Vcpkg target triplet (ex. x86-windows)" FORCE)
endif()
endmacro()

# fix unicode and main function entry on mingw
macro(mingw_unicode target)
is_mingw(_is_mingw)
if(${_is_mingw})
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-municode" _cxx_supports_municode)
if(${_cxx_supports_municode})
target_compile_definitions(${target} INTERFACE "UNICODE" "_UNICODE")
target_compile_options(${target} INTERFACE "-municode")
endif()
endif()
endmacro()
3 changes: 2 additions & 1 deletion src/Optimization.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ macro(enable_interprocedural_optimization project_name)
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
include(CheckIPOSupported)
check_ipo_supported(RESULT result OUTPUT output)
if(result)
is_mingw(_is_mingw)
if(result AND NOT ${_is_mingw})
# If a static library of this project is used in another project that does not have `CMAKE_INTERPROCEDURAL_OPTIMIZATION` enabled, a linker error might happen.
# TODO set this option in `package_project` function.
message(
Expand Down
12 changes: 11 additions & 1 deletion src/Utilities.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,18 @@ function(detect_architecture arch)
STREQUAL
"")
set(_arch "${CMAKE_HOST_SYSTEM_PROCESSOR}")
else()
elseif(
NOT
"${CMAKE_SYSTEM_PROCESSOR}"
STREQUAL
"")
set(_arch "${CMAKE_SYSTEM_PROCESSOR}")
elseif(
NOT
"${DETECTED_CMAKE_SYSTEM_PROCESSOR}" # set by DetectCompiler.cmake
STREQUAL
"")
set(_arch "${DETECTED_CMAKE_SYSTEM_PROCESSOR}")
endif()

# make it lowercase for comparison
Expand Down
5 changes: 2 additions & 3 deletions src/VCEnvironment.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ macro(find_msvc)
))
message(STATUS "Finding MSVC cl.exe ...")
include(FetchContent)
FetchContent_Declare(
_msvc_toolchain
URL "https://github.com/aminya/Toolchain/archive/3994a752ea395120ffa3cc1c6b1a4cab80527e54.zip")
FetchContent_Declare(_msvc_toolchain
URL "https://github.com/aminya/Toolchain/archive/3994a752ea395120ffa3cc1c6b1a4cab80527e54.zip")
FetchContent_MakeAvailable(_msvc_toolchain)
include("${_msvc_toolchain_SOURCE_DIR}/Windows.MSVC.toolchain.cmake")
message(STATUS "Setting CMAKE_CXX_COMPILER to ${CMAKE_CXX_COMPILER}")
Expand Down
4 changes: 4 additions & 0 deletions src/Vcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ macro(run_vcpkg)
endif()
endif()

configure_mingw_vcpkg()

# Setting up vcpkg toolchain
list(APPEND VCPKG_FEATURE_FLAGS "versions")
set(CMAKE_TOOLCHAIN_FILE
${_vcpkg_args_VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake
CACHE STRING "vcpkg toolchain file")

configure_mingw_vcpkg_after()
endmacro()
16 changes: 16 additions & 0 deletions src/detect_compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.23)
enable_language(CXX)
enable_language(C)

set(CMAKE_CXX_COMPILER_ID
"${CMAKE_CXX_COMPILER_ID}"
CACHE STRING "The CXX compiler id.")
set(CMAKE_C_COMPILER_ID
"${CMAKE_C_COMPILER_ID}"
CACHE STRING "The C compiler id.")
set(CMAKE_SYSTEM_PROCESSOR
"${CMAKE_SYSTEM_PROCESSOR}"
CACHE STRING "The target system processor.")
set(CMAKE_HOST_SYSTEM_PROCESSOR
"${CMAKE_HOST_SYSTEM_PROCESSOR}"
CACHE STRING "The host system processor.")
21 changes: 19 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ set(PCH_HEADERS
<string>
<string_view>)

# Enable sanitizers and static analyzers when running the tests
set(ENABLE_SANITIZER_ADDRESS OFF)
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR OFF)
if(NOT
"${CMAKE_SYSTEM_NAME}"
STREQUAL
"Windows")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
else()
# or it is MSVC and has run vcvarsall
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if(MSVC AND "${index_of_vs_install_dir}" STREQUAL "-1")
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
endif()
endif()

# Initialize project_options
# uncomment the options to enable them
project_options(
Expand All @@ -45,9 +62,9 @@ project_options(
# ENABLE_USER_LINKER
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
# ENABLE_SANITIZER_ADDRESS
${ENABLE_SANITIZER_ADDRESS}
# ENABLE_SANITIZER_LEAK
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
# ENABLE_SANITIZER_THREAD
# ENABLE_SANITIZER_MEMORY
# CLANG_WARNINGS "-Weverything"
Expand Down
10 changes: 6 additions & 4 deletions test/src/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
int main() {
fmt::print("Hello from fmt{}", "!");

// populate an Eigen vector with the values
auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1);

// print the vector
Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3);
fmt::print("{}", eigen_vec);

#if !defined(__MINGW32__) && !defined(__MSYS__)// TODO fails
Eigen::VectorXd eigen_vec2 = Eigen::VectorXd::LinSpaced(10, 0, 1);
fmt::print("{}", eigen_vec2);
#endif

// trigger address sanitizer
// int *p = nullptr;
// *p = 1;
Expand Down
Loading

0 comments on commit 7e7b982

Please sign in to comment.