Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use of compile time option #276

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions tests/myproj/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16...3.21)
cmake_minimum_required(VERSION 3.16...3.30)

# set a default CXX standard used by the external tools like clang-tidy, cppcheck, etc.
# You can later set fine-grained standards for each target using `target_compile_features`
Expand Down Expand Up @@ -55,7 +55,7 @@ if(APPLE)
if(apple_version VERSION_GREATER_EQUAL 13)
# workaround for linkage error as described in https://github.com/Homebrew/homebrew-core/issues/145991
# although fixed, this problem still exists in github actions
add_link_options(-Wl,-ld_classic)
#XXX add_link_options(-Wl,-ld_classic)
set(ENABLE_INTERPROCEDURAL_OPTIMIZATION "")
endif()
endif()
Expand All @@ -71,7 +71,7 @@ project_options(
ENABLE_CLANG_TIDY
# ENABLE_INCLUDE_WHAT_YOU_USE
# ENABLE_GCC_ANALYZER
ENABLE_COVERAGE
#XXX ENABLE_COVERAGE
# ENABLE_PCH
# PCH_HEADERS
# ${PCH_HEADERS}
Expand Down Expand Up @@ -124,6 +124,13 @@ target_include_system_directories( # Test the fix of semicolon in genex issue
target_link_libraries(lib INTERFACE myproj_project_options myproj_project_warnings)
target_link_system_libraries(lib INTERFACE fmt::fmt Eigen3::Eigen)

option(LIB_WITH_EIGEN "Enable Eigen" OFF)
if(LIB_WITH_EIGEN)
target_compile_definitions(lib INTERFACE HAS_EIGEN)
endif()

target_link_libraries(main PRIVATE lib)

# Library
Comment on lines +127 to +133
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to export this option?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be passed from the builder of this library. project_options doesn't provide a specific mechanism for this.

add_library(lib2 "./src/mylib2/lib.cpp")
set(lib2_INCLUDE_DIR2 "include")
Expand Down Expand Up @@ -178,3 +185,6 @@ package_project(
)

package_project(NAME myproj_main TARGETS main)

set(CPACK_GENERATOR TGZ)
include(CPack)
18 changes: 14 additions & 4 deletions tests/myproj/include/mylib/lib.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

// test external pac
#include <Eigen/Dense>
#ifdef HAS_EIGEN
# include <Eigen/Dense>
#else
# include <vector>
#endif

#include <fmt/core.h>
#include <fmt/ranges.h>

Expand All @@ -17,14 +22,19 @@
#include <cstdint>
#include <cstring>

int some_fun() {
fmt::print("Hello from fmt{}", "!");
int some_fun()
{
fmt::print("Hello from lib{}\n", "!");

#ifdef HAS_EIGEN
// populate an Eigen vector with the values
auto eigen_vec = Eigen::VectorXd::LinSpaced(10, 0, 1);
#else
auto eigen_vec = std::vector<int>() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
#endif

// print the vector
fmt::print("[{}]", fmt::join(eigen_vec, ", "));
fmt::print("[{}]\n", fmt::join(eigen_vec, ", "));

return 0;
}
23 changes: 15 additions & 8 deletions tests/myproj/src/main/main.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
#include "mylib/lib.hpp"

// test external pac
#include <Eigen/Dense>
#include <fmt/core.h>
#include <fmt/ranges.h>

Check warning on line 5 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header ranges.h is not used directly [misc-include-cleaner]

Check warning on line 5 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header ranges.h is not used directly [misc-include-cleaner]

Check warning on line 5 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header ranges.h is not used directly [misc-include-cleaner]

Check warning on line 5 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header ranges.h is not used directly [misc-include-cleaner]

#ifdef HAS_EIGEN
# include <Eigen/Dense>
#endif

// test std libraries
#include <iostream>

Check warning on line 12 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header iostream is not used directly [misc-include-cleaner]

Check warning on line 12 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header iostream is not used directly [misc-include-cleaner]

Check warning on line 12 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header iostream is not used directly [misc-include-cleaner]

Check warning on line 12 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header iostream is not used directly [misc-include-cleaner]
#include <string>

Check warning on line 13 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header string is not used directly [misc-include-cleaner]

Check warning on line 13 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header string is not used directly [misc-include-cleaner]

Check warning on line 13 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header string is not used directly [misc-include-cleaner]

Check warning on line 13 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header string is not used directly [misc-include-cleaner]
#include <string_view>

Check warning on line 14 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header string_view is not used directly [misc-include-cleaner]

Check warning on line 14 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header string_view is not used directly [misc-include-cleaner]

Check warning on line 14 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header string_view is not used directly [misc-include-cleaner]

Check warning on line 14 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header string_view is not used directly [misc-include-cleaner]
#include <vector>

// test c libraries
#include <cassert>

Check warning on line 18 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header cassert is not used directly [misc-include-cleaner]

Check warning on line 18 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header cassert is not used directly [misc-include-cleaner]

Check warning on line 18 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header cassert is not used directly [misc-include-cleaner]

Check warning on line 18 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header cassert is not used directly [misc-include-cleaner]
#include <cctype>

Check warning on line 19 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header cctype is not used directly [misc-include-cleaner]

Check warning on line 19 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header cctype is not used directly [misc-include-cleaner]

Check warning on line 19 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header cctype is not used directly [misc-include-cleaner]

Check warning on line 19 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header cctype is not used directly [misc-include-cleaner]
#include <cstddef>

Check warning on line 20 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header cstddef is not used directly [misc-include-cleaner]

Check warning on line 20 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header cstddef is not used directly [misc-include-cleaner]

Check warning on line 20 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header cstddef is not used directly [misc-include-cleaner]

Check warning on line 20 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header cstddef is not used directly [misc-include-cleaner]
#include <cstdint>

Check warning on line 21 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header cstdint is not used directly [misc-include-cleaner]

Check warning on line 21 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header cstdint is not used directly [misc-include-cleaner]

Check warning on line 21 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header cstdint is not used directly [misc-include-cleaner]

Check warning on line 21 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header cstdint is not used directly [misc-include-cleaner]
#include <cstring>

Check warning on line 22 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

included header cstring is not used directly [misc-include-cleaner]

Check warning on line 22 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

included header cstring is not used directly [misc-include-cleaner]

Check warning on line 22 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

included header cstring is not used directly [misc-include-cleaner]

Check warning on line 22 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

included header cstring is not used directly [misc-include-cleaner]

int main() {
fmt::print("Hello from fmt{}", "!");
int main()

Check warning on line 24 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, gcc, true, true)

an exception may be thrown in function 'main' which should not throw exceptions [bugprone-exception-escape]

Check warning on line 24 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (windows-2022, llvm, true, true)

an exception may be thrown in function 'main' which should not throw exceptions [bugprone-exception-escape]

Check warning on line 24 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

an exception may be thrown in function 'main' which should not throw exceptions [bugprone-exception-escape]

Check warning on line 24 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

an exception may be thrown in function 'main' which should not throw exceptions [bugprone-exception-escape]
{
fmt::print("Hello from main{}\n", "!");

Eigen::VectorXd eigen_vec = Eigen::Vector3d(1, 2, 3);
fmt::print("[{}]", fmt::join(eigen_vec, ", "));
auto eigen_vec = std::vector<int>() = {1, 2, 3};
fmt::print("[{}]\n", fmt::join(eigen_vec, ", "));

Check warning on line 29 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, gcc, true, true)

no header providing "fmt::join" is directly included [misc-include-cleaner]

Check warning on line 29 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (ubuntu-22.04, llvm, true, true)

no header providing "fmt::join" is directly included [misc-include-cleaner]

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

// trigger address sanitizer
// int *p = nullptr;
// *p = 1;

// trigger compiler warnings, clang-tidy, and cppcheck
int a;
int a = some_fun();

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

variable 'a' of type 'int' can be declared 'const' [misc-const-correctness]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

Value stored to 'a' during its initialization is never read [clang-analyzer-deadcode.DeadStores]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

unused variable 'a' [clang-diagnostic-unused-variable]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

variable name 'a' is too short, expected at least 3 characters [readability-identifier-length]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

Variable 'a' is assigned a value that is never used. [unreadVariable]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

unused variable 'a' [-Wunused-variable]

Check warning on line 41 in tests/myproj/src/main/main.cpp

View workflow job for this annotation

GitHub Actions / Test (macos-12, llvm, true, true)

variable 'a' of type 'int' can be declared 'const' [misc-const-correctness]
}
Loading